The MCP4922 is a 12 bit DAC. This library should provide easy access to its basic functionality.
MCP4922.h@1:454c4d3eeae9, 2013-07-16 (annotated)
- Committer:
- JimmyTheHack
- Date:
- Tue Jul 16 06:10:10 2013 +0000
- Revision:
- 1:454c4d3eeae9
- Parent:
- 0:4e1ee1c4d3bb
separated SPI_DAC into outside library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JimmyTheHack | 0:4e1ee1c4d3bb | 1 | #ifndef MCP4922_H |
JimmyTheHack | 0:4e1ee1c4d3bb | 2 | #define MCP4922_H |
JimmyTheHack | 0:4e1ee1c4d3bb | 3 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 4 | #include "DAC_SPI.h" |
JimmyTheHack | 0:4e1ee1c4d3bb | 5 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 6 | /** |
JimmyTheHack | 0:4e1ee1c4d3bb | 7 | * The MCP4922 is a dual package 12 bit DAC. We should be able to produce two analog output voltages up to twice the externally wired voltage references (VrefA, VrefB). |
JimmyTheHack | 0:4e1ee1c4d3bb | 8 | The maximum voltage output is limited by the input voltage at V_DD which can go up to about 5.5V. |
JimmyTheHack | 0:4e1ee1c4d3bb | 9 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 10 | * Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22250A.pdf |
JimmyTheHack | 0:4e1ee1c4d3bb | 11 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 12 | * 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 | 13 | * + bit 15: select which DAC to use. |
JimmyTheHack | 0:4e1ee1c4d3bb | 14 | * + bit 14: 0=buffered , 1= unbuffered |
JimmyTheHack | 0:4e1ee1c4d3bb | 15 | * + bit 13: 0= gain x2, 1= gain x1 |
JimmyTheHack | 0:4e1ee1c4d3bb | 16 | * + bit 12: 0= DAC active, 1= shut down DAC |
JimmyTheHack | 0:4e1ee1c4d3bb | 17 | * + bit 11-0: Data bits for the DAC. |
JimmyTheHack | 0:4e1ee1c4d3bb | 18 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 19 | class MCP4922: public DAC_SPI |
JimmyTheHack | 0:4e1ee1c4d3bb | 20 | { |
JimmyTheHack | 0:4e1ee1c4d3bb | 21 | public: |
JimmyTheHack | 0:4e1ee1c4d3bb | 22 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 23 | /** Initializes the MCP 4922 DAC |
JimmyTheHack | 0:4e1ee1c4d3bb | 24 | * |
JimmyTheHack | 0:4e1ee1c4d3bb | 25 | * @param SPIchannelNum An int representing the SPI channel used by this DAC |
JimmyTheHack | 0:4e1ee1c4d3bb | 26 | * channel 0 for p5 and p7 |
JimmyTheHack | 0:4e1ee1c4d3bb | 27 | * channel 1 for p11 and p13 |
JimmyTheHack | 0:4e1ee1c4d3bb | 28 | * @param CS The chip select pin used to identify the device |
JimmyTheHack | 0:4e1ee1c4d3bb | 29 | * @param LDAC The LDAC pin used to synchronize updates of multiple devices |
JimmyTheHack | 0:4e1ee1c4d3bb | 30 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 31 | MCP4922(int SPIchannelNum, PinName CS, PinName LDAC); |
JimmyTheHack | 0:4e1ee1c4d3bb | 32 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 33 | /** Writes a value between 0-4095 to the currently selected DAC output |
JimmyTheHack | 0:4e1ee1c4d3bb | 34 | * @param DACvalue a value from 0-4095 to write to the DAC register |
JimmyTheHack | 0:4e1ee1c4d3bb | 35 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 36 | virtual void write(int DACvalue); |
JimmyTheHack | 0:4e1ee1c4d3bb | 37 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 38 | /** Writes a value in mV to the DAC outputs. |
JimmyTheHack | 0:4e1ee1c4d3bb | 39 | * The output will only be accurate if Vref is set to the appropriate voltage reference scaling factor. |
JimmyTheHack | 0:4e1ee1c4d3bb | 40 | * @param millivolte The desired voltage output in millivolts |
JimmyTheHack | 0:4e1ee1c4d3bb | 41 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 42 | virtual void write_mV(int millivolts); |
JimmyTheHack | 0:4e1ee1c4d3bb | 43 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 44 | /** If automatic updating is disabled, this will update the DAC output on command */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 45 | void update(); |
JimmyTheHack | 0:4e1ee1c4d3bb | 46 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 47 | /** select whether to use DAC A or DAC B. |
JimmyTheHack | 0:4e1ee1c4d3bb | 48 | * @param DACnum 0 to modify DAC A, 1 to modify DAC B |
JimmyTheHack | 0:4e1ee1c4d3bb | 49 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 50 | virtual void select(char DACnum); |
JimmyTheHack | 0:4e1ee1c4d3bb | 51 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 52 | /** If true, the DAC output will update as soon as the output value is modified. If false, it will wait for an update command*/ |
JimmyTheHack | 0:4e1ee1c4d3bb | 53 | bool autoUpdate; |
JimmyTheHack | 0:4e1ee1c4d3bb | 54 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 55 | /** The currently selected DAC channel. 0 for DAC A, 1 for DAC B*/ |
JimmyTheHack | 0:4e1ee1c4d3bb | 56 | char DACselect; |
JimmyTheHack | 0:4e1ee1c4d3bb | 57 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 58 | /** The output scaling factor in mV for channel A. |
JimmyTheHack | 0:4e1ee1c4d3bb | 59 | * The voltage wired to Vref will be used as the scaling factor which the DAC will use in producing the output voltage. |
JimmyTheHack | 0:4e1ee1c4d3bb | 60 | * If using the write_mV function, it is important that Vref is properly set. This library uses 5000mV by default. |
JimmyTheHack | 0:4e1ee1c4d3bb | 61 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 62 | int VrefA; |
JimmyTheHack | 0:4e1ee1c4d3bb | 63 | /** The output scaling factor in mV for channel B. |
JimmyTheHack | 0:4e1ee1c4d3bb | 64 | * The voltage wired to Vref will be used as the scaling factor which the DAC will use in producing the output voltage. |
JimmyTheHack | 0:4e1ee1c4d3bb | 65 | * If using the write_mV function, it is important that Vref is properly set. This library uses 5000mV by default. |
JimmyTheHack | 0:4e1ee1c4d3bb | 66 | */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 67 | int VrefB; |
JimmyTheHack | 0:4e1ee1c4d3bb | 68 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 69 | /** Manually set the gain of the DAC. The only valid values are 1 and 2. The default gain is x1 to match the wired reference voltage*/ |
JimmyTheHack | 0:4e1ee1c4d3bb | 70 | void setGain(int gain_value); |
JimmyTheHack | 0:4e1ee1c4d3bb | 71 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 72 | /** Turn on or off the output buffer. The default value for the buffer is off. See section 4.1.2 of the datasheet for more information. */ |
JimmyTheHack | 0:4e1ee1c4d3bb | 73 | char buffered; |
JimmyTheHack | 0:4e1ee1c4d3bb | 74 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 75 | private: |
JimmyTheHack | 0:4e1ee1c4d3bb | 76 | /** The output gain bit of the DAC. If set to 0, gain is x2. If set to 1 gain is x1. The default gain is 1.*/ |
JimmyTheHack | 0:4e1ee1c4d3bb | 77 | bool gain; |
JimmyTheHack | 0:4e1ee1c4d3bb | 78 | }; |
JimmyTheHack | 0:4e1ee1c4d3bb | 79 | |
JimmyTheHack | 0:4e1ee1c4d3bb | 80 | #endif //MCP4922_H |