为了扩展我对 Atmega 系列编程的理解和当前知识,我开始了自己的小项目。我决定通过直接寻址它的寄存器来对 MPU6050 I2C 传感器进行编程。为了测试传感器的工作和连接,我首先使用了这个预先编写的程序(使用 Arduino 平台提供的 Wire 库):
#include<Wire.h>
const int MPU=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,12,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
Serial.print("Accelerometer: ");
Serial.print("X = "); Serial.print(AcX);
Serial.print(" | Y = "); Serial.print(AcY);
Serial.print(" | Z = "); Serial.println(AcZ);
Serial.print("Gyroscope: ");
Serial.print("X = "); Serial.print(GyX);
Serial.print(" | Y = "); Serial.print(GyY);
Serial.print(" | Z = "); Serial.println(GyZ);
Serial.println(" ");
delay(333);
}
串行监视器显示传感器的正确读数,说明传感器连接正确并正常工作。同时我连接了我的总线盗版,它嗅探 I2C 总线并显示在传感器和 atmega 芯片(Atmega328p-pu)之间发送的十六进制值。虽然我无法将它们翻译成它们的确切含义,但交流看起来很正常。
在我上传了自己的代码后,使用传感器和 Atmega328p 的数据表创建,I2C 总线几乎没有显示任何内容。这是我的代码:
#include <Arduino.h>
#define DEBUG_LED_PIN PB5
#define DEBUG_LED_BANK DDRB
#define MPU6050_ADDR_WRITE 0x68
#define MPU6050_ADDR_READ 0x69
const uint8_t led_on = 0x01;
const uint8_t led_off = 0x00;
const uint8_t twi_write = 0x00;
const uint8_t twi_read = 0x01;
const uint8_t twi_start_transmitted = 0x08;
const uint8_t twi_repeated_start_transmitted = 0x10;
const uint8_t twi_ack_received_addr = 0x18;
const uint8_t twi_not_ack_received_addr = 0x20;
const uint8_t twi_ack_received_data = 0x28;
const uint8_t twi_not_ack_received_data = 0x30;
const uint8_t twi_arbitration_lost = 0x38;
void setup_debug_led();
void setup_twi();
void setup_mpu6050();
void control_debug_led(uint8_t status);
void send_data_to_device(uint8_t device_address, uint8_t* data, uint8_t data_length);
void send_start_signal();
void send_device_address(uint8_t device_address);
void send_data(uint8_t data);
void send_stop_signal();
void send_test_data_complete_sequence();
void setup() {
setup_debug_led();
control_debug_led(led_off);
setup_twi(); // Setup i2c protocol
}
void loop() {
// Test stuff
send_test_data_complete_sequence();
}
void setup_twi()
{
TWSR = 0;
TWCR = 0;
TWBR = 0x0C; // Set SCL to 400kHz
}
void send_test_data_complete_sequence()
{
// 1.
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Write start condition
// 2.
while(!(TWCR & (1 << TWINT))); // Wait for TWINT flag set. Indicates START has been transmitted
// 3.
if((TWSR & 0xF8) != twi_start_transmitted) // Check value TWI status register, if equals 0x08 continue
{
// Handle exception call
}
// 4.
TWDR = 0b11010000; // Load slave address including r/w bit
// 5.
TWCR = (1 << TWINT) | (1 << TWEN); // Clear TWINT bit to start tranmission
// 6.
while(!(TWCR & (1 << TWINT))); // Wait for TWINT flag set. Inidicates SLA+W has been transmitted
// 7.
if((TWSR & 0xF8) != twi_ack_received_addr) // Check for 0x18 (SLA+W ACK has been received)
{
// Handle exception call
}
// 8.
TWDR = 0x6B; // Load data into TWDR
// 9.
TWCR = (1 << TWINT) | (1 << TWEN); // Clear TWINT bit to start data tranmission
// 10.
while(!(TWCR & (1 << TWINT))); // Wait for TWINT flag set. Inidicates data has been transmitted
// 11.
if((TWSR & 0xF8) != twi_ack_received_data) // Check for 0x28 (data ACK has been received)
{
// Handle exception
}
// 12.
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); // Write stop condition
}
总线盗版在通用总线上显示以下通信数据:
[][[][][[[]]
与我传输到缓冲区的数据完全不同。如果我等待足够长的时间,我会发现一些偶然的 0x0 或 0x00。
我尝试更改从设备的地址和写入传感器的第一个数据字节。步骤 3、7 和 11 的异常永远不会被触发。
此代码中未触及引脚 A4 和 A5(SCL 和 SDA)。我尝试将它们配置为启用上拉电阻的输入,但这也没有给出预期的结果。
传感器连接到 arduino,如下图所示:
(Arduino) (MPU6050)
A4 (SDA) -> SDA
A5 (SCL) -> SCL
VCC -> VCC
GND -> GND
GND -> AD0
总线盗版与 SDA 和 SCL 连接并行连接以拦截其数据。
Atmega328p 的数据表简要总结了完成 I2C 传输所需的所有步骤。我尝试正确复制这些步骤(第 21.6 节“使用 TWI”,表 21-2)。
我希望你们能发现我的错误或丢失的代码。提前非常感谢!