Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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