SHARE
TWEET

Untitled

a guest Aug 19th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <limits.h>
  2.  
  3. #ifndef PIN_BCK
  4. #define PIN_BCK   22
  5. #endif
  6.  
  7. #ifndef PIN_WS
  8. #define PIN_WS    23
  9. #endif
  10.  
  11. #ifndef PIN_DIN
  12. #define PIN_DIN   24
  13. #endif
  14.  
  15. #define NOP __asm__ __volatile__ ("nop\n\t")
  16.  
  17. void writeDACChannel(short waveData)
  18. {
  19.   unsigned char pos = 16;
  20.  
  21.   // Send data into PT8211 in least significant bit justified (LSBJ) format.
  22.   while(pos > 0)
  23.   {
  24.     pos--;
  25.     digitalWrite(PIN_BCK, LOW);
  26.     // Write next bit in stream into DIN.
  27.     digitalWrite(PIN_DIN, (waveData & (1 << pos)) ? HIGH : LOW);
  28.     NOP;
  29.     // Toggle BCK.
  30.     digitalWrite(PIN_BCK, HIGH);
  31.     NOP;
  32.   }
  33. }
  34.  
  35. void writeDAC(short waveData)
  36. {
  37.   digitalWrite(PIN_WS, LOW);
  38.   digitalWrite(PIN_BCK, LOW);
  39.   // Write data into right channel of DAC.
  40.   writeDACChannel(waveData);
  41.   digitalWrite(PIN_WS, HIGH);
  42.   // Write data into left channel of DAC.
  43.   writeDACChannel(waveData);
  44. }
  45.  
  46. void setup()
  47. {
  48.   pinMode(PIN_BCK, OUTPUT);
  49.   pinMode(PIN_WS, OUTPUT);
  50.   pinMode(PIN_DIN, OUTPUT);
  51. }
  52.  
  53. void loop()
  54. {
  55.   short pos = 0;
  56.   while(pos < 360)
  57.   {
  58.     pos += 5;
  59.     if(pos > 360)
  60.     {
  61.       pos = 0;
  62.       continue;
  63.     }
  64.  
  65.     writeDAC(sin(radians(pos)) * SHRT_MAX);
  66.   }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top