![](/media/cache/profiles/nikki.png.50x50_q85.jpg)
I2C_2 bus scanner for STM32L476RG.
main.cpp@9:c403dc9b86cc, 2021-11-20 (annotated)
- Committer:
- tzwell
- Date:
- Sat Nov 20 13:08:09 2021 +0000
- Revision:
- 9:c403dc9b86cc
- Parent:
- 8:ead6147e31a4
First commit, first publish-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tzwell | 9:c403dc9b86cc | 1 | /* I2C scanner for NUCLEO-L476RG |
tzwell | 9:c403dc9b86cc | 2 | * |
tzwell | 9:c403dc9b86cc | 3 | * November, 2021. |
tzwell | 9:c403dc9b86cc | 4 | */ |
joeata2wh | 0:befb557584fb | 5 | #include "mbed.h" |
joeata2wh | 0:befb557584fb | 6 | |
tzwell | 9:c403dc9b86cc | 7 | #define D_SDA PB_14 |
tzwell | 9:c403dc9b86cc | 8 | #define D_SCL PB_13 |
tzwell | 9:c403dc9b86cc | 9 | #define MIN_ADD 0 |
tzwell | 9:c403dc9b86cc | 10 | #define MAX_ADD 127 |
tzwell | 9:c403dc9b86cc | 11 | #define DATA "00" |
tzwell | 9:c403dc9b86cc | 12 | #define LENGTH 1 |
tzwell | 9:c403dc9b86cc | 13 | #define SMALL_DELAY_MS 10 |
tzwell | 9:c403dc9b86cc | 14 | #define BIG_DELAY 2 |
joeata2wh | 0:befb557584fb | 15 | |
tzwell | 9:c403dc9b86cc | 16 | I2C i2c(D_SDA, D_SCL); |
joeata2wh | 0:befb557584fb | 17 | DigitalOut myled(LED1); |
joeata2wh | 0:befb557584fb | 18 | |
joeata2wh | 4:ad1195c10812 | 19 | int ack; |
tzwell | 9:c403dc9b86cc | 20 | int address; |
tzwell | 9:c403dc9b86cc | 21 | |
joeata2wh | 0:befb557584fb | 22 | void scanI2C() { |
tzwell | 9:c403dc9b86cc | 23 | for(address = MIN_ADD; address < MAX_ADD; address++) { |
tzwell | 9:c403dc9b86cc | 24 | ack = i2c.write(address << 1, DATA, LENGTH); |
tzwell | 9:c403dc9b86cc | 25 | if (!ack) { |
tzwell | 9:c403dc9b86cc | 26 | printf("\tFound at: %3d <--> %3x\r\n", address,address); |
joeata2wh | 0:befb557584fb | 27 | } |
tzwell | 9:c403dc9b86cc | 28 | wait_ms(SMALL_DELAY_MS); |
joeata2wh | 0:befb557584fb | 29 | } |
joeata2wh | 0:befb557584fb | 30 | } |
joeata2wh | 0:befb557584fb | 31 | |
joeata2wh | 0:befb557584fb | 32 | int main() { |
tzwell | 9:c403dc9b86cc | 33 | printf("I2C scanner \r\n"); |
joeata2wh | 0:befb557584fb | 34 | scanI2C(); |
tzwell | 9:c403dc9b86cc | 35 | printf("Finished Scan\r\n"); |
joeata2wh | 5:a4fddd74263c | 36 | // just blink to let us know the CPU is alive |
tzwell | 9:c403dc9b86cc | 37 | while(true) { |
tzwell | 9:c403dc9b86cc | 38 | wait(BIG_DELAY); |
joeata2wh | 0:befb557584fb | 39 | myled = !myled; |
joeata2wh | 0:befb557584fb | 40 | } |
joeata2wh | 0:befb557584fb | 41 | } |
joeata2wh | 0:befb557584fb | 42 |