need help in 16f877a timer programe.

Status
Not open for further replies.

kannel820

Newbie level 1
Joined
Dec 22, 2009
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,286
hi all,

im new in PIC for 16f877a, can anyone have any example on how to have a timer triggering in this PIC.

i need a timer which can provide me a signal input in every 3hours. and restart the timer when i need it to continue count for another 3 hours.

pls help.
 

One way to do this is to have a global clock.
You configure Timer 1 to interrupt, say every 5mS. You can use Mplab Sim to check the timing using a breakpoint in the interrupt routine. Use the Mplab Sim stop watch to check the interrupt frequency. Pre-load Timer 1 so you get a 5mS interrupt.
You then use the interrupt routine to increment the global clock.


Code:
/*--- Global clock ---*/

unsigned char seconds;
unsigned char minutes;
unsigned char hours;

/*--- Initialise Timer 1 ---*/

static void init_timer1(void)
  {
  T1CON = 0x30; /* Prescale 1:8 */
  TMR1IF = 0;
  TMR1IE = 1; 
  TMR1ON = 1; 

  seconds = 0;
  minutes = 0;
  hours = 0;
  }


/*--- Interrupt vector ---*/

void interrupt isr(void)
  {
  static unsigned char five_mS = 0;

  if(TMR1IF)
    {
    five_mS++;

    if(five_mS >= 200){
      seconds++;
      five_mS = 0;
      }

    if(seconds >= 60){
      seconds = 0;
      minutes++;
      }

    if(minutes >= 60){
      minutes = 0;
      hours++;
      }


    TMR1ON = 0;      /* Pre-load Timer 1 for 5mS interrupt frequency */
    TMR1H = 0xfd;
    TMR1L = 0x94;
    TMR1ON = 1;
    TMR1IF = 0;
    } 
  }

/*--- End of File ---*/
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…