请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
器件型号:TMS320F28027 大家好!
我正在尝试将 VL53L0x 测距传感器与 tms320f28027连接。在调试模式下运行后、我们应该在表达式窗口中看到结果为(x)=device_id、值应该为"\xee"。但我得到的结果是"\x00"。
代码:
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
//
// Note: I2C Macros used in this example can be found in the
// f2802x_I2C_defines.h file
//
#define VL53L0X_EXPECTED_DEVICE_ID (0xEE)
#define REG_IDENTIFICATION_MODEL_ID (0xC0)
//
// Function Prototypes
//
void I2CA_Init(void);
bool i2c_write_byte(uint8_t byte);
bool i2c_read_byte(uint8_t *received_byte);
bool i2c_read_byte(uint8_t *received_byte);
bool i2c_read_addr16_data8(uint8_t addr, uint8_t *data);
static bool device_is_booted();
// Main
uint8_t registervalue=0;
void main(void)
{
//
// WARNING: Always ensure you call memcpy before running any functions from
// RAM InitSysCtrl includes a call to a RAM based function and without a
// call to memcpy first, the processor will go "into the weeds"
#ifdef _FLASH
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif
InitSysCtrl();
InitI2CGpio();
DINT;
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
InitPieVectTable();
I2CA_Init();
device_is_booted();
}
// I2CA_Init -
//
void
I2CA_Init(void)
{
//
// Initialize I2C
//
I2caRegs.I2CSAR = 0x52; // Slave address - EEPROM control code
//
// I2CCLK = SYSCLK/(I2CPSC+1)
//
/*#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
#endif*/
#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts
//
// Take I2C out of reset. Stop I2C when suspended
//
I2caRegs.I2CMDR.all = 0x0020;
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
return;
}
bool i2c_write_byte(uint8_t byte)
{
bool success = false;
// Set up master as transmitter and send start condition
I2caRegs.I2CSAR =0X52; // Set slave address
I2caRegs.I2CCNT = 1; // Transmit one byte
I2caRegs.I2CMDR.bit.TRX = 1; // Set transmitter mode
I2caRegs.I2CMDR.bit.MST = 1; // Send start condition
// Fill the transmit buffer with the byte to be sent
I2caRegs.I2CDXR = byte;
// Wait for start condition to be sent
while (I2caRegs.I2CSTR.bit.BB == 1);
// Check for NACK error flag
success = !(I2caRegs.I2CSTR.bit.NACK);
if (success) {
// Wait for byte to be sent
while (I2caRegs.I2CSTR.bit.XRDY);
// Check for NACK error flag
success = !(I2caRegs.I2CSTR.bit.NACK);
}
// Send stop condition
I2caRegs.I2CMDR.bit.STP = 1;
// Wait for stop condition to be sent
while (I2caRegs.I2CMDR.bit.STP == 1);
// Check for NACK error flag
success = !(I2caRegs.I2CSTR.bit.NACK);
return success;
}
bool i2c_read_byte(uint8_t *received_byte)
{
bool success = false;
// Set up master as receiver and send start condition
I2caRegs.I2CSAR = 0x52; // Set slave address
I2caRegs.I2CCNT = 1; // Receive one byte
I2caRegs.I2CMDR.bit.TRX = 0; // Set receiver mode
I2caRegs.I2CMDR.bit.MST = 1; // Send start condition
// Wait for start condition to be sent
while (I2caRegs.I2CSTR.bit.BB == 1);
// Check for NACK error flag
success = !(I2caRegs.I2CSTR.bit.NACK);
// Send stop condition because we only want to read one byte
I2caRegs.I2CMDR.bit.STP = 1;
// Wait for stop condition to be sent
while (I2caRegs.I2CMDR.bit.STP == 1);
// Check for NACK error flag
success &= !(I2caRegs.I2CSTR.bit.NACK);
if (success) {
// Wait for byte before reading the buffer
while (I2caRegs.I2CSTR.bit.RRDY == 0);
// Read received byte
*received_byte = (uint8_t)I2caRegs.I2CDRR;
// Clear RX interrupt flag
}
return success;
}
bool i2c_read_addr8_data8(uint8_t addr, uint8_t *data)
{ bool success = false;
// Configure master for transmitting bytes
I2caRegs.I2CCNT = 1;
I2caRegs.I2CDXR = addr;
I2caRegs.I2CMDR.all = 0x6E20;
while (I2caRegs.I2CSTR.bit.BB == 1);
// Reconfigure master for reading bytes
I2caRegs.I2CCNT = 1;
I2caRegs.I2CMDR.all = 0x2620;
while (I2caRegs.I2CSTR.bit.BB == 1);
// Read the byte (register value)
if (I2caRegs.I2CSTR.bit.NACK == 0 && I2caRegs.I2CSTR.bit.RRDY == 1) {
uint8_t registervalue = I2caRegs.I2CDRR;
}
return success;
}
static bool device_is_booted()
{
uint8_t device_id = 0;
if (!i2c_read_addr8_data8(REG_IDENTIFICATION_MODEL_ID, &device_id)) {
return false;
}
return device_id == VL53L0X_EXPECTED_DEVICE_ID;
}