dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
Analog Devices DAC8512 - 12-bit SPI DAC - Example Program |
Posted: Sun Mar 04, 2012 6:26 pm |
|
|
Code: |
#include <18F4550.h>
//
////////////////////////////////////////////////////////////////////////////
//// Example to control a 12 bit DAC8512 Digital to Analog chip ////
//// using a PIC18F4550 ////
//// ////
//// Written by DYeatman 3/4/2012 ////
//// ////
////////////////////////////////////////////////////////////////////////////
#FUSES NOWDT // No Watch Dog Timer
#FUSES PUT // No Power Up Timer
#FUSES NODEBUG // No Debug mode
#FUSES MCLR // Master Clear
#FUSES INTRC // Use internal R/C oscillator
//
// Mode definitiona
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H) //CPOL=0 CPHA=0
#define SPI_MODE_1 (SPI_L_TO_H) //CPOL=0 CPHA=1
#define SPI_MODE_2 (SPI_H_TO_L) //CPOL=1 CPHA=0
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) //CPOL=1 CPHA=1
//
#use delay(clock=8000000)
//
// 18F4550 Hardware SPI pins
#define SPI_DO PIN_A0 // data output
#define SPI_DI PIN_A1 // data input
#define SPI_CLK PIN_A2 // clock output
// define control pins
#define SPI_CS PIN_B2 // enable pin
#define SPI_LD PIN_B3 // load pin
#define SPI_CLR PIN_B4 // clear pin
//
#use spi(DI=SPI_DI, DO=SPI_DO, CLK=SPI_CLK, enable=Pin_A3, BITS=12, mode=3)
//
void send_data(int16 data)
{
output_high(SPI_LD);
delay_us(5);
output_low(SPI_CS); // select the chip
delay_us(5);
spi_xfer(Data); // send the new value to the DAC register
delay_us(5);
output_high(SPI_CS); // deselect the SPI data input
delay_us(5);
output_low(SPI_LD); // latch the data transfer to the outputs
delay_us(5);
output_high(SPI_LD);
}
//
void DAC_reset()
{
output_low(SPI_CLR); // reset the DAC
delay_ms(1);
output_high(SPI_CLR);
output_high(SPI_CS); // de-select the chip
output_high(SPI_LD); // set control signals back to inactive
output_high(SPI_CLK);
}
//
void main()
{
int16 DACoutMV;
//
setup_oscillator(OSC_8MHZ | OSC_INTRC);
setup_adc(no_analogs);
// set control signals to their initial states
output_high(SPI_CLR); //
output_high(SPI_CS); // de-select the chip
output_high(SPI_LD);
output_high(SPI_CLK);
//
while(1) // this loop outputs a square wave and triangle wave every 500ms
{
DACoutMV= 2000; // value 0 to 4095 (0xfff) = 0 to 4.095 volts
send_data(Dacoutmv); // start a square wave 2 volts high
delay_ms(30); // make the square wave 30ms long
DACoutMV= 0; // set the DAC output back to zero using data
send_data(Dacoutmv); // end of 30ms square wave
delay_ms(100); // wait 100ms before starting triangle wave
// start of triangle wave
for(DACOutMV=0x00;DACoutMV<0xfff;DACoutMV++)
send_data(Dacoutmv);
// end of triangle wave
delay_ms(30); // wait 30ms at the top
dac_reset(); // reset everything back to zero
//
output_toggle(PIN_C1); // bink LED to indicate end of loop
delay_ms(500); // wait 500ms before we do it again
}
}
|
_________________ Google and Forum Search are some of your best tools!!!! |
|