PIC program not running

Discussion in 'Embedded Systems and Microcontrollers' started by nnxen, Aug 5, 2013.

  1. nnxen

    Thread Starter New Member

    Dec 22, 2012
    Hi everyone, I hope you might be able to help me.

    Last year I delved in to microcontrollers a little as part of my university work to make simple MIDI controllers, and it wet my appetite a little to have a play around myself.

    I bought a 18F4550 with a very basic cheap development board for me to play with as it left many of the pins free for me to use with my own circuits and modules. I also bought a PICkit2 clone to program with and I'm using MPLAB v8.92 with the Hi-Tech C Lite Compiler since that is the software and compiler we used in lectures.

    The schematic of the development board is available here:

    I just tried a simple LED flash code to make sure everything works correctly. My code is as follows:

    Code ( (Unknown Language)):
    2. #include <htc.h>
    4. __CONFIG(1, FCMDIS & IESODIS & XT);
    5. __CONFIG(2, BORDIS & BORV45 & WDTDIS & WDTPS1);
    8. __CONFIG(5, UNPROTECT);
    9. __CONFIG(6, UNPROTECT);
    10. __CONFIG(7, UNPROTECT);
    12. void main(void)
    13. {
    16.     ADCON0=0b00000000;
    17.     ADCON1=7;
    18.     CMCON=7;
    19.     TRISA=0b00000000;
    20.     PORTA=0b00000000;
    22.     while(1){
    23.         PORTA=255;
    24.         _delay(100);
    25.         PORTA=0;
    26.         _delay(100);
    27.     }
    29. }
    I can compile the code, program the pic and verify it, but it never seems to run... just sits there with the LED off (LED jumper is bridged to enable it by the way.)

    Its probably something very simple, but I've been stuck for hours on this first hurdle! :(

    Out of interest, the PICkit2 clone I have has no connection to the PGM pin of the chip, could this be an issue?

    My PICkit 2 log is below:

    Code ( (Unknown Language)):
    2. Initializing PICkit 2 version
    3. Found PICkit 2 - Operating System Version 2.32.0
    4. PICkit 2 Unit ID = iCP01-V2.0
    5. Target power detected ( 4.99V)
    6. PIC18F4550 found (Rev 0x7)
    7. PICkit 2 Ready
    9. Programming Target (05/08/2013  20:46:23)
    10. PIC18F4550 found (Rev 0x7)
    11. Erasing Target
    12. Programming Program Memory (0x0 - 0x1F)
    13. Programming Program Memory (0x7D80 - 0x7DBF)
    14. Verifying Program Memory (0x0 - 0x1F)
    15. Verifying Program Memory (0x7D80 - 0x7DBF)
    16. Programming Configuration Memory
    17. Verifying Configuration Memory
    18. Setting MCLR Vdd
    19. PICkit 2 Ready
    Hope you can help :)
  2. Markd77

    Senior Member

    Sep 7, 2009
    I don't know much about C or that chip, but it seems to have programmed fine. There is no need to have PGM connected for normal high voltage programming using the MCLR pin.
    nnxen likes this.
  3. JohnInTX


    Jun 26, 2012
    ADCON1 should be 0x0f. 0x07 leaves RA0 an analog input. It may be running but unable to output anything.
    Make your writes to LATA, not PORTA.

    Good luck!
    nnxen likes this.
  4. nnxen

    Thread Starter New Member

    Dec 22, 2012
    No luck I'm afraid. I've noticed if I try to use the PICkit2 in debugger mode rather than programmer mode (with MCLR enabled) I get the error "PK2Error0028: Unable to enter debug mode" and occasionally "PK2Error0024: PICKit 2 was unable to establish a valid Vdd on the target (Attempted 5.00V - Read 0.00V)"

    Code ( (Unknown Language)):
    1. Found PICkit 2 - Operating System Version 2.32.0
    2. PICkit 2 Unit ID = iCP01-V2.0
    3. Target power detected ( 4.99V)
    4. PIC18F4550 found (Rev 0x7)
    5. PICkit 2 Ready
    7. Resetting
    8. Programming Target (05/08/2013  22:23:37)
    9. PIC18F4550 found (Rev 0x7)
    10. Erasing Target
    11. Programming Program Memory (0x0 - 0x1F)
    12. Programming Program Memory (0x7D80 - 0x7DBF)
    13. Verifying Program Memory (0x0 - 0x1F)
    14. Verifying Program Memory (0x7D80 - 0x7DBF)
    15. Programming Debug Executive (0x-7DC0 - 0x7FFF)
    16. Verifying Debug Executive (0x7DC0 - 0x7FFF)
    17. Programming Debug Vector
    18. Verifying Debug Vector
    19. Programming Configuration Memory
    20. Verifying Configuration Memory
    21. PK2Error0028:  Unable to enter debug mode
    22. PICkit 2 Ready
    24. Running Target
    25. PICkit 2 Ready
    Could this be the source of my problems?
  5. JohnInTX


    Jun 26, 2012
    In CONFIG 4, you have the debugger disabled.

    In MPLAB 8.x:, set the programmer to NONE and the debugger to PICkit2. Rebuild. It will then program the chip with the debugging hooks you need. You should be able to step the code from the MPLAB toolbar.

    In Debugger->Settings, make sure you have the boxes checked for Vdd source and level. EDIT. It looks like the board supplies Vdd so turn it off on the pk2.

    Does that help?
    Last edited: Aug 5, 2013
  6. nnxen

    Thread Starter New Member

    Dec 22, 2012
    Sorry, I forgot to mention that I'd also enabled debug - it didn't work but further reading makes me believe it probably has something to do with this... http://www.microchip.com/forums/tm.aspx?m=245280

    To be honest, I'm not too bothered about having debugging working so long as my chip will run code :( I can cope without debug.

    I've checked the pins with a voltmeter and once coded and the PK2 disconnected: MCLR is at 5.2v as is Vdd. Vss has good ground. I can't understand why it just won't run to be honest - I've tried setting all the ports to output and switching them all and none of them are doing anything. Verifying the code seems to work so the code should actually be on there.
  7. JohnInTX


    Jun 26, 2012
    For the 20MHz XTAL, set your oscillator type to HS. In MPLAB be sure to have Debugger set to NONE and select RELEASE on the toolbar.

    I think you will be missing out on a powerful tool if you do that.

    EDIT: You should either enable MCLR or PWRT (or preferably both) since the board has a reset circuit. Power On Reset Timer gives the clock time to come up and stabilize before running the chip. It might cause startup problems not having either.

    Consider setting LATD to output all 0 before your LED loop. If the processor is running, the port bits will be logic low. If not, they will be floating (use a light pullup on one bit to be sure). Easy test to see if its running.

    Your watchdog should be disabled with your config but it wouldn't hurt to put a clrwdt() in the loop to be sure.
    To make the delays work, don't you need to specify the XTAL frequency somewhere for the compiler?

    Have fun.
    Last edited: Aug 5, 2013
    nnxen likes this.
  8. nnxen

    Thread Starter New Member

    Dec 22, 2012
    Thats awesome, Thanks John! :D

    I didn't realise there was a different config value for xtals over 10Mhz. Replacing XT with HS instantly fixed everything, even debug :)
  9. JohnInTX


    Jun 26, 2012
    Glad it worked! Good job hanging in there and pushing on to the fix!