Lluis Nadal
/
MCP4725_Test
Test for MCP4725. 12-bit DAC with I2C interface.
main.cpp@0:07ef132e042b, 2011-08-11 (annotated)
- Committer:
- lnadal
- Date:
- Thu Aug 11 07:03:33 2011 +0000
- Revision:
- 0:07ef132e042b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lnadal | 0:07ef132e042b | 1 | #include "mbed.h" |
lnadal | 0:07ef132e042b | 2 | |
lnadal | 0:07ef132e042b | 3 | /* |
lnadal | 0:07ef132e042b | 4 | *************************************************************************************** |
lnadal | 0:07ef132e042b | 5 | MCP4725_Test. 12-bit DAC with i2C interface. |
lnadal | 0:07ef132e042b | 6 | pin 3(Vdd)= 3.3V. pin 6(A0) = 0. pin 4(SDA) and pin 5(SCL): 4,7k pull-up resistors. |
lnadal | 0:07ef132e042b | 7 | Attach multimeter to pin1(Vout) and ground. |
lnadal | 0:07ef132e042b | 8 | Author: Lluis Nadal. August 2011. |
lnadal | 0:07ef132e042b | 9 | *************************************************************************************** |
lnadal | 0:07ef132e042b | 10 | */ |
lnadal | 0:07ef132e042b | 11 | |
lnadal | 0:07ef132e042b | 12 | I2C i2c(p9, p10); // SDA, SCL |
lnadal | 0:07ef132e042b | 13 | Serial pc(USBTX, USBRX); |
lnadal | 0:07ef132e042b | 14 | |
lnadal | 0:07ef132e042b | 15 | const int addr_R = 0xC1; // Address to read |
lnadal | 0:07ef132e042b | 16 | const int addr_W = 0xC0; // Address to write |
lnadal | 0:07ef132e042b | 17 | |
lnadal | 0:07ef132e042b | 18 | void write(float v) { // 0 < v < 3.3 V |
lnadal | 0:07ef132e042b | 19 | char H; // High byte |
lnadal | 0:07ef132e042b | 20 | char L; // Low byte |
lnadal | 0:07ef132e042b | 21 | int n; |
lnadal | 0:07ef132e042b | 22 | n = (int)(v*4096/3.3); |
lnadal | 0:07ef132e042b | 23 | pc.printf(" n= %d\r\n", n); |
lnadal | 0:07ef132e042b | 24 | H = n>>4; |
lnadal | 0:07ef132e042b | 25 | L = (n<<4 & 0xF0); |
lnadal | 0:07ef132e042b | 26 | pc.printf(" (H, L)=(%d, %d)\r\n", H, L); |
lnadal | 0:07ef132e042b | 27 | pc.printf("\r\n"); |
lnadal | 0:07ef132e042b | 28 | i2c.start(); |
lnadal | 0:07ef132e042b | 29 | i2c.write(addr_W); // Write address to write |
lnadal | 0:07ef132e042b | 30 | i2c.write(0x40); // Write command |
lnadal | 0:07ef132e042b | 31 | i2c.write(H); // Write high byte |
lnadal | 0:07ef132e042b | 32 | i2c.write(L); // Write low byte |
lnadal | 0:07ef132e042b | 33 | i2c.stop(); |
lnadal | 0:07ef132e042b | 34 | } |
lnadal | 0:07ef132e042b | 35 | |
lnadal | 0:07ef132e042b | 36 | void write_fast(float v) { |
lnadal | 0:07ef132e042b | 37 | char H=0; |
lnadal | 0:07ef132e042b | 38 | char L=0; |
lnadal | 0:07ef132e042b | 39 | int n; |
lnadal | 0:07ef132e042b | 40 | n = (int)(v*4096/3.3); |
lnadal | 0:07ef132e042b | 41 | pc.printf(" n= %d\r\n", n); |
lnadal | 0:07ef132e042b | 42 | L = n & 0xFF; |
lnadal | 0:07ef132e042b | 43 | H = (n>>8) & 0x0F; |
lnadal | 0:07ef132e042b | 44 | pc.printf(" (H, L)=(%d, %d)\r\n", H, L); |
lnadal | 0:07ef132e042b | 45 | pc.printf("\r\n"); |
lnadal | 0:07ef132e042b | 46 | i2c.start(); |
lnadal | 0:07ef132e042b | 47 | i2c.write(addr_W); |
lnadal | 0:07ef132e042b | 48 | i2c.write(H); |
lnadal | 0:07ef132e042b | 49 | i2c.write(L); |
lnadal | 0:07ef132e042b | 50 | i2c.stop(); |
lnadal | 0:07ef132e042b | 51 | } |
lnadal | 0:07ef132e042b | 52 | |
lnadal | 0:07ef132e042b | 53 | void write_EEPROM(float v) { // 0 < v < 3.3 V |
lnadal | 0:07ef132e042b | 54 | char H; // High byte |
lnadal | 0:07ef132e042b | 55 | char L; // Low byte |
lnadal | 0:07ef132e042b | 56 | int n; |
lnadal | 0:07ef132e042b | 57 | n = (int)(v*4096/3.3); |
lnadal | 0:07ef132e042b | 58 | pc.printf(" n= %d\r\n", n); |
lnadal | 0:07ef132e042b | 59 | H = n>>4; |
lnadal | 0:07ef132e042b | 60 | L = (n<<4 & 0xF0); |
lnadal | 0:07ef132e042b | 61 | pc.printf(" (H, L)=(%d, %d)\r\n", H, L); |
lnadal | 0:07ef132e042b | 62 | pc.printf("\r\n"); |
lnadal | 0:07ef132e042b | 63 | i2c.start(); |
lnadal | 0:07ef132e042b | 64 | i2c.write(addr_W); // Write address to write |
lnadal | 0:07ef132e042b | 65 | i2c.write(0x60); // Write command |
lnadal | 0:07ef132e042b | 66 | i2c.write(H); // Write high byte |
lnadal | 0:07ef132e042b | 67 | i2c.write(L); // Write low byte |
lnadal | 0:07ef132e042b | 68 | i2c.stop(); |
lnadal | 0:07ef132e042b | 69 | } |
lnadal | 0:07ef132e042b | 70 | |
lnadal | 0:07ef132e042b | 71 | |
lnadal | 0:07ef132e042b | 72 | int main() { |
lnadal | 0:07ef132e042b | 73 | |
lnadal | 0:07ef132e042b | 74 | i2c.frequency(100000); |
lnadal | 0:07ef132e042b | 75 | |
lnadal | 0:07ef132e042b | 76 | // On power-up the voltage is the data stored in EEPROM. |
lnadal | 0:07ef132e042b | 77 | pc.printf(" On power-up the voltage is the data stored in EEPROM.\r\n"); |
lnadal | 0:07ef132e042b | 78 | pc.printf("\r\n"); |
lnadal | 0:07ef132e042b | 79 | wait(5); |
lnadal | 0:07ef132e042b | 80 | |
lnadal | 0:07ef132e042b | 81 | // Write to DAC register in normal mode |
lnadal | 0:07ef132e042b | 82 | pc.printf(" Normal mode\r\n"); |
lnadal | 0:07ef132e042b | 83 | write(1.3585); |
lnadal | 0:07ef132e042b | 84 | wait(5); |
lnadal | 0:07ef132e042b | 85 | write(2.6758); |
lnadal | 0:07ef132e042b | 86 | wait(5); |
lnadal | 0:07ef132e042b | 87 | write(1.7554); |
lnadal | 0:07ef132e042b | 88 | wait(5); |
lnadal | 0:07ef132e042b | 89 | |
lnadal | 0:07ef132e042b | 90 | // Write to DAC register in fast mode |
lnadal | 0:07ef132e042b | 91 | pc.printf(" Fast mode\r\n"); |
lnadal | 0:07ef132e042b | 92 | write_fast(1.3585); |
lnadal | 0:07ef132e042b | 93 | wait(5); |
lnadal | 0:07ef132e042b | 94 | write_fast(2.6758); |
lnadal | 0:07ef132e042b | 95 | wait(5); |
lnadal | 0:07ef132e042b | 96 | write_fast(1.7554); |
lnadal | 0:07ef132e042b | 97 | |
lnadal | 0:07ef132e042b | 98 | // Write to EEPROM |
lnadal | 0:07ef132e042b | 99 | pc.printf(" Write to EEPROM\r\n"); |
lnadal | 0:07ef132e042b | 100 | write_EEPROM(0.5); // On power_up the voltage will be 0.5V. |
lnadal | 0:07ef132e042b | 101 | |
lnadal | 0:07ef132e042b | 102 | |
lnadal | 0:07ef132e042b | 103 | } |