Demoapplication for MCP3428

Dependencies:   mbed

Committer:
aAXEe
Date:
Wed Nov 05 07:22:42 2014 +0000
Revision:
0:ac3e88ab7ac6
basic handling working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aAXEe 0:ac3e88ab7ac6 1 #include "mbed.h"
aAXEe 0:ac3e88ab7ac6 2
aAXEe 0:ac3e88ab7ac6 3 #define ADC_ADDR (0xD0)
aAXEe 0:ac3e88ab7ac6 4
aAXEe 0:ac3e88ab7ac6 5 I2C i2c(I2C_SDA, I2C_SCL);
aAXEe 0:ac3e88ab7ac6 6
aAXEe 0:ac3e88ab7ac6 7
aAXEe 0:ac3e88ab7ac6 8 Serial pc(SERIAL_TX, SERIAL_RX);
aAXEe 0:ac3e88ab7ac6 9
aAXEe 0:ac3e88ab7ac6 10 bool mcp3428_writeConfig(uint8_t conf){
aAXEe 0:ac3e88ab7ac6 11 return i2c.write(ADC_ADDR, (char*) &conf, 1, 0) == 0;
aAXEe 0:ac3e88ab7ac6 12 }
aAXEe 0:ac3e88ab7ac6 13
aAXEe 0:ac3e88ab7ac6 14 bool mcp3428_read(uint16_t* data, uint8_t* conf){
aAXEe 0:ac3e88ab7ac6 15 char buf[3];
aAXEe 0:ac3e88ab7ac6 16 int ret = i2c.read(ADC_ADDR, buf, 3, 0);
aAXEe 0:ac3e88ab7ac6 17
aAXEe 0:ac3e88ab7ac6 18 if(ret != 0)
aAXEe 0:ac3e88ab7ac6 19 return false;
aAXEe 0:ac3e88ab7ac6 20
aAXEe 0:ac3e88ab7ac6 21 *data = buf[0] << 8 | buf[1];
aAXEe 0:ac3e88ab7ac6 22 *conf = buf[2];
aAXEe 0:ac3e88ab7ac6 23
aAXEe 0:ac3e88ab7ac6 24 return true;
aAXEe 0:ac3e88ab7ac6 25 }
aAXEe 0:ac3e88ab7ac6 26
aAXEe 0:ac3e88ab7ac6 27 int adc_test(void){
aAXEe 0:ac3e88ab7ac6 28 uint8_t conf = 0x90;
aAXEe 0:ac3e88ab7ac6 29 uint16_t data = 0;
aAXEe 0:ac3e88ab7ac6 30
aAXEe 0:ac3e88ab7ac6 31 if(!mcp3428_writeConfig(conf)) return -1;
aAXEe 0:ac3e88ab7ac6 32
aAXEe 0:ac3e88ab7ac6 33 int ret = mcp3428_read(&data, &conf);
aAXEe 0:ac3e88ab7ac6 34
aAXEe 0:ac3e88ab7ac6 35 if(!ret) return -2;
aAXEe 0:ac3e88ab7ac6 36
aAXEe 0:ac3e88ab7ac6 37 return data;
aAXEe 0:ac3e88ab7ac6 38
aAXEe 0:ac3e88ab7ac6 39 }
aAXEe 0:ac3e88ab7ac6 40
aAXEe 0:ac3e88ab7ac6 41 int main()
aAXEe 0:ac3e88ab7ac6 42 {
aAXEe 0:ac3e88ab7ac6 43
aAXEe 0:ac3e88ab7ac6 44 printf("Hello ADC MCP3428\n");
aAXEe 0:ac3e88ab7ac6 45 while (1) {
aAXEe 0:ac3e88ab7ac6 46 pc.printf("adc_test: %i\n", adc_test());
aAXEe 0:ac3e88ab7ac6 47 wait(1.0);
aAXEe 0:ac3e88ab7ac6 48 }
aAXEe 0:ac3e88ab7ac6 49
aAXEe 0:ac3e88ab7ac6 50 }
aAXEe 0:ac3e88ab7ac6 51