楼主: 驴友花雕
|
[教程] 一块扩展板完成Arduino的10类37项实验(代码+图形+仿真) |
本帖最后由 驴友花雕 于 2019-8-7 17:52 编辑 26 BH1750FVI 数字光强度模块光照传感器(数值类 I2C接口) [mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 扩展实验 26 BH1750FVI 数字光强度模块光照传感器(数值类 I2C接口) 项目:测试I2C接口,串口读取环境光照波形 Module UNO VCC —— 5V GND —— GND SCL —— A5 SDA —— A4 ADD —— NC */ #include <Wire.h> #include <math.h> int BH1750address = 0x23; byte buff[2]; void setup() { Wire.begin(); Serial.begin(57600); } void loop() { int i; uint16_t val=0; BH1750_Init(BH1750address); delay(200); if(2==BH1750_Read(BH1750address)) { val=((buff[0]<<8)|buff[1])/1.2; Serial.println(val,DEC); } delay(150); } int BH1750_Read(int address) { int i=0; Wire.beginTransmission(address); Wire.requestFrom(address, 2); while(Wire.available()) { buff = Wire.read(); i++; } Wire.endTransmission(); return i; } void BH1750_Init(int address) { Wire.beginTransmission(address); Wire.write(0x10); Wire.endTransmission(); } [/mw_shl_code] |
28 LCD1602液晶屏模块(输出显示器类, IIC/I2C接口) [mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 扩展实验 28 LCD1602液晶屏模块(输出显示器类 IIC/I2C接口) 项目一:寻找查询设备IIC地址,在串口上即可看到1602的地址 这里查询结果是“0x27”(这个地址不对的话,实测无法烧录程序) Arduino------LCD1602 5V-------------VCC GND-----------GND A4-----------SDA IIC 数据线 A5-----------SCL IIC 时钟线 */ #include <Wire.h> void setup(){ Wire.begin(); Serial.begin(9600); Serial.println("\nI2C Scanner"); } void loop(){ byte error, address; int nDevices; Serial.println("Eagler8 Scanning..."); nDevices = 0; for (address = 1; address < 127; address++ ){ Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0){ Serial.print("I2C device found at address 0x"); if (address < 16) Serial.print("0"); Serial.print(address, HEX); Serial.println(" !"); nDevices++; }else if (error == 4){ Serial.print("Unknow error at address 0x"); if (address < 16) Serial.print("0"); Serial.println(address, HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(5000); } [/mw_shl_code] |
[mw_shl_code=c,true]/* Eagler8系列实验程序列表 第十类 扩展实验 28 LCD1602液晶屏模块(输出显示器类 IIC/I2C接口) 项目二:分二行显示“Welcome to Eagler8” Arduino------LCD1602 5V------------VCC GND-----------GND A4-----------SDA IIC 数据线 A5-----------SCL IIC 时钟线 */ #include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd(0x27,16,2); void MyPrintLCD(String MyString) { for (int i=0;i<MyString.length();i++) lcd.write(MyString.charAt(i)); } void setup() { lcd.init(); lcd.backlight(); MyPrintLCD("Welcome to "); lcd.setCursor(0,1); MyPrintLCD(" Eagler8"); } void loop() { }[/mw_shl_code] |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed