arduino - Stop timer set new value and start again AVR (Interrupt) -
i have avr mcu.
playing timer now.
need ? have timer counting frequency. in each interrupt incrementing variable, , somewhere need check value of variable , if equals 100, need stop timer count, set new value frequency , continue counting down.
cannot how stop timer , set new value compare.
have tried select no clock source using mux selector register, continue count up.
correct way this. here code
// arduino timer ctc interrupt example // www.engblaze.com // avr-libc library includes #include <avr/io.h> #include <avr/interrupt.h> #define ledpin_up 9 #define ledpin_down 8 int current_value = 0; void setup() { serial.begin(9600); // pinmode(ledpin_up, output); // initialize timer1 cli(); // disable global interrupts tccr1a = 0; // set entire tccr1a register 0 tccr1b = 0; // same tccr1b // set compare match register desired timer count: // ocr1a = 3123; ocr1a = 1562; // turn on ctc mode: tccr1b |= (1 << wgm12); // set cs10 , cs12 bits 1024 prescaler: tccr1b |= (1 << cs10); tccr1b |= (1 << cs12); // enable timer compare interrupt: timsk1 |= (1 << ocie1a); // enable global interrupts: sei(); } void loop() { //digitalwrite(ledpin_up, current_value); serial.println(current_value); if(current_value==255) { tccr1b |= (0 << cs10); tccr1b |= (0 << cs12); serial.println("reseting timer"); } } isr(timer1_compa_vect) { current_value++; }
tccr1b |= (0 << cs10); tccr1b |= (0 << cs12);
does not expecting. since using "or" |
, value placed 0|1
1, not 0 desire.
the usual way clear bit is
tccr1b &= ~(1 << cs10);
to clear 2 bits @ once, use
tccr1b &= ~(1 << cs10 | 1 << cs12);
as counting down, want use variable indicate direction going, , use variable in isr. perhaps,
int dir = 1; isr(timer1_compa_vect) { current_value += dir; }
and change dir -1 when want count down instead.
Comments
Post a Comment