Demo for MCP4728

Dependencies:   mbed

Committer:
aAXEe
Date:
Wed Nov 05 08:41:34 2014 +0000
Revision:
0:5b2cf8f997d8
basic handling working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aAXEe 0:5b2cf8f997d8 1 #include "mbed.h"
aAXEe 0:5b2cf8f997d8 2
aAXEe 0:5b2cf8f997d8 3 #define DAC_ADDR (0xc0)
aAXEe 0:5b2cf8f997d8 4
aAXEe 0:5b2cf8f997d8 5 I2C i2c(I2C_SDA, I2C_SCL);
aAXEe 0:5b2cf8f997d8 6
aAXEe 0:5b2cf8f997d8 7
aAXEe 0:5b2cf8f997d8 8 Serial pc(SERIAL_TX, SERIAL_RX);
aAXEe 0:5b2cf8f997d8 9
aAXEe 0:5b2cf8f997d8 10 bool mcp4728_setChannel(uint8_t channel, bool UseInternalVRef, uint8_t powerDownMode, bool use2xGain, uint16_t value){
aAXEe 0:5b2cf8f997d8 11 char buf[3];
aAXEe 0:5b2cf8f997d8 12 buf[0] = 0x40 | ((channel & 0x3) << 1);
aAXEe 0:5b2cf8f997d8 13
aAXEe 0:5b2cf8f997d8 14 buf[1] = ((uint8_t)UseInternalVRef << 7) | ((powerDownMode & 0x3) << 5) | ((uint8_t)use2xGain << 4);
aAXEe 0:5b2cf8f997d8 15 buf[1] |= (value & 0x0f00)>>8;
aAXEe 0:5b2cf8f997d8 16
aAXEe 0:5b2cf8f997d8 17 buf[2] = value & 0xff;
aAXEe 0:5b2cf8f997d8 18
aAXEe 0:5b2cf8f997d8 19 return i2c.write(DAC_ADDR, buf, 3, 0) == 0;
aAXEe 0:5b2cf8f997d8 20 }
aAXEe 0:5b2cf8f997d8 21
aAXEe 0:5b2cf8f997d8 22 int dac_test(void){
aAXEe 0:5b2cf8f997d8 23 static uint16_t value = 0;
aAXEe 0:5b2cf8f997d8 24 int ret;
aAXEe 0:5b2cf8f997d8 25
aAXEe 0:5b2cf8f997d8 26 ret = mcp4728_setChannel(0, 0, 0, 0, value);
aAXEe 0:5b2cf8f997d8 27 value++;
aAXEe 0:5b2cf8f997d8 28 if(!ret) return -2;
aAXEe 0:5b2cf8f997d8 29
aAXEe 0:5b2cf8f997d8 30 return 1;
aAXEe 0:5b2cf8f997d8 31 }
aAXEe 0:5b2cf8f997d8 32
aAXEe 0:5b2cf8f997d8 33 int main()
aAXEe 0:5b2cf8f997d8 34 {
aAXEe 0:5b2cf8f997d8 35
aAXEe 0:5b2cf8f997d8 36 printf("Hello DAC MCP4728\n");
aAXEe 0:5b2cf8f997d8 37 while (1) {
aAXEe 0:5b2cf8f997d8 38 pc.printf("dac_test: %i\n", dac_test());
aAXEe 0:5b2cf8f997d8 39 wait(0.1);
aAXEe 0:5b2cf8f997d8 40 }
aAXEe 0:5b2cf8f997d8 41
aAXEe 0:5b2cf8f997d8 42 }
aAXEe 0:5b2cf8f997d8 43