10,000套小e体验板免费申请地址:
https://www.cirmall.com/bbs/thread-47660-1-1.html
【小e智能硬件开发平台】 基于 小e开发板(ESP8266) 的I2C总线扫描 因为挂在I2C总线的装置, 经常不确定该装置的I2C地址是什么, 因此使用ArduinoIDE做了个I2C总线扫描程序! 程序使用小e开发板 的I2C , GPIO02:SDA,GPIO14:SCL. 气压传感器与OLED的I2C跳线接法如下图, ADC拨码可任意设置.
扫描后由USB串口输出结果. 气压传感器(BMP180) : 0x77 OLED : 0x3C ADC拨码可任意拨动, 观查读取的值.
主程序如下: - [hide]
- #include "Wire.h"
- /*
- * xiao_e1 : http://www.kaifakuai.com/kfkdev/Downloatd2.html
- *
- * jumper assignment ( ":" is open, "|" is short)
- * Audio4 RGB3 BARO2 OLED1
- * :::: |:: || ||
- *
- */
- int pinLED = 12; // GPIO12 for xiao_e1 ESP8266 LED-Red
- int xiao_e_SDA = 2; // GPIO02 for xiao_e1 ESP8266 I2C-SDA
- int xiao_e_SCL = 14; // GPIO14 for xiao_e1 ESP8266 I2C-SCL
- int xiao_e_ADC = 17; // ADC (A0=17) for xiao_e1 ESP8266 I2C-SCL
- byte start_address = 1; // scan I2C device from 0x01
- byte end_address = 127; // scan I2C device end to 0xFF
- void scanI2CBus(byte from_addr, byte to_addr)
- {
- byte rc;
- byte data = 0; // not used, just an address to feed to twi_writeTo()
- for( byte addr = from_addr; addr <= to_addr; addr++ ) {
- rc = twi_writeTo(addr,&data,0,1); // for xiao_e1 ESP8266
- if(rc==0) {
- Serial.print("addr: ");
- Serial.print(addr,DEC);
- Serial.print("\t HEX: 0x");
- Serial.print(addr,HEX);
- Serial.println("\t found!");
- }
- }
- }
- void setup()
- {
- pinMode(pinLED,OUTPUT);
- Wire.begin(xiao_e_SDA, xiao_e_SCL);
- Serial.begin(9600);
- delay(5000);
- Serial.println("--- I2C Bus Scanner Test ---");
- Serial.print("starting scanning of I2C bus from ");
- Serial.print(start_address,DEC);
- Serial.print(" to ");
- Serial.print(end_address,DEC);
- Serial.println("...");
- Serial.println();
- scanI2CBus( start_address, end_address );
- Serial.println("--- I2C Bus Scanner Complete ---");
- }
- void loop()
- {
- Serial.print("ADC : ");
- Serial.println(analogRead(xiao_e_ADC));
- digitalWrite(pinLED,HIGH);
- delay(1000);
- digitalWrite(pinLED,LOW);
- delay(1000);
- }[/hide]
复制代码 |