|
这是针对 ATMega8 修改的代码
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
/*
ATMega8 Port Define:
PB0 Left eye LED
PB1 Right eye LED
OC2 Left ear speaker (base_frequency)
OC1B Right ear speaker (Offset Frequencies for binaural beats)
*/
struct brainwaveElement
{
char bwType; // 'a' for Alpha, 'b' for Beta, 't' for Theta, or 'd' for Delta ('0' signifies last entry in table
float bwDuration; // Duration of this Brainwave Type (ms)
} const brainwaveTab[] PROGMEM =
{
{ 'b', 60000 },
{ 'a', 10000 },
{ 'b', 20000 },
{ 'a', 15000 },
{ 'b', 15000 },
{ 'a', 20000 },
{ 'b', 10000 },
{ 'a', 30000 },
{ 'b', 5000 },
{ 'a', 60000 },
{ 't', 10000 },
{ 'a', 30000 },
{ 't', 20000 },
{ 'a', 30000 },
{ 't', 30000 },
{ 'a', 15000 },
{ 't', 60000 },
{ 'a', 15000 },
{ 'b', 1000 },
{ 'a', 15000 },
{ 't', 60000 },
{ 'd', 1000 },
{ 't', 10000 },
{ 'd', 1000 },
{ 't', 10000 },
{ 'd', 1000 },
{ 't', 30000 },
{ 'a', 15000 },
{ 'b', 1000 },
{ 'a', 15000 },
{ 't', 30000 },
{ 'a', 15000 },
{ 'b', 1000 },
{ 'a', 20000 },
{ 'b', 5000 },
{ 'a', 20000 },
{ 'b', 15000 },
{ 'a', 15000 },
{ 'b', 20000 },
{ 'a', 10000 },
{ 'b', 25000 },
{ 'a', 5000 },
{ 'b', 60000 },
{ '0', 0 }
};
void blink_LEDs( float duration, float onTimeMS, float offTimeMS )
{
for( int i=0; i<(duration/(onTimeMS+offTimeMS)); i++ )
{
PORTB |= 0b00000011; // turn on LEDs at PB0, PB1
_delay_ms( onTimeMS );
PORTB &= 0b11111100; // turn off LEDs at PB0, PB1
_delay_ms( offTimeMS );
}
}
void do_brainwave_element(int index)
{
char brainChr = pgm_read_byte(&brainwaveTab[index].bwType);
// Beta
if( brainChr == 'b' )
{
OCR1A = 19090; // 214.719Hz for a binaural beat of 14.4Hz
blink_LEDs( pgm_read_float(&brainwaveTab[index].bwDuration), 34.7, 34.7 );
return;
}
// Alpha
else if (brainChr == 'a')
{
OCR1A = 19400; // 211.416Hz for a binaural beat of 11.1Hz
blink_LEDs( pgm_read_float(&brainwaveTab[index].bwDuration), 45.1, 45.0 );
return;
}
// Theta
else if (brainChr == 't')
{
OCR1A = 19886; // 206.324Hz for a binaural beat of 6.0Hz
blink_LEDs( pgm_read_float(&brainwaveTab[index].bwDuration), 83.5, 83.5 );
return;
}
// Delta
else if (brainChr == 'd')
{
OCR1A = 20280; // 202.521Hz, for a binaural beat of 2.2Hz
blink_LEDs( pgm_read_float(&brainwaveTab[index].bwDuration), 225.3, 225.3 );
}
else
{
// this should never be executed, since we catch the end of table in the main loop
return;
}
}
int main(void)
{
TIMSK = 0x00; // no Timer interrupts enabled
DDRB = 0xFF; // set all PortB pins as outputs
PORTB = 0x00; // all PORTB output pins Off
TCCR2 = 0b00011110;
OCR2 = 79;
TCCR1A = 0b00010000;
TCCR1B = 0b00001001;
int j = 0;
while( pgm_read_byte(&brainwaveTab[j].bwType) != '0' )
{
do_brainwave_element( j );
j++;
}
TCCR1B &= 0b11111000; // CS12:CS10=000 to stop Timer1 (turn off audio in Left ear speaker)
MCUCR |= 0b00100000; // SE=1 (bit 5)
MCUCR |= 0b00010000; // SM1:0=01 to enable Power Down Sleep Mode (bits 6, 4)
_delay_ms( 1000 );
PORTB = 0x00; // turn off all PORTB outputs
DDRB = 0x00; // make PORTB all inputs
sleep_cpu(); // put CPU into Power Down Sleep Mode
}
|
|