Hi,
This source code is meant to be a timer. But I do not know why when i start my run the timer it will run until 0.49sec and start all over again from 0sec. It only happens when i start to run my program each time. Can anyone tell me where is the error??
This source code is meant to be a timer. But I do not know why when i start my run the timer it will run until 0.49sec and start all over again from 0sec. It only happens when i start to run my program each time. Can anyone tell me where is the error??
Code:
#define _XTAL_FREQ 20000000
#include <htc.h>
#include <stdio.h>
#include <p18f4550.h>
#include "config.h"
#include "lcd.h"
#include <delays.h>
//global variable
int count=0,millisec=0,sec=0;
char TIMER_sec[20];
void interrupt tm1()
{
if(TMR1IE&&TMR1IF)
{
count++;
// if(count==3)
if(count==6)
{
if(millisec<99)
millisec++;
else
{
millisec=0;
sec++;
}
count=0;
}
TMR1IF=0;
TMR1IE=0;
}
}
void init()
{
TRISB=0B00000000;
PORTB=0B00000000;
TRISD=0B00000000;
PORTD=0B00000000; //LCD
//interrupts
RCONbits.IPEN=1; //Enables priority levels on interrupts
PEIE=1; //Enables all unmasked peripheral interrupts(PEIE)
GIE=1; //Enables all unmasked interrupts(GIE)
PIE1bits.TMR1IE=1; //Enables the TMR1 overflow interrupt
IPR1bits.TMR1IP=1; //TMR1 Overflow Interrupt Priority bit (high priority)
PIR1bits.TMR1IF=1; //TMR1 register did not overflowed
//Timer1 Control Register
T1CONbits.RD16 = 1; // 16-Bit = Read/Write Mode Enable bit
T1CONbits.T1RUN = 1; // Device clock is derived from another source
T1CONbits.T1CKPS1 = 1; //1:8 Prescaler value
T1CONbits.T1CKPS0 = 1;
T1CONbits.T1OSCEN = 0; //Timer1 oscillator is shut down
T1CONbits.T1SYNC = 1; //Do not synchronize external clock input
T1CONbits.TMR1CS = 1; //External clock from RC0/T1OSO/T13CKI pin (on the rising edge)
T1CONbits.TMR1ON = 1; // starts Timer1
}
void main ()
{
init();
lcd_init();
while(1)
{
if(T1CONbits.TMR1ON)
{
TMR1IE=1; // Enables the TMR1 overflow interrupt
TMR1IF=1;
}
sprintf( TIMER_sec , "%d.%d sec", sec,millisec);
lcd_goto(1,1);
lcd_puts(TIMER_sec);
}
}
Last edited by a moderator: