Code: Select all
/*
Copyright : Chathura Dhananjaya Yapa Bandara
Eyenix Technologies
137/4C, Main Street ,Rajamalwaththa, Baththaramulla
Compiler : Mikroc Pro (mikroelektronika)
Microcontroller : PIC18f252
Crystal:20Mhz
Objective : 16 bit Timer 1 interrupt handling & Testing the Timer capabilities
interrupt period : 0.1 s
Timer value : 1Second can be changed by changing the value of timeval variable
when timeval =10 , timer period =1 Second
Description : Timer 1 interrupt will occur every 0.1 s. So the the time period
can be handled by changing the timeval variable. here I have assigned it 10 which
is equivalent to 1 second. So by default the code will toggle portb every 1 second
*/
unsigned short count = 0;
unsigned short timeval = 10; // 10 x interrupt value (1Sec)
void main() {
trisb=0;
// Prescaler=1:8; TMR1 Preset=3036; Freq=20.00Hz; Period=.1
T1CKPS1_bit = 1;// bits 5-4 Prescaler Rate Select bits
T1CKPS0_bit = 1;//
T1OSCEN_bit = 0;// bit 3 Timer1 Oscillator Enable Control: bit 1=on
T1SYNC_bit = 1;// bit 2 Timer1 External Clock Input Synchronization Control bit: 1=Do not synchronize external clock input
TMR1CS_bit = 0;// bit 1 Timer1 Clock Source Select bit: 0=Internal clock (FOSC/4) / 1 = External clock from pin T13CKI (on the rising edge)
TMR1ON_bit = 1;// bit 0 enables timer : off to be enabled in the burst charge mode
TMR1H = 0xB; // preset for timer1 MSB register
TMR1L = 0xDC; // preset for timer1 LSB register
PEIE_bit = 1; // pheripheral interrupt enable for
TMR1IE_bit = 1; // Timer 1 interrupt enable.
GIE_bit = 1; // Global interrupt enable
portb = 0x00;
while(1);
}
void interrupt()
{
if (TMR1IF_bit)
{
TMR1IF_bit = 0;
TMR1H = 0xB; // preset for timer1 MSB register
TMR1L = 0xDC; // preset for timer1 LSB register
count++;
if(count==timeval)
{
// the work to be done which is toggle portb as an example
count=0;
portb=~portb;
}
}
}