Example of i2c scanner for RHOMBIO_L476DMW1K

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* i2c-scanner-rhombio-l476dmw1k
00002  * 
00003  * Copyright (c) 2019 rhomb.io
00004  * 
00005  * MIT License
00006  * 
00007  * Permission is hereby granted, free of charge, to any person
00008  * obtaining a copy of this software and associated documentation
00009  * files (the "Software"), to deal in the Software without
00010  * restriction, including without limitation the rights to use,
00011  * copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the
00013  * Software is furnished to do so, subject to the following
00014  * conditions:
00015  * 
00016  * The above copyright notice and this permission notice shall be
00017  * included in all copies or substantial portions of the Software.
00018  * 
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00020  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
00021  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00022  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
00023  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
00024  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00025  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00026  * OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028 
00029 #include "mbed.h"
00030 
00031 
00032 I2C i2c(I2C_SDA, I2C_SCL); 
00033 DigitalOut led(LED1);
00034 
00035 
00036 int main()
00037 {
00038     led = 1;
00039      
00040     i2c.frequency(100000);
00041     printf("Rhomb.io example code - I2C Scanner\n\r");
00042     printf("-----------------------------------\n\r");
00043    
00044     while (1) {
00045         printf("Searching for I2C devices...\n\r");
00046 
00047         int count = 0;
00048         for (int address = 0; address < 255; address +=2) { // check only for device's read addres
00049             if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
00050                 printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n\r", address >> 1, address);  // the address is without LSB, which is R/W flag. shoft it right once
00051                 count++;
00052                 led = 1;
00053             }
00054             wait(0.02);
00055         }
00056         if (count)
00057             printf("%d", count);
00058         else
00059             printf("No");
00060         printf(" device%c found\n\r\n", count == 1?'\0':'s');
00061         led = 0;
00062         wait (1.8);
00063     }
00064 }