I'm trying different methods for sound synthesis.
The following code is meant to sweep a squarewave from 65Hz to 2Khz using mode 14 fast pwm mode but it there are gaps in the audio output as it sweeps up. Any ideas what's wrong.
void setup() {
// set to mode 14 fast pwm.
// TCCR1B
// Bit 7 6 5 4 3 2 1 0
// Bit Name COM1A1 COM1A0 COM1B1 COM1B0 ----- ----- WGM11 WGM10
// Initial Value 0 0 0 0 0 0 0 0
// changed to 1 1 1 1 0 0 1 0
TCCR1A = B11110010;
// TCCR1B
// Bit 7 6 5 4 3 2 1 0
// Bit Name ICNC1 ICES1 ----- WGM13 WGM12 CS12 CS11 CS10
// Initial Value 0 0 0 0 0 0 0 0
// changed to 0 0 0 1 1 0 1 1
TCCR1B = B00011011;
ICR1 = 5256; // initial "top" value.
OCR1A = 11; // initial pulse duty value
OCR1B = 14; // initial pulse duty value
pinMode(10,OUTPUT); // set pin 10 for output
}
void loop()
{
for (float note_val=65; note_val < 2000 ; note_val++) // generate a note value from 65Hz to 2KHz
{
float timer_val;
unsigned int final_val;
timer_val = (1 / note_val) * 1000000 ; // convert Hz to time period in micro seconds
final_val = timer_val; // convert float to unsigned int
ICR1 = final_val; // Load up ICR1 with desired frequency value
OCR1B = final_val / 2; // set duty cycle to 50%. A pure squarewave
delay (10);
}
}