16f628 test project - ok what am i doing wrong?

Discussion in 'Embedded Systems and Microcontrollers' started by bluebrakes, Nov 5, 2009.

  1. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    I've built a test circuit for learning how to program the 16f628 pic but I can't understand why my circuit or my code isn't working properly.

    My code is...
    Still learning so bear with me. :)

    LIST P=16F628, R=DEC ; Use the PIC16F628 and decimal system
    #include "P16F628.INC" ; Include header file
    __config _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_ON
    CBLOCK 0x20 ; Declare variable addresses starting at 0x20
    Loop1,Loop2
    ENDC
    ;
    ; -----------
    ; INITIALIZE
    ; -----------
    ;
    ORG 0x000 ; Program starts at 0x000

    CLRF PORTA ; Initialize port A
    CLRF PORTB ; Initialize port B

    BSF STATUS,RP0 ; RAM bank 1

    CLRF TRISA ; All pins port A output
    CLRF TRISB ; All pins port B output

    BCF STATUS,RP0 ; RAM bank 0
    ;
    ; ------------------------
    ; FUNCTION OF PORT A PINS
    ; ------------------------
    ;
    MOVLW 7
    MOVWF CMCON ; Comparators off, all pins digital I/O
    ;
    ; ----------
    ; MAIN LOOP
    ; ----------
    ;
    Main BSF PORTA,2 ; Turn on LED connected to RA2
    CALL delay
    BCF PORTA,2 ; Turn off LED connected to RA2
    CALL delay

    BSF PORTB,2 ; Turn on LED connected to RB2
    CALL delay
    BCF PORTB,2 ; Turn off LED connected to RB2
    CALL delay

    BSF PORTB,3 ; Turn on LED connected to RB3
    CALL delay
    BCF PORTB,3 ; Turn off LED connected to RB3
    CALL delay

    BSF PORTB,4 ; Turn on LED connected to RB4
    CALL delay
    BCF PORTB,4 ; Turn off LED connected to RB4
    CALL delay

    BSF PORTB,5 ; Turn on LED connected to RB5
    CALL delay
    BCF PORTB,5 ; Turn off LED connected to RB5
    CALL delay

    GOTO Main
    ;
    ; ---------------
    ; DELAY 250 MSEC
    ; ---------------
    ;
    delay MOVLW 250
    MOVWF Loop1
    Outer MOVLW 200
    MOVWF Loop2
    Inner NOP
    NOP
    DECFSZ Loop2,F
    GOTO Inner ; Inner loop = 5 usec.
    DECFSZ Loop1,F
    GOTO Outer
    RETURN

    END

    my circuit is attached.

    Thanks,
    Dominic
     
  2. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    I forgot to mention d5, d6, d7 all flash as they should but d3 stays on all the time and none of the others flash/on (d1,d2, d4)

    This is what the problem is.
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    You have a software simulator in MPLAB. Have you used this to step through your program step by step?
     
  4. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    This is something I haven't quite worked out how to do properly. I can run it through the program but I can't seem to see the results of the coding?

    Any hints?
     
  5. eng1

    New Member

    Oct 25, 2009
    13
    0
    D1,D2,D4 are not supposed to flash because you don't control the respective pins of PORTA in your code.

    When RA5 function is /MCLR, as in your code, this is an active low pin and you need to connect it to Vdd through resistor R9 during operation, and the switch to ground.
     
  6. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    That's really bad of me. the schematic is slightly wrong, the MCLR it is indeed wired to switch to ground.

    As for the coding, it doesn't explain why one of the LEDs is stuck on and why another won't even light.

    Another problem i'm having is when I go to reprogram the pic, I erase it, which it says is sucessful but when i do a blank check, it says its not blank? If i erase it and put the pic back in the circuit it's still running the code. grrr!
     
  7. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    what programmer are you using???


    and try this for your config so it matches code and circuit....

    __config _INTRC_OSC_NOCLKOUT & _LVP_ON & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF

     
  8. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    picstart plus with the latest firmware chip 4.5

    I keep getting this sometimes, when i erase and then blank check it.

    ----------------------------------
    Erase operation is successful.

    Device is NOT blank:
    Program Memory
    Configuration Bits
    ----------------------------------
     
  9. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    If you are programming the uc via ICSP, you need to set LOW VOLTAGE PROGRAMMING to ON, and you can not have anything connected to the LVP/PGM pin RB4, since you have an LED on that pin, could cause programming and erasing issues on the uc when you are programming it....
     
  10. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    here's what it says in the datasheet:

    I ran into same issues with the 628A just a few days ago.....
     
  11. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    I do the programming on the picstart ZIF, not in the circuit, so the pic is removed from the circuit each time for erasing/programming.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Unless you have a ZIF socket on your trainer I would recommend that you do incircuit programming. As this reduce wear and tear on your circuit board and PIC. You must do a similar arrangement as explained here. http://ww1.microchip.com/downloads/en/DeviceDoc/30277d.pdf The signal is routed to the corresponding pins on the picstart plus unit(as your pic was plugged in the ZIF). The picstart plus have very poor driver capabilities so you must use a short cable, and perhaps also disconnect the decoupling cap. And while programming the pins used must be disconnected from everything but the pic. Use jumper or dip-switch
    Here is a picture to give you and idea. If I am not wrong this picture is from blueroomelectronics.
    [​IMG]
     
  13. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    You might also be running into the read, modify, write problem. When you do a bit operation on a port, the whole port is read and then rewritten. This can mean that pins that you didn't want to change could be affected. It is much better to do the bit operations on a file and only use MOV on the port itself.
     
  14. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    thanks guys.

    I really like the idea of that in-circuit programmer mod.

    Would you be able to explain a bit more on the bit operations?

    I just can't see why this would affect programming it off the circuit and in the pic programmer zif? I know you guys probably are right on this, I'm just not experienced enough to understand.
     
  15. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Put TB3009 into the search box on the microchip site.
    Title: Common 8-Bit PIC® Microcontroller I/O Pin Issues.
    The read modify write problem may not be affecting you this time - it normally only happens with fast port switching or with high loads, but worth knowing about.
     
  16. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    In the theory it is the same. I do not know what kind of prototype board you have. But you have perhaps some solder type and a dip socket for the PIC. Every time you jack your PIC out of the socket you will tear the socket,the PIC, and the solder joints. So pretty soon your system will fail due to this wear and tear. A DIP socket is not made for this, in contrast to a ZIF socket. That is my experience. I also guess your work bench is not ESD protected. So each time you take your PIC out for programming you may risk ESD damage. But if it stay in the socket it will be more protected.
     
  17. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    I feel like a right idiot!

    I came back to this project tonight and i still couldn't get it to erase.

    Anyway to cut a long story short I found the answer, feel free to laugh...

    I'm using a 16f628 pic but I missed one crucial detail in the selection of the chip.

    I selected 16f628 instead of 16f628A on the MPLAB device select.

    A real head slap! DOH! :-D
     
Loading...