Lluis Nadal / Mbed 2 deprecated MCP4725_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 MCP4725_Test. 12-bit DAC with i2C interface.
00006 pin 3(Vdd)= 3.3V. pin 6(A0) = 0. pin 4(SDA) and pin 5(SCL): 4,7k pull-up resistors.
00007 Attach multimeter to pin1(Vout) and ground.
00008 Author: Lluis Nadal. August 2011.
00009 ***************************************************************************************
00010 */
00011 
00012 I2C i2c(p9, p10); // SDA, SCL
00013 Serial pc(USBTX, USBRX);
00014 
00015 const int addr_R = 0xC1; // Address to read
00016 const int addr_W = 0xC0; // Address to write
00017 
00018 void write(float v) { // 0 < v < 3.3 V
00019     char H;      // High byte
00020     char L;      // Low byte
00021     int n;
00022     n = (int)(v*4096/3.3);
00023     pc.printf(" n= %d\r\n", n);
00024     H = n>>4;
00025     L = (n<<4 & 0xF0);
00026     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00027     pc.printf("\r\n");
00028     i2c.start();
00029     i2c.write(addr_W); // Write address to write
00030     i2c.write(0x40);   // Write command
00031     i2c.write(H);      // Write high byte
00032     i2c.write(L);      // Write low byte
00033     i2c.stop();
00034 }
00035 
00036 void write_fast(float v) {
00037     char H=0;
00038     char L=0;
00039     int n;
00040     n = (int)(v*4096/3.3);
00041     pc.printf(" n= %d\r\n", n);
00042     L = n & 0xFF;
00043     H = (n>>8) & 0x0F;
00044     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00045     pc.printf("\r\n");
00046     i2c.start();
00047     i2c.write(addr_W);
00048     i2c.write(H);
00049     i2c.write(L);
00050     i2c.stop();
00051 }
00052 
00053 void write_EEPROM(float v) { // 0 < v < 3.3 V
00054     char H;      // High byte
00055     char L;      // Low byte
00056     int n;
00057     n = (int)(v*4096/3.3);
00058     pc.printf(" n= %d\r\n", n);
00059     H = n>>4;
00060     L = (n<<4 & 0xF0);
00061     pc.printf(" (H, L)=(%d, %d)\r\n", H, L);
00062     pc.printf("\r\n");
00063     i2c.start();
00064     i2c.write(addr_W); // Write address to write
00065     i2c.write(0x60);   // Write command
00066     i2c.write(H);      // Write high byte
00067     i2c.write(L);      // Write low byte
00068     i2c.stop();
00069 }
00070 
00071 
00072 int main() {
00073 
00074     i2c.frequency(100000);
00075 
00076     // On power-up the voltage is the data stored in EEPROM.
00077     pc.printf(" On power-up the voltage is the data stored in EEPROM.\r\n");
00078     pc.printf("\r\n");
00079     wait(5);
00080 
00081     // Write to DAC register in normal mode
00082     pc.printf(" Normal mode\r\n");
00083     write(1.3585);
00084     wait(5);
00085     write(2.6758);
00086     wait(5);
00087     write(1.7554);
00088     wait(5);
00089 
00090     // Write to DAC register in fast mode
00091     pc.printf(" Fast mode\r\n");
00092     write_fast(1.3585);
00093     wait(5);
00094     write_fast(2.6758);
00095     wait(5);
00096     write_fast(1.7554);
00097 
00098     // Write to EEPROM
00099     pc.printf(" Write to EEPROM\r\n");
00100     write_EEPROM(0.5); // On power_up the voltage will be 0.5V.
00101 
00102 
00103 }