FlexBook / Mbed 2 deprecated FlexBook171204a

Dependencies:   SDFileSystem app epson mbed msp430 pl tests

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2c_utils.cpp Source File

i2c_utils.cpp

00001 //
00002 // Filename: i2c_utils.cpp
00003 //
00004 // I2C utilities.
00005 //
00006 
00007 #include "i2c_utils.h"
00008 
00009 uint16_t GetAddressRangeEnd(uint16_t bits)
00010 {
00011     uint16_t end = 0;
00012     switch(bits)
00013     {
00014         case 7:
00015             end = 0x80;
00016             break;
00017 
00018         case 10:
00019             end = 0x400;
00020             break;
00021 
00022         default:
00023             printf("Bad address range\n");
00024     }
00025     
00026     return end;
00027 }
00028 
00029 void I2C_Scan(I2C &i2c, uint16_t bits)
00030 {
00031     printf("Scanning I2C...\n");
00032 
00033     const uint16_t end = GetAddressRangeEnd(bits);
00034 
00035     char data;
00036     for(uint16_t address = 0; address < end; address++)
00037     {
00038         if(i2c.read(address << 1, &data, 1) == 0)
00039             printf("Address 0x%02x found\n", address);
00040     }
00041 }