Hello freaks,
As a newbie I tried to measure a pulslength. According to the Atmel documentation this should be very easy. But I don't get it working. The only thing my program does is counting interrupts that are obviously generated by the program itself, because it does so even when I ground the inputpin.
I try to measure the length of pulses of about 75 to 300 msec. The accucacy is not important.
The programm displays 4 registers on a LCD which should make visible the pulslength and pulspause. The only thing that happen is counting up followed by a reset, for ever. The speed depends on the clock divide factor.
Who can tell me what is wrong in my program? Thanks!
My program:
Code:
; pulse.asm
.include "m162def.inc" ; ATMEGA162 4Mc
.org $0000
rjmp start
.org $0018
rjmp TIM1_CAPT ; timer1 event capture interrupt
.org $0030
start:
ldi r16,low(RAMEND)
out spl,r16
ldi r16,high(RAMEND)
out sph,r16
;init timer1
clr r16
ldi r16,1<<TICIE1 ; set timer1 capture event interrupt
out TIMSK,r16
ldi r16,(1<<ICES1)|(1<<ICNC1)|(1<<CS10)|(1<<CS12) ;noise; rising; /1024
out TCCR1B,r16
ser r17 ; now rising edge r17=ff
ser r16
out DDRC,r16 ; port C output
cbi DDRC,PC0 ; port C pin 0 capture
sei
loop:
; this loop displays r18,r19,r20 and r21 on a LCD
; rcall LCD
rjmp loop
;timer input capture interrupt
TIM1_CAPT:
push r16
in r16,SREG ; save SREG
push r16
in r18,ICR1L ; read ICR low
in r19,ICR1H ; read ICR high
sbrs r17,1 ; was rising(r17=ff)?skip
rjmp pulslength
rjmp pulspause
pulslength: ; event was end of puls
mov r20,r18 ; r20/21 pulslength
mov r21,r19
clr r16
ldi r16,(1<<ICNC1)|(1<<ICES1)|(1<<CS12)|(1<<CS10) ;nonoise; rising; /1024
out TCCR1B, r16
ser r17 ; remember rising r17=ff
rjmp clearandexit
pulspause: ;event end of pause
;r18/19 all on pauselenth
clr r16
ldi r16,(1<<ICNC1)|(0<<ICES1)|(1<<CS12)|(1<<CS10) ;nonoise, decreasing, /1024
out TCCR1B, r16
clr r17 ; remember decreasing r17=00
clearandexit:
clr r16
out TCNT1H,r16 ; clear timerreg
out TCNT1L,r16 ; first high then low
pop r16
out SREG,r16
pop r16
reti
; PM: include LCD display routine
|