PIC16F877A LED Blinking Program

Discussion in 'Programmer's Corner' started by ActivePower, Jul 8, 2012.

  1. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    I am a complete noob in programming a PIC microcontroller and I have been trying to get the most basic of the tasks (flashing an LED) done.

    I am using MPLAB v8.73 with HiTech C Compiler and a PIC16F877A

    I found an circuit online (http://justanotherlanguage.org/sites/default/files/blink_a_led_circuit.jpg) and built the circuit with a few modifications in the component values.

    The code is as follows:

    Code ( (Unknown Language)):
    1. #include<pic.h>
    2. #define _XTAL_FREQ 20000000
    3.  
    4. void main()
    5. {
    6.      TRISB=0;
    7.      for (;;)
    8.      {
    9.         RB0=1;
    10.         __delay_ms(250);
    11.         RB0=0;
    12.         __delay_ms(250);
    13.      }
    14. }
    15.  
    The code builds successfully but does not blink the LED :( Maybe there is a problem with the circuit but I wanted to be sure about the code first.

    Thanks
     
    #1
  2. takao21203

    takao21203 Well-Known Member

    Joined:
    Apr 28, 2012
    3,322
    435
    Good work!

    You need to modify the ANSEL register setting for this PIC model, if I guess right. Also configuration needs to be set correctly, including to select HS oscillator.

    In the schematic the LED is connected to PORTA.
     
    #2
  3. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    Hey, thanks! I had changed the LED to the RB0 position on the PIC. Does that make any difference? And I am using a 20 MHz crystal oscillator, so I included 'XT' in the config bits from the datasheet. And what is the syntax for changing the 'ANSEL', I am afraid I am unaware of that.

    Thanks!
     
    #3
  4. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    You have not set the config bits as far as I can see. The config bits are related to section 14.0 SPECIAL FEATURES OF THE CPU in the datasheet.
     
    #4
  5. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    I set the config bits from the MPLAB IDE option 'Configuration Bits' and unchecked the 'Use configuration bits set in code' and selected the XT oscillator from the drop-down menu. Does that not count? I am sorry I do not know the details.
     
    #5
  6. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    It is OK for a beginner to it that way. But it is poor for documentation. Anyway If you are using a 20MHz crystal. The setting should be HS and not XT
     
    #6
  7. takao21203

    takao21203 Well-Known Member

    Joined:
    Apr 28, 2012
    3,322
    435
    XT and 20 MHz does never works that's that.
     
    #7
  8. JohnInTX

    JohnInTX Well-Known Member

    Joined:
    Jun 26, 2012
    1,460
    490
    RP1/0 in STATUS have to select RAM bank 1 or 3 for TRISB and then change to 0 or 2 for PORTB. Step through your code in MPLAB with a watch window open to be sure that STATUS is correct when addressing the I/O. 'Watch' PORTB as well to see if the LSB is being set/cleared.

    You should not have to in this case but instead of RB0 = 1, try writing (unsigned char) 1 and 0 to the port (LSB of char is RB0). Sometimes flipping individual bits on midrange PICs can be problematic.

    As others have indicated, HS is the correct oscillator to use for 20MHz. HS drives the XTAL harder than XT.

    You can indeed set the config in MPLAB but eventually it should be in your source code.

    Finally, you can step in the Disassembly Output window in MPLAB to see what's going on on the code level.
     
    #8
  9. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    The C compiler will take care of this
     
    #9
  10. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    Here is the code after modifications:

    Code ( (Unknown Language)):
    1. #include<pic.h>
    2. #define _XTAL_FREQ 20000000
    3.  
    4. __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & WRT_OFF & DEBUG_ON & CPD_OFF & CP_OFF)
    5.  
    6. //Should I leave DEBUG ON? I have just a programmer and I do not know what it does apart from programming the PIC
    7.  
    8. void main()
    9. {
    10.     TRISB=0;
    11.     for (;;)
    12.     {
    13.        RB0=1;
    14.        __delay_ms(250);
    15.        RB=0;
    16.       __delay_ms(250);
    17.     }
    18. }
    19.  
    This builds successfully but would it run on the PIC?
    Also, would it make any difference if I use RA pins or RB pins? The circuit is configured for RA but I wrote the code from the HiTech C Quickstart guide and it suggests RB pins.
    On a slightly unrelated note, for my PIC circuit-related queries should I make a post here or in the Projects Forum?

    Thanks!
     
    #10
  11. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    If you do not have any debugger leave it off. I think if debugger specified the PIC will wait for debugger to connect before starting running the code
     
    #11
  12. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    I do not have a separate debugger for the PIC. All I have is a JDM programmer and the WinPicProg software.

    EDIT: It worked!! The LED blinks and it looks and feels awesome.. The only change I made was to replace the XT with HS. Funny how something as small as that could alter the final result. Although, I am going to change the delay time and check again just to know that it wasn't a fluke...will let you know what happens.. Thanks everyone for your help!
     
    #12
  13. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    One small thing. Your program worked fine this time. But in the future replace
    Code ( (Unknown Language)):
    1. #include<pic.h>
    with
    Code ( (Unknown Language)):
    1. #include <htc.h>
     
    #13
    ActivePower likes this.
  14. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    I am really very sorry to post in this thread as I know it is archived under 'Programmer's Corner' but I didn't feel like creating a new thread for a question which is more relevant to this post.

    I got my LEDs blinking away last time around. However, when I tried to go for something more (http://forum.allaboutcircuits.com/showthread.php?t=72133), I had a few problems (http://forum.allaboutcircuits.com/showthread.php?goto=newpost&t=72145).

    Then I rebuilt my LED blink circuit and it didn't work and now I am hopelessly stuck again. There is no problem with the code, I believe, for:

    1. I took it from the HiTech C compiler Quickstart Guide.
    2. It was running perfectly yesterday.

    Here is the code anyway:

    Code ( (Unknown Language)):
    1. //main.c
    2.  
    3. #include<htc.h>
    4. #define _XTAL_FREQ 20000000
    5.  
    6. volatile unsigned char counter;         //value can be changed automatically during execution without modification through a function
    7.  
    8. __CONFIG(FOSC_HS & BOREN_OFF & DEBUG_ON & WDTE_OFF & CP_OFF & CPD_OFF & LVP_ON & PWRTE_OFF);
    9.  
    10. void init()
    11. {
    12.     PIE1=0b00000001;                        //Peripheral Interrupt Enable
    13.     INTCON=0b01000000;                          //Interrupt Control Register
    14.     OPTION_REG=0b10000000;                  //Pullup resistor enabled; active low bit ~RBPU
    15.     T1CON=0b00110101;                       //Timer 1 Control Register: Enable TMR1 using Internal Oscillator at FOSC/4
    16.     TRISB=0x0;                          //Configure as Output
    17. }
    18.  
    19. void main(void)
    20. {
    21.     counter=0;
    22.     init();         //initialize
    23.     ei();           //enable interrupts
    24.  
    25. /* Always initialize interrupts first and enable them later */
    26.  
    27.     while(1)
    28.     {
    29.         PORTB=counter;
    30.     }
    31. }
    32.  
    and the ISR

    Code ( (Unknown Language)):
    1. //IntFunc.c
    2.  
    3. #include<htc.h>
    4.  
    5. extern volatile char counter;       //var defined somewhere else but used here
    6.  
    7. void interrupt my_isr()             //associate the function with the Interrupt Vector using keyword
    8. {
    9.     if((TMR1IE)&(TMR1IF))           //if TIMER1 interrupt is enabled & TIMER1 is overflowing
    10.     {
    11.         counter++;
    12.         TMR1IF=0;                   //reset TMR1 interrupt flag
    13.     }
    14. }
    Can anyone please help me troubleshoot the circuit/code?

    Thanks!

    EDIT:
    1. The IC on the right is the L293D and I had stuck it in with great difficulty last night, so I did not actually want to remove it. Sorry for my laziness!
    2. Also my protoboard isn't labelled so the uppermost row is the power rail (I haven't connected the battery pack yet) and the last but one is the GND.
     
    Last edited: Jul 12, 2012
    #14
  15. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    Have you done some debugging like placing some breakpoints or using the watch option. And/or singlestepping of the code? You can do this using the MPLAB simulator. Also have you gone back and tested the last working code
     
    #15
  16. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    5,939
    1,222
    Here you have set LVP_ON I do not think the JDM programmer support this. Erase your chip compleatly and set LVP_OFF. Just a hunch though but the cost of doing it is quite low
     
    #16
  17. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    As far as I know the code is fine. It was working perfectly yesterday. That is why I think the circuit may be the cause of the trouble.
     
    #17
  18. takao21203

    takao21203 Well-Known Member

    Joined:
    Apr 28, 2012
    3,322
    435
    Code ( (Unknown Language)):
    1. if((TMR1IE)&(TMR1IF))
    should not this be

    Code ( (Unknown Language)):
    1. if((TMR1IE)&&(TMR1IF))
    ??

    As far as I recall & is numerical AND,
    && is the logical AND.
     
    #18
    ActivePower likes this.
  19. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    155
    23
    Thanks for pointing that out! I corrected it in my program, although it still did not help. I have tested all my previous working codes. None of them work. I strongly suspect something is up with the circuitry. I used my 6 V battery pack to test a dc motor last night for a few minutes in which it became quite hot. Could that have drained it enough not to supply the activating voltage? (I am sorry if that seems impossible I am thinking about any and all possibilities here; anything to get the darn thing working)

    EDIT: I spent some time with the circuit and my multimeter last night checking all loopy wires and stuff and after a while fiddling with the power connections, it worked! Apparently, the battery pack was super-drained out and could not maintain a constant 5V supply. I replaced the pack with a 9V cell and a 7805 and the LED is flashing away on my board as we speak.

    Thanks everyone for your help!
     
    Last edited: Jul 13, 2012
    #19
Loading...