PIC16F877A led blinking problem

Discussion in 'Embedded Systems and Microcontrollers' started by johnny_sjs, Jul 14, 2011.

  1. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    Hello Friends,
    I am using PIC16F877A for a project.
    I am using "MPLAB - HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.82" for coding.
    I managed to write simple led blinking program, building was success, tried simulator as well.
    But none of the leds are blinking.
    Can someone please take a look at what's wrong.
    I am also doubtful about setting the configuration bits.
    Program:

    #include<htc.h>
    #include<pic.h>
    //__CONFIG(FOSC_HS|WDTE_OFF|PWRTE_ON|LVP_OFF|DEBUG_OFF);
    __CONFIG(FOSC_HS|WDTE_OFF|PWRTE_ON|BOREN_OFF|LVP_OFF|CPD_OFF|WRT_OFF|DEBUG_OFF|CP_OFF);

    void main(void)
    {
    TRISA=0;
    TRISB=0;
    TRISC=0;
    TRISD=0;
    PORTA=0xFF;
    PORTB=0xFF;
    PORTC=0xFF;
    PORTD=0xFF;

    while(1)
    {
    PORTA=0xFF;
    PORTB=0xFF;
    PORTC=0xFF;
    PORTD=0xFF;
    }
    }


    Thanks.
     
  2. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    Sorry not a blinking program, just glowing program.
     
  3. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Try setting 2 of the ports to 0 and then see if it works in the simulator and measure voltages in the real world to see if anything is happening.
    Clearly there is no blinking going on here.
     
  4. blah2222

    Well-Known Member

    May 3, 2010
    554
    33
    Yeah, what you've got there is a bunch of LEDs that just light up. Try turning them off (setting them to 0) with a delay, then back on again.
     
  5. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    Thanks for the Reply.

    The problem is they aren't glowing at all. I think something is missing or configuration problem is there.

    I tried it on PORTB alone, but still no luck, when checked with multimeter, it was showing in milli-volts.

    I found general hex file for led blinking, and it worked perfectly, so it eliminates IC problem.
    But I couldn't find C file, just for understanding general skeleton.

    Please Help.
    PS-Sorry for the confusion.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Got a schematic?

    Your config bits say you use an external crystal, do you have one?

    Any pin with both analog and digital functions will default to analog, and you have not done any "turn off the dern analog" commands. A few pins are pure digital so you may see some go high if you check them all (and the crystal is working).
     
  7. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    @ErnieM-yes, I am using 8 MHz metal crystall.
    I tried it on portB as well, but didn't work.
    Thanks.
     
  8. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    Hi all,
    while searching about configuration, I came across this line:
    __CONFIG(0x3F3A);
    I have seen hex values related to different configuration bits in pic16f877a header file like:
    // Config Register: CONFIG
    #define CONFIG 0x2007
    // Oscillator Selection bits
    // RC oscillator
    #define FOSC_EXTRC 0xFFFF
    // HS oscillator
    #define FOSC_HS 0xFFFE
    // XT oscillator
    #define FOSC_XT 0xFFFD
    // LP oscillator
    #define FOSC_LP 0xFFFC
    // Watchdog Timer Enable bit
    // WDT enabled
    #define WDTE_ON 0xFFFF
    // WDT disabled
    #define WDTE_OFF 0xFFFB
    // Power-up Timer Enable bit
    // PWRT disabled
    #define PWRTE_OFF 0xFFFF
    // PWRT enabled
    #define PWRTE_ON 0xFFF7
    // Brown-out Reset Enable bit
    // BOR enabled
    #define BOREN_ON 0xFFFF
    // BOR disabled
    #define BOREN_OFF 0xFFBF
    // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit
    // RB3/PGM pin has PGM function; low-voltage programming enabled
    #define LVP_ON 0xFFFF
    // RB3 is digital I/O, HV on MCLR must be used for programming
    #define LVP_OFF 0xFF7F
    // Data EEPROM Memory Code Protection bit
    // Data EEPROM code protection off
    #define CPD_OFF 0xFFFF
    // Data EEPROM code-protected
    #define CPD_ON 0xFEFF
    // Flash Program Memory Write Enable bits
    // Write protection off; all program memory may be written to by EECON control
    #define WRT_OFF 0xFFFF
    // 0000h to 00FFh write-protected; 0100h to 1FFFh may be written to by EECON control
    #define WRT_256 0xFDFF
    // 0000h to 07FFh write-protected; 0800h to 1FFFh may be written to by EECON control
    #define WRT_1FOURTH 0xFBFF
    // 0000h to 0FFFh write-protected; 1000h to 1FFFh may be written to by EECON control
    #define WRT_HALF 0xF9FF
    // In-Circuit Debugger Mode bit
    // In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
    #define DEBUG_OFF 0xFFFF
    // In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
    #define DEBUG_ON 0xF7FF
    // Flash Program Memory Code Protection bit
    // Code protection off
    #define CP_OFF 0xFFFF
    // All program memory code-protected
    #define CP_ON 0xDFFF

    I want to know how the final value came i.e "0x3F3A"
    is it AND, OR operation etc.
    Thank you.
     
  9. johnny_sjs

    Thread Starter New Member

    Aug 3, 2010
    10
    0
    hey its working now!

    I figured it out, it can be done from :
    Configure->configuration bits
    after selecting the bits, whatever is the value is shown on left side of oscillator bit option is the overall config value.
    In my case it turned out to be FFBA,
    hence,
    __CONFIG(0xFFBA);
    that's it.

    Thought this might help someone like me!
     
  10. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I did not catch your error before now. The config setting should be combined by using logical AND (&). You have combined them using bitwise or. Hence your trouble
     
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Heh, I missed that too. Config settings are the worst thing to do, as typically if you get 1 bit wrong then nothing works.

    Good job!
     
  12. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    Always write them out and you'll never have that problem like this
    __CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _HS_OSC & _WRT_OFF & _LVP_OFF & _CPD_OFF
     
Loading...