Path: Home => AVR-Overview => Tutorial => Part 3

; Test 3: Learn more about the timer in polling mode


; New things to learn here:

; - Timer in polling mode
; - MOV-command

.NOLIST
.INCLUDE "8515def.inc"
.LIST

; universal register definition


.DEF   mp = R16

; Counter for the number of time-outs of the timer


.DEF   z1 = R0

; Reset-vector to adress 0000


   RJMP   main

; Main program starts here:


main:   LDI   mp,LOW(RAMEND) ;Initiate Stackpointer (Unterprogramme!)
   OUT   SPL,mp
   LDI   mp,HIGH(RAMEND)
   OUT   SPH,mp

; Set software counter-register to zero

   LDI   mp,0 ; z1 is not directly accessible, so we first
   MOV   z1,mp ; write the zero to mp and copy that

; Prescaler of the timer = 1024, 4 MHz/1024 = 3906,25 Hz

; this equals a timer tick every 256 Ás
   LDI   mp,0x05 ;Initiate Timer/Counter 0 prescaler
   OUT   TCCR0,mp ; to Timer 0 Control Register

; Port B is LED-port

   LDI   mp,0xFF ; all bits are output
   OUT   DDRB,mp ; to data direction register

; Main program loop reads the counter until he reaches zero,
; the software counter in incremented and displayed on the LEDs.
; 256 Timer ticks equal 65.536 ms or a frequency of 15.25878906 Hz.
loop:
   IN   mp,TCNT0
; read the 8-bit timer 0
   CPI   mp,0 ; test for zero
   BRNE   loop ; if not zero, return to the loop
   RCALL   IncZ1 ; call subroutine software-counter-incrementation
   RCALL   Display ; call subroutine display-counter
warte:   IN   mp,TCNT0 ; read again timer 0
   CPI   mp,0 ; test for zero
   BREQ   warte ; wait until not zero any more
   RJMP   loop ; goto next round

IncZ1:   INC   z1 ; Increment software-counter
   RET ; return to the main program

Display:
   MOV   mp,z1
; copy counter value to mp
   COM   mp ; One-complement = XOR(FF) due to the LEDs drivers
   OUT   PORTB,mp ; write to LED port
   RET ; Back to Main program


©2002 by http://www.avr-asm-tutorial.net