Skip to content

Instantly share code, notes, and snippets.

@cagerton
Created April 20, 2010 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cagerton/371905 to your computer and use it in GitHub Desktop.
Save cagerton/371905 to your computer and use it in GitHub Desktop.
//MOSI:
#define DATAOUT 11
//MISO: (not used, but part of builtin SPI)
#define DATAIN 12
#define SPICLOCK 13
#define SLAVESELECT 10
#define LDAC 8
void setup() {
byte clr;
pinMode(LDAC, OUTPUT);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(SLAVESELECT,OUTPUT);
digitalWrite(SLAVESELECT,HIGH); //disable device
SPCR = (1<<SPE)|(1<<MSTR); // SPI-Control = SPi-Enable, MaSTeR
clr=SPSR; // read reg twice
clr=SPDR;
delay(10);
}
void write_values(uint16_t samplex, uint16_t sampley)
{
uint8_t dacSPI0 = 0;
uint8_t dacSPI1 = 0;
dacSPI0 = (samplex >> 8) & 0x00FF; // data bits 15-12.
dacSPI0 |= dacSPI0 |= B00110000;;
dacSPI1 = samplex & 0x00FF; // data
digitalWrite(SLAVESELECT,LOW);
SPDR = dacSPI0; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
SPDR = dacSPI1;
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
digitalWrite(LDAC, LOW);
digitalWrite(LDAC, HIGH);
digitalWrite(SLAVESELECT,HIGH);
dacSPI0 = 0;
dacSPI1 = 0;
dacSPI0 = (sampley >> 8) & 0x00FF; // data bits 15-12.
dacSPI0 |= B10110000;
dacSPI1 = sampley & 0x00FF; // data
digitalWrite(SLAVESELECT,LOW);
SPDR = dacSPI0; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
SPDR = dacSPI1;
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
digitalWrite(SLAVESELECT,HIGH);
}
void loop(){
//...
}
@cagerton
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment