 | Discussion Forum |  |
|
|
TimerNext Thread | Thread List | Previous Thread Start a Thread | Settings | Details | Message |
|---|
Read-Only Author David M Posted 24-Mar-2003 01:17 GMT Toolset None |  Timer David M Hi
This program i am using has two different timer routines and i dont what each one of them is doing.In the first one i do not understand what is meant by the Timer Register for the high byte(TH0) and v being shifted by 8. Also for routine number 2, what is meant by SCON = 0x50; and TMOD |= 0x20 ;.Where did the value 213 for TH1 come from(TH1=213)?
1)void timer0 (void) interrupt 1 using 1 { /* Int Vector at 000BH, Reg Bank 1 */ unsigned char i; unsigned int v; TR0 = 0; // stop timer v = TL0 | (TH0>>8); v += (unsigned int) (T0_CLOCK); TL0 = (unsigned char) v; TH0 = (unsigned char) (v >> 8); TR0 = 1; // start timer
2)void main (void) { bit CanMessageReq = 0; // Remote Frame Requested // init serial interface SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */ TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */ TH1 = 213; /* TH1: reload value for 1200 baud @ 20MHz */ TR1 = 1; /* TR1: timer 1 run */ TI = 1; /* TI: set TI to send first char of UART */ // ToDo take timer from RTX51 Tiny #define PERIOD -500 /* 250 usec interrupt period */ /* setup the timer 0 interrupt */ TL0 = (unsigned char) (T0_CLOCK); TH0 = (unsigned char) (T0_CLOCK >> 8); TMOD = TMOD | 0x01; /* select mode 1 */ TR0 = 1; /* start timer 0 */ ET0 = 1; /* enable timer 0 interrupt */
| | Read-Only Author Stefan Duncanson Posted 24-Mar-2003 10:09 GMT Toolset None |  RE: Timer Stefan Duncanson v is a 16 bit integer, TH0 is an 8 bit register containing the high 8 bits of a 16 bit timer. So, to get the high 8 bits of v into TH0 you have to rightshift by 8, likewise to transfer the contents of TH0 into the high half of v you have to rightshift it by 8.
Regarding the contents of TMOD and SCON look in the device datasheet for a full explanation. The datasheet will also show you how the reload value of TH1 is calculated for baudrate generation.
Stefan | | Read-Only Author Stefan Duncanson Posted 24-Mar-2003 10:16 GMT Toolset None |  RE: Timer Stefan Duncanson Oops! Some of that was rubbish. To transfer TH0 into the high half of v you would have to leftshift it.
I think your code was doing something different, unfortunately I can't see it while typing this reply.
Stefan | | Read-Only Author Stefan Duncanson Posted 24-Mar-2003 10:19 GMT Toolset None |  RE: Timer Stefan Duncanson The line:
v = TL0 | (TH0>>8);
is a bit strange as TH0>>8 would always be zero. Maybe it should be TH0<<8?
Stefan | | Read-Only Author Martin M Posted 24-Mar-2003 18:33 GMT Toolset None |  RE: Timer Martin M hi
Thanx for your help Stefan.I do not know if it is right shifting or left shifting but i will find out.One more thing, do you know what is the code below doing?
/******************************************************************************/ /* Timer 0 interrupt service function */ /* executes each 1ms @ 20 MHz Crystal Clock */ /******************************************************************************/ #define INT_CLOCK 0.001 // timer interval 0.001Sec #define XTAL 20000000 #define X2 0 // set to 1 if in X2 Mode
#define _CLOCKVAL ((unsigned int) ((XTAL/(12/(X2+1))) * INT_CLOCK))
#define T0_CLOCK ((-_CLOCKVAL)+13) | | Read-Only Author Martin M Posted 24-Mar-2003 18:35 GMT Toolset None |  RE: Timer Martin M hi
Thanx for your help Stefan.I do not know if it is right shifting or left shifting but i will find out.One more thing, do you know what is the code below doing?
/******************************************************************************/ /* Timer 0 interrupt service function */ /* executes each 1ms @ 20 MHz Crystal Clock */ /******************************************************************************/ #define INT_CLOCK 0.001 // timer interval 0.001Sec #define XTAL 20000000 #define X2 0 // set to 1 if in X2 Mode
#define _CLOCKVAL ((unsigned int) ((XTAL/(12/(X2+1))) * INT_CLOCK))
#define T0_CLOCK ((-_CLOCKVAL)+13)
| | Read-Only Author Stefan Duncanson Posted 25-Mar-2003 09:57 GMT Toolset None |  RE: Timer Stefan Duncanson Well, I'd guess it's calculating the reload value for timer 0 required to generate an interrupt approximately every millisecond with a 20MHz clock.
I think you need to have a look at a good book on 'C'. I'd recommend:
"C: A Reference Manual" by Harbison and Steele ISBN 0-13-326224-3
I think you should also read the datasheet for the device in conjunction with the "Intel MCS 51 Microcontroller Family User's Manual" which you can find on the Intel website.
Stefan | |
Next Thread | Thread List | Previous Thread Start a Thread | Settings |
|