View previous topic :: View next topic |
Author |
Message |
ex4
Joined: 13 Jan 2006 Posts: 1062 Location: indonesia
|
Posted: Tue Dec 12, 2006 5:33 pm Post subject: 1 second delay using timer1 |
|
|
hello,
i got 24MHz crystal with AT89S8252. I want to generate 1second delay timer using Timer1 in interrupt mode. Anybody can gave me some enlightment?? i got stuck here with the calculation of the timer |
|
Back to top |
|
|
Gianni
Joined: 09 Jul 2004 Posts: 61 Location: Italy
|
Posted: Wed Dec 13, 2006 10:07 am Post subject: |
|
|
The lowest interrupt rate you can get with 24MHz clock is 24e6/12/65536 = 30.51Hz = 32.768ms.
So for 1Hz you can,for example,decide an interrupt rate of 50Hz (24e6/12/50=40000 timer reload value = 65536-40000 = 25536 )and use a variable to count 50 interrupts.
Regards |
|
Back to top |
|
|
ex4
Joined: 13 Jan 2006 Posts: 1062 Location: indonesia
|
|
Back to top |
|
|
Gianni
Joined: 09 Jul 2004 Posts: 61 Location: Italy
|
Posted: Mon Dec 18, 2006 2:44 pm Post subject: |
|
|
Do not use the simulator for that.Timer1 is used for baud rate generator.Use Timer0 instead if you need to use the simulator.BTW you must stop the timer when you write a 16 bit value.
Regards |
|
Back to top |
|
|
ex4
Joined: 13 Jan 2006 Posts: 1062 Location: indonesia
|
Posted: Mon Dec 18, 2006 2:53 pm Post subject: |
|
|
mmm, that's why i got bizarre result
if i used Timer0 for the timer and Timer1 as a 16bit counter, can i get 1second exact delay to measure the frequency at T1 pin?? |
|
Back to top |
|
|
Gianni
Joined: 09 Jul 2004 Posts: 61 Location: Italy
|
Posted: Mon Dec 18, 2006 4:42 pm Post subject: |
|
|
Not very easy to do!
Is not a good idea use many interrupts as time base for frequency measurement.To get precise delay you have to run with interrupt disabled and use a do loop to reach close as possible 1sec. then adjust with nops to get exactly 1sec.
Unfortunately this limit the count to 65535 otherwise you have to manage the counter overflow
Do not forget to trim the xtal oscillator for 24.000.000Hz
Have a look here:
http://www.mcselec.com/index.php?option=com_content&task=view&id=169&Itemid=57 |
|
Back to top |
|
|
ex4
Joined: 13 Jan 2006 Posts: 1062 Location: indonesia
|
Posted: Tue Dec 19, 2006 12:05 am Post subject: |
|
|
Quote: | For Delayword = 1 To 45440
Next Delayword |
that line i don't get it??? where that came from?? any explanation?
i simulate it and got 969ms |
|
Back to top |
|
|
Gianni
Joined: 09 Jul 2004 Posts: 61 Location: Italy
|
Posted: Tue Dec 19, 2006 12:37 pm Post subject: |
|
|
I do not know.
I can suppose that they take in account some interrupts from timer0 overflow.
A more reliable solution is an external precise 1sec pulse on the gate (int0/1) timer pin.
But this require more hardware and for a 2Mhz counter (Max count external frequency for timer is xtal/12)i suppose is not a good choice.
Regards |
|
Back to top |
|
|
ex4
Joined: 13 Jan 2006 Posts: 1062 Location: indonesia
|
Posted: Tue Dec 19, 2006 12:45 pm Post subject: |
|
|
well, really diver from the AVR, but nevermind, i'm not intend to make it really precise one. have build and test it and got 1-2Hz difference and rise as much as 1KHz for counting upto 2MHz. Mmm, 1/2000 difference, not so bad |
|
Back to top |
|
|
Gianni
Joined: 09 Jul 2004 Posts: 61 Location: Italy
|
Posted: Tue Dec 19, 2006 2:14 pm Post subject: |
|
|
If your measurement range are low frequency (<1KHz) instead of measure the frequency you can measure the period,you can put together T0 pin and Int0 pin.You can read the counter when the input signal is low and then calculate the frequency; of course this works for 50% duty cycle |
|
Back to top |
|
|
|