大家好、
我遇到了无法在 TM4C 上初始化 I2C 的问题。 这是我的代码。 我想 i2c send 函数是可以的、但我认为我的问题是要初始化 i2c3_base。 我搜索了一些其他代码(示例或其他代码)、但无法使其为真。 请提供帮助。
#include
#include
#include
#include "inc/hw_i2c.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/gpio.h"
#include "driverlib/i2c.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
typedef 枚举 I2CResultTypes
{
ACK_Received、
ACK_ERROR
}I2CResultType;
typedef 枚举 I2CTypes
{
I2C0、
I2C1、
I2C2、
I2C3
}I2CType;
typedef 枚举 I2cRegisters
{
VREG_A = 0x11、
VREG_B、
NVREG_A = 0x21、
NVREG_B、
}I2c 注册;
typedef 枚举 I2cAddes
{
ADDRESS0 = 0x28、
地址1 = 0x29、
地址2 = 0x2A、
地址3 = 0x2B、
地址4 = 0x2C、
地址5 = 0x2D、
地址6 = 0x2E、
地址7 = 0x2F
}I2c 地址;
I2CResultType I2C_Send (I2CType i2cPort、unsigned char slaveAddress、unsigned char regAddress、unsigned char 数据)
{
INT 结果;
I2CMasterIntClear (i2cPort);
I2CMasterSlaveAddrSet (i2cPort、slaveAddress、0);
I2CMasterDataPut (i2cPort、regAddress);
I2CMasterControl (i2cPort、I2C_MASTER_CMD_BURST_SEND_FINISH);
while (I2CMasterBusy (i2cPort));
结果= I2CMasterErr (i2cPort);
if (结果!= I2C_MASTER_ERR_NONE)
{
返回 ACK_ERROR;
}
I2CMasterDataPut (i2cPort、data);
I2CMasterControl (i2cPort、I2C_MASTER_CMD_BURST_SEND_FINISH);
while (I2CMasterBusy (i2cPort));
结果= I2CMasterErr (i2cPort);
if (结果!= I2C_MASTER_ERR_NONE)
{
返回 ACK_ERROR;
}
返回 ACK_received;
}
void MAX5479_SetValue2 (无符号字符值)
{
I2CResultType 结果= ACK_ERROR;
I2CType I2C = I2C3;
I2cAddress 地址= 0x29;
I2c 寄存器 regAddress =0x11;
结果= I2C_Send (I2C3、0x29、0x11、值);
如果(结果= ACK_received)
{
asm ("NOP");
}
其他
{
asm ("NOP");
}
}
空 I2CInit (空)
{
SysCtlPeripheralEnable (SYSCTL_Periph_I2C3);
SysCtlPeripheralReset (SYSCTL_Periph_I2C3);
SysCtlPeripheralEnable (SYSCTL_Periph_GPIOG);
//SysCtlPeripheralReset (SYSCTL_Periph_GPIOG);
GPIOPinTypeI2CSCL (GPIO_PORTG_BASE、GPIO_PIN_0);
GPIOPinTypeI2C (GPIO_PORTG_base、GPIO_PIN_1);
GPIOPinConfigure (GPIO_PG0_I2C3SCL);
GPIOPinConfigure (GPIO_PG1_I2C3SDA);
I2CMasterInitExpClk (I2C3_base、SysCtlClockGet ()、false);
}
内部
main (空)
{
volatile uint32_t ui32Loop;
SysCtlClockSet (SYSCTL_SYSDIV_3 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_25MHz);
I2CInit();
SysCtlPeripheralEnable (SYSCTL_Periph_GPIOK);
//
//检查是否启用了外设访问。
//
while (!SysCtlPeripheralReady (SYSCTL_Periph_GPIOK))
{
}
GPIOPinTypeGPIOOutput (GPIO_PORTK_base、GPIO_PIN_3);
while (1)
{
MAX5479_SetValue2 (20);
}
}