Basic timer program in asm

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Basic timer program in asm

Jump to solution
642 Views
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.

Labels (1)
Tags (1)
0 Kudos
1 Solution
473 Views
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

 

View solution in original post

0 Kudos
2 Replies
474 Views
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 Kudos
473 Views
MindMond
Contributor I

Thank you, I will try to use that code.

0 Kudos