I have successfully programmed an Attiny85 to produce multiple channels of audio (Horray!).
Although it's only 2, and I need help with the third. For some reason PB2 is not toggled, and PB3 & PB4 just over-lap on PB1 & PB0 and create an awful sound.
I don't know if it has something to do with the code "PORTB ^= PBx", I'm not sure if this is the correct way to toggle pins. Since PB0 & PB1 are the only ones working properly, maybe I'm switching between pins 0 & 1, instead of toggling them?
#include "Tone.h"
unsigned int counter[5];
unsigned int togglespeed[5];
void setup()
{
togglespeed[0] = 32768 / NOTE_F5;
togglespeed[1] = 32768 / NOTE_C5;
togglespeed[2] = 32768 / NOTE_E4;
togglespeed[3] = 32768 / NOTE_C4;
togglespeed[4] = 32768 / NOTE_E3;
// Set timer1 to CTC mode, the max prescaler speed, and clear the OC1A pin.
TCCR1 = 1 << CTC1 | 2 << COM1A1 | 1;
// Set the timer overflow interrupts on timer1
TIMSK = TIMSK | 1 << TOIE1;
TIFR = TIFR | 1 << TOV1;
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
}
ISR(TIMER1_OVF_vect)
{
counter[0]++;
counter[1]++;
counter[2]++;
counter[3]++;
counter[4]++;
if(counter[0] >= togglespeed[0])
{
PORTB ^= PB0;
counter[0] = 0;
}
if(counter[1] >= togglespeed[1])
{
PORTB ^= PB1;
counter[1] = 0;
}
if(counter[2] >= togglespeed[2])
{
PORTB ^= PB2;
counter[2] = 0;
}
}
void loop()
{
}
Note: this "Tone.h" file only contains the pitch definitions.
Thanks.