![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
i2c master with interrupt
Fork of freemaster2_1 by
main.cpp@2:2dd5bd9a64fb, 2014-12-10 (annotated)
- Committer:
- greenroshks
- Date:
- Wed Dec 10 16:39:17 2014 +0000
- Revision:
- 2:2dd5bd9a64fb
- Parent:
- 1:8e4b5e1f8039
i2c master with interrupt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
viswachaitanya | 0:9ea68477caaf | 1 | #include "mbed.h" |
viswachaitanya | 0:9ea68477caaf | 2 | |
viswachaitanya | 0:9ea68477caaf | 3 | const int addr = 0x20; //slave address |
viswachaitanya | 0:9ea68477caaf | 4 | I2C master (D14,D15); //configure pins p27,p28 as I2C master |
viswachaitanya | 0:9ea68477caaf | 5 | Serial pc (USBTX,USBRX); |
greenroshks | 2:2dd5bd9a64fb | 6 | DigitalOut interrupt(D9); |
greenroshks | 2:2dd5bd9a64fb | 7 | InterruptIn data_ready(D10); |
greenroshks | 2:2dd5bd9a64fb | 8 | void FUNC_I2C_MASTER() |
viswachaitanya | 0:9ea68477caaf | 9 | { |
greenroshks | 2:2dd5bd9a64fb | 10 | //printf("press backspace for master to start \n\r"); |
greenroshks | 2:2dd5bd9a64fb | 11 | //while(1){ |
greenroshks | 2:2dd5bd9a64fb | 12 | // interrupt = 0; |
greenroshks | 2:2dd5bd9a64fb | 13 | // if(pc.getc()=='z'){ |
greenroshks | 2:2dd5bd9a64fb | 14 | interrupt = 1; |
greenroshks | 2:2dd5bd9a64fb | 15 | printf("\nEntered master function\n"); |
greenroshks | 2:2dd5bd9a64fb | 16 | int ack0 =true; |
viswachaitanya | 0:9ea68477caaf | 17 | char *data = new char; |
viswachaitanya | 0:9ea68477caaf | 18 | *data = 'b'; |
viswachaitanya | 0:9ea68477caaf | 19 | bool loopvariable0 = true; |
raizel_varun | 1:8e4b5e1f8039 | 20 | master.frequency(100000); //set clock frequency |
raizel_varun | 1:8e4b5e1f8039 | 21 | //master.start(); |
greenroshks | 2:2dd5bd9a64fb | 22 | while(ack0) |
greenroshks | 2:2dd5bd9a64fb | 23 | { |
raizel_varun | 1:8e4b5e1f8039 | 24 | //printf("2 master clk freq setup and addressing slave\n\r"); |
greenroshks | 2:2dd5bd9a64fb | 25 | |
viswachaitanya | 0:9ea68477caaf | 26 | master.start(); //initiating the data transfer |
greenroshks | 2:2dd5bd9a64fb | 27 | ack0 = master.read(addr,data,1); |
greenroshks | 2:2dd5bd9a64fb | 28 | } |
greenroshks | 2:2dd5bd9a64fb | 29 | if(!ack0) |
greenroshks | 2:2dd5bd9a64fb | 30 | { |
greenroshks | 2:2dd5bd9a64fb | 31 | printf("\n master has read %c from slave\n\r",*data); |
viswachaitanya | 0:9ea68477caaf | 32 | loopvariable0=false; |
viswachaitanya | 0:9ea68477caaf | 33 | } |
greenroshks | 2:2dd5bd9a64fb | 34 | //master.stop(); |
greenroshks | 2:2dd5bd9a64fb | 35 | |
greenroshks | 2:2dd5bd9a64fb | 36 | //printf("done\n\r"); |
greenroshks | 2:2dd5bd9a64fb | 37 | delete data; |
greenroshks | 2:2dd5bd9a64fb | 38 | interrupt=0; |
greenroshks | 2:2dd5bd9a64fb | 39 | } |
greenroshks | 2:2dd5bd9a64fb | 40 | void main() |
greenroshks | 2:2dd5bd9a64fb | 41 | { |
greenroshks | 2:2dd5bd9a64fb | 42 | data_ready.rise(&FUNC_I2C_MASTER); |
greenroshks | 2:2dd5bd9a64fb | 43 | printf("\nstarted master\n"); |
greenroshks | 2:2dd5bd9a64fb | 44 | while(1) |
greenroshks | 2:2dd5bd9a64fb | 45 | { |
greenroshks | 2:2dd5bd9a64fb | 46 | ; |
viswachaitanya | 0:9ea68477caaf | 47 | } |
greenroshks | 2:2dd5bd9a64fb | 48 | } |