View previous topic :: View next topic |
Author |
Message |
alext
Joined: 27 Mar 2006 Posts: 11
|
Posted: Mon Mar 27, 2006 8:58 pm Post subject: Problem with Timers |
|
|
Hi everybody
I have a real problem trying to config a timer for counts of a few seconds. I'm using a 89c2051 microprocessor and I know the timers/counters in this micro reach 65535 time units in mode 1, but at 12MHz clcok its only 65 ms. I was trying to make a program using a external variable.
The timer is configured to count 50000 us and the variable n counts how many times the timer overflowes. I'm trying to recharge the timer with the value 15535 so teh timer counts 50000 us again, but the problem is that after the first overflow the timer doesnt charge on 15535.
Here is the code I wrote:
Code: | $regfile = "8052.dat"
$crystal = 12000000
$baud = 4800
Config Timer0 = Timer , Gate = Internal , Mode = 1
Enable Interrupts
Enable Int0
Enable Timer0
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.0 , Rs = P1.1
Dim N As Byte
Init:
Counter0 = 15535
Start Timer0
Do
nop
Loop
Finish:
End
On Int0 Int_0 Nosave
Int_0:
Display On
Cls
Lcd "On"
Wait 1
Return
On Timer0 Int_t0
Int_t0:
Cls
Lcd "Inter. Timer 0"
N = N + 1
If N = 99 Then
N = 0
Goto Finish
Else
Goto Init
End If
Return |
What I'm doing wrong? How could I do this easier?
Please help me. |
|
Back to top |
|
|
Vilko
Joined: 25 May 2004 Posts: 190 Location: Ljubljana, Slovenia
|
Posted: Mon Mar 27, 2006 9:22 pm Post subject: Well |
|
|
Well, if i may express my humble opinion about your program:
1. I miss Load Timer0
2. Timer interrupt program Int_t0 shoud not have statement
Goto Anyplaceinmainprogram
But i may be wrong. |
|
Back to top |
|
|
alext
Joined: 27 Mar 2006 Posts: 11
|
Posted: Tue Mar 28, 2006 8:27 pm Post subject: |
|
|
Hi Vilko
Thanks, for the reply.
About the code, must I use the instruction Load to charge the value on the timer? I thought this instruction is used qhen you work on mode 2, but On this mode I only can reach 255 time units max? What's the difference between Load TimerX, value and CounterX = value
Best regards,
ALEX |
|
Back to top |
|
|
Vilko
Joined: 25 May 2004 Posts: 190 Location: Ljubljana, Slovenia
|
Posted: Tue Mar 28, 2006 8:59 pm Post subject: Well |
|
|
Well, i understand, Timer is like alarm clock, you must set the clock, when you would have interrupt.
Timer measures time in let us say Timer units.
Timer unit is 12 / f where f is crystal frequency. So if your crystal is 12 Mhz, then so called timer unit is exactly 1 usec.
With Mode parameter you specify a multiplication factor which is
1 when mode is 2
32 when mode is 0 and
256 when mode is 1
Then you must set the alarm clock with load timer value. This is max 255 because it takes one byte
So if you set for example
load timer0 200
mode = 1
you will get interrupt after 200 * 256 = 51200 usec
When this occurs your Timer interrupt subroutine takes control. This routine mostly counts howmany times the timer interrupt has in a special counter defined by you, reloads Timer for next interrupt and return control with return statement to interrupted main program.
Vilko |
|
Back to top |
|
|
alext
Joined: 27 Mar 2006 Posts: 11
|
Posted: Wed Apr 05, 2006 7:05 pm Post subject: Thanks |
|
|
Hi Vilko
Sorry for the late post. Thanks a lot for the last reply it was very usuful
and finally I managed to complete the 1 second count.
Thanks again |
|
Back to top |
|
|
Vilko
Joined: 25 May 2004 Posts: 190 Location: Ljubljana, Slovenia
|
Posted: Thu Apr 06, 2006 6:42 am Post subject: I am glad |
|
|
I am glad, if i was usefull. |
|
Back to top |
|
|
EngMedhatSaber
Joined: 27 Jul 2004 Posts: 4
|
Posted: Tue May 02, 2006 7:11 pm Post subject: |
|
|
Hi vilko
you are alwayes usefull you are good support eng. |
|
Back to top |
|
|
|