sshahryiar
 
 
  Joined: 05 May 2010 Posts: 94 Location: Dhaka, Bangladesh 
			
			 
			 
			 
			 
			
			
			
			
			
  
		  | 
		
			
				| MCP4921 SPI DAC Driver | 
			 
			
				 Posted: Wed Dec 03, 2014 7:36 am     | 
				     | 
			 
			
				
  | 
			 
			
				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 | 
			 
		  |