master writing to slave

Dependencies:   mbed

Committer:
viswachaitanya
Date:
Sat Dec 06 06:19:00 2014 +0000
Revision:
0:47311d5ff74b
m_w_s1_1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
viswachaitanya 0:47311d5ff74b 1 #include "mbed.h"
viswachaitanya 0:47311d5ff74b 2
viswachaitanya 0:47311d5ff74b 3 const int addr = 0x20; //slave address
viswachaitanya 0:47311d5ff74b 4 I2C master (D14,D15); //configure pins p27,p28 as I2C master
viswachaitanya 0:47311d5ff74b 5 Serial pc (USBTX,USBRX);
viswachaitanya 0:47311d5ff74b 6
viswachaitanya 0:47311d5ff74b 7 void main()
viswachaitanya 0:47311d5ff74b 8 {
viswachaitanya 0:47311d5ff74b 9 printf("press backspace for master to start \n\r");
viswachaitanya 0:47311d5ff74b 10 while(1){
viswachaitanya 0:47311d5ff74b 11 if(pc.getc()==8){
viswachaitanya 0:47311d5ff74b 12 bool ack0 =true;
viswachaitanya 0:47311d5ff74b 13 char* data = new char;
viswachaitanya 0:47311d5ff74b 14 *data = 'b';
viswachaitanya 0:47311d5ff74b 15 bool loopvariable0 = true;
viswachaitanya 0:47311d5ff74b 16 while(loopvariable0){
viswachaitanya 0:47311d5ff74b 17 printf("2 master clk freq setup and addressing slave\n\r");
viswachaitanya 0:47311d5ff74b 18 master.frequency(100000); //set clock frequency
viswachaitanya 0:47311d5ff74b 19 master.start(); //initiating the data transfer
viswachaitanya 0:47311d5ff74b 20 ack0 = (bool) master.write(addr|0x00,data,1);
viswachaitanya 0:47311d5ff74b 21 if(!ack0){
viswachaitanya 0:47311d5ff74b 22 printf("3 master has written %c to slave\n\r",*data);
viswachaitanya 0:47311d5ff74b 23 loopvariable0=false;
viswachaitanya 0:47311d5ff74b 24 }
viswachaitanya 0:47311d5ff74b 25 }
viswachaitanya 0:47311d5ff74b 26 printf("done\n\r");
viswachaitanya 0:47311d5ff74b 27 }
viswachaitanya 0:47311d5ff74b 28 }
viswachaitanya 0:47311d5ff74b 29 }