Basic timer program in asm

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Basic timer program in asm

跳至解决方案
1,395 次查看
MindMond
Contributor I

Hello,

 

I´m really confused about the behavior of TPM, so I wonder if someone could give me a basic example of a program that uses a timer (TPM) in assembly language. I want, for example, to turn on a LED, wait 3 seconds and then turn off the LED. I'm using the microcontroller MC9S08QE128.

 

Thank you.

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
1,226 次查看
bigmac
Specialist III

Hello,

 

Simple tasks involving long time delays, such as to control the flashing of a LED, do not usually require very precise timing intervals (who would notice whether the LED flash was actually 2.9 or 3 seconds duration).  With this in mind, a frequently used approach is to divide the longer time interval into a sequence of much shorter intervals.  The period of the shorter interval will determine delay resolution and timing uncertainty.

 

The TPM module can be used for the shorter interval timing (or "tic" interval).  For this purpose, a TPM channel can be utilized, using software output compare mode.  Alternatively, the tic interval could be based on the period between TPM overflows - an even simpler process.

 

For example, if we assume a bus frequency of 8MHz, a prescale division by 4, and a free-running TPM, the TPM overflow period will be 32.77 ms.  A delay of approximately 3 seconds will require 92 overflow periods, with the actual delay being somewhere between 91 and 92 periods.

 

For each delay "channel" of this type, and depending on the maximum delay required, you would define a RAM counter variable of either 8-bit or 16-bit size.  Within the TPM overflow ISR code you might have the following code for each 8-bit variable, assuming that the variables are within zero page RAM.

 

     LDA   DLYCNT

     BEQ   *+4    ; Skip next if already zero

     DEC   DLYCNT

 

The following code provides a sub-routine that does not exit until the delay period is complete.

 

; DELAY SUB-ROUTINE:; On entry ACC = delay value in multiples of 31.77 msWAIT_DELAY:        STA  DLYCNTWD1:    ; Clear watchdog timer macro here        LDA  DLYCNT        BNE  WD1      ; Loop if no timeout        RTS

 

Regards,

Mac

 

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,227 次查看
bigmac
Specialist III

Hello,

 

Simple tasks involving long time delays, such as to control the flashing of a LED, do not usually require very precise timing intervals (who would notice whether the LED flash was actually 2.9 or 3 seconds duration).  With this in mind, a frequently used approach is to divide the longer time interval into a sequence of much shorter intervals.  The period of the shorter interval will determine delay resolution and timing uncertainty.

 

The TPM module can be used for the shorter interval timing (or "tic" interval).  For this purpose, a TPM channel can be utilized, using software output compare mode.  Alternatively, the tic interval could be based on the period between TPM overflows - an even simpler process.

 

For example, if we assume a bus frequency of 8MHz, a prescale division by 4, and a free-running TPM, the TPM overflow period will be 32.77 ms.  A delay of approximately 3 seconds will require 92 overflow periods, with the actual delay being somewhere between 91 and 92 periods.

 

For each delay "channel" of this type, and depending on the maximum delay required, you would define a RAM counter variable of either 8-bit or 16-bit size.  Within the TPM overflow ISR code you might have the following code for each 8-bit variable, assuming that the variables are within zero page RAM.

 

     LDA   DLYCNT

     BEQ   *+4    ; Skip next if already zero

     DEC   DLYCNT

 

The following code provides a sub-routine that does not exit until the delay period is complete.

 

; DELAY SUB-ROUTINE:; On entry ACC = delay value in multiples of 31.77 msWAIT_DELAY:        STA  DLYCNTWD1:    ; Clear watchdog timer macro here        LDA  DLYCNT        BNE  WD1      ; Loop if no timeout        RTS

 

Regards,

Mac

 

0 项奖励
回复
1,226 次查看
MindMond
Contributor I

Thank you, I will try to use that code.

0 项奖励
回复