CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Analog Devices DAC8512 - 12-bit SPI DAC - Example Program

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
dyeatman



Joined: 06 Sep 2003
Posts: 1784
Location: Norman, OK

View user's profile Send private message

Analog Devices DAC8512 - 12-bit SPI DAC - Example Program
PostPosted: Sun Mar 04, 2012 6:26 pm     Reply with quote

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!!!!
asmboy



Joined: 20 Nov 2007
Posts: 2116
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Mar 05, 2012 5:21 pm     Reply with quote

nice code
great imbedded documentation
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group