??? 12/05/09 04:32 Read: times |
#171463 - now using timer 0 and ext intr 0 Responding to: ???'s previous message |
Dear members,
I was not successful in measuring frequency with the timer . So i used the timer 0 in 16 bit auto reload mode and the external interrupt 0. 1)According to my calculation for a frequency of 100Hz, the timer should count from 0000h to 2400h. but if i run my code, the counter counts from 0000h to 1FF1 and overflows 1 time. 2)According to my calculation for a frequency of 50Hz, the timer should count from 0000h to 4801h. but if i run my code, the counter counts from 0000h to 3FF4 and overflows 2 time. 3)And,According to my calculation for a frequency of 20Hz, the timer should count from 0000h to B402h. but if i run my code, the counter counts from 0000h to 9FF7 and overflows 5 time. Now if i sum this up, the time taken (Ideally) by one timer tick should be 1.085 micro second(11.0592Mhz crystal). But if i see it practically in my code when it runs, the time taken is 0.1356micro second. Although this is a constant value over all frequency values that i have checked, it is a bit weird. Am i dog something wrong ?? why is it not matching my calculations ?? Here's my code. In this, whenever a negative edge comes, we jump to the external interrupt ISR. In this we stop the timer store the values of the timer register and the overflow value. then we reset the timer register and start it. Now then we have to send the values(stored) to the PC, we stop both the interrupts, send the values and again start the interrupts. #include <reg51.h> #include <generic.h>//this contains the functions for delays(sec, mili sec and micro sec) #include<serial.h> /*============================================================================= SET FREQUENCY : 50Hz Each half of the AC cycle lasts 0.01 sec. and the timer counts 0000h to FFFFh in this time. We will control this time and switch on and off pin 1.0 that is the output. We will take the ext int 0 negative edges as the start point. /*=============================================================================*/ char present_value_L = 0x00, present_value_H = 0x00,count = 0x00,count_send = 0x00; /*==============EXTERNAL INTERRUPT 0 SUBROUTINE================================*/ /* When there is the Negative edge at the P3^2 the control jumps to the ISR This routine first stops the timer, then stores the present timer register values using present_value_L and present_value_H respectively. Then reset the timer register and starts the timer. */ void ex0_isr (void) interrupt 0 { TR0 = 0; //here i stop the timer present_value_L = TL0; //store the present timer lower byte present_value_H = TH0; //store the present timer higher byte count_send = count; //store the number of overflows count = 0x00; //resets the number of overflows TL0 = 0x00; //reset the timer register lower byte TH0 = 0x00; //reset the timer register higher byte TR0 = 1; } /*====================TIMER INTERRUPT 0 SUBROUTINE==============================*/ /* When the timer overflows it stops the trigger. I here stop the timer as well so that it does not keep on counting. */ void timer0_ISR (void) interrupt 1 { count++;//counts the number of times the counter has overflown } /*=============================================================================*/ void initialize_global_interrupt(void) { IE = 0x83;//10000011 global enable, ext int 0 ,timer int 0 and timer int 1 (for baud rate) enabled } /*=============================================================================*/ void initialize_timer_0(void) { TR0 = 0; TMOD = 0x01; //00000001Set Mode(16-bit timer with reload) TL0 = 0x00; //Loading the initial value, lower Byte TH0 = 0x00; //Loading the initial value, Higher Byte } /*=============================================================================*/ void initialize_external_int_0(void) { IT0 = 1; //making the external interrupt edge trigerred } /*============================================================================= /*=============================================================================*/ void main (void) { //Here we start the initialization of interrupts initialize_timer_0(); initialize_external_int_0(); initialize_serial(); initialize_global_interrupt(); //dummy values sent while(1) { TR0 = 0; //here we stop the timer EX0 = 0; //disable the external interrupt msec_wait(1); //just a wait SBUF = present_value_H; //send the higher byte serially msec_wait(1); SBUF = present_value_L; //send lower byte serially msec_wait(1); SBUF = count_send; //send the overflow value serially msec_wait(1); EX0 = 1; //enable ext. interrupt TR0 = 1; //start the timer sec_wait(5); //wait for 5 sec } } /*====================================*/ |
Topic | Author | Date |
Using timer 2 for frequency measurement | 01/01/70 00:00 | |
Latencies | 01/01/70 00:00 | |
Big differences | 01/01/70 00:00 | |
I'm using a PLC and a Oscilloscope !! | 01/01/70 00:00 | |
2% jitter... | 01/01/70 00:00 | |
Try a free-running timer and average 10 periods | 01/01/70 00:00 | |
Dear Per | 01/01/70 00:00 | |
You may use timer2 if you want | 01/01/70 00:00 | |
Latency... | 01/01/70 00:00 | |
Frequency Measure | 01/01/70 00:00 | |
now using timer 0 and ext intr 0 | 01/01/70 00:00 | |
giving up without proof that ... | 01/01/70 00:00 | |
Your calculations are OK![]() | 01/01/70 00:00 |