Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

1 second delay using timer1

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-8051 Archive
View previous topic :: View next topic  
Author Message
ex4

Bascom Member



Joined: 13 Jan 2006
Posts: 1064
Location: indonesia

indonesia.gif
PostPosted: Tue Dec 12, 2006 5:33 pm    Post subject: 1 second delay using timer1 Reply with quote

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 Crying or Very sad Crying or Very sad
Back to top
View user's profile Visit poster's website AIM Address Yahoo Messenger
Gianni

Bascom Member



Joined: 09 Jul 2004
Posts: 61
Location: Italy

blank.gif
PostPosted: Wed Dec 13, 2006 10:07 am    Post subject: Reply with quote

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
View user's profile
ex4

Bascom Member



Joined: 13 Jan 2006
Posts: 1064
Location: indonesia

indonesia.gif
PostPosted: Sun Dec 17, 2006 11:11 am    Post subject: Reply with quote

how come this is not 1ms??? (i see it on the simulator) Confused Confused

Code:

$crystal = 24000000
$regfile = "89s8252.dat"

Dim X As Word

Config Timer1 = Timer , Mode = 1 , Gate = Internal            'timer1 as 16bit timer
Stop Timer1                                                   'stop the timer
On Timer1 Tmr1_ovl                                            'routine tmr1 overflow

Enable Interrupts                                             'enable global interrupts
Enable Timer1                                                 'enable tmr1 overflow

'Load Timer1 , 63535                                           '1ms timer at 24MHz
Th1 = &HF8
Tl1 = &H2F
Start Timer1                                                  'timer1 start

Do
Loop

Tmr1_ovl:
   'Load Timer1 , 63535
   Th1 = &HF8
   Tl1 = &H2F
   Incr X
Return
 
Back to top
View user's profile Visit poster's website AIM Address Yahoo Messenger
Gianni

Bascom Member



Joined: 09 Jul 2004
Posts: 61
Location: Italy

blank.gif
PostPosted: Mon Dec 18, 2006 2:44 pm    Post subject: Reply with quote

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
View user's profile
ex4

Bascom Member



Joined: 13 Jan 2006
Posts: 1064
Location: indonesia

indonesia.gif
PostPosted: Mon Dec 18, 2006 2:53 pm    Post subject: Reply with quote

mmm, that's why i got bizarre result Laughing Laughing
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
View user's profile Visit poster's website AIM Address Yahoo Messenger
Gianni

Bascom Member



Joined: 09 Jul 2004
Posts: 61
Location: Italy

blank.gif
PostPosted: Mon Dec 18, 2006 4:42 pm    Post subject: Reply with quote

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
View user's profile
ex4

Bascom Member



Joined: 13 Jan 2006
Posts: 1064
Location: indonesia

indonesia.gif
PostPosted: Tue Dec 19, 2006 12:05 am    Post subject: Reply with quote

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
View user's profile Visit poster's website AIM Address Yahoo Messenger
Gianni

Bascom Member



Joined: 09 Jul 2004
Posts: 61
Location: Italy

blank.gif
PostPosted: Tue Dec 19, 2006 12:37 pm    Post subject: Reply with quote

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
View user's profile
ex4

Bascom Member



Joined: 13 Jan 2006
Posts: 1064
Location: indonesia

indonesia.gif
PostPosted: Tue Dec 19, 2006 12:45 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website AIM Address Yahoo Messenger
Gianni

Bascom Member



Joined: 09 Jul 2004
Posts: 61
Location: Italy

blank.gif
PostPosted: Tue Dec 19, 2006 2:14 pm    Post subject: Reply with quote

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
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-8051 Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum