![](/media/cache/profiles/nikki.png.50x50_q85.jpg)
I2C_2 bus scanner for STM32L476RG.
main.cpp@1:2363995f603f, 2016-03-20 (annotated)
- Committer:
- joeata2wh
- Date:
- Sun Mar 20 23:48:48 2016 +0000
- Revision:
- 1:2363995f603f
- Parent:
- 0:befb557584fb
- Child:
- 2:e90c47d4f358
working version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joeata2wh | 0:befb557584fb | 1 | /* Scan I2C bus on specified pins and prints out |
joeata2wh | 0:befb557584fb | 2 | * the address of all pins where a active responder |
joeata2wh | 0:befb557584fb | 3 | * is found. |
joeata2wh | 0:befb557584fb | 4 | * |
joeata2wh | 0:befb557584fb | 5 | * By Joe Elsworth CTO of A2WH |
joeata2wh | 0:befb557584fb | 6 | * Free use for al but no warranty, no promises. |
joeata2wh | 0:befb557584fb | 7 | * Don't forget 3K pullup sda,scl */ |
joeata2wh | 0:befb557584fb | 8 | |
joeata2wh | 0:befb557584fb | 9 | #include "mbed.h" |
joeata2wh | 0:befb557584fb | 10 | |
joeata2wh | 0:befb557584fb | 11 | |
joeata2wh | 0:befb557584fb | 12 | Serial pc(USBTX, USBRX); |
joeata2wh | 0:befb557584fb | 13 | |
joeata2wh | 1:2363995f603f | 14 | #define D_SDA PB_7 |
joeata2wh | 0:befb557584fb | 15 | #define D_SCL PB_6 |
joeata2wh | 0:befb557584fb | 16 | // sda=PB7, scl=PB_6 Pins specific to Nucleo-F303K8 |
joeata2wh | 0:befb557584fb | 17 | // must change pins to match your board. |
joeata2wh | 0:befb557584fb | 18 | |
joeata2wh | 0:befb557584fb | 19 | I2C i2c(D_SDA, D_SCL); |
joeata2wh | 0:befb557584fb | 20 | |
joeata2wh | 0:befb557584fb | 21 | DigitalOut myled(LED1); |
joeata2wh | 0:befb557584fb | 22 | |
joeata2wh | 0:befb557584fb | 23 | void scanI2C() { |
joeata2wh | 0:befb557584fb | 24 | int address; |
joeata2wh | 0:befb557584fb | 25 | int ack; |
joeata2wh | 0:befb557584fb | 26 | for(address=1;address<127;address++) { |
joeata2wh | 0:befb557584fb | 27 | ack = i2c.write(address, "11", 1); |
joeata2wh | 0:befb557584fb | 28 | if (ack == 0) { |
joeata2wh | 0:befb557584fb | 29 | pc.printf("\tFound at %3d -- %3x\r\n", address,address); |
joeata2wh | 0:befb557584fb | 30 | } |
joeata2wh | 0:befb557584fb | 31 | wait(0.05); |
joeata2wh | 0:befb557584fb | 32 | } |
joeata2wh | 0:befb557584fb | 33 | } |
joeata2wh | 0:befb557584fb | 34 | |
joeata2wh | 0:befb557584fb | 35 | int main() { |
joeata2wh | 0:befb557584fb | 36 | pc.baud(9600); |
joeata2wh | 0:befb557584fb | 37 | pc.printf("I2C scanner \r\n"); |
joeata2wh | 0:befb557584fb | 38 | scanI2C(); |
joeata2wh | 0:befb557584fb | 39 | pc.printf("Finished Scan\r\n"); |
joeata2wh | 0:befb557584fb | 40 | |
joeata2wh | 0:befb557584fb | 41 | ack = i2c.write(71, "11", 1); |
joeata2wh | 0:befb557584fb | 42 | |
joeata2wh | 0:befb557584fb | 43 | while(1) { |
joeata2wh | 0:befb557584fb | 44 | wait(5.0); |
joeata2wh | 0:befb557584fb | 45 | myled = !myled; |
joeata2wh | 0:befb557584fb | 46 | } |
joeata2wh | 0:befb557584fb | 47 | } |
joeata2wh | 0:befb557584fb | 48 |