Skip to content

Instantly share code, notes, and snippets.

@dilshan
Last active June 8, 2023 17:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dilshan/9044b895dcc8aa05d9df0cac1e928f8e to your computer and use it in GitHub Desktop.
Save dilshan/9044b895dcc8aa05d9df0cac1e928f8e to your computer and use it in GitHub Desktop.
PT8211 DAC verification script for Arduino
#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