Jasper Smit
/
nucleo_i2c
:^)
main.cpp@0:8e0901b4cd75, 2018-05-15 (annotated)
- Committer:
- choppa
- Date:
- Tue May 15 11:59:47 2018 +0000
- Revision:
- 0:8e0901b4cd75
:^);
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
choppa | 0:8e0901b4cd75 | 1 | #include "mbed.h" |
choppa | 0:8e0901b4cd75 | 2 | #include <stdint.h> |
choppa | 0:8e0901b4cd75 | 3 | |
choppa | 0:8e0901b4cd75 | 4 | I2C i2c(PB_9, PB_8); |
choppa | 0:8e0901b4cd75 | 5 | Serial usb_serial(SERIAL_TX, SERIAL_RX); |
choppa | 0:8e0901b4cd75 | 6 | |
choppa | 0:8e0901b4cd75 | 7 | #define ADDR_FSR0 0x90 |
choppa | 0:8e0901b4cd75 | 8 | #define ADDR_FSR1 0x29 |
choppa | 0:8e0901b4cd75 | 9 | #define ADDR_PZT 0x96 |
choppa | 0:8e0901b4cd75 | 10 | |
choppa | 0:8e0901b4cd75 | 11 | #define AD1115_CONFIG 0x01 |
choppa | 0:8e0901b4cd75 | 12 | |
choppa | 0:8e0901b4cd75 | 13 | #define PZT0 0x10 |
choppa | 0:8e0901b4cd75 | 14 | #define PZT1 0x20 |
choppa | 0:8e0901b4cd75 | 15 | |
choppa | 0:8e0901b4cd75 | 16 | int main() { |
choppa | 0:8e0901b4cd75 | 17 | int16_t raw_data, config; |
choppa | 0:8e0901b4cd75 | 18 | double voltage0, voltage1; |
choppa | 0:8e0901b4cd75 | 19 | usb_serial.baud(230400); |
choppa | 0:8e0901b4cd75 | 20 | i2c.frequency(200000); |
choppa | 0:8e0901b4cd75 | 21 | char rxbuf[10]; |
choppa | 0:8e0901b4cd75 | 22 | char txbuf[10]; |
choppa | 0:8e0901b4cd75 | 23 | // set mux to pzt0 |
choppa | 0:8e0901b4cd75 | 24 | config = 0x84E3 | PZT0; |
choppa | 0:8e0901b4cd75 | 25 | txbuf[0] = AD1115_CONFIG; |
choppa | 0:8e0901b4cd75 | 26 | txbuf[1] = config >> 8; |
choppa | 0:8e0901b4cd75 | 27 | txbuf[2] = config & 0xFF; |
choppa | 0:8e0901b4cd75 | 28 | i2c.write(ADDR_PZT, txbuf, 3); |
choppa | 0:8e0901b4cd75 | 29 | txbuf[0] = 0; |
choppa | 0:8e0901b4cd75 | 30 | i2c.write(ADDR_PZT, txbuf, 1); |
choppa | 0:8e0901b4cd75 | 31 | while(1) { |
choppa | 0:8e0901b4cd75 | 32 | i2c.read(ADDR_PZT, rxbuf, 2); |
choppa | 0:8e0901b4cd75 | 33 | raw_data = (rxbuf[0] << 8) | rxbuf[1]; |
choppa | 0:8e0901b4cd75 | 34 | voltage0 = 5.0/65535.0 * (double)raw_data; |
choppa | 0:8e0901b4cd75 | 35 | usb_serial.printf("%f\r\n", voltage0); |
choppa | 0:8e0901b4cd75 | 36 | wait_ms(1); |
choppa | 0:8e0901b4cd75 | 37 | } |
choppa | 0:8e0901b4cd75 | 38 | } |