program for I2C master device

Dependencies:   mbed

Fork of I2C_HelloWorld_Mbed by mbed official

Committer:
marcus255
Date:
Sun Dec 06 21:24:01 2015 +0000
Revision:
5:8dc6d7d81250
Parent:
4:1ec553356dcc
Child:
6:6ee93a0f8d24
still not complete write-read sequence

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcus255 4:1ec553356dcc 1 // I2C Master
marcus255 4:1ec553356dcc 2 #include "main.h"
mbed_official 0:f76c26307f9a 3
marcus255 4:1ec553356dcc 4 int main() {
marcus255 4:1ec553356dcc 5 init();
marcus255 4:1ec553356dcc 6 while (1) {}
marcus255 4:1ec553356dcc 7 }
mbed_official 0:f76c26307f9a 8
marcus255 5:8dc6d7d81250 9 void write_and_read(){
marcus255 5:8dc6d7d81250 10 //write_data();
marcus255 5:8dc6d7d81250 11 wait_ms(20);
marcus255 5:8dc6d7d81250 12 read_data();
marcus255 5:8dc6d7d81250 13 }
marcus255 5:8dc6d7d81250 14
marcus255 5:8dc6d7d81250 15 void write_data(){
marcus255 5:8dc6d7d81250 16 //writing to slave
marcus255 5:8dc6d7d81250 17 int write_ack = i2c.write(SLAVE_ADDR, data, DATA_SIZE-1);
marcus255 4:1ec553356dcc 18 if (!write_ack) {
marcus255 4:1ec553356dcc 19 i2c.stop();
marcus255 4:1ec553356dcc 20 pc.printf("<Writing> OK\n\r");
marcus255 4:1ec553356dcc 21 }
marcus255 4:1ec553356dcc 22 else
marcus255 4:1ec553356dcc 23 pc.printf("<Writing> Error\n\r");
marcus255 5:8dc6d7d81250 24 //wait_ms(20);
marcus255 5:8dc6d7d81250 25 }
marcus255 5:8dc6d7d81250 26
marcus255 5:8dc6d7d81250 27 void read_data(){
marcus255 4:1ec553356dcc 28 //reading from slave
marcus255 4:1ec553356dcc 29 char fromSlave[DATA_SIZE];
marcus255 5:8dc6d7d81250 30 int read_ack = i2c.read(SLAVE_ADDR, fromSlave, DATA_SIZE - 1);
marcus255 5:8dc6d7d81250 31 if (!read_ack) {
marcus255 4:1ec553356dcc 32 pc.printf("<Reading> OK\n\r");
marcus255 5:8dc6d7d81250 33 fromSlave[DATA_SIZE - 1] = '\0';
marcus255 5:8dc6d7d81250 34 pc.printf("Data from Slave: %s\n\n\r", fromSlave);
marcus255 5:8dc6d7d81250 35 }
marcus255 4:1ec553356dcc 36 else
marcus255 5:8dc6d7d81250 37 pc.printf("<Reading> Error\n\r");
marcus255 5:8dc6d7d81250 38 }
marcus255 5:8dc6d7d81250 39
marcus255 4:1ec553356dcc 40 /*
marcus255 4:1ec553356dcc 41 void i2cReadWrite(void){
marcus255 4:1ec553356dcc 42 for(int a = 0; a < MSG_SIZE; a++) fromSlave[a] = 0;
marcus255 4:1ec553356dcc 43
marcus255 4:1ec553356dcc 44 int ack = i2c.write(SLAVE_ADDR, data, strlen(data));
marcus255 4:1ec553356dcc 45 if (!ack)
marcus255 4:1ec553356dcc 46 pc.printf("data send to slave\n\r");
marcus255 4:1ec553356dcc 47 else
marcus255 4:1ec553356dcc 48 pc.printf("NACK!\n\r");
mbed_official 0:f76c26307f9a 49
marcus255 4:1ec553356dcc 50 wait(0.1);
marcus255 4:1ec553356dcc 51
marcus255 4:1ec553356dcc 52 int read_ack = i2c.read(SLAVE_ADDR, fromSlave, MSG_SIZE - 1);
marcus255 4:1ec553356dcc 53 if (!read_ack)
marcus255 4:1ec553356dcc 54 pc.printf("data received from slave\n\r");
marcus255 4:1ec553356dcc 55 else
marcus255 4:1ec553356dcc 56 pc.printf("data not received\n\r");
marcus255 4:1ec553356dcc 57
marcus255 4:1ec553356dcc 58 pc.printf("Data from Slave: %s\n\r", fromSlave);
marcus255 4:1ec553356dcc 59
marcus255 4:1ec553356dcc 60 for(int y = 0; y < 32; y++) fromSlave[y]='x';
marcus255 4:1ec553356dcc 61 }*/
marcus255 4:1ec553356dcc 62
marcus255 4:1ec553356dcc 63 void init(void){
marcus255 4:1ec553356dcc 64 i2c.frequency(100000);
marcus255 5:8dc6d7d81250 65 i2c_ticker.attach(&write_and_read, 2.0f);
marcus255 4:1ec553356dcc 66 pc.baud(921600);
marcus255 4:1ec553356dcc 67 pc.printf("Master says: Hello World!\n\r");
marcus255 4:1ec553356dcc 68 for (uint8_t i = 0; i < DATA_SIZE; i++)
marcus255 5:8dc6d7d81250 69 data[i] = i+65;
marcus255 5:8dc6d7d81250 70 data[0]=0;
mbed_official 0:f76c26307f9a 71 }