i2c slave working version 1.0
Fork of freeslave_write2master_working by
main.cpp@3:5e68dd4bbb70, 2014-12-10 (annotated)
- Committer:
- greenroshks
- Date:
- Wed Dec 10 05:21:50 2014 +0000
- Revision:
- 3:5e68dd4bbb70
- Parent:
- 2:458f9e603814
- Child:
- 4:133b49d51b18
slave code combined
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:24e80ff2c7b1 | 1 | #include "mbed.h" |
viswachaitanya | 0:24e80ff2c7b1 | 2 | |
viswachaitanya | 0:24e80ff2c7b1 | 3 | void write_to_master(char); //function to write data to master |
viswachaitanya | 0:24e80ff2c7b1 | 4 | |
viswachaitanya | 0:24e80ff2c7b1 | 5 | I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave |
viswachaitanya | 0:24e80ff2c7b1 | 6 | Serial pc (USBTX,USBRX); |
greenroshks | 2:458f9e603814 | 7 | InterruptIn interrupt(D9); |
greenroshks | 3:5e68dd4bbb70 | 8 | void FUNC_I2C_WRITE2CDMS(char * data,int length) |
greenroshks | 2:458f9e603814 | 9 | { |
viswachaitanya | 0:24e80ff2c7b1 | 10 | |
greenroshks | 2:458f9e603814 | 11 | int slave_status = 1; |
greenroshks | 2:458f9e603814 | 12 | while(slave_status) |
greenroshks | 2:458f9e603814 | 13 | { |
viswachaitanya | 0:24e80ff2c7b1 | 14 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 15 | if(slave.receive()==1) |
greenroshks | 2:458f9e603814 | 16 | { |
greenroshks | 3:5e68dd4bbb70 | 17 | slave_status=slave.write(data,length); |
greenroshks | 3:5e68dd4bbb70 | 18 | // printf("\n In the while loop\n"); |
greenroshks | 2:458f9e603814 | 19 | |
greenroshks | 3:5e68dd4bbb70 | 20 | /* if(!slave_status) |
greenroshks | 2:458f9e603814 | 21 | { |
greenroshks | 3:5e68dd4bbb70 | 22 | printf("slave wrote %c to master\n\r",*data); |
greenroshks | 2:458f9e603814 | 23 | |
greenroshks | 3:5e68dd4bbb70 | 24 | }*/ |
greenroshks | 2:458f9e603814 | 25 | |
viswachaitanya | 0:24e80ff2c7b1 | 26 | } |
greenroshks | 3:5e68dd4bbb70 | 27 | else |
greenroshks | 3:5e68dd4bbb70 | 28 | if(slave.receive()==3 || slave.receive()==2) |
greenroshks | 3:5e68dd4bbb70 | 29 | { |
greenroshks | 3:5e68dd4bbb70 | 30 | slave_status=slave.read(data,length); |
greenroshks | 3:5e68dd4bbb70 | 31 | /*if(!slave_status) |
greenroshks | 3:5e68dd4bbb70 | 32 | { |
greenroshks | 3:5e68dd4bbb70 | 33 | printf("1 read %c from master\n\r",*data); |
greenroshks | 3:5e68dd4bbb70 | 34 | }*/ |
greenroshks | 3:5e68dd4bbb70 | 35 | } |
greenroshks | 2:458f9e603814 | 36 | //slave.stop(); |
greenroshks | 2:458f9e603814 | 37 | } |
viswachaitanya | 0:24e80ff2c7b1 | 38 | printf("2 done"); |
greenroshks | 2:458f9e603814 | 39 | } |
greenroshks | 2:458f9e603814 | 40 | |
greenroshks | 3:5e68dd4bbb70 | 41 | /*void FUNC_I2C_READFCDMS() |
greenroshks | 2:458f9e603814 | 42 | |
greenroshks | 2:458f9e603814 | 43 | { |
greenroshks | 2:458f9e603814 | 44 | printf("\nEntered slave code\n"); |
greenroshks | 2:458f9e603814 | 45 | //read_loop=true; |
greenroshks | 2:458f9e603814 | 46 | char *data_rec = new char; |
greenroshks | 2:458f9e603814 | 47 | int slave_status =1; |
greenroshks | 2:458f9e603814 | 48 | |
greenroshks | 2:458f9e603814 | 49 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 50 | |
greenroshks | 2:458f9e603814 | 51 | |
greenroshks | 2:458f9e603814 | 52 | |
greenroshks | 2:458f9e603814 | 53 | printf("\nentered slave while loop\n"); |
greenroshks | 2:458f9e603814 | 54 | //if(slave.receive()==3) |
greenroshks | 2:458f9e603814 | 55 | while(slave_status==1) |
greenroshks | 2:458f9e603814 | 56 | slave_status=slave.read(data_rec,1); |
greenroshks | 2:458f9e603814 | 57 | |
greenroshks | 2:458f9e603814 | 58 | if(slave_status==0){printf("1 read %c from master\n\r",*data_rec);} |
greenroshks | 2:458f9e603814 | 59 | // read_loop = false;} |
greenroshks | 2:458f9e603814 | 60 | |
greenroshks | 2:458f9e603814 | 61 | printf("\n done \n \r"); |
greenroshks | 2:458f9e603814 | 62 | |
greenroshks | 2:458f9e603814 | 63 | delete data_rec; |
raizel_varun | 1:4c71a7675750 | 64 | |
greenroshks | 2:458f9e603814 | 65 | } |
greenroshks | 3:5e68dd4bbb70 | 66 | */ |
greenroshks | 2:458f9e603814 | 67 | void main() |
greenroshks | 2:458f9e603814 | 68 | { |
greenroshks | 2:458f9e603814 | 69 | printf("\nStarted slave... waiting for master\n"); |
greenroshks | 3:5e68dd4bbb70 | 70 | char * data = new char; |
greenroshks | 3:5e68dd4bbb70 | 71 | *data = 'a'; |
greenroshks | 2:458f9e603814 | 72 | |
greenroshks | 2:458f9e603814 | 73 | while(1) |
greenroshks | 2:458f9e603814 | 74 | { |
greenroshks | 2:458f9e603814 | 75 | //*(data_send)=pc.getc(); |
greenroshks | 2:458f9e603814 | 76 | |
greenroshks | 3:5e68dd4bbb70 | 77 | FUNC_I2C_WRITE2CDMS(data,1); |
greenroshks | 3:5e68dd4bbb70 | 78 | printf("\n The data send or received by slave is %c\n",*data); |
greenroshks | 3:5e68dd4bbb70 | 79 | (*data)++; |
greenroshks | 2:458f9e603814 | 80 | //wait(1); |
greenroshks | 2:458f9e603814 | 81 | } |
greenroshks | 2:458f9e603814 | 82 | } |