Lluis Nadal / Mbed 2 deprecated AD5627_Test

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 /*
00004 **************************************************************************************
00005 AD5627.  Dual 12-bit DAC with i2C interface and external reference pin.
00006 (The AD56x7R have an internal reference). Power supply: 2.7V to 5.5V.
00007 
00008 If needed, the two DACs can be updated simultaneously by LDAC (pin 4)or by software.
00009 
00010 Wiring:
00011 pin 3 (GND) to ground. pin 9 (Vdd) to 3.3V.
00012 pin 10 (Vref) to 3.3V, pin 6 (ADDR) to 3.3V (A1=0, A0=0).
00013 4.7k pull-up resistors on pins 8 and 7 (SDA, SCL)(to mBed's pins 9 and 10).
00014 pin 4 (LDAC) and pin 5 (CLR) to mBed's digital output pins 11 and 12.
00015 
00016 Attach voltmeter to ground and Vout A (pin 1)  or  Vout B (pin 2).
00017 
00018 Command byte: 0 S C2 C1 C0 A2 A1 A0. s=1: multiple block write,  s=0: one block write.
00019 
00020 Commands C2 C1 C0:
00021 000: Write to input register.
00022 001: Update DAC register.
00023 010: Write to input register n, update all (software LDAC).
00024 011: Write and update DAC channel n.
00025 100: Power up / power down.
00026 101: Reset.
00027 110: LDAC register setup.
00028 111: Internal reference setup (on/off).
00029 
00030 DAC address command A2 A1 A0:
00031 000: DAC A.
00032 001: DAC B.
00033 111: Both DACs.
00034 
00035 
00036 Author: Lluis Nadal. August 2011.
00037 **************************************************************************************
00038 */
00039 
00040 I2C i2c(p9, p10); // SDA, SCL
00041 Serial pc(USBTX, USBRX);
00042 DigitalOut LDAC (p11);
00043 DigitalOut CLR (p12);
00044 
00045 DigitalOut L1(LED1);
00046 DigitalOut L2(LED2);
00047 DigitalOut L3(LED3);
00048 DigitalOut L4(LED4);
00049 
00050 const int addr_R = 0x19; // Address to read
00051 const int addr_W = 0x18; // Address to write
00052 
00053 const int CA1 = 0x18; // Command: DAC A one block write
00054 const int CB1 = 0x19; // Command: DAC B one block write
00055 const int CA2 = 0x58; // Command: DAC A multiple block write
00056 const int CB2 = 0x59; // Command: DAC B multiple block write
00057 const int CA3 = 0x00; // Command: DAC A using LDAC to update.
00058 const int CB3 = 0x01; // Command: DAC B using LDAC to update.
00059 
00060 
00061 void write_A(float v) { // 0 < v < 3.3 V
00062     char H;      // High byte
00063     char L;      // Low byte
00064     int n;
00065     n = (int)(v*4096/3.3);
00066     pc.printf(" n= %d\r\n", n);
00067     L = n<<4 & 0xF0;
00068     H = n>>4;
00069     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00070     pc.printf("\r\n");
00071     i2c.start();
00072     i2c.write(addr_W); // Write address to write
00073     i2c.write(CA1);    // Write command
00074     i2c.write(H);      // Write high byte
00075     i2c.write(L);      // Write low byte
00076     i2c.stop();
00077 }
00078 
00079 
00080 void write_B(float v) { // 0 < v < 3.3 V
00081     char H;      // High byte
00082     char L;      // Low byte
00083     int n;
00084     n = (int)(v*4096/3.3);
00085     pc.printf(" n= %d\r\n", n);
00086     L = n<<4 & 0xF0;
00087     H = n>>4;
00088     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00089     pc.printf("\r\n");
00090     i2c.start();
00091     i2c.write(addr_W); // Write address to write
00092     i2c.write(CB1);    // Write command
00093     i2c.write(H);      // Write high byte
00094     i2c.write(L);      // Write low byte
00095     i2c.stop();
00096 }
00097 
00098 void write_A_LDAC(float v) { // 0 < v < 3.3 V
00099     char H;      // High byte
00100     char L;      // Low byte
00101     int n;
00102     n = (int)(v*4096/3.3);
00103     pc.printf(" n= %d\r\n", n);
00104     L = n<<4 & 0xF0;
00105     H = n>>4;
00106     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00107     pc.printf("\r\n");
00108     i2c.start();
00109     i2c.write(addr_W); // Write address to write
00110     i2c.write(CA3);    // Write command
00111     i2c.write(H);      // Write high byte
00112     i2c.write(L);      // Write low byte
00113     i2c.stop();
00114 }
00115 
00116 void write_B_LDAC(float v) { // 0 < v < 3.3 V
00117     char H;      // High byte
00118     char L;      // Low byte
00119     int n;
00120     n = (int)(v*4096/3.3);
00121     pc.printf(" n= %d\r\n", n);
00122     L = n<<4 & 0xF0;
00123     H = n>>4;
00124     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00125     pc.printf("\r\n");
00126     i2c.start();
00127     i2c.write(addr_W); // Write address to write
00128     i2c.write(CB3);    // Write command
00129     i2c.write(H);      // Write high byte
00130     i2c.write(L);      // Write low byte
00131     i2c.stop();
00132 }
00133 
00134 
00135 int main() {
00136     L1 = 0;
00137     i2c.frequency(100000);
00138 
00139     // Pulsing this pin low allows any or all DAC registers to be updated if they have new data.
00140     LDAC = 0;
00141 
00142     CLR = 0; // Outputs the two DACs to 0V.
00143     wait(5);
00144     CLR = 1;
00145     L1 = 1;
00146 
00147     // DAC A one block write
00148     write_A(2.537);
00149     wait(5);
00150     
00151     // DAC A one block write
00152     write_A(1.777);
00153     L2 = 1;
00154     wait(5);
00155 
00156     // DAC B one block write
00157     write_B(0.666);
00158     wait(5);
00159     
00160     // DAC B one block write
00161     write_B(3.000);
00162     wait(5);
00163 
00164 
00165     // Using LDAC pin
00166     LDAC = 1;
00167     write_A_LDAC(1.000); // DAC register A is not updated
00168     write_B_LDAC(1.500); // DAC register B is not updated
00169     L3 = 1;
00170     wait(5);
00171 
00172     LDAC = 0; // DAC registers A and B are updated simultaneously
00173     L4 = 1;
00174 
00175 }