![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
while loop removed ( master cant send)
Fork of slave_working_obama by
main.cpp@2:458f9e603814, 2014-12-09 (annotated)
- Committer:
- greenroshks
- Date:
- Tue Dec 09 12:35:12 2014 +0000
- Revision:
- 2:458f9e603814
- Parent:
- 1:4c71a7675750
- Child:
- 3:5e68dd4bbb70
working slave codes for both communications
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 | 2:458f9e603814 | 8 | void FUNC_I2C_WRITE2CDMS(char * data_send,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 | 2:458f9e603814 | 17 | slave_status=slave.write(data_send,length); |
greenroshks | 2:458f9e603814 | 18 | printf("\n In the while loop\n"); |
greenroshks | 2:458f9e603814 | 19 | |
greenroshks | 2:458f9e603814 | 20 | if(!slave_status) |
greenroshks | 2:458f9e603814 | 21 | { |
greenroshks | 2:458f9e603814 | 22 | printf("slave wrote %c to master\n\r",*data_send); |
greenroshks | 2:458f9e603814 | 23 | |
viswachaitanya | 0:24e80ff2c7b1 | 24 | } |
greenroshks | 2:458f9e603814 | 25 | |
viswachaitanya | 0:24e80ff2c7b1 | 26 | } |
greenroshks | 2:458f9e603814 | 27 | |
greenroshks | 2:458f9e603814 | 28 | //slave.stop(); |
greenroshks | 2:458f9e603814 | 29 | } |
viswachaitanya | 0:24e80ff2c7b1 | 30 | printf("2 done"); |
greenroshks | 2:458f9e603814 | 31 | } |
greenroshks | 2:458f9e603814 | 32 | |
greenroshks | 2:458f9e603814 | 33 | void FUNC_I2C_READFCDMS() |
greenroshks | 2:458f9e603814 | 34 | |
greenroshks | 2:458f9e603814 | 35 | { |
greenroshks | 2:458f9e603814 | 36 | printf("\nEntered slave code\n"); |
greenroshks | 2:458f9e603814 | 37 | //read_loop=true; |
greenroshks | 2:458f9e603814 | 38 | char *data_rec = new char; |
greenroshks | 2:458f9e603814 | 39 | int slave_status =1; |
greenroshks | 2:458f9e603814 | 40 | |
greenroshks | 2:458f9e603814 | 41 | slave.address(0x20); |
greenroshks | 2:458f9e603814 | 42 | |
greenroshks | 2:458f9e603814 | 43 | |
greenroshks | 2:458f9e603814 | 44 | |
greenroshks | 2:458f9e603814 | 45 | printf("\nentered slave while loop\n"); |
greenroshks | 2:458f9e603814 | 46 | //if(slave.receive()==3) |
greenroshks | 2:458f9e603814 | 47 | while(slave_status==1) |
greenroshks | 2:458f9e603814 | 48 | slave_status=slave.read(data_rec,1); |
greenroshks | 2:458f9e603814 | 49 | |
greenroshks | 2:458f9e603814 | 50 | if(slave_status==0){printf("1 read %c from master\n\r",*data_rec);} |
greenroshks | 2:458f9e603814 | 51 | // read_loop = false;} |
greenroshks | 2:458f9e603814 | 52 | |
greenroshks | 2:458f9e603814 | 53 | printf("\n done \n \r"); |
greenroshks | 2:458f9e603814 | 54 | |
greenroshks | 2:458f9e603814 | 55 | delete data_rec; |
raizel_varun | 1:4c71a7675750 | 56 | |
greenroshks | 2:458f9e603814 | 57 | } |
greenroshks | 2:458f9e603814 | 58 | |
greenroshks | 2:458f9e603814 | 59 | void main() |
greenroshks | 2:458f9e603814 | 60 | { |
greenroshks | 2:458f9e603814 | 61 | printf("\nStarted slave... waiting for master\n"); |
greenroshks | 2:458f9e603814 | 62 | char * data_send = new char; |
greenroshks | 2:458f9e603814 | 63 | *data_send = 'a'; |
greenroshks | 2:458f9e603814 | 64 | |
greenroshks | 2:458f9e603814 | 65 | while(1) |
greenroshks | 2:458f9e603814 | 66 | { |
greenroshks | 2:458f9e603814 | 67 | //*(data_send)=pc.getc(); |
greenroshks | 2:458f9e603814 | 68 | |
greenroshks | 2:458f9e603814 | 69 | FUNC_I2C_READFCDMS(); |
greenroshks | 2:458f9e603814 | 70 | (*data_send)++; |
greenroshks | 2:458f9e603814 | 71 | //wait(1); |
greenroshks | 2:458f9e603814 | 72 | } |
greenroshks | 2:458f9e603814 | 73 | } |