Chaitanya Viswa
/
freeslave5_1
funtions sorted
main.cpp@0:21ffb42911bd, 2014-12-08 (annotated)
- Committer:
- viswachaitanya
- Date:
- Mon Dec 08 12:36:07 2014 +0000
- Revision:
- 0:21ffb42911bd
write and read functions sorted
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:21ffb42911bd | 1 | #include "mbed.h" |
viswachaitanya | 0:21ffb42911bd | 2 | |
viswachaitanya | 0:21ffb42911bd | 3 | char* read_from_master(); //function to write data to master |
viswachaitanya | 0:21ffb42911bd | 4 | int write_to_master(char*,int); |
viswachaitanya | 0:21ffb42911bd | 5 | |
viswachaitanya | 0:21ffb42911bd | 6 | I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave |
viswachaitanya | 0:21ffb42911bd | 7 | Serial pc (USBTX,USBRX); |
viswachaitanya | 0:21ffb42911bd | 8 | |
viswachaitanya | 0:21ffb42911bd | 9 | bool read_loop = true; |
viswachaitanya | 0:21ffb42911bd | 10 | bool write_loop = true; |
viswachaitanya | 0:21ffb42911bd | 11 | char *data_rec = new char; |
viswachaitanya | 0:21ffb42911bd | 12 | char *data_send = new char; |
viswachaitanya | 0:21ffb42911bd | 13 | char *rec_data = new char; |
viswachaitanya | 0:21ffb42911bd | 14 | int slave_status = 1; |
viswachaitanya | 0:21ffb42911bd | 15 | int k; |
viswachaitanya | 0:21ffb42911bd | 16 | |
viswachaitanya | 0:21ffb42911bd | 17 | |
viswachaitanya | 0:21ffb42911bd | 18 | int main(){ |
viswachaitanya | 0:21ffb42911bd | 19 | |
viswachaitanya | 0:21ffb42911bd | 20 | slave.address(0x20); |
viswachaitanya | 0:21ffb42911bd | 21 | while(1){ |
viswachaitanya | 0:21ffb42911bd | 22 | slave.stop(); |
viswachaitanya | 0:21ffb42911bd | 23 | printf("11 press any character for slave to start \n\r"); |
viswachaitanya | 0:21ffb42911bd | 24 | *data_send = pc.getc(); |
viswachaitanya | 0:21ffb42911bd | 25 | |
viswachaitanya | 0:21ffb42911bd | 26 | printf("1 in main\n\r"); |
viswachaitanya | 0:21ffb42911bd | 27 | k = write_to_master(data_send,1); |
viswachaitanya | 0:21ffb42911bd | 28 | printf("4 done transfer with return %d\n\r",k); |
viswachaitanya | 0:21ffb42911bd | 29 | } |
viswachaitanya | 0:21ffb42911bd | 30 | |
viswachaitanya | 0:21ffb42911bd | 31 | } |
viswachaitanya | 0:21ffb42911bd | 32 | |
viswachaitanya | 0:21ffb42911bd | 33 | |
viswachaitanya | 0:21ffb42911bd | 34 | char* read_from_master(){ |
viswachaitanya | 0:21ffb42911bd | 35 | |
viswachaitanya | 0:21ffb42911bd | 36 | while(slave_status) |
viswachaitanya | 0:21ffb42911bd | 37 | slave_status=slave.read(data_rec,1); |
viswachaitanya | 0:21ffb42911bd | 38 | |
viswachaitanya | 0:21ffb42911bd | 39 | return(data_rec); |
viswachaitanya | 0:21ffb42911bd | 40 | |
viswachaitanya | 0:21ffb42911bd | 41 | |
viswachaitanya | 0:21ffb42911bd | 42 | //read_loop = true; |
viswachaitanya | 0:21ffb42911bd | 43 | //slave.stop(); |
viswachaitanya | 0:21ffb42911bd | 44 | |
viswachaitanya | 0:21ffb42911bd | 45 | } |
viswachaitanya | 0:21ffb42911bd | 46 | |
viswachaitanya | 0:21ffb42911bd | 47 | int write_to_master(char* send, int length){ |
viswachaitanya | 0:21ffb42911bd | 48 | slave_status=1; |
viswachaitanya | 0:21ffb42911bd | 49 | while(slave_status){ |
viswachaitanya | 0:21ffb42911bd | 50 | printf("2 wait for status\n\r"); |
viswachaitanya | 0:21ffb42911bd | 51 | //if(slave.receive()==1) |
viswachaitanya | 0:21ffb42911bd | 52 | { |
viswachaitanya | 0:21ffb42911bd | 53 | slave_status=slave.write(send,length); |
viswachaitanya | 0:21ffb42911bd | 54 | } |
viswachaitanya | 0:21ffb42911bd | 55 | } |
viswachaitanya | 0:21ffb42911bd | 56 | printf("3 wrote %c to master\n\r",*data_send); |
viswachaitanya | 0:21ffb42911bd | 57 | |
viswachaitanya | 0:21ffb42911bd | 58 | //slave.stop(); |
viswachaitanya | 0:21ffb42911bd | 59 | return(1); |
viswachaitanya | 0:21ffb42911bd | 60 | } |