Library to control the 12 Bits Analog Digital Converter MCP4921
Mcp4921.h@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 | #ifndef MCP4921_H |
adelino | 0:38e03f9fa18a | 2 | #define MCP4921_H |
adelino | 0:38e03f9fa18a | 3 | |
adelino | 0:38e03f9fa18a | 4 | #include "mbed.h" |
adelino | 0:38e03f9fa18a | 5 | |
adelino | 0:38e03f9fa18a | 6 | /** |
adelino | 0:38e03f9fa18a | 7 | * The MCP4921 is a single 12 bit DAC. |
adelino | 0:38e03f9fa18a | 8 | The maximum voltage output is limited by the input voltage at V_DD which can go up to about 5.5V. |
adelino | 0:38e03f9fa18a | 9 | |
adelino | 0:38e03f9fa18a | 10 | * Datasheet: http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en020398 |
adelino | 0:38e03f9fa18a | 11 | * |
adelino | 1:1352606a60c6 | 12 | *Exemple: |
adelino | 1:1352606a60c6 | 13 | @code |
adelino | 1:1352606a60c6 | 14 | //Mbed Mcp4921 |
adelino | 1:1352606a60c6 | 15 | //p11: Mosi <---> p4:Sdi |
adelino | 1:1352606a60c6 | 16 | //p12: Miso No Connected |
adelino | 1:1352606a60c6 | 17 | //p13: Sck <---> p3:Sck |
adelino | 1:1352606a60c6 | 18 | //p14 <---> p2:Cs |
adelino | 1:1352606a60c6 | 19 | //Vout <---> p1:Vdd |
adelino | 1:1352606a60c6 | 20 | //Vout <---> p6: Vref |
adelino | 1:1352606a60c6 | 21 | //Gnd <---> p7:Gnd |
adelino | 1:1352606a60c6 | 22 | //Gnd <---> p5:Ldac |
adelino | 1:1352606a60c6 | 23 | |
adelino | 1:1352606a60c6 | 24 | #include "mbed.h" |
adelino | 1:1352606a60c6 | 25 | #include "Mcp4921.h" |
adelino | 1:1352606a60c6 | 26 | |
adelino | 1:1352606a60c6 | 27 | Mcp4921 cna(p11,p13,p14,500000); |
adelino | 1:1352606a60c6 | 28 | int main() |
adelino | 1:1352606a60c6 | 29 | { |
adelino | 1:1352606a60c6 | 30 | static int mv; |
adelino | 1:1352606a60c6 | 31 | while(1) { |
adelino | 1:1352606a60c6 | 32 | cna.write_mV(mv); |
adelino | 1:1352606a60c6 | 33 | |
adelino | 1:1352606a60c6 | 34 | mv+=500; |
adelino | 1:1352606a60c6 | 35 | if(mv>4000) |
adelino | 1:1352606a60c6 | 36 | mv=0; |
adelino | 1:1352606a60c6 | 37 | } |
adelino | 1:1352606a60c6 | 38 | } |
adelino | 1:1352606a60c6 | 39 | |
adelino | 1:1352606a60c6 | 40 | @endcode |
adelino | 0:38e03f9fa18a | 41 | */ |
adelino | 0:38e03f9fa18a | 42 | |
adelino | 0:38e03f9fa18a | 43 | class Mcp4921 |
adelino | 0:38e03f9fa18a | 44 | { |
adelino | 0:38e03f9fa18a | 45 | |
adelino | 0:38e03f9fa18a | 46 | public: |
adelino | 1:1352606a60c6 | 47 | /**Initializes the MCP 4921 DAC with the frequency |
adelino | 1:1352606a60c6 | 48 | |
adelino | 0:38e03f9fa18a | 49 | */ |
adelino | 0:38e03f9fa18a | 50 | Mcp4921(PinName _mosi,PinName _sck,PinName _cs, int _frequency); |
adelino | 0:38e03f9fa18a | 51 | |
adelino | 1:1352606a60c6 | 52 | /** @returns The frequency use by the Mcp4921 |
adelino | 0:38e03f9fa18a | 53 | */ |
adelino | 0:38e03f9fa18a | 54 | int getFrequency(void); |
adelino | 0:38e03f9fa18a | 55 | |
adelino | 0:38e03f9fa18a | 56 | /** Writes a value between 0-4095 to the currently selected DAC output |
adelino | 1:1352606a60c6 | 57 | * @param dataIntNum A value from 0-4095 to write to the DAC register |
adelino | 0:38e03f9fa18a | 58 | */ |
adelino | 0:38e03f9fa18a | 59 | void write(int dataIntNum); |
adelino | 0:38e03f9fa18a | 60 | |
adelino | 0:38e03f9fa18a | 61 | /** Writes a float value between 0.0-1.0 to the currently selected DAC output |
adelino | 1:1352606a60c6 | 62 | * @param dataFloat A value from 0.0-1.0 to write to the DAC register |
adelino | 0:38e03f9fa18a | 63 | */ |
adelino | 0:38e03f9fa18a | 64 | void write(float dataFloat); |
adelino | 0:38e03f9fa18a | 65 | |
adelino | 0:38e03f9fa18a | 66 | /** Writes a value in mV to the DAC outputs. |
adelino | 0:38e03f9fa18a | 67 | * The output will only be accurate if Vref is set to the appropriate voltage reference scaling factor. |
adelino | 0:38e03f9fa18a | 68 | * @param millivolt The desired voltage output in millivolts |
adelino | 0:38e03f9fa18a | 69 | */ |
adelino | 0:38e03f9fa18a | 70 | void write_mV(int millivolt); |
adelino | 0:38e03f9fa18a | 71 | |
adelino | 0:38e03f9fa18a | 72 | /**An operator for shorthand write(int) |
adelino | 0:38e03f9fa18a | 73 | */ |
adelino | 0:38e03f9fa18a | 74 | void operator=(int dataIntNum); |
adelino | 0:38e03f9fa18a | 75 | |
adelino | 0:38e03f9fa18a | 76 | |
adelino | 0:38e03f9fa18a | 77 | /**An operator for shorthand write(float) |
adelino | 0:38e03f9fa18a | 78 | */ |
adelino | 0:38e03f9fa18a | 79 | void operator=(float dataFloat); |
adelino | 0:38e03f9fa18a | 80 | |
adelino | 0:38e03f9fa18a | 81 | |
adelino | 0:38e03f9fa18a | 82 | protected: |
adelino | 0:38e03f9fa18a | 83 | // |
adelino | 0:38e03f9fa18a | 84 | void setup(void); |
adelino | 0:38e03f9fa18a | 85 | SPI mySPI; |
adelino | 0:38e03f9fa18a | 86 | DigitalOut myCs; |
adelino | 0:38e03f9fa18a | 87 | int myFrequency; |
adelino | 0:38e03f9fa18a | 88 | |
adelino | 0:38e03f9fa18a | 89 | }; |
adelino | 0:38e03f9fa18a | 90 | |
adelino | 0:38e03f9fa18a | 91 | #endif |