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
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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:
    #include<pic.h>
    #define _XTAL_FREQ 20000000
    
    void main()
    {
         TRISB=0;
         for (;;)
         {
            RB0=1;
            __delay_ms(250);
            RB0=0;
            __delay_ms(250);
         }
    }
    
    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
  2. takao21203

    takao21203 Well-Known Member

    Joined:
    Apr 28, 2012
    Messages:
    2,572
    Location:
    Ireland
    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.
  3. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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!
  4. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    Messages:
    5,939
    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.
  5. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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.
  6. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    Messages:
    5,939
    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
  7. takao21203

    takao21203 Well-Known Member

    Joined:
    Apr 28, 2012
    Messages:
    2,572
    Location:
    Ireland
    XT and 20 MHz does never works that's that.
  8. JohnInTX

    JohnInTX Well-Known Member

    Joined:
    Jun 26, 2012
    Messages:
    1,003
    Location:
    Texas GMT-5
    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.
  9. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    Messages:
    5,939
    The C compiler will take care of this
  10. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    Here is the code after modifications:

    Code:
    #include<pic.h>
    #define _XTAL_FREQ 20000000
    
    __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & WRT_OFF & DEBUG_ON & CPD_OFF & CP_OFF)
    
    //Should I leave DEBUG ON? I have just a programmer and I do not know what it does apart from programming the PIC
    
    void main()
    {
        TRISB=0;
        for (;;)
        {
           RB0=1;
           __delay_ms(250);
           RB=0;
          __delay_ms(250);
        }
    }
    
    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!
  11. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    Messages:
    5,939
    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
  12. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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!
  13. t06afre

    t06afre AAC Fanatic!

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

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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:
    //main.c
    
    #include<htc.h>
    #define _XTAL_FREQ 20000000
    
    volatile unsigned char counter;			//value can be changed automatically during execution without modification through a function 
    
    __CONFIG(FOSC_HS & BOREN_OFF & DEBUG_ON & WDTE_OFF & CP_OFF & CPD_OFF & LVP_ON & PWRTE_OFF);
    
    void init()
    {
    	PIE1=0b00000001;						//Peripheral Interrupt Enable
    	INTCON=0b01000000;							//Interrupt Control Register
    	OPTION_REG=0b10000000;					//Pullup resistor enabled; active low bit ~RBPU
    	T1CON=0b00110101;						//Timer 1 Control Register: Enable TMR1 using Internal Oscillator at FOSC/4
    	TRISB=0x0;							//Configure as Output
    }
    
    void main(void)
    {
    	counter=0;
    	init();			//initialize
    	ei();			//enable interrupts
    
    /* Always initialize interrupts first and enable them later */
    
    	while(1)
    	{
    		PORTB=counter;
    	}
    }
    
    and the ISR

    Code:
    //IntFunc.c
    
    #include<htc.h>
    
    extern volatile char counter;		//var defined somewhere else but used here
    
    void interrupt my_isr()				//associate the function with the Interrupt Vector using keyword
    {
    	if((TMR1IE)&(TMR1IF))			//if TIMER1 interrupt is enabled & TIMER1 is overflowing
    	{
    		counter++;
    		TMR1IF=0;					//reset TMR1 interrupt flag
    	}
    }
    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.

    Attached Files:

    Last edited: Jul 12, 2012
  15. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    Messages:
    5,939
    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
  16. t06afre

    t06afre AAC Fanatic!

    Joined:
    May 11, 2009
    Messages:
    5,939
    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
  17. ActivePower

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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.
  18. takao21203

    takao21203 Well-Known Member

    Joined:
    Apr 28, 2012
    Messages:
    2,572
    Location:
    Ireland
    Code:
    if((TMR1IE)&(TMR1IF))
    should not this be

    Code:
    if((TMR1IE)&&(TMR1IF))
    ??

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

    ActivePower Thread Starter Member

    Joined:
    Mar 15, 2012
    Messages:
    153
    Location:
    India (GMT + 5.5)
    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
Similar Threads
Forum Title Date
Programmer's Corner I2C in PIC16F877A Aug 11, 2013
Programmer's Corner Serial comm using pic16f877a.... Apr 11, 2013
Programmer's Corner Adc in pic16f877a... Mar 23, 2013
Programmer's Corner Help needed for programming PIC16F877A... Jan 4, 2013
Programmer's Corner how to code the 12 dc motor using pic16f877a in c language with l293 Dec 21, 2012

Share This Page