Library to control the 12 Bits Analog Digital Converter MCP4921
Mcp4921.cpp@1:1352606a60c6, 2013-11-17 (annotated)
- Committer:
- adelino
- Date:
- Sun Nov 17 13:34:18 2013 +0000
- Revision:
- 1:1352606a60c6
- Parent:
- 0:38e03f9fa18a
Library to control the 12 Bits Analog Digital Converter; MCP4921
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adelino | 0:38e03f9fa18a | 1 | #include "Mcp4921.h" |
adelino | 0:38e03f9fa18a | 2 | |
adelino | 0:38e03f9fa18a | 3 | Mcp4921::Mcp4921(PinName _mosi,PinName _sck,PinName _cs, int _frequency):mySPI(_mosi,NC,_sck),myCs(_cs) |
adelino | 0:38e03f9fa18a | 4 | { |
adelino | 0:38e03f9fa18a | 5 | myFrequency=_frequency; |
adelino | 0:38e03f9fa18a | 6 | |
adelino | 0:38e03f9fa18a | 7 | setup(); |
adelino | 0:38e03f9fa18a | 8 | } |
adelino | 0:38e03f9fa18a | 9 | //--------------------------------------- |
adelino | 0:38e03f9fa18a | 10 | int Mcp4921::getFrequency(void) |
adelino | 0:38e03f9fa18a | 11 | { |
adelino | 0:38e03f9fa18a | 12 | return myFrequency; |
adelino | 0:38e03f9fa18a | 13 | } |
adelino | 0:38e03f9fa18a | 14 | //-------------------------------------- |
adelino | 0:38e03f9fa18a | 15 | void Mcp4921::write(int dataIntNum) |
adelino | 0:38e03f9fa18a | 16 | { |
adelino | 0:38e03f9fa18a | 17 | //valid input values are 0 - 4095. 4096 should scale to Vref. |
adelino | 0:38e03f9fa18a | 18 | char i=0; |
adelino | 0:38e03f9fa18a | 19 | |
adelino | 0:38e03f9fa18a | 20 | if(dataIntNum >4095) |
adelino | 0:38e03f9fa18a | 21 | dataIntNum=4095; |
adelino | 0:38e03f9fa18a | 22 | |
adelino | 0:38e03f9fa18a | 23 | if(dataIntNum <0) |
adelino | 0:38e03f9fa18a | 24 | dataIntNum=0; |
adelino | 0:38e03f9fa18a | 25 | |
adelino | 0:38e03f9fa18a | 26 | int16_t temp=0; |
adelino | 0:38e03f9fa18a | 27 | temp=dataIntNum | 0x7000; |
adelino | 0:38e03f9fa18a | 28 | |
adelino | 0:38e03f9fa18a | 29 | myCs=0; |
adelino | 0:38e03f9fa18a | 30 | for(i=0; i<50; i++); |
adelino | 0:38e03f9fa18a | 31 | mySPI.write(temp); |
adelino | 0:38e03f9fa18a | 32 | |
adelino | 0:38e03f9fa18a | 33 | for(i=0; i<50; i++); |
adelino | 0:38e03f9fa18a | 34 | myCs=1; |
adelino | 0:38e03f9fa18a | 35 | } |
adelino | 0:38e03f9fa18a | 36 | //-------------------------------------- |
adelino | 0:38e03f9fa18a | 37 | void Mcp4921::write_mV(int millivolt){ |
adelino | 0:38e03f9fa18a | 38 | |
adelino | 0:38e03f9fa18a | 39 | if(millivolt >3300) |
adelino | 0:38e03f9fa18a | 40 | millivolt=3300; |
adelino | 0:38e03f9fa18a | 41 | |
adelino | 0:38e03f9fa18a | 42 | if(millivolt <0) |
adelino | 0:38e03f9fa18a | 43 | millivolt=0; |
adelino | 0:38e03f9fa18a | 44 | |
adelino | 0:38e03f9fa18a | 45 | int val=(millivolt*4095)/3300; |
adelino | 0:38e03f9fa18a | 46 | |
adelino | 0:38e03f9fa18a | 47 | write(val); |
adelino | 0:38e03f9fa18a | 48 | } |
adelino | 0:38e03f9fa18a | 49 | //-------------------------------------- |
adelino | 0:38e03f9fa18a | 50 | void Mcp4921::write(float dataFloat) |
adelino | 0:38e03f9fa18a | 51 | { |
adelino | 0:38e03f9fa18a | 52 | //valid input values are 0.0 - 1.0 with 1.0 should scale to Vref. |
adelino | 0:38e03f9fa18a | 53 | if(dataFloat >1.0) |
adelino | 0:38e03f9fa18a | 54 | dataFloat=1.0; |
adelino | 0:38e03f9fa18a | 55 | |
adelino | 0:38e03f9fa18a | 56 | if(dataFloat <0) |
adelino | 0:38e03f9fa18a | 57 | dataFloat=0; |
adelino | 0:38e03f9fa18a | 58 | |
adelino | 0:38e03f9fa18a | 59 | float temp=4095*dataFloat; |
adelino | 0:38e03f9fa18a | 60 | |
adelino | 0:38e03f9fa18a | 61 | write((int)temp); |
adelino | 0:38e03f9fa18a | 62 | } |
adelino | 0:38e03f9fa18a | 63 | //-------------------------------------- |
adelino | 0:38e03f9fa18a | 64 | void Mcp4921::operator=(int dataIntNum) |
adelino | 0:38e03f9fa18a | 65 | { |
adelino | 0:38e03f9fa18a | 66 | write(dataIntNum); |
adelino | 0:38e03f9fa18a | 67 | } |
adelino | 0:38e03f9fa18a | 68 | //-------------------------------------- |
adelino | 0:38e03f9fa18a | 69 | void Mcp4921::operator=(float dataFloat) |
adelino | 0:38e03f9fa18a | 70 | { |
adelino | 0:38e03f9fa18a | 71 | write(dataFloat); |
adelino | 0:38e03f9fa18a | 72 | } |
adelino | 0:38e03f9fa18a | 73 | //-------------------------------------- |
adelino | 0:38e03f9fa18a | 74 | void Mcp4921::setup(void) |
adelino | 0:38e03f9fa18a | 75 | { |
adelino | 0:38e03f9fa18a | 76 | myCs=1; |
adelino | 0:38e03f9fa18a | 77 | mySPI.frequency(myFrequency); // |
adelino | 0:38e03f9fa18a | 78 | mySPI.format(16,0); //16 bits + mode 0 |
adelino | 0:38e03f9fa18a | 79 | } |
adelino | 0:38e03f9fa18a | 80 | // |