pins change for LPC1114FN28
Fork of MCP4922 by
MCP4922.cpp@3:3702b6bfb50e, 2015-08-25 (annotated)
- Committer:
- kohacraft
- Date:
- Tue Aug 25 21:24:15 2015 +0000
- Revision:
- 3:3702b6bfb50e
- Parent:
- 2:622d022eed87
pins change for LPC1114FN28
Who changed what in which revision?
User | Revision | Line number | New 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 |
kohacraft | 2:622d022eed87 | 47 | char gain2 = 0; |
kohacraft | 2:622d022eed87 | 48 | if( gain == 0 ) |
kohacraft | 2:622d022eed87 | 49 | gain2 = 1; |
kohacraft | 2:622d022eed87 | 50 | char controlbits=(DACselect<<3) + (buffered<<2) + (gain2<<1) +1; |
JimmyTheHack | 0:4e1ee1c4d3bb | 51 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 52 | int message= (controlbits<<12)+value; |
JimmyTheHack | 0:4e1ee1c4d3bb | 53 | (*DACspi).write(message); |
JimmyTheHack | 0:4e1ee1c4d3bb | 54 | CS=1; //signal end of message. The data will be loaded into the internal registers. |
JimmyTheHack | 0:4e1ee1c4d3bb | 55 | if(autoUpdate){ LDAC=0;} //trigger the update of the output voltage. |
JimmyTheHack | 0:4e1ee1c4d3bb | 56 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 57 | } |
JimmyTheHack | 0:4e1ee1c4d3bb | 58 | void MCP4922::update(){ |
JimmyTheHack | 0:4e1ee1c4d3bb | 59 | //triggers the DAC to update output on command. Useful if we wish to synchronize the DAC output value with another event. |
JimmyTheHack | 0:4e1ee1c4d3bb | 60 | LDAC=0; |
JimmyTheHack | 0:4e1ee1c4d3bb | 61 | } |
JimmyTheHack | 0:4e1ee1c4d3bb | 62 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 63 | //Set the multiplying factor for the output. Valid inputs are gains of 1 and 2.*/ |
JimmyTheHack | 0:4e1ee1c4d3bb | 64 | void MCP4922::setGain(int gain_value){ |
JimmyTheHack | 0:4e1ee1c4d3bb | 65 | if (gain_value>1){ |
JimmyTheHack | 0:4e1ee1c4d3bb | 66 | gain =1; //Set gain to x2 |
JimmyTheHack | 0:4e1ee1c4d3bb | 67 | } |
JimmyTheHack | 0:4e1ee1c4d3bb | 68 | else{ |
JimmyTheHack | 0:4e1ee1c4d3bb | 69 | gain=0; //Set gain to x1. Limits range to 2.098mV |
JimmyTheHack | 0:4e1ee1c4d3bb | 70 | } |
JimmyTheHack | 0:4e1ee1c4d3bb | 71 | } |