Advertisement
Guest User

sine_test2.ino

a guest
Jan 4th, 2013
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.80 KB | None | 0 0
  1. // http://supertechman.blogspot.com.au/2010/09/new-sine-test-wave-for-vs1053-shield-on.html
  2. #include <SPI.h>
  3. int CS_pin = 9;
  4. int DREQ_pin = 3;
  5. void setup() {
  6.     Serial.begin(9600);
  7.   pinMode(CS_pin, OUTPUT);
  8.   pinMode(DREQ_pin, INPUT);
  9.   SPI.begin();
  10.   SPI.setBitOrder(MSBFIRST);
  11.   //CPOL = 0, CPHA = 1
  12.   //see http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Mode_Numbers
  13.   //and decoder chip datasheet
  14.   SPI.setDataMode(SPI_MODE1);
  15.   //max SDI clock freq = CLKI/7 and (datasheet) CLKI = 36.864, hence max clock = 5MHz
  16.   //SPI clock arduino = 16MHz. 16/ 4 = 4MHz -- ok!
  17.   SPI.setClockDivider(SPI_CLOCK_DIV4);
  18.   initialize();
  19. }
  20. void loop(){
  21.     Serial.println("starting sine test");
  22.  
  23.   digitalWrite(CS_pin, HIGH);
  24.   chip_write(0x00, 0x0c20); // sets sci_mode register, SM_SDINEW, SM_SDISHARE
  25.                           // SM_TESTS.  pg 25, 26
  26.   chip_sineTest(0xAA);   // test tone frequency (pg 35)
  27.  
  28.   Serial.println("end of loop");
  29. }
  30. void chip_write (unsigned int address, word data){
  31.     byte aux;
  32.     digitalWrite(CS_pin, LOW);
  33.     delay(1);
  34.     SPI.transfer(0x02);  //write command
  35.     SPI.transfer(address); //SDI_MODE register
  36.     //extract and send higher byte of data
  37.     aux = data >> 8;
  38.     SPI.transfer(aux);
  39.     //extract and send lower byte of data
  40.     aux = data & 0b11111111;
  41.     SPI.transfer(aux);
  42.     //wait for the chip to finish executing command
  43.     //while (!digitalRead(DREQ_pin)){};
  44.     digitalWrite(CS_pin, HIGH);
  45.     delay(1);
  46. }
  47. void chip_sineTest(int pitch){
  48.    digitalWrite(CS_pin, HIGH);
  49.    delay(1);
  50.    SPI.transfer(0x53);
  51.    SPI.transfer(0xEF);
  52.    SPI.transfer(0x6E);
  53.    SPI.transfer(pitch);
  54.    SPI.transfer(0);
  55.    SPI.transfer(0);
  56.    SPI.transfer(0);
  57.    SPI.transfer(0);
  58.    digitalWrite(CS_pin, LOW);
  59.    delay(1);  
  60. }
  61. void initialize(){
  62.  
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement