![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
gen master public
Fork of freemaster3_1 by
main.cpp@0:cdc20d6056de, 2014-12-05 (annotated)
- Committer:
- viswachaitanya
- Date:
- Fri Dec 05 10:35:38 2014 +0000
- Revision:
- 0:cdc20d6056de
gen free frdm master(mas write to slave and read from slave)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:cdc20d6056de | 1 | #include "mbed.h" |
viswachaitanya | 0:cdc20d6056de | 2 | |
viswachaitanya | 0:cdc20d6056de | 3 | const int addr = 0x20; //slave address |
viswachaitanya | 0:cdc20d6056de | 4 | I2C master (D14,D15); //configure pins p27,p28 as I2C master |
viswachaitanya | 0:cdc20d6056de | 5 | Serial pc (USBTX,USBRX); |
viswachaitanya | 0:cdc20d6056de | 6 | void main() |
viswachaitanya | 0:cdc20d6056de | 7 | { |
viswachaitanya | 0:cdc20d6056de | 8 | printf("press backspace for master to start \n\r"); |
viswachaitanya | 0:cdc20d6056de | 9 | while(1){ |
viswachaitanya | 0:cdc20d6056de | 10 | if(pc.getc()==8){ |
viswachaitanya | 0:cdc20d6056de | 11 | bool master_status_write = true; |
viswachaitanya | 0:cdc20d6056de | 12 | bool master_status_read = true; |
viswachaitanya | 0:cdc20d6056de | 13 | char* data_send = new char; |
viswachaitanya | 0:cdc20d6056de | 14 | *data_send = 'b'; |
viswachaitanya | 0:cdc20d6056de | 15 | char *data_rec = new char; |
viswachaitanya | 0:cdc20d6056de | 16 | *data_rec = 'b'; |
viswachaitanya | 0:cdc20d6056de | 17 | bool write_loop = true; |
viswachaitanya | 0:cdc20d6056de | 18 | bool read_loop = true; |
viswachaitanya | 0:cdc20d6056de | 19 | while(write_loop){ |
viswachaitanya | 0:cdc20d6056de | 20 | printf("2 master clk freq setup and addressing slave\n\r"); |
viswachaitanya | 0:cdc20d6056de | 21 | master.frequency(100000); //set clock frequency |
viswachaitanya | 0:cdc20d6056de | 22 | master.start(); //initiating the data transfer |
viswachaitanya | 0:cdc20d6056de | 23 | master_status_write = (bool) master.write(addr|0x00,data_send,1); |
viswachaitanya | 0:cdc20d6056de | 24 | if(!master_status_write){ |
viswachaitanya | 0:cdc20d6056de | 25 | printf("3 master has written %c to slave\n\r",*data_send); |
viswachaitanya | 0:cdc20d6056de | 26 | write_loop=false; |
viswachaitanya | 0:cdc20d6056de | 27 | } |
viswachaitanya | 0:cdc20d6056de | 28 | } |
viswachaitanya | 0:cdc20d6056de | 29 | printf("done\n\r"); |
viswachaitanya | 0:cdc20d6056de | 30 | |
viswachaitanya | 0:cdc20d6056de | 31 | |
viswachaitanya | 0:cdc20d6056de | 32 | while(read_loop){ |
viswachaitanya | 0:cdc20d6056de | 33 | printf("4 master clk freq setup and addressing slave\n\r"); |
viswachaitanya | 0:cdc20d6056de | 34 | master.frequency(100000); //set clock frequency |
viswachaitanya | 0:cdc20d6056de | 35 | master.start(); //initiating the data transfer |
viswachaitanya | 0:cdc20d6056de | 36 | master_status_read = (bool) master.read(addr,data_rec,1); |
viswachaitanya | 0:cdc20d6056de | 37 | if(!master_status_read){ |
viswachaitanya | 0:cdc20d6056de | 38 | printf("5 master has read %c from slave\n\r",*data_rec); |
viswachaitanya | 0:cdc20d6056de | 39 | read_loop=false; |
viswachaitanya | 0:cdc20d6056de | 40 | } |
viswachaitanya | 0:cdc20d6056de | 41 | } |
viswachaitanya | 0:cdc20d6056de | 42 | printf("done\n\r"); |
viswachaitanya | 0:cdc20d6056de | 43 | } |
viswachaitanya | 0:cdc20d6056de | 44 | } |
viswachaitanya | 0:cdc20d6056de | 45 | } |