Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimerOne.h>
- #define DATAOUT 11//MOSI
- #define DATAIN 12//MISO
- #define SPICLOCK 13//sck
- #define SLAVESELECT 10//ss
- //opcodes
- #define WREN 6
- #define WRDI 4
- #define RDSR 5
- #define WRSR 1
- #define READ 3
- #define WRITE 2
- byte eeprom_output_data;
- byte clr;
- long address=0;
- void setup()
- {
- pinMode(0, OUTPUT);
- pinMode(1, OUTPUT);
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, OUTPUT);
- pinMode(6, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(DATAOUT, OUTPUT);
- pinMode(DATAIN, INPUT);
- pinMode(SPICLOCK,OUTPUT);
- pinMode(SLAVESELECT,OUTPUT);
- pinMode(8,OUTPUT);
- digitalWrite(SLAVESELECT,HIGH); //disable device
- // SPCR = 01010000
- //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
- //sample on leading edge of clk,system clock/4 rate (fastest)
- SPCR = (1<<SPE)|(1<<MSTR);
- //SPSR = (1<<SPI2X);
- clr=SPSR;
- clr=SPDR;
- delay(1000);
- Timer1.initialize(91); // this was set to approximately 11 kHz, which was the sampling frequency of my audio file.
- Timer1.attachInterrupt( timerIsr ); // attach the service routine here
- }
- char spi_transfer(volatile char data)
- {
- SPDR = data; // Start the transmission
- while (!(SPSR & (1<<SPIF))) // Wait for the end of the transmission
- {
- };
- return SPDR; // return the received byte
- }
- byte read_eeprom(long EEPROM_address)
- {
- //READ EEPROM
- int data;
- digitalWrite(SLAVESELECT,LOW);
- spi_transfer(READ); //transmit read opcode
- spi_transfer((char)(EEPROM_address>>16)); //send MSByte address first
- spi_transfer((char)(EEPROM_address>>8)); //send MSByte address first
- spi_transfer((char)(EEPROM_address)); //send LSByte address
- data = spi_transfer(0x00); //get data byte
- digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
- return data;
- }
- void loop()
- {
- // Main code loop
- // TODO: Put your regular (non-ISR) logic here
- }
- void timerIsr()
- {
- digitalWrite(8,LOW);
- eeprom_output_data = read_eeprom(address);
- PORTD=eeprom_output_data;
- digitalWrite(8,HIGH);
- address++;
- address=(address>948028)?0:address; // the routine reads the audio samples from the memory one sample at the time. This line resets the pointer to 0 when the end of the audio file is reached (the audio was some 86 seconds long)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement