Go Down

Topic: Hardware CTC (Read 764 times) previous topic - next topic

berlineddy

Hi Guys,

I want to do toggle a LED on and off every half a second via the hardware.

my Hardware is a Duemilanove running at 16 MHz

this is the code i am currently trying to run:


Code: [Select]
#include <avr/io.h>

void setup(){
  // reset registers
  TIMSK1 = 0x00;
  TCCR1A = 0x00;
  TCCR1B = 0x00;
  TCNT1 = 0x00; 
 
  // OC1A as output
  pinMode(9, OUTPUT);
 
  // count 0.521 sec
  OCR1A = 15000;
 
  // CTC
  TCCR1B |= ( 1 << WGM13 );
  TCCR1B |= ( 1 << WGM12 );
 
  TCCR1A |= ( 0 << WGM11 );
  TCCR1A |= ( 0 << WGM10 );
 
  // Prescaler to 1024
  TCCR1B |= ( 1 << CS12 );
  TCCR1B |= ( 0 << CS11 );
  TCCR1B |= ( 1 << CS10 );
 
 
  // TOGGLE OC1A
  TCCR1A |= ( 0 << COM1A1 );
  TCCR1A |= ( 1 << COM1A0 );
}

void loop(){
 
}


the problem with this code, is that it only turn the led on and then does nothing else :(

has anybody a suspicion what might be wrong her?

greetings from Berlin,
Eddy

CrossRoads

Could be written a little simpler:
Code: [Select]

void setup(){
pinMode (9, OUTPUT);
}
void loop (){
digitalWrite (9, HIGH);
delay (500);
digitalWrite (9, LOW);
delay (500);
}
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

berlineddy

ohhhh

i fixed it myselve

i had to change

Code: [Select]
  TCCR1B |= ( 1 << WGM13 );

to

Code: [Select]
  TCCR1B |= ( 0 << WGM13 );

CrossRoads

Also, just noticing you have everthing in void setup, which only runs once.
And nothing in void loop, which runs repeatedly.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

berlineddy

yes thats the nice thing about hardware timers.

you just set them up once then you can put your cpu to sleep but the timer-modules on the chip does all the timing and everything for you.


Go Up