Advertisement
adria_junyent

Arduino code to read 8 bit audio samples from an SPI memory

Nov 29th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <TimerOne.h>
  2.  
  3. #define DATAOUT 11//MOSI
  4. #define DATAIN  12//MISO
  5. #define SPICLOCK  13//sck
  6. #define SLAVESELECT 10//ss
  7.  
  8. //opcodes
  9. #define WREN  6
  10. #define WRDI  4
  11. #define RDSR  5
  12. #define WRSR  1
  13. #define READ  3
  14. #define WRITE 2
  15.  
  16. byte eeprom_output_data;
  17. byte clr;
  18. long address=0;
  19.  
  20. void setup()
  21. {
  22.   pinMode(0, OUTPUT);    
  23.   pinMode(1, OUTPUT);    
  24.   pinMode(2, OUTPUT);    
  25.   pinMode(3, OUTPUT);    
  26.   pinMode(4, OUTPUT);    
  27.   pinMode(5, OUTPUT);    
  28.   pinMode(6, OUTPUT);    
  29.   pinMode(7, OUTPUT);
  30.  
  31.   pinMode(DATAOUT, OUTPUT);
  32.   pinMode(DATAIN, INPUT);
  33.   pinMode(SPICLOCK,OUTPUT);
  34.   pinMode(SLAVESELECT,OUTPUT);
  35.   pinMode(8,OUTPUT);
  36.   digitalWrite(SLAVESELECT,HIGH); //disable device
  37.  
  38.   // SPCR = 01010000
  39.   //interrupt disabled,spi enabled,msb 1st,master,clk low when idle,
  40.   //sample on leading edge of clk,system clock/4 rate (fastest)
  41.   SPCR = (1<<SPE)|(1<<MSTR);
  42.   //SPSR = (1<<SPI2X);
  43.   clr=SPSR;
  44.   clr=SPDR;
  45.   delay(1000);
  46.  
  47.   Timer1.initialize(91); // this was set to approximately 11 kHz, which was the sampling frequency of my audio file.
  48.   Timer1.attachInterrupt( timerIsr ); // attach the service routine here
  49. }
  50.  
  51. char spi_transfer(volatile char data)
  52. {
  53.   SPDR = data;                    // Start the transmission
  54.   while (!(SPSR & (1<<SPIF)))     // Wait for the end of the transmission
  55.   {
  56.   };
  57.   return SPDR;                    // return the received byte
  58. }
  59.  
  60. byte read_eeprom(long EEPROM_address)
  61. {
  62.   //READ EEPROM
  63.   int data;
  64.   digitalWrite(SLAVESELECT,LOW);
  65.   spi_transfer(READ); //transmit read opcode
  66.   spi_transfer((char)(EEPROM_address>>16));   //send MSByte address first
  67.   spi_transfer((char)(EEPROM_address>>8));   //send MSByte address first
  68.   spi_transfer((char)(EEPROM_address));      //send LSByte address
  69.   data = spi_transfer(0x00); //get data byte
  70.   digitalWrite(SLAVESELECT,HIGH); //release chip, signal end transfer
  71.   return data;
  72. }
  73.  
  74. void loop()
  75. {
  76.   // Main code loop
  77.   // TODO: Put your regular (non-ISR) logic here
  78. }
  79.  
  80. void timerIsr()
  81. {
  82.   digitalWrite(8,LOW);
  83.   eeprom_output_data = read_eeprom(address);
  84.   PORTD=eeprom_output_data;
  85.   digitalWrite(8,HIGH);
  86.   address++;
  87.   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)
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement