Basic libraries for functionality of the MCP4822 DAC, an easy to use DAC outputting up to 4.096V.

Dependencies:   DAC

Committer:
JimmyTheHack
Date:
Tue Jul 16 06:23:19 2013 +0000
Revision:
2:39a2c041eefe
Parent:
1:a710d52d3d19
DAC_SPI now in separate library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimmyTheHack 0:a10e8793cf0a 1 #ifndef MCP4822_H
JimmyTheHack 0:a10e8793cf0a 2 #define MCP4822_H
JimmyTheHack 0:a10e8793cf0a 3 #include "DAC_SPI.h"
JimmyTheHack 0:a10e8793cf0a 4
JimmyTheHack 1:a710d52d3d19 5 /** The MCP4822 is a dual package 12 bit DAC. It has a simple pinout and is pretty easy to use.
JimmyTheHack 0:a10e8793cf0a 6 * We should be able to produce two analog output voltages up to 4.096V, by using a gain of 2 with the internal reference diode of 2.048V.
JimmyTheHack 0:a10e8793cf0a 7 * This is a simple library intended to help beginners get started quickly.
JimmyTheHack 0:a10e8793cf0a 8
JimmyTheHack 0:a10e8793cf0a 9 *Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/22249A.pdf
JimmyTheHack 0:a10e8793cf0a 10
JimmyTheHack 0:a10e8793cf0a 11 *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:a10e8793cf0a 12 * + bit 15: select which DAC to use.
JimmyTheHack 0:a10e8793cf0a 13 * + bit 14: unused
JimmyTheHack 0:a10e8793cf0a 14 * + bit 13: 0= gain x2, 1= gain x1
JimmyTheHack 0:a10e8793cf0a 15 * + bit 12: 0= DAC active, 1= shut down DAC
JimmyTheHack 0:a10e8793cf0a 16 * + bit 11-0: Data bits for the DAC.
JimmyTheHack 0:a10e8793cf0a 17 */
JimmyTheHack 0:a10e8793cf0a 18 class MCP4822: public DAC_SPI
JimmyTheHack 0:a10e8793cf0a 19 {
JimmyTheHack 0:a10e8793cf0a 20
JimmyTheHack 0:a10e8793cf0a 21 public:
JimmyTheHack 0:a10e8793cf0a 22 /** Initializes the MCP 4822 DAC
JimmyTheHack 0:a10e8793cf0a 23 *
JimmyTheHack 0:a10e8793cf0a 24 * @param SPIchannelNum An int representing the SPI channel used by this DAC
JimmyTheHack 0:a10e8793cf0a 25 * channel 0 for p5 and p7
JimmyTheHack 0:a10e8793cf0a 26 * channel 1 for p11 and p13
JimmyTheHack 0:a10e8793cf0a 27 * @param CS The chip select pin used to identify the device
JimmyTheHack 0:a10e8793cf0a 28 * @param LDAC The LDAC pin used to synchronize updates of multiple devices
JimmyTheHack 0:a10e8793cf0a 29 */
JimmyTheHack 0:a10e8793cf0a 30 MCP4822(int SPIchannelNum, PinName CS, PinName LDAC);
JimmyTheHack 0:a10e8793cf0a 31
JimmyTheHack 0:a10e8793cf0a 32 /** Writes a value between 0-4095 to the currently selected DAC output
JimmyTheHack 0:a10e8793cf0a 33 * @param DACvalue a value from 0-4095 to write to the DAC register
JimmyTheHack 0:a10e8793cf0a 34 */
JimmyTheHack 0:a10e8793cf0a 35 virtual void write(int millivolts);
JimmyTheHack 0:a10e8793cf0a 36
JimmyTheHack 0:a10e8793cf0a 37 /** Writes a value between 0-4095mV to the currently selected DAC output. Unless the gain has been modified this will produce the same result as write().
JimmyTheHack 0:a10e8793cf0a 38 * @param DACvalue a value from 0-4095mV to write to the DAC register
JimmyTheHack 0:a10e8793cf0a 39 */
JimmyTheHack 0:a10e8793cf0a 40 virtual void write_mv(int millivolts);
JimmyTheHack 0:a10e8793cf0a 41 /** If automatic updating is disabled, this will update the DAC output on command */
JimmyTheHack 0:a10e8793cf0a 42 void update();
JimmyTheHack 0:a10e8793cf0a 43 /** select whether to use DAC A or DAC B.
JimmyTheHack 0:a10e8793cf0a 44 * @param DACnum 0 to modify DAC A, 1 to modify DAC B
JimmyTheHack 0:a10e8793cf0a 45 */
JimmyTheHack 0:a10e8793cf0a 46 void select(char DACnum); //select whether to use DAC A or DAC B.
JimmyTheHack 0:a10e8793cf0a 47 /** 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:a10e8793cf0a 48
JimmyTheHack 0:a10e8793cf0a 49
JimmyTheHack 0:a10e8793cf0a 50 /** 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:a10e8793cf0a 51 bool autoUpdate;
JimmyTheHack 0:a10e8793cf0a 52 /** The currently selected DAC channel. 0 for DAC A, 1 for DAC B*/
JimmyTheHack 0:a10e8793cf0a 53 bool DACselect;
JimmyTheHack 0:a10e8793cf0a 54
JimmyTheHack 0:a10e8793cf0a 55 /** Manually set the gain of the DAC. The only valid values are 1 and 2. The default gain is x2, allowing an output range of up to 4.096V*/
JimmyTheHack 0:a10e8793cf0a 56 void setGain(int gain_value);
JimmyTheHack 0:a10e8793cf0a 57
JimmyTheHack 0:a10e8793cf0a 58 private:
JimmyTheHack 0:a10e8793cf0a 59 /** 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 2, allowing a range of up to 4.096V.*/
JimmyTheHack 0:a10e8793cf0a 60 bool gain;
JimmyTheHack 0:a10e8793cf0a 61 };
JimmyTheHack 0:a10e8793cf0a 62
JimmyTheHack 0:a10e8793cf0a 63 #endif