主题中讨论的其他器件:LAUNCHXL-F28027F、C2000WARE、 ENERGIA
工具/软件:
有没有人可以帮助我使用 C2000 Piccolo 的 bmp 传感器在 i2c 接口上工作 Launchxl-f28027f 我已经尝试了许多方法没有得到请检查
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
工具/软件:
有没有人可以帮助我使用 C2000 Piccolo 的 bmp 传感器在 i2c 接口上工作 Launchxl-f28027f 我已经尝试了许多方法没有得到请检查
大家好、Aishwarya Rajesh
"我已经编写了使用 I2C 将 BMP280传感器与 TMS320F28027F 连接的逻辑、但我无法获得任何输出。 请看一下代码、让我知道可能有什么问题。"
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
#include <stdio.h> // For sprintf
#include <stdint.h> // For uint8_t, int16_t, etc.
// BMP280 Definitions
#define BMP280_ADDR 0x76 // BMP280 I2C address (0x76 if SDO is low, 0x77 if SDO is high)
#define BMP280_CALIB_DATA_SIZE 24
// BMP280 Calibration Data
uint16_t dig_T1;
int16_t dig_T2, dig_T3;
uint16_t dig_P1;
int16_t dig_P2, dig_P3, dig_P4, dig_P5, dig_P6, dig_P7, dig_P8, dig_P9;
// Global variable for fine temperature calculation
int32_t t_fine;
// Function Prototypes
void InitI2C(void);
void BMP280_Init(void);
void I2C_Write(uint8_t slave_addr, uint8_t reg_addr, uint8_t data);
void I2C_Read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);
void BMP280_ReadCalibrationData(void);
void BMP280_ReadData(float *temperature, float *pressure);
float compensate_T(int32_t adc_T);
float compensate_P(int32_t adc_P);
void InitSCI(void);
void SCI_Print(char *str);
// Main Function
void main(void)
{
float temperature, pressure;
char buffer[50]; // Buffer to store formatted strings
// Initialize system
InitSysCtrl(); // Initialize the system control
InitI2C(); // Initialize I2C for BMP280 communication
InitSCI(); // Initialize SCI for UART communication
// Test UART communication
SCI_Print("Hello, BMP280!\n");
// Initialize BMP280 sensor
BMP280_Init();
BMP280_ReadCalibrationData(); // Read calibration data from BMP280
while (1)
{
// Read temperature and pressure from BMP280
BMP280_ReadData(&temperature, &pressure);
// Format and send temperature data
sprintf(buffer, "Temperature: %.2f °C\n", temperature);
SCI_Print(buffer);
// Format and send pressure data
sprintf(buffer, "Pressure: %.2f Pa\n", pressure);
SCI_Print(buffer);
DELAY_US(1000000); // Delay for 1 second
}
}
// Initialize I2C module
void InitI2C(void)
{
// Enable I2C peripheral clock
SysCtrlRegs.PCLKCR0.bit.I2CAENCLK = 1;
// Configure I2C pins (GPIO32 as SDA and GPIO33 as SCL)
EALLOW;
GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for SDA (GPIO32)
GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for SCL (GPIO33)
GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // Configure GPIO32 as I2C SDA
GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // Configure GPIO33 as I2C SCL
EDIS;
// Initialize I2C
I2caRegs.I2CMDR.all = 0x0000; // Put I2C in reset state
I2caRegs.I2CPSC.all = 6; // Prescaler for 12.5 MHz SYSCLK
I2caRegs.I2CCLKL = 10; // Low time = 10 * I2CPSC
I2caRegs.I2CCLKH = 10; // High time = 10 * I2CPSC
I2caRegs.I2CMDR.all = 0x0020; // Master mode, 7-bit address, free data format
}
// Initialize BMP280 sensor
void BMP280_Init(void)
{
// Set oversampling and mode
I2C_Write(BMP280_ADDR, 0xF4, 0x27); // Control measurement register: x1 temp, x1 pressure, normal mode
I2C_Write(BMP280_ADDR, 0xF5, 0x00); // Configuration register: filter off, standby time 0.5ms
}
// Write to BMP280 register
void I2C_Write(uint8_t slave_addr, uint8_t reg_addr, uint8_t data)
{
I2caRegs.I2CSAR = slave_addr; // Set slave address
I2caRegs.I2CCNT = 2; // Set byte count (register address + data)
I2caRegs.I2CDXR = reg_addr; // Load register address
I2caRegs.I2CDXR = data; // Load data
I2caRegs.I2CMDR.all = 0x6E20; // Start condition, master transmitter mode
uint16_t timeout = 10000; // Timeout counter
while (I2caRegs.I2CSTR.bit.ARDY == 0 && timeout--); // Wait for stop condition
if (timeout == 0) {
SCI_Print("I2C Write Timeout!\n");
}
}
// Read from BMP280 register
void I2C_Read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8_t length)
{
I2caRegs.I2CSAR = slave_addr; // Set slave address
I2caRegs.I2CCNT = 1; // Set byte count for register address
I2caRegs.I2CDXR = reg_addr; // Load register address
I2caRegs.I2CMDR.all = 0x6E20; // Start condition, master transmitter mode
uint16_t timeout = 10000; // Timeout counter
while (I2caRegs.I2CSTR.bit.ARDY == 0 && timeout--); // Wait for stop condition
if (timeout == 0) {
SCI_Print("I2C Read Address Timeout!\n");
}
I2caRegs.I2CCNT = length; // Set byte count for data
I2caRegs.I2CMDR.all = 0x2C20; // Start condition, master receiver mode
unsigned char i; // Declare loop variable
for (i = 0; i < length; i++) {
timeout = 10000; // Reset timeout for each byte
while (I2caRegs.I2CSTR.bit.RRDY == 0 && timeout--); // Wait for receive ready
if (timeout == 0) {
SCI_Print("I2C Read Data Timeout!\n");
break;
}
data[i] = I2caRegs.I2CDRR; // Read data
}
}
// Read BMP280 calibration data
void BMP280_ReadCalibrationData(void)
{
uint8_t calib_data[BMP280_CALIB_DATA_SIZE];
I2C_Read(BMP280_ADDR, 0x88, calib_data, BMP280_CALIB_DATA_SIZE);
dig_T1 = (calib_data[1] << 8) | calib_data[0];
dig_T2 = (calib_data[3] << 8) | calib_data[2];
dig_T3 = (calib_data[5] << 8) | calib_data[4];
dig_P1 = (calib_data[7] << 8) | calib_data[6];
dig_P2 = (calib_data[9] << 8) | calib_data[8];
dig_P3 = (calib_data[11] << 8) | calib_data[10];
dig_P4 = (calib_data[13] << 8) | calib_data[12];
dig_P5 = (calib_data[15] << 8) | calib_data[14];
dig_P6 = (calib_data[17] << 8) | calib_data[16];
dig_P7 = (calib_data[19] << 8) | calib_data[18];
dig_P8 = (calib_data[21] << 8) | calib_data[20];
dig_P9 = (calib_data[23] << 8) | calib_data[22];
}
// Read temperature and pressure data from BMP280
void BMP280_ReadData(float *temperature, float *pressure)
{
uint8_t data[6];
I2C_Read(BMP280_ADDR, 0xF7, data, 6); // Read pressure and temperature data
int32_t adc_P = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4);
int32_t adc_T = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4);
*temperature = compensate_T(adc_T);
*pressure = compensate_P(adc_P);
}
// Temperature compensation formula
float compensate_T(int32_t adc_T)
{
int32_t var1, var2;
var1 = ((((adc_T >> 3) - ((int32_t)dig_T1 << 1))) * ((int32_t)dig_T2)) >> 11;
var2 = (((((adc_T >> 4) - ((int32_t)dig_T1)) * ((adc_T >> 4) - ((int32_t)dig_T1))) >> 12) * ((int32_t)dig_T3)) >> 14;
t_fine = var1 + var2; // Update t_fine for pressure compensation
return (t_fine * 5 + 128) >> 8; // Return temperature in °C
}
// Pressure compensation formula
float compensate_P(int32_t adc_P)
{
int64_t var1, var2, p;
var1 = ((int64_t)t_fine) - 128000;
var2 = var1 * var1 * (int64_t)dig_P6;
var2 = var2 + ((var1 * (int64_t)dig_P5) << 17);
var2 = var2 + ((int64_t)dig_P4 << 35);
var1 = ((var1 * var1 * (int64_t)dig_P3) >> 8) + ((var1 * (int64_t)dig_P2) << 12);
var1 = (((int64_t)1 << 47) + var1) * ((int64_t)dig_P1) >> 33;
if (var1 == 0) return 0; // Avoid division by zero
p = 1048576 - adc_P;
p = (((p << 31) - var2) * 3125) / var1;
var1 = ((int64_t)dig_P9 * (p >> 13) * (p >> 13)) >> 25;
var2 = ((int64_t)dig_P8 * p) >> 19;
p = ((p + var1 + var2) >> 8) + ((int64_t)dig_P7 << 4);
return (float)p / 256.0; // Return pressure in Pa
}
// Initialize SCI for UART communication
void InitSCI(void)
{
// Enable SCI peripheral clock
SysCtrlRegs.PCLKCR0.bit.SCIAENCLK = 1;
// Configure SCI pins
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // Enable pull-up for SCITXDA (GPIO28)
GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0; // Enable pull-up for SCIRXDA (GPIO29)
GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1; // Configure GPIO28 as SCITXDA
GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1; // Configure GPIO29 as SCIRXDA
EDIS;
// Initialize SCI
SciaRegs.SCICCR.all = 0x0007; // 1 stop bit, no parity, 8-bit data, idle-line protocol
SciaRegs.SCICTL1.all = 0x0003; // Enable TX and RX, internal SCICLK
SciaRegs.SCIHBAUD = 0x0001; // Baud rate = 9600 (for 12.5 MHz SYSCLK)
SciaRegs.SCILBAUD = 0x00E7;
SciaRegs.SCICTL1.all = 0x0023; // Relinquish SCI from reset
}
// Print a string via SCI
void SCI_Print(char *str)
{
while (*str) {
while (SciaRegs.SCICTL2.bit.TXRDY == 0); // Wait for TX ready
SciaRegs.SCITXBUF = *str++; // Send character
}
}
Kishor、
虽然我无法查看代码、但请告诉我您是否 在 GPIO 上看到任何输出、或者根本不看到任何输出? 如果引脚上没有输出、请查看 GPIO 配置以及 MCU 与 EEPROM 之间的物理连接。 如果有输出、那么我们可以进一步查看代码配置。 请指出代码的特定部分可能存在问题。
以下 是一个 相关的主题: (+) TMS320F28027F:I2C 在不收到命令的情况下设置停止条件- C2000微控制器论坛- C2000︎ 微控制器- TI E2E 支持论坛。 论坛上还有其他 E2E 可能 会有所帮助。
此致、
Aishwarya
Kishor、
您能否确认在示波器上是否看到任何输出并分享所看到的内容。 我不清楚这一点。 此外、确保您有一个阻值约为2.2k Ω 的上拉电阻将 SDA 和 SCL 连接到 VDDIO。 有关更多信息、也请参阅此应用手册: 使用 C2000 I2C 模块连接 EEPROM
还请给我1-2天时间来进一步研究这个问题、并返回给您。
此致、
Aishwarya
Kishor、
请尝试参考这些主题、如果您有任何其他问题、请告诉我:
CCS/LAUNCHXL-F28027:如何在 LAUNCHXL-F28027和 MCP9808之间建立 I2C 通信?
此致、
Aishwarya