The MAX500 is an 8 bit DAC that can output to higher voltages (~12V). The chip has 4 onboard DAC channels.
MAX500.cpp@1:90b59ae53d25, 2013-07-16 (annotated)
- Committer:
- JimmyTheHack
- Date:
- Tue Jul 16 06:06:21 2013 +0000
- Revision:
- 1:90b59ae53d25
- Parent:
- 0:b74d88185698
attempt at making DAC_SPI libraries separate
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimmyTheHack | 0:b74d88185698 | 1 | #include "MAX500.h" |
JimmyTheHack | 0:b74d88185698 | 2 | |
JimmyTheHack | 0:b74d88185698 | 3 | MAX500::MAX500(int SPIchannelNum, PinName _LOAD, PinName _LDAC) : DAC_SPI(SPIchannelNum, _LOAD, _LDAC){ //We use the 3 wire serial interface, since the timing is more standardized. |
JimmyTheHack | 0:b74d88185698 | 4 | (*DACspi).format(10,2); //messages use a 10 bit word, and read data on the falling edge with the clock defaulting to HIGH. |
JimmyTheHack | 0:b74d88185698 | 5 | frequency(5000000); //set default frequency to 5MHz since unstated |
JimmyTheHack | 0:b74d88185698 | 6 | autoUpdate=1; |
JimmyTheHack | 0:b74d88185698 | 7 | VrefAB=5000; //assume a 5V reference voltage for use with write_mv(). This value can be configured; |
JimmyTheHack | 0:b74d88185698 | 8 | VrefC=5000; //assume a 5V reference voltage for use with write_mv(). This value can be configured; |
JimmyTheHack | 0:b74d88185698 | 9 | VrefD=5000; //assume a 5V reference voltage for use with write_mv(). This value can be configured; |
JimmyTheHack | 0:b74d88185698 | 10 | CS=1; //CS is our LOAD pin |
JimmyTheHack | 0:b74d88185698 | 11 | } |
JimmyTheHack | 0:b74d88185698 | 12 | |
JimmyTheHack | 0:b74d88185698 | 13 | void MAX500::select(char DACnum){ |
JimmyTheHack | 0:b74d88185698 | 14 | DACselect=DACnum & 0x03; //choose between DAC A , B, C or D |
JimmyTheHack | 0:b74d88185698 | 15 | } |
JimmyTheHack | 0:b74d88185698 | 16 | |
JimmyTheHack | 0:b74d88185698 | 17 | void MAX500::write_mV(int mV){ |
JimmyTheHack | 0:b74d88185698 | 18 | int Vref; |
JimmyTheHack | 0:b74d88185698 | 19 | if (DACselect==0 || DACselect ==1){ |
JimmyTheHack | 0:b74d88185698 | 20 | Vref=VrefAB; |
JimmyTheHack | 0:b74d88185698 | 21 | } |
JimmyTheHack | 0:b74d88185698 | 22 | else if (DACselect ==2) { |
JimmyTheHack | 0:b74d88185698 | 23 | Vref=VrefC; |
JimmyTheHack | 0:b74d88185698 | 24 | } |
JimmyTheHack | 0:b74d88185698 | 25 | else{ |
JimmyTheHack | 0:b74d88185698 | 26 | Vref=VrefD; |
JimmyTheHack | 0:b74d88185698 | 27 | } |
JimmyTheHack | 0:b74d88185698 | 28 | int DACvalue= mV* 255/Vref ; //scale voltage to a DAC value. |
JimmyTheHack | 0:b74d88185698 | 29 | write(DACvalue); |
JimmyTheHack | 0:b74d88185698 | 30 | } |
JimmyTheHack | 0:b74d88185698 | 31 | |
JimmyTheHack | 0:b74d88185698 | 32 | void MAX500::write(int value){ |
JimmyTheHack | 0:b74d88185698 | 33 | //valid input values are 0 - 255. 255 should scale to Vref. |
JimmyTheHack | 0:b74d88185698 | 34 | //All serial commands are 10 bit words. The highest 2 bits are control bits, while the last 8 are the data bits for the 8-bit DAC MAX500. |
JimmyTheHack | 0:b74d88185698 | 35 | |
JimmyTheHack | 0:b74d88185698 | 36 | //bit 9-8: Selection bits for DAC |
JimmyTheHack | 0:b74d88185698 | 37 | //bit 7-0: Data bits for the DAC. |
JimmyTheHack | 0:b74d88185698 | 38 | |
JimmyTheHack | 0:b74d88185698 | 39 | if (value > 0x0FF){ |
JimmyTheHack | 0:b74d88185698 | 40 | value = 0x0FF; //any out of range values will be truncated to our max value |
JimmyTheHack | 0:b74d88185698 | 41 | } |
JimmyTheHack | 0:b74d88185698 | 42 | value=value & 0x0FF; //limit our value to 8 bits. |
JimmyTheHack | 0:b74d88185698 | 43 | |
JimmyTheHack | 0:b74d88185698 | 44 | //SCK=0; //set the clock low. |
JimmyTheHack | 0:b74d88185698 | 45 | LDAC=1; |
JimmyTheHack | 0:b74d88185698 | 46 | CS=1; //enable the chip to recieve data |
JimmyTheHack | 0:b74d88185698 | 47 | |
JimmyTheHack | 0:b74d88185698 | 48 | int message= (DACselect<<8)+value; |
JimmyTheHack | 0:b74d88185698 | 49 | (*DACspi).write(message); |
JimmyTheHack | 0:b74d88185698 | 50 | //wait_us(5); |
JimmyTheHack | 0:b74d88185698 | 51 | CS=0; //signal end of message. The data will be loaded into the internal registers. |
JimmyTheHack | 0:b74d88185698 | 52 | //wait_us(5); |
JimmyTheHack | 0:b74d88185698 | 53 | CS=1; |
JimmyTheHack | 0:b74d88185698 | 54 | if(autoUpdate){ LDAC=0;} //trigger the update of the output voltage. |
JimmyTheHack | 0:b74d88185698 | 55 | |
JimmyTheHack | 0:b74d88185698 | 56 | } |
JimmyTheHack | 0:b74d88185698 | 57 | void MAX500::update(){ |
JimmyTheHack | 0:b74d88185698 | 58 | //triggers the DAC to update output on command. Useful if we wish to synchronize the DAC output value with another event. |
JimmyTheHack | 0:b74d88185698 | 59 | LDAC=0; |
JimmyTheHack | 0:b74d88185698 | 60 | } |
JimmyTheHack | 0:b74d88185698 | 61 |