PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Timer-Problem AT90CAN



almic
30.01.2009, 08:56
Hallo,

ich möchte mit Timer0 einen 50ms Takt erzeugen:

Quartz: 16 MHz.
uC: AT90CAN128
CKDIV8 ist aus (avrstudio)




#ifndef F_CPU
#define F_CPU 16000000UL // Takt des Quarz
#endif

// Header-Dateien
// -------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <stdbool.h>
#include <util\delay.h>

volatile unsigned short t0_prescaler = 0;
bool fuenfzig_ms = false;




// Funtkionsprototypen
//--------------------------------
void init(void);

int main()
{
init(); // Initialisierungsfunktion

while(1) // Endlossschleife
{
if (fuenfzig_ms = true)
{
fuenfzig_ms = false;
}

}
}



void init (void)
{
sei();
//
// Initialisierung Timer 0
// Waveform Generation Mode: normal Mode
// Compare Output Mode: disconnected
// Clock Select: CLK/1024
// Timer Overflow Interrupt enabled ----> abfangen mit: ISR (TIMER0_OVF_vect){ ; }
TCCR0A |= (1<<CS00) | (0<<CS01) | (1<<CS02);
TIMSK0 |= (1<<TOIE0);
}


ISR (TIMER0_OVF_vect)
{
t0_prescaler++;

if (t0_prescaler >= 781 )
{
fuenfzig_ms = true;
t0_prescaler = 0;
}
}


Das Problem ist, dass der TIMER0-Overflow-Interrupt nur alle 16,38ms ausgelöst werden.
Nach Rechnung sollte er aber alle 64us ausgelöst werden (=1024/16MHz).


Also ich reg mich hier nur noch auf :( Ich verstehs einfach nicht...


Viele Grüße
Almic

lorcan
30.01.2009, 09:15
Kann es sein dass du die interne Taktquelle nutzt?
Die ist nämlich im Auslieferungszustand aktiviert.
Das wäre der erste Ansatzpunkt.
Ein weiterer Punkt ist, dass du den Timer nicht zurücksetzt. Das kann er automatisch, wenn du im TCCR0A das WGM01-Bit setzt, nennt sich "Clear Timer on Compare match (CTC) mode", Datenblatt Seite 110.
Gruß
Lorcan

almic
30.01.2009, 09:30
Hallo,

die interne Taktquelle benutz ich nicht. Siehe Bild.

Im Moment ist der Timer auf "normal Mode" konfiguriert.
Und dort gibt es bei 0xFF ein Überlauf und der fängt wieder bei 0x00 an. Meiner Meinung nach braucht man da nix zurücksetzen.

Seite 104:


The simplest mode of operation is the Normal mode (WGM01:0 = 0). In this mode the counting
direction is always up (incrementing), and no counter clear is performed. The counter simply
overruns when it passes its maximum 8-bit value (TOP = 0xFF) and then restarts from the bottom
(0x00). In normal operation the Timer/Counter Overflow Flag (TOV0) will be set in the same
timer clock cycle as the TCNT0 becomes zero. The TOV0 flag in this case behaves like a ninth
bit, except that it is only set, not cleared. However, combined with the timer overflow interrupt
that automatically clears the TOV0 flag, the timer resolution can be increased by software. There
are no special cases to consider in the Normal mode, a new counter value can be written
anytime.


lg
Almic

lorcan
30.01.2009, 09:41
Ok, du nimmst den Overflow als Auslöser, der erfolgt nach 256 Schritten, dann löst der Interrupt aus, das bedeutet alle 16,38ms, die entsprechen nämlich deinen 64µs mal 256.



Gruss Lorcan

almic
30.01.2009, 09:50
](*,)
](*,)
](*,)

D.h. alle 64µs inkrementiert der sein internen Zähler und wenn dann der Overflow passiert hat der das 256 mal gemacht und wir sind bei 16,38ms.

Daran hab ich nicht gedacht. Ich bin davon ausgegangen dass er alle 64µs einen Interrupt auslöst.

Merci!

Viele Grüße
Alex