Yes friends
#include <timer interrupt.h>
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int1 two_sec_timer_flag = FALSE,five_sec_timer_flag =FALSE,ten_sec_timer_flag = FALSE ;
int16 two_sec_count,five_sec_count,ten_sec_count;
#INT_TIMER2
void timer2_isr()
{
two_sec_count++;
five_sec_count++;
ten_sec_count++;
{
if(two_sec_count == 400) // 400 * 5 msec = 2000 msec = 2 sec
{
two_sec_count = 0;
two_sec_timer_flag = TRUE;
}
if(five_sec_count==1000) /// 1000 * 5 msec = 5000 msec = 5 sec
{
five_sec_count=0;
five_sec_timer_flag=TRUE;
}
if(ten_sec_count == 2000) // 2000 * 5 =10000 msec = 10 sec
{
ten_sec_count = 0 ;
ten_sec_timer_flag = TRUE;
}
}
}
void main ()
{
int1 last_2sec,last_5sec,last_10sec;
//setup_timer_2(T2_DIV_BY_4,78,16);//setup up timer2 to interrupt every 1ms
setup_timer_2(T2_DIV_BY_4,250,5); //setup up timer2 to interrupt every 1ms
//setup_timer_2 (mode, period, post scale)
//mode - T2_DISABLED, T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16
//period is a int 0-255 that determines when the clock value is reset,
//post scale is a number 1-16 that determines how many timer overflows before
//an interrupt: (1 means once, 2 means twice, and so on).
// calculating timer reload value to generate 1 msec interrupt
// T = 1/f
// T = 1/4MHZ = 1/4* 1000000
// But Microcontroller hardware will divide the clock by 4
// and if we chosen T2_DIV_BY_4 then again there will
// one more division happen by 4
// So T = 1/(4 * 1000000)/ 4 * 4 / T=4/4*1000000
// T = 4*4/4*1000000 = 0.000004 sec = 0.4 usec //4 usec
// ************************************************
// At4mhz, the timer will increment every 0.4 us // 4 usec *// 0.000004 sec
// ************************************************
// ********************************************************
// if period is chosen 250 then timer wi
//ll overflow every *
// 4 * 250 = 1000 usec //1 msec *
// ********************************************************
// *******************************************************************
// And if we chosen post scalar as 5 then timer interrupt will occur *
//1000 usec * 5 = 5000 usec /// 5 msec
// *******************************************************************
last_2sec=FALSE;
last_5sec=FALSE;
last_10sec=FALSE;
enable_interrupts(INT_TIMER2); // enable timer2 interrupt
enable_interrupts(GLOBAL);
output_b(0x0); // off all theLEDS
putc('z');
//infine loop
while (1)
{
if( two_sec_timer_flag == TRUE)
{
two_sec_timer_flag = FALSE;
//printf("I am in 2 sec \n\r");
last_2sec = ~last_2sec;
if ( last_2sec == TRUE)
{
output_b( (input_b() | 0x01)); // ON LED 1 alone
}
else
{
output_b( (input_b() & 0xFE)); // OFF LED 1 alone
}
}
if( five_sec_timer_flag == TRUE )
{
five_sec_timer_flag = FALSE;
// printf ("I am in 5 sec \n\r" );
last_5sec = ~last_5sec;
if ( last_5sec == TRUE)
{
output_b( (input_b() | 0x02)); // ON LED 2 alone
}
else
{
output_b( (input_b() & 0xFD)); // OFF LED 2 alone
}
}
if( ten_sec_timer_flag == TRUE)
{
ten_sec_timer_flag = FALSE;
//printf("i am in 10 sec \n\r");
last_10sec = ~last_10sec;
if(last_10sec==TRUE)
{
output_b( (input_b() | 0x04) ); // ON LED3 alone
}
else
{
output_b( (input_b() & 0xFB)); // OFF LED 3 alone
}
} // end of while
} // end of main
}
- - - Updated - - -
anyone cite me the small change of code and i am in beginning stage of embedded c