slave working
Dependencies: mbed slave_sample
Fork of slave_sample by
main.cpp@1:d5cf8c6f7ef4, 2014-12-06 (annotated)
- Committer:
- raizel_varun
- Date:
- Sat Dec 06 13:24:32 2014 +0000
- Revision:
- 1:d5cf8c6f7ef4
- Parent:
- 0:17326cfa2586
sgd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sakthipriya | 0:17326cfa2586 | 1 | #include "mbed.h" |
sakthipriya | 0:17326cfa2586 | 2 | |
raizel_varun | 1:d5cf8c6f7ef4 | 3 | #define interrupt D9 |
sakthipriya | 0:17326cfa2586 | 4 | |
sakthipriya | 0:17326cfa2586 | 5 | void write_to_master(char); //function to write data to master |
sakthipriya | 0:17326cfa2586 | 6 | DigitalOut led1(LED_RED); |
sakthipriya | 0:17326cfa2586 | 7 | I2CSlave slave(D14,D15); //configuring pins p27, p28 as I2Cslave |
sakthipriya | 0:17326cfa2586 | 8 | Serial pc(USBTX,USBRX); |
sakthipriya | 0:17326cfa2586 | 9 | InterruptIn m2s_interrupt(interrupt); |
sakthipriya | 0:17326cfa2586 | 10 | //int m2s = m2s_interrupt; |
sakthipriya | 0:17326cfa2586 | 11 | bool read_loop = true; |
raizel_varun | 1:d5cf8c6f7ef4 | 12 | |
sakthipriya | 0:17326cfa2586 | 13 | void write2slave() |
raizel_varun | 1:d5cf8c6f7ef4 | 14 | |
sakthipriya | 0:17326cfa2586 | 15 | { |
sakthipriya | 0:17326cfa2586 | 16 | printf("\nEntered slave code\n"); |
raizel_varun | 1:d5cf8c6f7ef4 | 17 | read_loop=true; |
sakthipriya | 0:17326cfa2586 | 18 | char *data_rec = new char; |
raizel_varun | 1:d5cf8c6f7ef4 | 19 | int slave_status =1; |
raizel_varun | 1:d5cf8c6f7ef4 | 20 | |
raizel_varun | 1:d5cf8c6f7ef4 | 21 | slave.address(0x20); |
raizel_varun | 1:d5cf8c6f7ef4 | 22 | |
raizel_varun | 1:d5cf8c6f7ef4 | 23 | |
raizel_varun | 1:d5cf8c6f7ef4 | 24 | |
raizel_varun | 1:d5cf8c6f7ef4 | 25 | printf("\nentered slave while loop\n"); |
raizel_varun | 1:d5cf8c6f7ef4 | 26 | //if(slave.receive()==3) |
raizel_varun | 1:d5cf8c6f7ef4 | 27 | while(slave_status==1) |
sakthipriya | 0:17326cfa2586 | 28 | slave_status=slave.read(data_rec,1); |
raizel_varun | 1:d5cf8c6f7ef4 | 29 | |
raizel_varun | 1:d5cf8c6f7ef4 | 30 | if(slave_status==0){printf("1 read %c from master\n\r",*data_rec); |
raizel_varun | 1:d5cf8c6f7ef4 | 31 | read_loop = false;} |
raizel_varun | 1:d5cf8c6f7ef4 | 32 | |
raizel_varun | 1:d5cf8c6f7ef4 | 33 | printf("\n done \n \r"); |
raizel_varun | 1:d5cf8c6f7ef4 | 34 | |
raizel_varun | 1:d5cf8c6f7ef4 | 35 | delete data_rec; |
raizel_varun | 1:d5cf8c6f7ef4 | 36 | |
sakthipriya | 0:17326cfa2586 | 37 | } |
sakthipriya | 0:17326cfa2586 | 38 | |
raizel_varun | 1:d5cf8c6f7ef4 | 39 | |
sakthipriya | 0:17326cfa2586 | 40 | |
sakthipriya | 0:17326cfa2586 | 41 | |
sakthipriya | 0:17326cfa2586 | 42 | int main(){ |
sakthipriya | 0:17326cfa2586 | 43 | |
sakthipriya | 0:17326cfa2586 | 44 | printf("interrupt for slave to start \n\r"); |
raizel_varun | 1:d5cf8c6f7ef4 | 45 | m2s_interrupt.rise(&write2slave); |
raizel_varun | 1:d5cf8c6f7ef4 | 46 | //m2s_interrupt.fall(&write2slave); |
raizel_varun | 1:d5cf8c6f7ef4 | 47 | while(1) |
raizel_varun | 1:d5cf8c6f7ef4 | 48 | { |
raizel_varun | 1:d5cf8c6f7ef4 | 49 | ; |
raizel_varun | 1:d5cf8c6f7ef4 | 50 | } |
sakthipriya | 0:17326cfa2586 | 51 | //write2slave(); |
raizel_varun | 1:d5cf8c6f7ef4 | 52 | |
sakthipriya | 0:17326cfa2586 | 53 | } |
sakthipriya | 0:17326cfa2586 | 54 | |
sakthipriya | 0:17326cfa2586 | 55 | |
sakthipriya | 0:17326cfa2586 | 56 | |
sakthipriya | 0:17326cfa2586 | 57 |