//SPI example using AD7373 DAC from Analog Devices //set DAC for simultaneous update of two channels // //connect MOSI B.5 to DIN on AD7373 //connect SCLK B.7 to SCLK on AD7373 //connect B.4 to notSYNC on the AD7373 #include #include // SPI driver routine #define begin { #define end } unsigned char junk, Aout, DACctlOne, DACctlTwo; unsigned char DDSacc, DDSinc; void main(void) begin //set up SPI control register SPCR //bit 7 SPIE=0 no ISR //bit 6 SPE=1 enable spi //bit 5 DORD=0 msb first //bit 4 MSTR=1 Mega32 is spi master //bit 3 CPLO=1 clock polarity //bit 2 CPHA=1 clock phase //bit 1,0 rate sel=00 along with SPSR.0=1 sets clk to f/2 = 8 MHz SPCR = 0b01011100 ; SPSR = 1; //set up i/o data direction DDRB.4 = 1; //output chip select for ADC DDRB.5 = 1; //output MOSI //DDRB.6 = 0; //input MISO is not used by the DAC DDRB.7 = 1; //output SCLK DDSinc = 16; //sawtooth rate //DAC control word // bit 7 notINT/EXT set to notINT = 0. Use internal Vref // bit 6 = 0 // bit 5 LDAC = 0 load and update both channels when set // bit 4 PDB = 0 pwer down channel B // bit 3 PDA = 0 pwer down channel A // bit 2 notA/B = 0 chooses A // bit 1 CR1=0 control bits modify load mode // bit 0 CR0=1 set to load A from SR // Load A from shift register DACctlOne = 0b00000001 ; // Load B from SR and and update both outputs DACctlTwo = 0b00100100 ; while (1) begin PORTB.4 = 0; //SYNC low begins load SPDR = DACctlOne; //set DAC parameters while (SPSR.7==0) ; SPDR = DDSacc ; //send channel A while (SPSR.7==0) ; PORTB.4 = 1; //SYNC high ends load PORTB.4 = 0; //SYNC low begins load SPDR = DACctlTwo; //set DAC parameters while (SPSR.7==0) ; SPDR = DDSacc+64 ; //send channel B while (SPSR.7==0) ; PORTB.4 = 1; //SYNC high ends load DDSacc = DDSacc + DDSinc; // sawtooth generator end end