Код:
#include <SPI.h>
//pins
#define BC1 9
#define BCDIR 8
#define SS 4
#define AY_CLOCK 3
//clock
#define PRESCALE 1
#define OCR2A_VAL 3
SPISettings spiSettings(16000000, MSBFIRST, SPI_MODE0);
void setup() {
pinMode(SS, OUTPUT);
pinMode(BC1, OUTPUT);
pinMode(BCDIR, OUTPUT);
pinMode(AY_CLOCK, OUTPUT);
initAYClock();
SPI.begin();
}
void loop() {
pseudoInterrupt();
delay(20);
}
void initAYClock() {
TCCR2A = ((1 << WGM21) | (1 << COM2B0));
TCCR2B = (1 << CS20);
TIMSK2 = 0;
OCR2A = OCR2A_VAL;
}
uint8_t cb = 0;
uint8_t rawData[] = {
0xFF, 0x00, 0x8E, 0x02, 0x38, 0x03, 0x02, 0x04, 0x0E, 0x05, 0x02, 0x07,
0x1A, 0x08, 0x0F, 0x09, 0x10, 0x0A, 0x0E, 0x0B, 0x47, 0x0D, 0x0E, 0xFF,
0x00, 0x77, 0x04, 0x8E, 0x05, 0x03, 0x07, 0x3A, 0x08, 0x0E, 0x0A, 0x0D,
0xFF, 0x00, 0x5E, 0x04, 0x0E, 0x05, 0x05, 0x0A, 0x0C, 0xFF, 0x04, 0x8E,
0x05, 0x06, 0x07, 0x32, 0x08, 0x00, 0x0A, 0x0A, 0xFF, 0x05, 0x08, 0x0A,
0x07, 0xFF, 0x04, 0x0E, 0x05, 0x0A, 0x0A, 0x04, 0xFF, 0x00, 0x8E, 0x04,
0x8E, 0x05, 0x00, 0x07, 0x1E, 0x08, 0x0F, 0x0A, 0x0B, 0x0D, 0x0E, 0xFF,
0x00, 0x77, 0x08, 0x0E, 0x0A, 0x06, 0xFF, 0x00, 0x5E, 0x07, 0x3E, 0x0A,
0x00, 0xFF, 0x07, 0x36, 0x08, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x8E, 0x07,
0x33, 0x08, 0x0B, 0x0A, 0x0F, 0x0D, 0x0E, 0xFF, 0x04, 0x77, 0x08, 0x06,
0x0A, 0x0E, 0xFF, 0x04, 0x5E, 0x07, 0x3B, 0x08, 0x00, 0xFF, 0x07, 0x1B,
0x0A, 0x00, 0xFF, 0xFF, 0xFF, 0x02, 0x1C, 0x03, 0x01, 0x04, 0x8E, 0x07,
0x33, 0x08, 0x0B, 0x0A, 0x0B, 0x0B, 0x23, 0x0D, 0x0E, 0xFF, 0x04, 0x77,
0x08, 0x06, 0x0A, 0x0A, 0xFF, 0x04, 0x5E, 0x07, 0x3B, 0x08, 0x00, 0x0A,
0x09, 0xFF, 0x07, 0x1B, 0x0A, 0x00, 0xFF, 0xFF, 0xFF, 0x02, 0x8E, 0x03,
0x00, 0x04, 0x0E, 0x05, 0x01, 0x07, 0x18, 0x08, 0x0F, 0x09, 0x0B, 0x0A,
0x0E, 0xFF, 0x00, 0x77, 0x02, 0x77, 0x04, 0x8E, 0x06, 0x01, 0x08, 0x0E,
0x09, 0x0A, 0x0A, 0x0D, 0xFF, 0x00, 0x5E, 0x02, 0x5E, 0x04, 0x0E, 0x05,
0x02, 0x06, 0x02, 0x09, 0x09, 0x0A, 0x0C, 0xFF, 0x02, 0x8E, 0x04, 0x8E,
0x07, 0x30, 0x08, 0x00, 0x09, 0x08, 0x0A, 0x0A, 0xFF, 0x02, 0x77, 0xFF,
0xFF
};
void pseudoInterrupt(){
while(rawData[cb] < 0xFF){
writeAYRegister(rawData[cb], rawData[cb+1]);
cb += 2;
}
if(rawData[cb] == 0xff) {
cb++;
}
if (cb > 20*12) {
cb = 0;
}
}
void writeAYRegister(uint8_t address, uint8_t data) {
SPI.beginTransaction(spiSettings);
digitalWrite(BC1, LOW);
digitalWrite(BCDIR, LOW);
//write address
digitalWrite(SS, LOW);
SPI.transfer(address);
digitalWrite(SS, HIGH);
digitalWrite(BC1, HIGH);
digitalWrite(BCDIR, HIGH);
digitalWrite(BC1, LOW);
digitalWrite(BCDIR, LOW);
//write data
digitalWrite(BC1, LOW);
digitalWrite(BCDIR, HIGH);
digitalWrite(SS, LOW);
SPI.transfer(data);
digitalWrite(SS, HIGH);
digitalWrite(BC1, LOW);
digitalWrite(BCDIR, LOW);
SPI.endTransaction();
}