system
January 1, 2009, 10:58pm
1
I'm trying to do something fairly simple. Just generate a pitch sweeping tone using interrupts but I'm not getting a lot of success.
The OCR0A doesn't seem to change. What am I doing wrong.
#include <avr/interrupt.h>
void setup() {
pinMode(6,OUTPUT);
TCCR0A = _BV(COM0A0) | _BV(WGM01); // toggle output set mode to ctc
TCCR0B = _BV(CS01) | _BV(CS00); // CTC mode and prescaler to 64
OCR0A = 10;
}
void loop()
{
unsigned char timer_val = 100;
for (timer_val=10; timer_val < 250; timer_val++);
{
OCR0A = timer_val;
delay (500);
}
}
system
January 2, 2009, 11:24am
2
Okay. I got it to work but with a problem.
Here is the working code. Sounds like a siren.
#include <avr/interrupt.h>
#ifndef cbi // Definitions for setting and clearing register bits
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
void setup() {
Serial.begin(9600);
pinMode(6,OUTPUT);
TCCR0A = _BV(COM0A0) | _BV(WGM01); // toggle output set mode to ctc
TCCR0B = _BV(CS01) | _BV(CS00); // CTC mode and prescaler to 64
OCR0A = 10;
}
void loop()
{
for (unsigned char timer_val=0; timer_val<250; timer_val++)
{
cbi(TCCR0A,COM0A0) ; // disconnect pulse clock
OCR0A = timer_val; // update timer value
sbi(TCCR0A,COM0A0) ; // connect pulse clock
Serial.print(timer_val);
}
}
But I can't add a delay in the for loop. Any ideas why a delay won't work anymore?
mem
January 2, 2009, 11:45am
3
That code is using timer0 - this is the timer used by the delay function! Use timer1 or timer2 and you will get your delay functions back
system
January 2, 2009, 12:26pm
4
That code is using timer0 - this is the timer used by the delay function! Use timer1 or timer2 and you will get your delay functions back
Thanks for your help. I'm very new to Arduino. I'll try timer1 or timer2 and let you know how I get on.