# Beginner PIC Programming (16F84A, MPLAB, Hi-Tech C)

#### Recon

Joined Oct 16, 2009
30
Hi all,

I am a complete beginner with PIC programming and am trying to get the most basic possible program to work. I just want to make one LED light up.

I've figured out how to set the configuration bits, compile the C code into a hex file, and flash the hex file to my PIC16F84A. Everything seems to work fine until I try to light that LED!

I've connected Pin 14 to the +v rail and Pin 5 to the 0v rail as per the data sheet. I've tested the wires and the LED lights up just fine on its own. I've attached a (slightly blurry) photo of my breadboard.

Rich (BB code):
#include "htc.h"
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);

int main(void) {

PORTA=0; //set all as output
TRISA=0;
PORTB=0;
TRISB=0;

for(;;) {
RA1 = 1;
RB4 = 1;
}
}
Any ideas?

#### Attachments

• 290.1 KB Views: 144

#### t06afre

Joined May 11, 2009
5,934
One thing. I can not see any crystal on your board. That could explain it

#### John P

Joined Oct 14, 2008
1,992

Oops, not fast enough. Well, what do you see in section 6.2 of the manual?

#### Recon

Joined Oct 16, 2009
30
Ah, I must admit I didn't really understand that section. I wrongly assumed that it wasn't required for a simple circuit like this one. Looking at it again, it seems easiest to change to a resistor/capacitor configuration as timing isn't important.

I'll try tomorrow and let you know. Thanks for the help.

#### t06afre

Joined May 11, 2009
5,934
Ah, I must admit I didn't really understand that section. I wrongly assumed that it wasn't required for a simple circuit like this one. Looking at it again, it seems easiest to change to a resistor/capacitor configuration as timing isn't important.
I'll try tomorrow and let you know. Thanks for the help.
A RC oscillator may do fine for your simple circuit. Remember to change the __config setting also. Now your PIC is set up to use High Speed Crystal (FOSC_HS in the __config)

#### AlexR

Joined Jan 16, 2008
732
As well as the sorting out the oscillator you need to add a pull-up to the MCLR pin (pin 4), a resistor, somewhere between 1K and 10K from pin 4 to Vdd will be fine, also while you're about it a 100nF bypass capacitor from the Vdd pin to Vss would not go astray.

#### CVMichael

Joined Aug 3, 2007
419
Actually you don't really need an external oscillator, the internal one goes up to 4MHz. You just have to configure it properly.
And 'yes', put a 10K resistor on MCLR to +5V

#### MrChips

Joined Oct 2, 2009
27,738
Don't know about your code. I think RB4 = 1; should be RA1 = 0;

#### AlexR

Joined Jan 16, 2008
732
Actually you don't really need an external oscillator, the internal one goes up to 4MHz. You just have to configure it properly.
And 'yes', put a 10K resistor on MCLR to +5V
The chip is a PIC16F84A! It does not have an internal oscillator so the choice is to use either an external crystal/resonator or a RC timing circuit.

#### John P

Joined Oct 14, 2008
1,992
But that lead to a good point--the PIC16F84, even the A version, has been obsolete for years, but I think Microchip's attitude is that if people are willing to keep paying a higher price for an inferior product, they'll keep selling the things. Unless you have a pile of them that you want to get rid of, and you don't mind adding external components, it's generally a better plan to use a different processor.

#### Recon

Joined Oct 16, 2009
30
I added a pull-up to the MCLR pin and a resistor/capacitor oscillator, and changed the configuration setting to use the RC oscillator. It now works. Next step... flashing LED. Baby steps!

I was given a few PIC16F84A processors so I'm happy to use them for now, but I'll buy a newer model at some point.

Thanks for the help.

#### Recon

Joined Oct 16, 2009
30
I've encountered another problem adding a delay using the __delay_ms(n) macro described on page 174 of the Hi-Tech C manual. MPLAB X is underlining __delay_ms saying it's "unable to resolve identifier." I have also tried using __delay(n) but I have the same problem.

Is this because I'm using a RC timing circuit instead of an actual oscillator? I tried changing the __CONFIG back to FOSC_HS to see if it would recognise the macro, but it didn't.

I'm not looking for accurate timing (at the moment), but I'd like to make the LED flash if possible.

Rich (BB code):
#include "htc.h"
#define _XTAL_FREQ 4000000 //I know this isn't actually accurate

__CONFIG(FOSC_EXTRC & WDTE_OFF & PWRTE_ON & CP_OFF);

int main(void) {

TRISA = 0; //set outputs

for(;;) {
RA1 = 1;

__delay_ms(100);
__delay_ms(100);

RA1 = 0;

__delay_ms(100);
__delay_ms(100);
}
}

#### AlexR

Joined Jan 16, 2008
732
Your code compiles correctly without any errors when using MPLAB V8.80 so it could be a problem with the way MPLABX interacts with HiTechC. I know I did I tried MPLABX some time ago but gave it away as a bad idea when it would not recognise the free version of HiTechC. Maybe when the full release of MPLABX comes out I'll have another look at it but for now you are better off using the old MPLAB 8.xx

#### Recon

Joined Oct 16, 2009
30
Your code compiles correctly without any errors when using MPLAB V8.80 so it could be a problem with the way MPLABX interacts with HiTechC.
Actually, the code compiles without errors on MPLAB X, despite showing warning icons and that error message in the code. I'll download MPLAB v8.80 anyway to be sure.

Should my code work using a resistor/capacitor timing circuit, or do I need to use an oscillator to use the delay macros?

I've found a ZTT 10.00MT resonator. I'm going to try to set this up if I can find a 1M ohm resistor and some NOT gates (for a buffer, if my Googling is correct!)

#### AlexR

Joined Jan 16, 2008
732
It should be fine just using the RC timing, the actual delay time might be a bit out but that's not terribly important in this application. If you have access to frequency counter or oscilloscope you could measure the the clock frequency that the oscillator is running at but it is probably just as good to work out the theoretical clock frequency from the R and C values and plug that into the "#define _XTAL_FREQ" value.

You can connect the ZTT resonator straight across the PIC oscillator pins (pins 15, 16) and connect the center pin of the resonator to earth/Vss. Then set the oscillator config to "FOSC_HS".