//Unions to make accessing bytes (for sending to SPI) and individual bits easier
union { // Control word, 16 bits
unsigned int16 word;
struct {
unsigned int8 byte1;
unsigned int8 byte2;
} bytes;
struct {
unsigned int Reserved1:1;
unsigned int Reserved2:1;
unsigned int SyncOutEn:1; // Enable SyncOutput
unsigned int SyncSel:1; // SyncOutput control
unsigned int Mode:1; // Sweep envelope: 0: Triangle, 1=Ramp
unsigned int IntExtIncr:1; // 0=Incr controlled internally
unsigned int IntExtBurst:1; // 0=Burst controlled internally
unsigned int CWBurst:1; // CW/Burst
unsigned int MSBOutEn:1; // Enable MSB output
unsigned int TriSine:1; // 0=Triangle, 1=Sine output
unsigned int DACEnable:1; // DAC Enable
unsigned int B24:1; // 0=use seperate 12-bit regs, 1=use 24-bit load
unsigned int addr:4; // Register Address
} bits;
} Ctrl; // Control word
union { // Starting Frequency, 24 bits
unsigned int32 word;
struct {
unsigned int8 byte1;
unsigned int8 byte2;
unsigned int8 byte3;
unsigned int8 byte4;
} bytes;
} Freq;
union { // Number of Increments, 12 bits, minimum=0x02
unsigned int16 word;
struct {
unsigned int8 byte1;
unsigned int8 byte2;
} bytes;
} Nincr;
void write_ctrl()
{
spi_write(DDS[selectDDS].Ctrl.bytes.byte2);
spi_write(DDS[selectDDS].Ctrl.bytes.byte1);
}
void write_freq()
{
spi_write((DDS[selectDDS].Freq.bytes.byte2&0b00001111) | 0b11000000);
spi_write(DDS[selectDDS].Freq.bytes.byte1);
spi_write((DDS[selectDDS].Freq.bytes.byte3>>4) | 0b11010000);
spi_write((DDS[selectDDS].Freq.bytes.byte2>>4) | (DDS[selectDDS].Freq.bytes.byte3<<4));
}
void write_nincr()
{
spi_write(DDS[selectDDS].Nincr.bytes.byte2 | 0b00010000);
spi_write(DDS[selectDDS].Nincr.bytes.byte1);
}