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

MCP4921 SPI DAC Driver

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



Joined: 05 May 2010
Posts: 92
Location: Dhaka, Bangladesh

View user's profile Send private message Send e-mail Visit poster's website

MCP4921 SPI DAC Driver
PostPosted: Wed Dec 03, 2014 7:36 am     Reply with quote

MCP4921.h

Code:

#define CLK PIN_A0
#define CS PIN_A3
#define SDI PIN_A6
#define LDAC PIN_A7
                                       
#define ignore_cmd 0x80
#define DAC_write_cmd 0x00
#define Buffer_on 0x40
#define Buffer_off 0x00
#define Gain_1X 0x20
#define Gain_2X 0x00
#define Run_cmd 0x10
#define Shutdown 0x00                                 


void init_dac();
void DAC_write(unsigned char cmd, unsigned long data);   



MCP4921.c

Code:

#include "mcp4921.h"


void init_dac()
{
   output_high(CS);
   output_high(LDAC);
   output_high(CLK);
   output_high(SDI);
}                                                                   


void DAC_write(unsigned char cmd, unsigned long data)
{
    unsigned char s = 16;                                       
   
    unsigned long value = 0;
   
    value = cmd;
    value <<= 8;
    value |= (data & 0xFFF);
   
    output_low(CS);
                     
    while(s > 0)
    {
        if((value & 0x8000) != 0)
        {
            output_high(SDI);
        }
        else
        {
            output_low(SDI);
        }
        output_low(CLK);
        output_high(CLK);
        value <<= 1;
        s -= 1;
    }                           
   
    output_low(LDAC);
    output_high(CS);
    delay_us(10);
    output_high(LDAC);
}         

_________________
https://www.facebook.com/MicroArena

SShahryiar
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