找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7826|回复: 7
打印 上一主题 下一主题
收起左侧

arduino 多路I2C设备(MLX90614 )地址修改

[复制链接]
跳转到指定楼层
楼主
ID:209158 发表于 2019-6-7 17:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
红外温度传感器    MLX90614
// remap_mlx90614.ino
#include "i2cmaster.h"
// New slave address, purposefully left shifted
byte NewMLXAddr = 0x39;                                //传感器新I2C地址;传感器1,2,3,4,5依次为0x1A,0x2A,0x3A,0x4A,0x5A
// 0x5A is the default address - uncomment this to set it back
// byte NewMLXAddr = 0x5A;
  
void setup(){
   Serial.begin(9600);
   Serial.println("Setup...");
   // Initialise the i2c bus, enable pullups and then wait
   i2c_init();
   PORTC = (1 << PORTC4) | (1 << PORTC5);
   delay(5000);
   // Read current address bytes
   ReadAddr(0);
   // Change address to new value (NewMLXAddr);
   ChangeAddr(NewMLXAddr, 0x00);
   // Read address bytes
   ReadAddr(0);
   Serial.print("> Cycle power NOW to set address to: ");
   Serial.print(NewMLXAddr, HEX);
   Serial.println(" - you have 10 seconds");
   // Cycle power to MLX during this 10 second pause
   delay(10000);
   // Read temperature using default address
   ReadTemp(0);
   // Read temperature using new address (note left bit shift for reading)
   ReadTemp(NewMLXAddr<<1);
   Serial.println("**---DONE---**");
}
void loop(){
     delay(5000);
     ReadTemp(NewMLXAddr<<1);
}
word ChangeAddr(byte NewAddr1, byte NewAddr2) {
   Serial.println("> Change address");
   // Send start condition and write bit
   i2c_start_wait(0 + I2C_WRITE);
   // Send command for device to return address
   i2c_write(0x2E);
   // Send low byte zero to erase
   i2c_write(0x00);
   // Send high byte zero to erase
   i2c_write(0x00);
   if (i2c_write(0x6F) == 0) {
     // Release bus, end transaction
     i2c_stop();
     Serial.println("> Data erased.");
   }
   else {
     // Release bus, end transaction
     i2c_stop();
     Serial.println("> Failed to erase data");
     return -1;
   }
   Serial.print("> Writing data: ");
   Serial.print(NewAddr1, HEX);
   Serial.print(", ");
   Serial.println(NewAddr2, HEX);
   for (int a = 0; a != 256; a++) {
     // Send start condition and write bit
     i2c_start_wait(0 + I2C_WRITE);
     // Send command for device to return address
     i2c_write(0x2E);
     // Send low byte zero to erase
     i2c_write(NewAddr1);
     // Send high byte zero to erase
     i2c_write(NewAddr2);
     if (i2c_write(a) == 0) {
       // Release bus, end transaction then wait 10ms
       i2c_stop();
       delay(100);
       Serial.print("> Found correct CRC: 0x");
       Serial.println(a, HEX);
       return a;
     }
   }
   // Release bus, end transaction
   i2c_stop();
   Serial.println("> Correct CRC not found");
   return -1;
}
void ReadAddr(byte Address) {
   Serial.println("> Read address");
   // Inform the user
   Serial.print("  MLX address: ");
   Serial.print(Address, HEX);
   Serial.print(", Data: ");
   // Send start condition and write bit
   i2c_start_wait(Address + I2C_WRITE);
   // Send command for device to return address
   i2c_write(0x2E);
   i2c_rep_start(Address + I2C_READ);
   // Read 1 byte and then send ack (x2)
   Serial.print(i2c_readAck(), HEX);
   Serial.print(", ");
   Serial.print(i2c_readAck(), HEX);
   Serial.print(", ");
   Serial.println(i2c_readNak(), HEX);
   i2c_stop();
}
float ReadTemp(byte Address) {
   int data_low = 0;
   int data_high = 0;
   int pec = 0;
   Serial.println("> Read temperature");
   // Inform the user
   Serial.print("  MLX address: ");
   Serial.print(Address, HEX);
   Serial.print(", ");
   i2c_start_wait(Address + I2C_WRITE);
   // Address of temp bytes
   i2c_write(0x07);
   // Read - the famous repeat start
   i2c_rep_start(Address + I2C_READ);
   // Read 1 byte and then send ack (x2)
   data_low = i2c_readAck();
   data_high = i2c_readAck();
   pec = i2c_readNak();
   i2c_stop();
   // This converts high and low bytes together and processes the temperature
   // MSB is a error bit and is ignored for temperatures
   // Zero out the data
   float temp = 0x0000;
   // This masks off the error bit of the high byte, then moves it left
   // 8 bits and adds the low byte.
   temp = (float)(((data_high & 0x007F) << 8) + data_low);
   temp = (temp * 0.02) - 273.16;
   Serial.print(temp);
   Serial.println(" C");
   return temp;
}

激光测距传感器    ATK-VL53l0X
#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox=Adafruit_VL53L0X();
void setup() {
  // put your setup code here, to run once:
lox.begin(0x29);
}
void loop() {
  // put your main code here, to run repeatedly:
}

I2C地址修改.zip

5.58 KB, 下载次数: 39, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:652725 发表于 2019-12-10 17:36 | 只看该作者
你好,这个i2cmaster.h的库文件哪里可以下载呀
回复

使用道具 举报

板凳
ID:669584 发表于 2019-12-21 02:54 | 只看该作者
你好,请问可以用在其他传感器上嘛?
回复

使用道具 举报

地板
ID:234938 发表于 2020-1-14 23:11 | 只看该作者
若能任意修改I2C地址,那在设计中绝对是非常有意义的,感谢楼主分享。
回复

使用道具 举报

5#
ID:209158 发表于 2020-3-23 19:30 | 只看该作者
Euler271828 发表于 2019-12-10 17:36
你好,这个i2cmaster.h的库文件哪里可以下载呀

这个在arduinoIDE里面管理库里面就有  搜索一下
回复

使用道具 举报

6#
ID:209158 发表于 2020-3-23 19:33 | 只看该作者
SuperCat995 发表于 2019-12-21 02:54
你好,请问可以用在其他传感器上嘛?

这个抱歉,我不清楚   但是那个查询IIC地址的可以,修改的那个程序如果有条件的话可以一试   
还有一个办法就是不同的设备例如MPU6050和OLED可采用不同对的普通引脚设置为SCL、SDA,从而避免对默认地址的修改
回复

使用道具 举报

7#
ID:209158 发表于 2020-3-23 19:34 | 只看该作者
碌碌无为 发表于 2020-1-14 23:11
若能任意修改I2C地址,那在设计中绝对是非常有意义的,感谢楼主分享。

这个需要对具体的设备来论,不同的IIC设备会有一个地址范围,范围内均可以,这方面可根据数据手册查找
回复

使用道具 举报

8#
ID:881733 发表于 2021-1-30 21:26 | 只看该作者
关于changeaddr那部分代码,我看到楼主你用到了NewAddr1和2,但是没有看到具体哪一步改动的地址那,NewAddr1时OX39,NewAddr2时ox00,是怎么修改5个设备的地址的那?还有一个HEX的变量也没有看到在哪里引用的那?"i2cmaster.h"里面也没有找到
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表