Last active
June 8, 2023 17:00
-
-
Save dilshan/9044b895dcc8aa05d9df0cac1e928f8e to your computer and use it in GitHub Desktop.
PT8211 DAC verification script for Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <limits.h> | |
#ifndef PIN_BCK | |
#define PIN_BCK 22 | |
#endif | |
#ifndef PIN_WS | |
#define PIN_WS 23 | |
#endif | |
#ifndef PIN_DIN | |
#define PIN_DIN 24 | |
#endif | |
#define NOP __asm__ __volatile__ ("nop\n\t") | |
void writeDACChannel(short waveData) | |
{ | |
unsigned char pos = 16; | |
// Send data into PT8211 in least significant bit justified (LSBJ) format. | |
while(pos > 0) | |
{ | |
pos--; | |
digitalWrite(PIN_BCK, LOW); | |
// Write next bit in stream into DIN. | |
digitalWrite(PIN_DIN, (waveData & (1 << pos)) ? HIGH : LOW); | |
NOP; | |
// Toggle BCK. | |
digitalWrite(PIN_BCK, HIGH); | |
NOP; | |
} | |
} | |
void writeDAC(short waveData) | |
{ | |
digitalWrite(PIN_WS, LOW); | |
digitalWrite(PIN_BCK, LOW); | |
// Write data into right channel of DAC. | |
writeDACChannel(waveData); | |
digitalWrite(PIN_WS, HIGH); | |
// Write data into left channel of DAC. | |
writeDACChannel(waveData); | |
} | |
void setup() | |
{ | |
pinMode(PIN_BCK, OUTPUT); | |
pinMode(PIN_WS, OUTPUT); | |
pinMode(PIN_DIN, OUTPUT); | |
} | |
void loop() | |
{ | |
short pos = 0; | |
while(pos < 360) | |
{ | |
pos += 5; | |
if(pos > 360) | |
{ | |
pos = 0; | |
continue; | |
} | |
writeDAC(sin(radians(pos)) * SHRT_MAX); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment