program for I2C master device

Dependencies:   mbed

Fork of I2C_HelloWorld_Mbed by mbed official

Committer:
marcus255
Date:
Tue Dec 15 22:44:03 2015 +0000
Revision:
7:b07706b16279
Parent:
6:6ee93a0f8d24
Child:
8:3e6945d5d9a1
memore read/write works fine

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 6:6ee93a0f8d24 10 write_data();
marcus255 5:8dc6d7d81250 11 wait_ms(20);
marcus255 7:b07706b16279 12 char regaddr = 0x24;
marcus255 7:b07706b16279 13 read_data(&regaddr); // works OK (pure read, without pointer setting)
marcus255 7:b07706b16279 14 wait_ms(20);
marcus255 5:8dc6d7d81250 15 }
marcus255 5:8dc6d7d81250 16
marcus255 5:8dc6d7d81250 17 void write_data(){
marcus255 5:8dc6d7d81250 18 //writing to slave
marcus255 7:b07706b16279 19 int write_ack = i2c.write(SLAVE_ADDR, data, DATA_SIZE);
marcus255 4:1ec553356dcc 20 if (!write_ack) {
marcus255 7:b07706b16279 21 //i2c.stop();
marcus255 7:b07706b16279 22 pc.printf("<Writing> OK\n\r");
marcus255 4:1ec553356dcc 23 }
marcus255 4:1ec553356dcc 24 else
marcus255 7:b07706b16279 25 pc.printf("<Writing> Error\n\r");
marcus255 5:8dc6d7d81250 26 //wait_ms(20);
marcus255 5:8dc6d7d81250 27 }
marcus255 5:8dc6d7d81250 28
marcus255 7:b07706b16279 29 void read_data(char* cell_addr){
marcus255 7:b07706b16279 30 /*int write_ack = i2c.write(SLAVE_ADDR, cell_addr, 1);
marcus255 7:b07706b16279 31 if (!write_ack) {
marcus255 7:b07706b16279 32 //i2c.stop();
marcus255 7:b07706b16279 33 pc.printf("<Addressing> OK\n\r");
marcus255 7:b07706b16279 34 }
marcus255 7:b07706b16279 35 else {
marcus255 7:b07706b16279 36 pc.printf("<Addressing> Error\n\r");
marcus255 7:b07706b16279 37 return;
marcus255 7:b07706b16279 38 }
marcus255 7:b07706b16279 39 //wait_ms(5);
marcus255 7:b07706b16279 40 */
marcus255 4:1ec553356dcc 41 //reading from slave
marcus255 7:b07706b16279 42 char fromSlave[DATA_SIZE + 1];
marcus255 7:b07706b16279 43 int read_ack = i2c.read(SLAVE_ADDR, fromSlave, DATA_SIZE);
marcus255 5:8dc6d7d81250 44 if (!read_ack) {
marcus255 7:b07706b16279 45 //i2c.stop();
marcus255 7:b07706b16279 46 pc.printf("<Reading> OK\n\r");
marcus255 7:b07706b16279 47 fromSlave[DATA_SIZE] = '\0';
marcus255 5:8dc6d7d81250 48 pc.printf("Data from Slave: %s\n\n\r", fromSlave);
marcus255 7:b07706b16279 49
marcus255 5:8dc6d7d81250 50 }
marcus255 4:1ec553356dcc 51 else
marcus255 7:b07706b16279 52 pc.printf("<Reading> Error\n\r");
marcus255 5:8dc6d7d81250 53 }
marcus255 5:8dc6d7d81250 54
marcus255 4:1ec553356dcc 55 /*
marcus255 4:1ec553356dcc 56 void i2cReadWrite(void){
marcus255 4:1ec553356dcc 57 for(int a = 0; a < MSG_SIZE; a++) fromSlave[a] = 0;
marcus255 4:1ec553356dcc 58
marcus255 4:1ec553356dcc 59 int ack = i2c.write(SLAVE_ADDR, data, strlen(data));
marcus255 4:1ec553356dcc 60 if (!ack)
marcus255 4:1ec553356dcc 61 pc.printf("data send to slave\n\r");
marcus255 4:1ec553356dcc 62 else
marcus255 4:1ec553356dcc 63 pc.printf("NACK!\n\r");
mbed_official 0:f76c26307f9a 64
marcus255 4:1ec553356dcc 65 wait(0.1);
marcus255 4:1ec553356dcc 66
marcus255 4:1ec553356dcc 67 int read_ack = i2c.read(SLAVE_ADDR, fromSlave, MSG_SIZE - 1);
marcus255 4:1ec553356dcc 68 if (!read_ack)
marcus255 4:1ec553356dcc 69 pc.printf("data received from slave\n\r");
marcus255 4:1ec553356dcc 70 else
marcus255 4:1ec553356dcc 71 pc.printf("data not received\n\r");
marcus255 4:1ec553356dcc 72
marcus255 4:1ec553356dcc 73 pc.printf("Data from Slave: %s\n\r", fromSlave);
marcus255 4:1ec553356dcc 74
marcus255 4:1ec553356dcc 75 for(int y = 0; y < 32; y++) fromSlave[y]='x';
marcus255 4:1ec553356dcc 76 }*/
marcus255 4:1ec553356dcc 77
marcus255 4:1ec553356dcc 78 void init(void){
marcus255 4:1ec553356dcc 79 i2c.frequency(100000);
marcus255 5:8dc6d7d81250 80 i2c_ticker.attach(&write_and_read, 2.0f);
marcus255 4:1ec553356dcc 81 pc.baud(921600);
marcus255 7:b07706b16279 82 //pc.printf("Master says: Hello World!\n\r");
marcus255 4:1ec553356dcc 83 for (uint8_t i = 0; i < DATA_SIZE; i++)
marcus255 5:8dc6d7d81250 84 data[i] = i+65;
marcus255 7:b07706b16279 85 //data[0]=0;
mbed_official 0:f76c26307f9a 86 }