Tedd OKANO
/
i2c_demo
demo program
main.cpp@0:236c04f081b4, 2015-05-29 (annotated)
- Committer:
- okano
- Date:
- Fri May 29 06:20:17 2015 +0000
- Revision:
- 0:236c04f081b4
initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okano | 0:236c04f081b4 | 1 | #include "mbed.h" |
okano | 0:236c04f081b4 | 2 | |
okano | 0:236c04f081b4 | 3 | I2C i2c( p28, p27 ); // SDA, SCL |
okano | 0:236c04f081b4 | 4 | |
okano | 0:236c04f081b4 | 5 | int main() { |
okano | 0:236c04f081b4 | 6 | char a[ 2 ]; |
okano | 0:236c04f081b4 | 7 | |
okano | 0:236c04f081b4 | 8 | a[ 0 ] = 0x08; // register address for write |
okano | 0:236c04f081b4 | 9 | a[ 1 ] = 0x52; // writing data |
okano | 0:236c04f081b4 | 10 | |
okano | 0:236c04f081b4 | 11 | char w[ 1 ]; |
okano | 0:236c04f081b4 | 12 | char r[ 1 ]; // buffer for read |
okano | 0:236c04f081b4 | 13 | |
okano | 0:236c04f081b4 | 14 | w[ 0 ] = 0x08; // register address for read |
okano | 0:236c04f081b4 | 15 | |
okano | 0:236c04f081b4 | 16 | while(1) { |
okano | 0:236c04f081b4 | 17 | // writing |
okano | 0:236c04f081b4 | 18 | i2c.write( 0x02, a, 2 ); |
okano | 0:236c04f081b4 | 19 | wait( 0.001 ); |
okano | 0:236c04f081b4 | 20 | |
okano | 0:236c04f081b4 | 21 | // reading |
okano | 0:236c04f081b4 | 22 | i2c.write( 0x02, w, 1 ); |
okano | 0:236c04f081b4 | 23 | i2c.read( 0x02, r, 1 ); |
okano | 0:236c04f081b4 | 24 | wait( 0.003 ); |
okano | 0:236c04f081b4 | 25 | |
okano | 0:236c04f081b4 | 26 | printf( "register read : 0x%02X\r\n", r[ 0 ] ); |
okano | 0:236c04f081b4 | 27 | } |
okano | 0:236c04f081b4 | 28 | } |