# PIC program not running

#### nnxen

Joined Dec 22, 2012
10
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:
http://www.bravekit.com/document/PIC_boards/PIC_P40/PIC-P40_schem.pdf

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

Rich (BB code):
#include <htc.h>

__CONFIG(1, FCMDIS & IESODIS & XT);
__CONFIG(2, BORDIS & BORV45 & WDTDIS & WDTPS1);
__CONFIG(3, CCP2RB3 & LPT1DIS & MCLRDIS & PBDIGITAL);
__CONFIG(4, DEBUGDIS & XINSTDIS & LVPDIS & STVRDIS);
__CONFIG(5, UNPROTECT);
__CONFIG(6, UNPROTECT);
__CONFIG(7, UNPROTECT);

void main(void)
{

CMCON=7;
TRISA=0b00000000;
PORTA=0b00000000;

while(1){
PORTA=255;
_delay(100);
PORTA=0;
_delay(100);
}

}
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:

Rich (BB code):
Initializing PICkit 2 version 0.0.3.63
Found PICkit 2 - Operating System Version 2.32.0
PICkit 2 Unit ID = iCP01-V2.0
Target power detected ( 4.99V)
PIC18F4550 found (Rev 0x7)

Programming Target (05/08/2013  20:46:23)
PIC18F4550 found (Rev 0x7)
Erasing Target
Programming Program Memory (0x0 - 0x1F)
Programming Program Memory (0x7D80 - 0x7DBF)
Verifying Program Memory (0x0 - 0x1F)
Verifying Program Memory (0x7D80 - 0x7DBF)
Programming Configuration Memory
Verifying Configuration Memory
Setting MCLR Vdd
PICkit 2 Ready
Hope you can help

#### Markd77

Joined Sep 7, 2009
2,806
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.

#### JohnInTX

Joined Jun 26, 2012
3,912
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

Joined Dec 22, 2012
10
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)"

Rich (BB code):
Found PICkit 2 - Operating System Version 2.32.0
PICkit 2 Unit ID = iCP01-V2.0
Target power detected ( 4.99V)
PIC18F4550 found (Rev 0x7)

Resetting
Programming Target (05/08/2013  22:23:37)
PIC18F4550 found (Rev 0x7)
Erasing Target
Programming Program Memory (0x0 - 0x1F)
Programming Program Memory (0x7D80 - 0x7DBF)
Verifying Program Memory (0x0 - 0x1F)
Verifying Program Memory (0x7D80 - 0x7DBF)
Programming Debug Executive (0x-7DC0 - 0x7FFF)
Verifying Debug Executive (0x7DC0 - 0x7FFF)
Programming Debug Vector
Verifying Debug Vector
Programming Configuration Memory
Verifying Configuration Memory
PK2Error0028:  Unable to enter debug mode

Running Target
PICkit 2 Ready
Could this be the source of my problems?

#### JohnInTX

Joined Jun 26, 2012
3,912
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:

#### nnxen

Joined Dec 22, 2012
10
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.

#### JohnInTX

Joined Jun 26, 2012
3,912
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.

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 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:

#### nnxen

Joined Dec 22, 2012
10
Thats awesome, Thanks John!

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

#### JohnInTX

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