Alberto Petrucci
/
Nucleo_I2C
NUCLEO I2C MASTER
Fork of I2C_HelloWorld_Mbed by
main.cpp@1:677b47408246, 2017-03-01 (annotated)
- Committer:
- capriele
- Date:
- Wed Mar 01 08:06:44 2017 +0000
- Revision:
- 1:677b47408246
- Parent:
- 0:f76c26307f9a
NUCLEO I2C MASTER
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:f76c26307f9a | 1 | #include "mbed.h" |
capriele | 1:677b47408246 | 2 | #include <stdio.h> |
capriele | 1:677b47408246 | 3 | #include <inttypes.h> |
capriele | 1:677b47408246 | 4 | |
capriele | 1:677b47408246 | 5 | Serial pc(USBTX, USBRX); // tx, rx |
mbed_official | 0:f76c26307f9a | 6 | |
capriele | 1:677b47408246 | 7 | const int addr = (0x07); |
capriele | 1:677b47408246 | 8 | I2C i2c(D14, D15); //sda, scl |
mbed_official | 0:f76c26307f9a | 9 | |
capriele | 1:677b47408246 | 10 | //LEDS |
capriele | 1:677b47408246 | 11 | DigitalOut led0(D2); |
capriele | 1:677b47408246 | 12 | DigitalOut led1(D3); |
capriele | 1:677b47408246 | 13 | DigitalOut led2(D4); |
capriele | 1:677b47408246 | 14 | DigitalOut led3(D5); |
mbed_official | 0:f76c26307f9a | 15 | |
mbed_official | 0:f76c26307f9a | 16 | int main() { |
capriele | 1:677b47408246 | 17 | i2c.frequency(100000);//100khz |
capriele | 1:677b47408246 | 18 | |
capriele | 1:677b47408246 | 19 | char num0[1]; |
capriele | 1:677b47408246 | 20 | num0[0] = 0x00; |
capriele | 1:677b47408246 | 21 | |
capriele | 1:677b47408246 | 22 | char cmd[1]; |
capriele | 1:677b47408246 | 23 | cmd[0] = 0x00; |
mbed_official | 0:f76c26307f9a | 24 | while (1) { |
capriele | 1:677b47408246 | 25 | //i2c.write(addr, cmd, 1, false); |
capriele | 1:677b47408246 | 26 | //i2c.read(addr, num0, 2, false); |
capriele | 1:677b47408246 | 27 | |
capriele | 1:677b47408246 | 28 | //cmd[0] = 0x01; |
capriele | 1:677b47408246 | 29 | i2c.write(addr, cmd, 1, false); |
capriele | 1:677b47408246 | 30 | i2c.read(addr, num0, 1, false); |
capriele | 1:677b47408246 | 31 | |
capriele | 1:677b47408246 | 32 | //Accendo i led |
capriele | 1:677b47408246 | 33 | led0 = num0[0] & 0x01; |
capriele | 1:677b47408246 | 34 | led1 = num0[0] & 0x02; |
capriele | 1:677b47408246 | 35 | led2 = num0[0] & 0x03; |
capriele | 1:677b47408246 | 36 | led3 = num0[0] & 0x04; |
capriele | 1:677b47408246 | 37 | printf("RECEIVED = %02X \n", num0[0]); |
mbed_official | 0:f76c26307f9a | 38 | wait(0.5); |
mbed_official | 0:f76c26307f9a | 39 | } |
mbed_official | 0:f76c26307f9a | 40 | } |