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

Discussion in 'Embedded Systems and Microcontrollers' started by Recon, Nov 21, 2011.

  1. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    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.

    Code ( (Unknown Language)):
    1. #include "htc.h"
    2. __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF);
    3.  
    4. int main(void) {
    5.  
    6.     PORTA=0; //set all as output
    7.     TRISA=0;
    8.     PORTB=0;
    9.     TRISB=0;
    10.    
    11.     for(;;) {
    12.         RA1 = 1;
    13.         RB4 = 1;
    14.     }
    15. }
    Any ideas? :confused:
     
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    One thing. I can not see any crystal on your board. That could explain it ;)
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    Where's your oscillator?

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

    Thread Starter Member

    Oct 16, 2009
    30
    0
    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.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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)
     
  6. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  7. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    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
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,432
    3,360
    Don't know about your code. I think RB4 = 1; should be RA1 = 0;
     
  9. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  10. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    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.
     
  11. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    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.
     
  12. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    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.

    Code ( (Unknown Language)):
    1. #include "htc.h"
    2. #define _XTAL_FREQ 4000000 //I know this isn't actually accurate
    3.  
    4. __CONFIG(FOSC_EXTRC & WDTE_OFF & PWRTE_ON & CP_OFF);
    5.  
    6. int main(void) {
    7.  
    8.     TRISA = 0; //set outputs
    9.    
    10.     for(;;) {
    11.         RA1 = 1;
    12.        
    13.         __delay_ms(100);
    14.         __delay_ms(100);
    15.        
    16.         RA1 = 0;
    17.        
    18.         __delay_ms(100);
    19.         __delay_ms(100);
    20.     }
    21. }
     
  13. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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
     
  14. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    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!) :confused:
     
  15. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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".
     
  16. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    Fantastic. It worked after entering the theoretical frequency for the RC circuit. Then, swapping over to using the resonator worked first time! Thanks for the help. :D
     
Loading...