![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
gen slave
Fork of freeslave3_1 by
main.cpp@0:4ac2da8142db, 2014-12-05 (annotated)
- Committer:
- viswachaitanya
- Date:
- Fri Dec 05 10:37:01 2014 +0000
- Revision:
- 0:4ac2da8142db
gen slave(master writing and reading from slave
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:4ac2da8142db | 1 | #include "mbed.h" |
viswachaitanya | 0:4ac2da8142db | 2 | |
viswachaitanya | 0:4ac2da8142db | 3 | void write_to_master(char); //function to write data to master |
viswachaitanya | 0:4ac2da8142db | 4 | |
viswachaitanya | 0:4ac2da8142db | 5 | I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave |
viswachaitanya | 0:4ac2da8142db | 6 | Serial pc (USBTX,USBRX); |
viswachaitanya | 0:4ac2da8142db | 7 | |
viswachaitanya | 0:4ac2da8142db | 8 | void main(){ |
viswachaitanya | 0:4ac2da8142db | 9 | printf("press backspace for slave to start \n\r"); |
viswachaitanya | 0:4ac2da8142db | 10 | while(1){ |
viswachaitanya | 0:4ac2da8142db | 11 | if(pc.getc()==8){ |
viswachaitanya | 0:4ac2da8142db | 12 | bool read_loop = true; |
viswachaitanya | 0:4ac2da8142db | 13 | char *data_rec = new char; |
viswachaitanya | 0:4ac2da8142db | 14 | bool write_loop= true; |
viswachaitanya | 0:4ac2da8142db | 15 | char *data_send = new char; |
viswachaitanya | 0:4ac2da8142db | 16 | *data_send = 'k'; |
viswachaitanya | 0:4ac2da8142db | 17 | slave.address(0x20); |
viswachaitanya | 0:4ac2da8142db | 18 | int slave_status=1; |
viswachaitanya | 0:4ac2da8142db | 19 | while(read_loop){ |
viswachaitanya | 0:4ac2da8142db | 20 | if(slave.receive()==3){ |
viswachaitanya | 0:4ac2da8142db | 21 | slave_status=slave.read(data_rec,1); |
viswachaitanya | 0:4ac2da8142db | 22 | if(!slave_status){ |
viswachaitanya | 0:4ac2da8142db | 23 | printf("1 read %c from master\n\r",*data_rec); |
viswachaitanya | 0:4ac2da8142db | 24 | read_loop = false; |
viswachaitanya | 0:4ac2da8142db | 25 | |
viswachaitanya | 0:4ac2da8142db | 26 | while(write_loop){ |
viswachaitanya | 0:4ac2da8142db | 27 | if(slave.receive()==1){ |
viswachaitanya | 0:4ac2da8142db | 28 | slave_status=slave.write(data_send,1); |
viswachaitanya | 0:4ac2da8142db | 29 | if(!slave_status){ |
viswachaitanya | 0:4ac2da8142db | 30 | printf("2 wrote %c to master\n\r",*data_send); |
viswachaitanya | 0:4ac2da8142db | 31 | write_loop = false; |
viswachaitanya | 0:4ac2da8142db | 32 | } |
viswachaitanya | 0:4ac2da8142db | 33 | } |
viswachaitanya | 0:4ac2da8142db | 34 | } |
viswachaitanya | 0:4ac2da8142db | 35 | } |
viswachaitanya | 0:4ac2da8142db | 36 | } |
viswachaitanya | 0:4ac2da8142db | 37 | printf("3 done"); |
viswachaitanya | 0:4ac2da8142db | 38 | } |
viswachaitanya | 0:4ac2da8142db | 39 | } |
viswachaitanya | 0:4ac2da8142db | 40 | } |
viswachaitanya | 0:4ac2da8142db | 41 | } |