Pages: [1]   Go Down
Author Topic: Timer interrupt  (Read 223 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 11
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need a function which send one pulse of a specific lenght.

I have an arduino mega.

void Timer1Init(void)
 // TCCR1B |= 0x08;          //WGM12 bitje hoog maken in TCCR1B voor CTC timer mode
 // Gebruik timer1 'normal mode'

void Timer1Start(int wachttijd)
  digitalWrite(38, HIGH);     //Maak pin hoog bij het starten van de timer
  TCCR1B |= 0x02;              //CS11 bitje hoog maken in TCCR1B voor prescaler van 8, start de timer
  OCR1A = wachttijd;          //Geef de Top aan

void setup()
  pinMode(38, OUTPUT);
  Timer1Init();          //Configureer timer 1

void loop()
  Timer1Start(2000);    //Wait 2000 x 5*10^-7 = 1 ms

ISR (TIMER1_COMPA_vect)  //Voer ISR uit bij een Timer CTC compare match met OCR1A
  digitalWrite(38, LOW);
  TCCR1B &= 0xFD;        //Stop de clock source, schakel de timer uit

I use Timer1 in normal mode. In the function 'Timer1Start', the pin needs to go HIGH, start the timer, wait for the specific time length. If timer1 = 2000 (1 ms) the ISR should be executed in which pin 38 will go low and stop the timer.

I expected the pin to toggle every 1 ms, but nothing happened.

I hope someone could help me further.


edit: I forgot the OCIE1A bit in TIMSK1.... but the signal isn't very stable now, anyone suggestions why this is the case?
« Last Edit: December 08, 2012, 04:22:23 pm by emielzij » Logged

Pages: [1]   Go Up
Jump to: