The MCP4922 is a 12 bit DAC. This library should provide easy access to its basic functionality.

Dependencies:   DAC

Committer:
JimmyTheHack
Date:
Tue Jul 16 06:10:10 2013 +0000
Revision:
1:454c4d3eeae9
Parent:
0:4e1ee1c4d3bb
separated SPI_DAC into outside library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimmyTheHack 0:4e1ee1c4d3bb 1 #include "MCP4922.h"
JimmyTheHack 0:4e1ee1c4d3bb 2
JimmyTheHack 0:4e1ee1c4d3bb 3 MCP4922::MCP4922(int SPIchannelNum, PinName _CS, PinName _LDAC) : DAC_SPI(SPIchannelNum, _CS, _LDAC){
JimmyTheHack 0:4e1ee1c4d3bb 4 messageBits(16); //messages use a 16 bit word
JimmyTheHack 0:4e1ee1c4d3bb 5 frequency(20000000); //set default frequency to 20MHz
JimmyTheHack 0:4e1ee1c4d3bb 6 autoUpdate=1;
JimmyTheHack 0:4e1ee1c4d3bb 7 VrefA=5000; //assume a 5V reference voltage for use with write_mv(). This value can be configured;
JimmyTheHack 0:4e1ee1c4d3bb 8 VrefB=5000;
JimmyTheHack 0:4e1ee1c4d3bb 9 gain =1; //set gain to x1 to match Vref.
JimmyTheHack 0:4e1ee1c4d3bb 10 buffered =0; //leave output buffer off
JimmyTheHack 0:4e1ee1c4d3bb 11 }
JimmyTheHack 0:4e1ee1c4d3bb 12
JimmyTheHack 0:4e1ee1c4d3bb 13 void MCP4922::select(char DACnum){
JimmyTheHack 0:4e1ee1c4d3bb 14 DACselect=DACnum & 0x01; //choose between DAC A and DAC B
JimmyTheHack 0:4e1ee1c4d3bb 15 }
JimmyTheHack 0:4e1ee1c4d3bb 16
JimmyTheHack 0:4e1ee1c4d3bb 17 void MCP4922::write_mV(int mV){
JimmyTheHack 0:4e1ee1c4d3bb 18 int Vref;
JimmyTheHack 0:4e1ee1c4d3bb 19 if (DACselect==1){
JimmyTheHack 0:4e1ee1c4d3bb 20 Vref=VrefB;
JimmyTheHack 0:4e1ee1c4d3bb 21 }
JimmyTheHack 0:4e1ee1c4d3bb 22 else{
JimmyTheHack 0:4e1ee1c4d3bb 23 Vref=VrefA;
JimmyTheHack 0:4e1ee1c4d3bb 24 }
JimmyTheHack 0:4e1ee1c4d3bb 25 int DACvalue= mV* 4096/Vref *(gain+1); //scale voltage to a DAC value.
JimmyTheHack 0:4e1ee1c4d3bb 26 write(DACvalue);
JimmyTheHack 0:4e1ee1c4d3bb 27 }
JimmyTheHack 0:4e1ee1c4d3bb 28
JimmyTheHack 0:4e1ee1c4d3bb 29 void MCP4922::write(int value){
JimmyTheHack 0:4e1ee1c4d3bb 30 //valid input values are 0 - 4095. 4096 should scale to Vref.
JimmyTheHack 0:4e1ee1c4d3bb 31 //All serial commands are 16 bit words. The highest 4 bits are control bits, while the last 12 are the data bits for the 12-bit DAC MCP4822.
JimmyTheHack 0:4e1ee1c4d3bb 32 //bit 15: select which DAC to use.
JimmyTheHack 0:4e1ee1c4d3bb 33 //bit 14: 0=unbuffered , 1= buffered
JimmyTheHack 0:4e1ee1c4d3bb 34 //bit 13: 0= gain x2, 1= gain x1
JimmyTheHack 0:4e1ee1c4d3bb 35 //bit 12: 0= DAC active, 1= shut down DAC
JimmyTheHack 0:4e1ee1c4d3bb 36 //bit 11-0: Data bits for the DAC.
JimmyTheHack 0:4e1ee1c4d3bb 37
JimmyTheHack 0:4e1ee1c4d3bb 38 if (value > 0xFFF){
JimmyTheHack 0:4e1ee1c4d3bb 39 value = 0xFFF; //any out of range values will be truncated to our max value
JimmyTheHack 0:4e1ee1c4d3bb 40 }
JimmyTheHack 0:4e1ee1c4d3bb 41 value=value & 0xFFF; //limit our value to 12 bits.
JimmyTheHack 0:4e1ee1c4d3bb 42
JimmyTheHack 0:4e1ee1c4d3bb 43 //SCK=0; //set the clock low. Data is read on the rising edge of the clock.
JimmyTheHack 0:4e1ee1c4d3bb 44 LDAC=1;
JimmyTheHack 0:4e1ee1c4d3bb 45 CS=0; //enable the chip to recieve data
JimmyTheHack 0:4e1ee1c4d3bb 46 //bit 13: 0= gain x2, 1= gain x1
JimmyTheHack 0:4e1ee1c4d3bb 47 char controlbits=(DACselect<<3) + (buffered<<2) + (gain<<1) +1;
JimmyTheHack 0:4e1ee1c4d3bb 48
JimmyTheHack 0:4e1ee1c4d3bb 49 int message= (controlbits<<12)+value;
JimmyTheHack 0:4e1ee1c4d3bb 50 (*DACspi).write(message);
JimmyTheHack 0:4e1ee1c4d3bb 51 CS=1; //signal end of message. The data will be loaded into the internal registers.
JimmyTheHack 0:4e1ee1c4d3bb 52 if(autoUpdate){ LDAC=0;} //trigger the update of the output voltage.
JimmyTheHack 0:4e1ee1c4d3bb 53
JimmyTheHack 0:4e1ee1c4d3bb 54 }
JimmyTheHack 0:4e1ee1c4d3bb 55 void MCP4922::update(){
JimmyTheHack 0:4e1ee1c4d3bb 56 //triggers the DAC to update output on command. Useful if we wish to synchronize the DAC output value with another event.
JimmyTheHack 0:4e1ee1c4d3bb 57 LDAC=0;
JimmyTheHack 0:4e1ee1c4d3bb 58 }
JimmyTheHack 0:4e1ee1c4d3bb 59
JimmyTheHack 0:4e1ee1c4d3bb 60 //Set the multiplying factor for the output. Valid inputs are gains of 1 and 2.*/
JimmyTheHack 0:4e1ee1c4d3bb 61 void MCP4922::setGain(int gain_value){
JimmyTheHack 0:4e1ee1c4d3bb 62 if (gain_value>1){
JimmyTheHack 0:4e1ee1c4d3bb 63 gain =1; //Set gain to x2
JimmyTheHack 0:4e1ee1c4d3bb 64 }
JimmyTheHack 0:4e1ee1c4d3bb 65 else{
JimmyTheHack 0:4e1ee1c4d3bb 66 gain=0; //Set gain to x1. Limits range to 2.098mV
JimmyTheHack 0:4e1ee1c4d3bb 67 }
JimmyTheHack 0:4e1ee1c4d3bb 68 }