funtions sorted

Dependencies:   mbed

Committer:
viswachaitanya
Date:
Mon Dec 08 12:38:54 2014 +0000
Revision:
0:a3f89fb87618
functions sorted

Who changed what in which revision?

UserRevisionLine numberNew contents of line
viswachaitanya 0:a3f89fb87618 1 #include "mbed.h"
viswachaitanya 0:a3f89fb87618 2
viswachaitanya 0:a3f89fb87618 3 const int addr = 0x20; //slave address
viswachaitanya 0:a3f89fb87618 4 I2C master (D14,D15); //configure pins p27,p28 as I2C master
viswachaitanya 0:a3f89fb87618 5 Serial pc (USBTX,USBRX);
viswachaitanya 0:a3f89fb87618 6
viswachaitanya 0:a3f89fb87618 7 int write_to_slave(char);
viswachaitanya 0:a3f89fb87618 8 char* read_from_slave();
viswachaitanya 0:a3f89fb87618 9
viswachaitanya 0:a3f89fb87618 10 bool write_ack = true;
viswachaitanya 0:a3f89fb87618 11 bool read_ack = true;
viswachaitanya 0:a3f89fb87618 12 char* data_send = new char;
viswachaitanya 0:a3f89fb87618 13 char* data_rec = new char;
viswachaitanya 0:a3f89fb87618 14 bool write_loop = true;
viswachaitanya 0:a3f89fb87618 15 bool read_loop = true;
viswachaitanya 0:a3f89fb87618 16
viswachaitanya 0:a3f89fb87618 17
viswachaitanya 0:a3f89fb87618 18 int main(){
viswachaitanya 0:a3f89fb87618 19
viswachaitanya 0:a3f89fb87618 20 while(1)
viswachaitanya 0:a3f89fb87618 21 {
viswachaitanya 0:a3f89fb87618 22 printf("1 press backspace for master to start \n\r");
viswachaitanya 0:a3f89fb87618 23 if(pc.getc()==8){
viswachaitanya 0:a3f89fb87618 24 master.frequency(100000);
viswachaitanya 0:a3f89fb87618 25 data_rec = read_from_slave();
viswachaitanya 0:a3f89fb87618 26 printf("4 in main read %c from slave\n\r",*data_rec);
viswachaitanya 0:a3f89fb87618 27 }
viswachaitanya 0:a3f89fb87618 28 }
viswachaitanya 0:a3f89fb87618 29 }
viswachaitanya 0:a3f89fb87618 30
viswachaitanya 0:a3f89fb87618 31 int write_to_slave(char send){
viswachaitanya 0:a3f89fb87618 32
viswachaitanya 0:a3f89fb87618 33 printf("2 master clk freq setup and addressing slave\n\r");
viswachaitanya 0:a3f89fb87618 34 master.frequency(100000); //set clock frequency
viswachaitanya 0:a3f89fb87618 35 write_loop = true;
viswachaitanya 0:a3f89fb87618 36 while(write_ack){
viswachaitanya 0:a3f89fb87618 37 master.start(); //initiating the data transfer
viswachaitanya 0:a3f89fb87618 38 printf("entered write loop \n\r");
viswachaitanya 0:a3f89fb87618 39
viswachaitanya 0:a3f89fb87618 40 write_ack = (bool) master.write(addr|0x00,&send,1,false);
viswachaitanya 0:a3f89fb87618 41 if(!write_ack){
viswachaitanya 0:a3f89fb87618 42 printf("3 master has written %c to slave\n\r",send);
viswachaitanya 0:a3f89fb87618 43 write_loop=false;
viswachaitanya 0:a3f89fb87618 44
viswachaitanya 0:a3f89fb87618 45 }
viswachaitanya 0:a3f89fb87618 46 }
viswachaitanya 0:a3f89fb87618 47 return(1);
viswachaitanya 0:a3f89fb87618 48 }
viswachaitanya 0:a3f89fb87618 49
viswachaitanya 0:a3f89fb87618 50 char* read_from_slave(){
viswachaitanya 0:a3f89fb87618 51 read_ack = true;
viswachaitanya 0:a3f89fb87618 52 printf("2 master entered function to read\n\r");
viswachaitanya 0:a3f89fb87618 53 while(read_ack){
viswachaitanya 0:a3f89fb87618 54 master.start(); //initiating the data transfer
viswachaitanya 0:a3f89fb87618 55 read_ack = (bool) master.read(addr|0x1,data_rec,1,false);
viswachaitanya 0:a3f89fb87618 56 //printf("3 addressing slave\n\r");
viswachaitanya 0:a3f89fb87618 57 }
viswachaitanya 0:a3f89fb87618 58 printf("4 master has read %c from slave\n\r",*data_rec);
viswachaitanya 0:a3f89fb87618 59 return(data_rec);
viswachaitanya 0:a3f89fb87618 60 }