Basic libraries for functionality of the MCP4822 DAC, an easy to use DAC outputting up to 4.096V.

Dependencies:   DAC

Committer:
JimmyTheHack
Date:
Tue Jul 16 06:23:19 2013 +0000
Revision:
2:39a2c041eefe
Parent:
0:a10e8793cf0a
DAC_SPI now in separate library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimmyTheHack 0:a10e8793cf0a 1 #include "MCP4822.h"
JimmyTheHack 0:a10e8793cf0a 2
JimmyTheHack 0:a10e8793cf0a 3 //initialization function
JimmyTheHack 0:a10e8793cf0a 4 MCP4822::MCP4822(int SPIchannelNum, PinName _CS, PinName _LDAC) : DAC_SPI(SPIchannelNum, _CS, _LDAC){
JimmyTheHack 0:a10e8793cf0a 5 messageBits(16);
JimmyTheHack 0:a10e8793cf0a 6 frequency(20000000);
JimmyTheHack 0:a10e8793cf0a 7 autoUpdate=1;
JimmyTheHack 0:a10e8793cf0a 8 gain =0; //set gain to x2 to get full range
JimmyTheHack 0:a10e8793cf0a 9 }
JimmyTheHack 0:a10e8793cf0a 10
JimmyTheHack 0:a10e8793cf0a 11 void MCP4822::select(char DACnum){
JimmyTheHack 0:a10e8793cf0a 12 DACselect=DACnum & 0x01; //choose between DAC A and DAC B
JimmyTheHack 0:a10e8793cf0a 13 }
JimmyTheHack 0:a10e8793cf0a 14
JimmyTheHack 0:a10e8793cf0a 15 void MCP4822::write_mv(int mVolts){ //since out reference diode is at 4.096V, we can directly map our voltage values to DAC values when gain is set to x2.
JimmyTheHack 0:a10e8793cf0a 16 if (gain==0){ //gain is x2
JimmyTheHack 0:a10e8793cf0a 17 write(mVolts);
JimmyTheHack 0:a10e8793cf0a 18 }
JimmyTheHack 0:a10e8793cf0a 19 else{ //gain is x1
JimmyTheHack 0:a10e8793cf0a 20 write(mVolts*2);
JimmyTheHack 0:a10e8793cf0a 21 }
JimmyTheHack 0:a10e8793cf0a 22 }
JimmyTheHack 0:a10e8793cf0a 23
JimmyTheHack 0:a10e8793cf0a 24 void MCP4822::write(int value){
JimmyTheHack 0:a10e8793cf0a 25 //valid voltage values are up to 4096mV. We directly map our value to mV.
JimmyTheHack 0:a10e8793cf0a 26 //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:a10e8793cf0a 27 //bit 15: select which DAC to use.
JimmyTheHack 0:a10e8793cf0a 28 //bit 14: unused
JimmyTheHack 0:a10e8793cf0a 29 //bit 13: 0= gain x2, 1= gain x1
JimmyTheHack 0:a10e8793cf0a 30 //bit 12: 0= DAC active, 1= shut down DAC
JimmyTheHack 0:a10e8793cf0a 31 //bit 11-0: Data bits for the DAC.
JimmyTheHack 0:a10e8793cf0a 32 if (value > 0xFFF){
JimmyTheHack 0:a10e8793cf0a 33 value = 0xFFF; //any out of range values will be truncated to our max value
JimmyTheHack 0:a10e8793cf0a 34 }
JimmyTheHack 0:a10e8793cf0a 35
JimmyTheHack 0:a10e8793cf0a 36 value=value & 0xFFF; //limit our value to 12 bits.
JimmyTheHack 0:a10e8793cf0a 37 //SCK=0; //set the clock low. Data is read on the rising edge of the clock.
JimmyTheHack 0:a10e8793cf0a 38 LDAC=1;
JimmyTheHack 0:a10e8793cf0a 39 CS=0; //enable the chip to recieve data
JimmyTheHack 0:a10e8793cf0a 40 char controlbits=(DACselect<<3)+(gain<<1) +1;
JimmyTheHack 0:a10e8793cf0a 41
JimmyTheHack 0:a10e8793cf0a 42 int message= (controlbits<<12)+value;
JimmyTheHack 0:a10e8793cf0a 43 (*DACspi).write(message);
JimmyTheHack 0:a10e8793cf0a 44 CS=1; //signal end of message. The data will be loaded into the internal registers.
JimmyTheHack 0:a10e8793cf0a 45 if(autoUpdate){ LDAC=0;} //trigger the update of the output voltage.
JimmyTheHack 0:a10e8793cf0a 46
JimmyTheHack 0:a10e8793cf0a 47 }
JimmyTheHack 0:a10e8793cf0a 48 void MCP4822::update(){
JimmyTheHack 0:a10e8793cf0a 49 //triggers the DAC to update output on command. Useful if we wish to synchronize the DAC output value with another event.
JimmyTheHack 0:a10e8793cf0a 50 LDAC=0;
JimmyTheHack 0:a10e8793cf0a 51 }
JimmyTheHack 0:a10e8793cf0a 52
JimmyTheHack 0:a10e8793cf0a 53 //Set the multiplying factor for the output. Valid inputs are gains of 1 and 2.*/
JimmyTheHack 0:a10e8793cf0a 54 void MCP4822::setGain(int gain_value){
JimmyTheHack 0:a10e8793cf0a 55 if (gain_value>1){
JimmyTheHack 0:a10e8793cf0a 56 gain =1; //Set gain to x2
JimmyTheHack 0:a10e8793cf0a 57 }
JimmyTheHack 0:a10e8793cf0a 58 else{
JimmyTheHack 0:a10e8793cf0a 59 gain=0; //Set gain to x1. Limits range to 2.098mV
JimmyTheHack 0:a10e8793cf0a 60 }
JimmyTheHack 0:a10e8793cf0a 61 }