-
problem mit CTC
Hi,
Ich hab irgendwie n Problem mit dem CTC Counter. Ich hab n magnetfeldsensor um ne drehzahl zu messen. Nun will ich, dass jede Sekunde die drehzahl aus m register abgefragt und zur geschwindigkeit hochgerechnet wird (geht um mein RC Car) und vorerst testweiße an UART gesendet wird. Problem dabei is, dass ich am Com-port nich jede Sekunde, sondern dauern, fortlaufen was bekomme.. dann natürlich 0, weil der sensor nich so schnell is.. Von der Logik her müsste es aber eigentlich funktionieren.. Ich habs mit 2 verschiedenen Atmega8 probiert,l bei beiden das selbe. Mein Code:
Code:
.include "m8def.inc"
.def temp = r16
.def counts = r17
.def kmh = r18
.def time1 = r21
.def time2 = r22
.equ CLOCK = 16000000
.equ BAUD = 9600
.equ UBRRVAL = CLOCK/(BAUD*16)-1#
.org 0x000
rjmp main
.org INT0addr
rjmp int0_handler
.org OC1Aaddr
rjmp timer1_compare
main:
ldi temp, LOW(RAMEND)
out SPL, temp
ldi temp, HIGH(RAMEND)
out SPH, temp
ldi temp, LOW(UBRRVAL)
out UBRRL, temp
ldi temp, HIGH (UBRRVAL)
out UBRRH, temp
ldi temp, high(40000-1)
out OCR1AH, temp
ldi temp, low(40000-1)
out OCR1AL, temp
ldi temp, ( 1 << WGM12 ) | ( 1 << CS00 )
out TCCR1B, temp
ldi temp, 1 << OCIE1A ; OCIE1A: Interrupt bei Timer Compare
out TIMSK, temp
ldi temp, (1<<URSEL)|(3<<UCSZ0)
out UCSRC, temp
ldi temp, 0x00
out DDRD, temp
;ldi temp, 0xFF
;out PORTD, temp
ldi temp, 0b00001010 ; INT0 und INT1 konfigurieren
out MCUCR, temp
ldi temp, 0b11000000 ; INT0 und INT1 aktivieren
out GICR, temp
sbi UCSRB,TXEN
sei
loop: rjmp loop
int0_handler:
ldi temp, 1
add counts, temp
reti
timer1_compare:
cpi time1, 100
breq time2u
brne time1w
time2u:
cpi time2, 4
breq rechnekmh
brne time2w
time1w:
inc time1
reti
time2w:
inc time2
reti
rechnekmh:
clr kmh
mov kmh, counts
lsl kmh
clr counts
rjmp uartu
uartu:
sbis UCSRA, UDRE
rjmp uartu
out UDR, kmh
reti
Ich hoffe ihr könnt mir helfen und findet den fehler
Edit: der Zähler zählt bis 40000, dann wird in nem register auf 100, in nem weiteren dann auf 4 gezählt. Somit sind das 16000000 Zählschritte, die auch dem 16MHZ Takt entsprechen, also 1 Sekunde
gruß homedom
-
Nachdem dein Zähler time2 den wert 4 erreicht hat wird der USART gestartet. Da Du aber in dieser Routine weder den Zähler time1 noch den zähler time2 zurücksetzt wird der USART durch die Comparematch Routine ständig getriggert und ständig der Wert 0 ausgegeben.
Ich hab auch noch ein paar andere Sachen umgestrickt von wegen Register sichern. Das nicht- sichern scheint ja in Mode zu kommen.
Den Fehler hab ich halt wieder mal mit dem Simulator vom AVR Studio gefunden - hat also doch seinen Sinn das Teil.
Code:
.include "m8def.inc"
.def temp = r16
.def counts = r17
.def kmh = r18
.def time1 = r21
.def time2 = r22
.equ CLOCK = 16000000
.equ BAUD = 9600
.equ UBRRVAL = CLOCK/(BAUD*16)-1#
.org 0x000
rjmp main
.org INT0addr
rjmp int0_handler
.org OC1Aaddr
rjmp timer1_compare
main:
ldi temp, LOW(RAMEND)
out SPL, temp
ldi temp, HIGH(RAMEND)
out SPH, temp
ldi temp, LOW(UBRRVAL)
out UBRRL, temp
ldi temp, HIGH (UBRRVAL)
out UBRRH, temp
ldi temp, high(40000-1)
out OCR1AH, temp
ldi temp, low(40000-1)
out OCR1AL, temp
ldi temp, ( 1 << WGM12 ) | ( 1 << CS00 )
out TCCR1B, temp
ldi temp, 1 << OCIE1A ; OCIE1A: Interrupt bei Timer Compare
out TIMSK, temp
ldi temp, (1<<URSEL)|(3<<UCSZ0)
out UCSRC, temp
ldi temp, 0x00
out DDRD, temp
;ldi temp, 0xFF
;out PORTD, temp
ldi temp, 0b00001010 ; INT0 und INT1 konfigurieren
out MCUCR, temp
ldi temp, 0b11000000 ; INT0 und INT1 aktivieren
out GICR, temp
sbi UCSRB,TXEN
sei
loop: rjmp loop
int0_handler:
push temp
in temp,sreg
push temp
ldi temp, 1
add counts, temp
pop temp
out sreg,temp
pop temp
reti
timer1_compare:
push temp
in temp,sreg
push temp
cpi time1, 100
breq time2u
brne time1w
time2u:
clr time1
cpi time2, 4
breq rechnekmh
brne time2w
time1w:
inc time1
rjmp outint0
time2w:
inc time2
rjmp outint0
rechnekmh:
clr time2
clr kmh
mov kmh, counts
lsl kmh
clr counts
rjmp uartu
uartu:
sbis UCSRA, UDRE
rjmp uartu
out UDR, kmh
outint0:
pop temp
out sreg,temp
pop temp
reti
-
naja obs immer so Sinnvoll ist ist fraglich, mit Seriellen Schnittstellen hat das ab und zu Probleme, auch in der "neuen" Mega familie kommt es mit den Pereperieregister nicht ganz kalr, die kann man nicht mit out ansprechen sondern muss sie las Speicher behandeln ( ergo sts ) und das Peilder der Sim nicht wirklich
-
@teslanikola
Klar hat der Simulator auch seine Macken und Unzulänglichkeiten.
Ich meine aber schon das die bei ATMEL darum bemüht sind diese auszubügeln.
Ich benutze den Simulator sehr gerne um Fehler aufzuspüren und das gelingt auch meistens.