Guest User

Untitled

a guest
Oct 20th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. /**
  2.     I2S example code
  3.  
  4.     Description:
  5.     This I2S example creates a Sine waveform on the RIGHT Audio channel of PT8211
  6.     and a Sawtooth waveform on the LEFT Audio channel.
  7.  
  8.     This is a very simple how-to-use an external I2S DAC example (DAC = Digital to Analog Converter).
  9.  
  10.     Created on 27 Aug 2015 by Vassilis Serasidis
  11.     email:  avrsite@yahoo.gr
  12.  
  13.     Connections between PT8211 DAC and the STM32F103C8T6
  14.     WS    <-->  PA3
  15.     BCK   <-->  PA5 <-->  BOARD_SPI1_SCK_PIN
  16.     DIN   <-->  PA7 <-->  BOARD_SPI1_MOSI_PIN
  17. */
  18.  
  19.  
  20. #include <SPI.h>
  21.  
  22. #define WS   PA3
  23. #define BCK  PA5
  24. #define DATA PA7
  25.  
  26. uint16_t hData, lData;
  27.  
  28. // A full cycle, 16-bit, 2's complement Sine wave lookup table
  29. uint16_t sine_table[256] = {
  30.  
  31.    0x0000, 0x0324, 0x0647, 0x096a, 0x0c8b, 0x0fab, 0x12c8, 0x15e2,
  32.    0x18f8, 0x1c0b, 0x1f19, 0x2223, 0x2528, 0x2826, 0x2b1f, 0x2e11,
  33.    0x30fb, 0x33de, 0x36ba, 0x398c, 0x3c56, 0x3f17, 0x41ce, 0x447a,
  34.    0x471c, 0x49b4, 0x4c3f, 0x4ebf, 0x5133, 0x539b, 0x55f5, 0x5842,
  35.    0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e8, 0x66cf, 0x68a6,
  36.    0x6a6d, 0x6c24, 0x6dca, 0x6f5f, 0x70e2, 0x7255, 0x73b5, 0x7504,
  37.    0x7641, 0x776c, 0x7884, 0x798a, 0x7a7d, 0x7b5d, 0x7c29, 0x7ce3,
  38.    0x7d8a, 0x7e1d, 0x7e9d, 0x7f09, 0x7f62, 0x7fa7, 0x7fd8, 0x7ff6,
  39.    0x7fff, 0x7ff6, 0x7fd8, 0x7fa7, 0x7f62, 0x7f09, 0x7e9d, 0x7e1d,
  40.    0x7d8a, 0x7ce3, 0x7c29, 0x7b5d, 0x7a7d, 0x798a, 0x7884, 0x776c,
  41.    0x7641, 0x7504, 0x73b5, 0x7255, 0x70e2, 0x6f5f, 0x6dca, 0x6c24,
  42.    0x6a6d, 0x68a6, 0x66cf, 0x64e8, 0x62f2, 0x60ec, 0x5ed7, 0x5cb4,
  43.    0x5a82, 0x5842, 0x55f5, 0x539b, 0x5133, 0x4ebf, 0x4c3f, 0x49b4,
  44.    0x471c, 0x447a, 0x41ce, 0x3f17, 0x3c56, 0x398c, 0x36ba, 0x33de,
  45.    0x30fb, 0x2e11, 0x2b1f, 0x2826, 0x2528, 0x2223, 0x1f19, 0x1c0b,
  46.    0x18f8, 0x15e2, 0x12c8, 0x0fab, 0x0c8b, 0x096a, 0x0647, 0x0324,
  47.    0x0000, 0xfcdc, 0xf9b9, 0xf696, 0xf375, 0xf055, 0xed38, 0xea1e,
  48.    0xe708, 0xe3f5, 0xe0e7, 0xdddd, 0xdad8, 0xd7da, 0xd4e1, 0xd1ef,
  49.    0xcf05, 0xcc22, 0xc946, 0xc674, 0xc3aa, 0xc0e9, 0xbe32, 0xbb86,
  50.    0xb8e4, 0xb64c, 0xb3c1, 0xb141, 0xaecd, 0xac65, 0xaa0b, 0xa7be,
  51.    0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b18, 0x9931, 0x975a,
  52.    0x9593, 0x93dc, 0x9236, 0x90a1, 0x8f1e, 0x8dab, 0x8c4b, 0x8afc,
  53.    0x89bf, 0x8894, 0x877c, 0x8676, 0x8583, 0x84a3, 0x83d7, 0x831d,
  54.    0x8276, 0x81e3, 0x8163, 0x80f7, 0x809e, 0x8059, 0x8028, 0x800a,
  55.    0x8000, 0x800a, 0x8028, 0x8059, 0x809e, 0x80f7, 0x8163, 0x81e3,
  56.    0x8276, 0x831d, 0x83d7, 0x84a3, 0x8583, 0x8676, 0x877c, 0x8894,
  57.    0x89bf, 0x8afc, 0x8c4b, 0x8dab, 0x8f1e, 0x90a1, 0x9236, 0x93dc,
  58.    0x9593, 0x975a, 0x9931, 0x9b18, 0x9d0e, 0x9f14, 0xa129, 0xa34c,
  59.    0xa57e, 0xa7be, 0xaa0b, 0xac65, 0xaecd, 0xb141, 0xb3c1, 0xb64c,
  60.    0xb8e4, 0xbb86, 0xbe32, 0xc0e9, 0xc3aa, 0xc674, 0xc946, 0xcc22,
  61.    0xcf05, 0xd1ef, 0xd4e1, 0xd7da, 0xdad8, 0xdddd, 0xe0e7, 0xe3f5,
  62.    0xe708, 0xea1e, 0xed38, 0xf055, 0xf375, 0xf696, 0xf9b9, 0xfcdc,
  63. };
  64.  
  65. void setup() {
  66.   Serial.begin(19200);
  67.   delay(100);
  68.   Serial.println("-= I2S Example =-");
  69.   // Setup SPI 1
  70.   SPI.begin(); //Initialize the SPI_1 port.
  71.   SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
  72.   SPI.setDataMode(SPI_MODE0); //Set the  SPI_2 data mode 0
  73.   SPI.setClockDivider(SPI_CLOCK_DIV16); // Slow speed (72 / 16 = 4.5 MHz SPI_1 speed)
  74.   pinMode(WS, OUTPUT); //Set the Word Select pin (WS) as output.
  75. }
  76.  
  77. void loop() {
  78.   uint16_t i;
  79.  
  80.   for (i=0;i<256;i++){
  81.     lData = sine_table[i];
  82.     hData = sine_table[i];
  83.     hData >>= 8;
  84.    
  85.     digitalWrite(WS, LOW);  //Select RIGHT Audio channel
  86.     SPI.transfer(hData);    // Data bits 15-8
  87.     SPI.transfer(lData);    // Data bits 7-0
  88.  
  89.     digitalWrite(WS, HIGH); //Select LEFT Audio channel
  90.     SPI.transfer(i);        //
  91.     SPI.transfer(0);        //
  92.   }
  93. }
Add Comment
Please, Sign In to add comment