使用8位定时器的汇编延迟

时间:2016-03-04 14:17:40

标签: assembly timer atmega

架构:ATmega8535

频率:1 MHz

;Program to sequence LEDs on port C using the 8 bit Timer/Counter0 to generate an interrupt delay 

 ;Stack and Stack Pointer Addresses 
.equ     SPH    =$3E              ;High Byte Stack Pointer Address 
.equ     SPL    =$3D              ;Low Byte Stack Pointer Address
.equ     RAMEND =$25F             ;Stack Address

 ;Interrupt Control Addresses 
.equ     TIMSK  =$39              ;Timer/Counter Interrupt Mask Address 

 ;Timer Addresses for 8 bit Timer/Counter0 
.equ     TCCR0  =$33              ;Timer/Counter0 Control Register Address
.equ     TCNT0  =$32              ;Timer/Counter0 Address 

 ;Port Addresses 
.equ     PORTC  =$15              ;Port C Output Address 
.equ     DDRC   =$14              ;Port C Data Direction Register Address 

 ;Interrupt Vector Addresses 
.equ     OVF0addr=$009            ;Overflow0 Interrupt Vector Address


 ;Register Definitions 
.def     leds   =r0               ;Register to store data for LEDs 
.def     temp   =r16              ;Temporary storage register 
.def     time   =r18              ;Timer Counter 

 ;Interrupt service vector 
.org     $0000 
        rjmp   reset             ;Reset vector 

 ;Set interrupt vectors 
.org     OVF0addr 
        rjmp   OVF0              ;Timer/Counter0 overflow vector 

.org     $0015                      ;Program address 
;Program Initialisation 
;Set stack pointer to end of memory 
reset:   ldi    temp,high(RAMEND) 
          out    SPH,temp          ;Load high byte of end of memory address 
          ldi    temp,low(RAMEND) 
          out    SPL,temp          ;Load low byte of end of memory address 

;Interrupt Initialisation 
          ldi    temp,$01
          out    TIMSK,temp        ;Timer/Counter0 overflow interrupt enable 

;Initialise Timer/Counter0 
          ldi    temp,$04 
          out    TCCR0,temp        ;Load Timer/Counter0 pre scalar = clock/256 
          rcall  cntreset           ;Set initial start value of counter 

;Initialise output ports 
          ldi    temp,$FF 
          out    DDRC,temp          ;Set Port C for output by sending $FF to direction register 

;Initialise Main Program 
          sec                      ;Set carry to 1 
          clr    leds              ;Clear LEDs 
          sei                      ;Enable interrupts 

;Main Program 
loop:    rjmp   loop             ;Repeat forever 


 cntreset:ldi    time,$9C          ;Start count from 156 to give a delay of 25.344 ms 
          out    TCNT0,time        ;Set Timer/Counter0 with start from count
          ret 

 ;Timer/Counter0 overflow Interrupt Service Routine 
OVF0:     rcall  cntreset         ;Set initial start counter  
          out    PORTC,leds       ;Display leds data on port C 
          rol    leds             ;Rotate leds left by 1 bit through carry flag  
          reti                    ;Return and enable interrupts again 

在此代码中有此注释Start count from 156 to give a delay of 25.344 ms我无法弄清楚如何计算25.344 TCCR0设置为$ 04,因此定时器预标量值设置为256.这是8位定时器。

如何从上述规范中获得25.344的延迟?

0 个答案:

没有答案