Hi, I have this code for playing audio from pin D3 on an Arduino Uno:
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
//
void Init_Register() {
// Set up Timer 2 to do pulse width modulation on the speaker
// pin.
pinMode(3, OUTPUT);
//use internal clock
cbi(ASSR,EXCLK); //disable external clock
cbi(ASSR,AS2); //clock timers from internal clock
//Fast PWM mode
sbi(TCCR2A,WGM20); //TOV flag set on max
sbi(TCCR2A,WGM21); //Update OCRx at bottom
cbi(TCCR2B,WGM22); //Top = 0xFF
//Set non-inverting PWM on OC2B pin
cbi(TCCR2A,COM2B0);
sbi(TCCR2A,COM2B1);
//set normal operation on OC2A pin
cbi(TCCR2A,COM2A0);
cbi(TCCR2A,COM2A1);
//no prescaler
sbi(TCCR2B,CS20);
cbi(TCCR2B,CS21);
cbi(TCCR2B,CS22);
// Set Timer 1 to update sample on interrupt
cli(); //pause interrupts
// Set CTC mode (Clear timer on compare match)
cbi(TCCR1A,WGM10);
cbi(TCCR1A,WGM11);
sbi(TCCR1B,WGM12);
cbi(TCCR1B,WGM13);
//No prescaler
sbi(TCCR1B,CS10);
cbi(TCCR1B,CS11);
cbi(TCCR1B,CS12);
// Set the compare register (OCR1A) initial value
OCR1A = F_CPU/(440*WAVE_LEN); //initialise with A=440hz.
// Timer 1, output compare A match interrupt enable
sbi(TIMSK1,OCIE1A);
sei(); //start interrupts
// faster ADC
// uses div factor of 8. standard arduino read is 128.
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
sbi(ADCSRA,ADPS0) ;
}
I need to convert it so it uses Timer 4 on an Arduino Esplora (Atmega 32u4). Timer 4 has different parameters to Timer 2 (it's 10 not 8 bit for starters). I'm really struggling to convert this code to use Timer4. I don't even know if it's possible. Any help would be appreciated!! Here's my attempt so far:
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
//
void Init_Register() {
// Set up Timer 2 to do pulse width modulation on the speaker
// pin.
pinMode(3, OUTPUT);
//use internal clock
//cbi(ASSR,EXCLK); //disable external clock
//cbi(ASSR,AS2); //clock timers from internal clock
//Fast PWM mode
cbi(TCCR4D,WGM40); //TOV flag set on max
cbi(TCCR4D,WGM41); //Update OCRx at bottom
sbi(TCCR4C,PWM4D); //Top = 0xFF
OCR4C = 255;
//Set non-inverting PWM on OC2B pin
cbi(TCCR4C,COM4D0);
sbi(TCCR4C,COM4D1);
//set normal operation on OC2A pin
//cbi(TCCR2A,COM2A0);
//cbi(TCCR2A,COM2A1);
//no prescaler
sbi(TCCR4B,CS40);
cbi(TCCR4B,CS41);
sbi(TCCR4B,CS42);
cbi(TCCR4B,CS43);
// Set Timer 1 to update sample on interrupt
cli(); //pause interrupts
// Set CTC mode (Clear timer on compare match)
cbi(TCCR1A,WGM10);
cbi(TCCR1A,WGM11);
sbi(TCCR1B,WGM12);
cbi(TCCR1B,WGM13);
//No prescaler
sbi(TCCR1B,CS10);
cbi(TCCR1B,CS11);
cbi(TCCR1B,CS12);
// Set the compare register (OCR1A) initial value
OCR1A = F_CPU/(440*WAVE_LEN); //initialise with A=440hz.
// Timer 1, output compare A match interrupt enable
sbi(TIMSK1,OCIE1A);
sei(); //start interrupts
// faster ADC
// uses div factor of 8. standard arduino read is 128.
sbi(ADCSRA,ADPS2) ;
cbi(ADCSRA,ADPS1) ;
sbi(ADCSRA,ADPS0) ;
}