NUCLEO I2C MASTER

Dependencies:   mbed

Fork of I2C_HelloWorld_Mbed by mbed official

main.cpp

Committer:
capriele
Date:
2017-03-01
Revision:
1:677b47408246
Parent:
0:f76c26307f9a

File content as of revision 1:677b47408246:

#include "mbed.h"
 #include <stdio.h>
#include <inttypes.h>

Serial pc(USBTX, USBRX); // tx, rx

const int addr = (0x07);
I2C i2c(D14, D15); //sda, scl

//LEDS
DigitalOut led0(D2);
DigitalOut led1(D3);
DigitalOut led2(D4);
DigitalOut led3(D5);

int main() {
    i2c.frequency(100000);//100khz
    
    char num0[1];
    num0[0] = 0x00;
    
    char cmd[1];
    cmd[0] = 0x00;
    while (1) {
        //i2c.write(addr, cmd, 1, false); 
        //i2c.read(addr, num0, 2, false); 
        
        //cmd[0] = 0x01;
        i2c.write(addr, cmd, 1, false); 
        i2c.read(addr, num0, 1, false); 
        
        //Accendo i led
        led0 = num0[0] & 0x01;
        led1 = num0[0] & 0x02;
        led2 = num0[0] & 0x03;
        led3 = num0[0] & 0x04;
        printf("RECEIVED = %02X \n", num0[0]);
        wait(0.5);
    }
}