Forum: Mikrocontroller und Digitale Elektronik probleme mit AtMega16 timer0 ctc modus


von markus (Gast)


Lesenswert?

Hallo,

ich bin gerade neu in die avr programmierung eingestiegen und hätte da
mal ein problem:
Ich möchte mit einem Atmel AtMega16 mit dem Timer0 gerne ein PWM Signal
generieren, bei welchem ich die Frequenz selbst (über OCR0)bestimmen
kann. Getaktet wird das System über einen externen 16Mhz Quarz.
Soweit funktioniert das ja auch alles, jedoch bekomme ich am Ausgang
(OC0) immer die selbe Freuquenz von 125000 Khz.
Ich habe jetzt schon diverses ausprobiert, jedoch ohne erfolg....

/* includes */
#include <avr/interrupt.h>
#include <avr/io.h>

/* function declarations */
void InitPorts (void);
void InitTimer0 (void);

void main ()
{
  InitPorts();
  InitTimer0();
  /* Set Timer 0 to CTC Mode */
  TCCR0 |= (1<<WGM01);
  /* Set OC0 to toggle at compare matches */
  TCCR0 |= (1<<COM00);
  /* set the prescaler to 64 (results in max frequency 125000 hz) */
  TCCR0 |= (1<<CS01) | (1<<CS00);
  /* set top value for frequency (255 = 488 hz)*/
  OCR0=255;
  for(;;)
  {
  }
}

/* Init for PORTS */
void InitPorts (void)
{
  /* configure pin 4 at portb as output */
  DDRB |= (1<<PB3);

}
/* init for Timer0 */
void InitTimer0 (void)
{
  /* Set Timer 0 to CTC Mode */
  TCCR0 |= (1<<WGM01);
  /* Set OC0 to toggle at compare matches */
  TCCR0 |= (1<<COM00);
  /* set the prescaler to 64 (results in max frequency 125000 hz) */
  TCCR0 |= (1<<CS01) | (1<<CS00);
}

von inoffizieller WM-Rahul (Gast)


Lesenswert?

>TCCR0 |= (1<<WGM01);
lass da mal den "|" weg - oder weisst du, was voher im Register
stand?
Im Allgemeinen initialisiert man den Timer auch in einer Zeile. Das ist
aber Geschmackssache.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.