工具与软件:
您好-我正在尝试通过我的 Arduino 上的 i2c 与 MCT8316A 通信、但无法正常工作。 我正在使用数据表(https://download.mikroe.com/documents/datasheets/MCT8316A_datasheet.pdf)中的一个示例、以及我在网上找到的部分代码、但运气差。 下面的代码以及引用数据表的器件添加了注释、以便您了解位值为什么是什么值。
#include <Wire.h>
const int deviceAddress = 0x00;
void setup() {
int error, n;
Wire.begin();
Wire.setClock(50000); // 50kHz, half the normal speed
Serial.begin(115200);
while(!Serial);
Serial.println("Starting");
// ------------------------------------------------
// Write data to a register
// ------------------------------------------------
const byte control_word1[] = {0x00, 0x50, 0x00, 0x80}; // <--- using example from pg 87 of data sheet
const byte data1[] = {0xCD, 0xAB, 0x34, 0x12}; // <-- same example as above from data sheet
Wire.beginTransmission(deviceAddress);
Wire.write(control_word1, 4);
Wire.write(data1, 4);
error = Wire.endTransmission();
if(error == 0)
{
Serial.println("Data succesfully sent");
}
else
{
Serial.println("Data not sent");
}
delay(500); // are delays needed?
// ------------------------------------------------
// Read data from a register
// ------------------------------------------------
const byte control_word2[] = {0x00, 0xD0, 0x00, 0x80}; // <--- reading based on example from page 88
Wire.beginTransmission(deviceAddress);
Wire.write(control_word2, 4);
error = Wire.endTransmission(false);
if(error == 0)
{
Serial.println("Register succesfully selected");
}
delay(500); // are delays needed?
n = Wire.requestFrom(deviceAddress, 8);
Serial.println(n);
if(n == 4)
{
Serial.println("Data received");
for( int i=0; i<4; i++)
{
Serial.print("0x");
Serial.print(Wire.read(), HEX);
Serial.print(", ");
}
Serial.println();
}
delay(500); // are delays needed?
}
我得到的输出为:
启动
未发送数据
已成功选择寄存器
0
非常感谢您提供任何帮助