Hi-tech C PIC16F877A need help

Discussion in 'Programmer's Corner' started by ericyeoh, Feb 12, 2011.

  1. ericyeoh

    Thread Starter Active Member

    Aug 2, 2009
    58
    0
    Guys,

    Any 1 hv ideas how to use Timer2 as counter to count the value of 1second. Cause i need the microcontroller PIC16F877A to interface with LCD to display current time in the form of
    HH:MM:SS AM/PM.

    Please kindly share your ideas.
     
  2. ELECTRONERD

    Senior Member

    May 26, 2009
    1,146
    16
    I haven't used Timer2 yet, but here's code for Timer1. It will be very similar, so you should be able to figure it out from here. This is a frequency generating function, but if you use the prescaler for the Timer2, you can make it have longer delays. Then just change the name to "timer" function! You won't need any input data for the function, so just put in "void". Also, you don't necessarily need GPIO, so don't output the data from GPIO.

    PIC12F629

    Code ( (Unknown Language)):
    1. void freq(char pins, unsigned int frqcy, unsigned int timer)       // Frequency Function using Timer1 (16-Bit)
    2.     {    
    3.             unsigned int times;
    4.             TMR1IF = 0;       // Clear OVF Flag
    5.             GPIO = pins;       // Initialize GPIO
    6.        
    7.             for(times=timer;times>0;times--)       // Duration of playing
    8.             {
    9.                 TMR1H = (frqcy >> 8) & 0xFF;       //Load TMR0H byte first
    10.                 TMR1L = frqcy & 0xFF;       // Load TMR0L byte next
    11.                 while(!TMR1IF);       // Wait for timer
    12.                 TMR1IF = 0;       // Clear OVF Flag
    13.                 GPIO = ~GPIO;       // Invert output  
    14.             }
    15.             TMR1IF = 0;    // Clear OVF Flag
    16.     }
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    This will depend on your cpu clock speed. The most common is to use Timer1 for this purpose. And a clock crystal. Will you be using interrupts?
     
Loading...