We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello, I am using timer 0 for generating 20msec interrupts, and Timer 1 as a baud rate generator. Now, I require Timer 2 in my project. So, I had studied the pdf's of Phillips 89C52X2 and started writing code as described below. if I need timer 2 in auto-reload mode in up-counting timer style. I need to toggle test led every 1 sec, which is working fine using Timer 0. But, it is not working if I use Timer 2. I need a solution
Please advise.
############## PART OF MY PROJECT CODE ################### unsigned int volatile time_keeper; void serialInit(void) { IE = 0; /* disable the interrupts */ RI=0; //serial port receiver and trans meter is TI=0; T2CON = 0x04; T2MOD = 0x02; TH2 = 0XB8; //load timer 2 for generating 20 msec delay TL2 = 0X00; ET2=1; //enable timer 2 interrupt RCAP2L = 0X00; RCAP2H = 0XB8; TR2=1; //run the timer 2 EA=1; //enable global interrupt flag } void onTimerISR(void)interrupt 5 using 1 { EA = 0; TR2 = 0; TH2 = 0xB8; TL2 = 0x00; TR2 = 1; time_keeper++; //incrementing the timer on every //interruptso we can have delay in // multiples of 20msec if(!(time_keeper%50)) test_led = ~test_led; // test led toggling every 1 second ET2=1; EA=1; //enable Interrupt } void main(void) { time_keeper = 0; serialInit(); demonTask(); } ############## PART OF MY PROJECT CODE ###################
Time to optimize your code manually.
You assign zero to a zero-initialized variable. To make it extra zero?
Note that % isn't the cheapest operator. Also, the max range for your counter is not an even multiple of 50, so your test led will make a strange blink on overflow. Switching to a frequency of 2^n ticks allows you to check a bit if the LED should be on or off.
There must be a number of other things to take a look at in your code.
our code size is too large, if we compile with level 8, it give error for overflow.
Per Westermark Time to optimize your code manually
excellent advice, however many avoid it because it involves 4 letter words like 'read' 'think' (OK, that's 5) and 'work'
Erik