1.TI
cpp
#include "DSP2833x_Device.h" // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h" // DSP2833x Examples Include File
#define SetData GpioDataRegs.GPADAT.bit.GPIO20 = 1
#define ClrData GpioDataRegs.GPADAT.bit.GPIO20 = 0
#define SetClk GpioDataRegs.GPADAT.bit.GPIO22 = 1
#define ClrClk GpioDataRegs.GPADAT.bit.GPIO22 = 0
#define LedReg (*((volatile Uint16 *)0x41FF))
Uint16 *ExRamStart = (Uint16 *)0x100000;
void InitI2C(void);
Uint16 ReadData(Uint16 *RamAddr, Uint16 RomAddress, Uint16 number);
Uint16 WriteData(Uint16 *Wdata, Uint16 RomAddress, Uint16 number);
Uint16 I2C_xrdy();
Uint16 I2C_rrdy();
Uint16 i;
void main(void)
{
Uint16 dat1[8]={0,0,0,0,0,0,0,0};
Uint16 dat[]={ 0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88};
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2833x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2833x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
InitXintf16Gpio(); //zq
InitI2CGpio();
InitI2C();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2833x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
LedReg = 0xFF;
WriteData(dat,0x60,8);
DELAY_US(10000);
ReadData(dat1,0x60,8);
for(; ;)
{
}
}
Uint16 WriteData(Uint16 *Wdata, Uint16 RomAddress, Uint16 number)
{
Uint16 i;
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
while(!I2C_xrdy());
I2caRegs.I2CSAR = 0x50;//Slave Address Register
I2caRegs.I2CCNT = number + 1;
I2caRegs.I2CDXR = RomAddress;//Data Transmitter Register
I2caRegs.I2CMDR.all = 0x6E20;//Mode Register
for (i=0; i<number; i++)
{
while(!I2C_xrdy());
I2caRegs.I2CDXR = *Wdata;
Wdata++;
if (I2caRegs.I2CSTR.bit.NACK == 1)//Status Register
return I2C_BUS_BUSY_ERROR;
}
return I2C_SUCCESS;
}
Uint16 ReadData(Uint16 *RamAddr, Uint16 RomAddress, Uint16 number)
{
Uint16 i,Temp;
if (I2caRegs.I2CSTR.bit.BB == 1)//bus busy
{
return I2C_BUS_BUSY_ERROR;
}
while(!I2C_xrdy());
I2caRegs.I2CSAR = 0x50;//Slave Address
I2caRegs.I2CCNT = 1;//number of data to transfer or receive
I2caRegs.I2CDXR = RomAddress;
I2caRegs.I2CMDR.all = 0x6620; //I2C Mode Register 0110 0110 0010 0000
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
DELAY_US(50);
while(!I2C_xrdy());
I2caRegs.I2CSAR = 0x50;
I2caRegs.I2CCNT = number;
I2caRegs.I2CMDR.all = 0x6C20;
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
for(i=0;i<number;i++)
{
while(!I2C_rrdy());
Temp = I2caRegs.I2CDRR;//Data Received Register 读取数据的16位寄存器
if (I2caRegs.I2CSTR.bit.NACK == 1)
return I2C_BUS_BUSY_ERROR;
*RamAddr = Temp;
RamAddr++;
}
return I2C_SUCCESS;
}
Uint16 I2C_xrdy()
{
Uint16 t;
t = I2caRegs.I2CSTR.bit.XRDY;//Transmit Data Ready
return t;
}
Uint16 I2C_rrdy()
{
Uint16 t;
t = I2caRegs.I2CSTR.bit.RRDY;//Receive Data Ready
return t;
}
//===========================================================================
// No more.
//===========================================================================
2.chatgpt
cpp
#include "F28335x_Device.h" // DSP28335 设备头文件
#include "F28335x_Examples.h" // DSP28335 示例头文件
#define I2C_SLAVE_ADDR 0x48 // 替换为你的设备地址
#define DATA_LENGTH 1 // 读取数据的长度
void InitI2C(void) {
// 初始化 I2C
EALLOW; // 允许修改保护寄存器
I2caRegs.I2CMDR.all = 0; // 清除所有控制位
I2caRegs.I2CPSC.all = 15; // 设定 I2C 时钟分频
I2caRegs.I2CCLKL = 10; // 设置 SCL 时钟高电平时间
I2caRegs.I2CCLKH = 10; // 设置 SCL 时钟低电平时间
I2caRegs.I2CMDR.bit.IRS = 1; // 启动 I2C
I2caRegs.I2CMDR.bit.FREE = 1; // 自由运行模式
EDIS; // 禁止修改保护寄存器
}
void I2CReadData(uint16_t regAddr, uint8_t* data, uint16_t length) {
// 发送读取命令
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; // 设置从设备地址
I2caRegs.I2CCNT = 1; // 发送寄存器地址的长度
I2caRegs.I2CMDR.bit.STT = 1; // 发送起始信号
while (I2caRegs.I2CMDR.bit.STT); // 等待起始信号发送完成
// 发送寄存器地址
I2caRegs.I2CDXR = regAddr; // 发送寄存器地址
while (I2caRegs.I2CSTR.bit.BB); // 等待总线空闲
// 发送重复起始信号
I2caRegs.I2CMDR.bit.STT = 1; // 发送起始信号
while (I2caRegs.I2CMDR.bit.STT); // 等待起始信号发送完成
// 设置读取操作
I2caRegs.I2CSAR = I2C_SLAVE_ADDR; // 设置从设备地址
I2caRegs.I2CCNT = length; // 读取数据的长度
I2caRegs.I2CMDR.bit.TRX = 1; // 设置为接收模式
I2caRegs.I2CMDR.bit.STT = 1; // 发送起始信号
while (I2caRegs.I2CMDR.bit.STT); // 等待起始信号发送完成
// 接收数据
for (uint16_t i = 0; i < length; i++) {
while (!I2caRegs.I2CSTR.bit.RRDY); // 等待接收就绪
data[i] = I2caRegs.I2CDRR; // 读取数据
}
// 发送停止信号
I2caRegs.I2CMDR.bit.STP = 1; // 发送停止信号
while (I2caRegs.I2CMDR.bit.STP); // 等待停止信号发送完成
}
void main(void) {
uint8_t receivedData[DATA_LENGTH];
// 系统初始化
InitSysCtrl();
DINT; // 禁止中断
// 初始化 I2C
InitI2C();
// 读取数据
I2CReadData(0x00, receivedData, DATA_LENGTH); // 假设从寄存器地址 0x00 读取数据
// 处理接收到的数据
while (1) {
// 在这里处理 receivedData
}
}
cpp
#include "DSP2833x_Device.h" // DSP28335 设备头文件
#include "DSP2833x_Examples.h" // DSP28335 示例头文件
// I2C 数据读取函数
void I2C_Init(void) {
// 1. 复位 I2C 模块
I2caRegs.I2CSAR = 0x50; // 设备地址(根据你的 I2C 设备修改)
I2caRegs.I2CMDR.all = 0; // 停止所有操作
I2caRegs.I2CPSC.all = 9; // 设定分频(10MHz / (1+9) = 1MHz)
I2caRegs.I2CCLKL = 10; // SCL 高电平时间
I2caRegs.I2CCLKH = 10; // SCL 低电平时间
I2caRegs.I2CMDR.bit.IRS = 1; // 启动 I2C 模块
I2caRegs.I2CMDR.bit.FREE = 1; // 自由模式
}
uint16_t I2C_ReadData(uint16_t regAddr) {
uint16_t data = 0;
// 2. 发送起始信号
I2caRegs.I2CMDR.bit.STT = 1; // 发送起始信号
while (I2caRegs.I2CSTR.bit.BB); // 等待总线空闲
// 3. 发送设备地址和写命令
I2caRegs.I2CSAR = 0x50; // 设备地址
I2caRegs.I2CDRR = regAddr; // 发送寄存器地址
I2caRegs.I2CMDR.bit.TRX = 1; // 设置为发送模式
// 4. 等待发送完成
while (!I2caRegs.I2CSTR.bit.XRDY); // 等待发送准备就绪
I2caRegs.I2CMDR.bit.STP = 1; // 发送停止信号
// 5. 发送起始信号以开始读取
I2caRegs.I2CMDR.bit.STT = 1; // 发送起始信号
while (I2caRegs.I2CSTR.bit.BB); // 等待总线空闲
// 6. 发送设备地址和读命令
I2caRegs.I2CSAR = 0x50; // 设备地址
I2caRegs.I2CMDR.bit.TRX = 0; // 设置为接收模式
// 7. 等待接收数据
while (!I2caRegs.I2CSTR.bit.RRDY); // 等待接收准备就绪
data = I2caRegs.I2CDRR; // 读取数据
// 8. 发送停止信号
I2caRegs.I2CMDR.bit.STP = 1; // 发送停止信号
return data; // 返回读取的数据
}
void main(void) {
InitSysCtrl(); // 初始化系统控制
// 初始化 I2C
I2C_Init();
// 读取数据示例
uint16_t regAddr = 0x01; // 需要读取的寄存器地址
uint16_t readData = I2C_ReadData(regAddr); // 从设备读取数据
// 在这里处理读取的数据
while (1); // 停止程序
}
dsp