|
新手求助!!
为什么发送没输出,是哪里存在问题吗!!!
是按照例程弄得程序~
求大神纠正~
#define 7708_ADDR 0x42
#define REG_ADDR1 0x0A
#define I2C_PORT GPIO_PB
#define I2C_SCL GPIO_PIN_7
#define I2C_SDA GPIO_PIN_8
void IIC_Init(void)
{
CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}};
CKCUClock.Bit.I2C0 = 1;
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.PA =1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
GPIO_OpenDrainConfig(HT_GPIOB, I2C_SCL, ENABLE);
GPIO_OpenDrainConfig(HT_GPIOB, I2C_SDA, ENABLE);
/* Configure GPIO to I2C mode for Master */
AFIO_GPxConfig(I2C_PORT, I2C_SCL, AFIO_FUN_I2C);
AFIO_GPxConfig(I2C_PORT, I2C_SDA, AFIO_FUN_I2C);
/* I2C Master configuration */
/* !!! NOTICE !!!
Notice that the local variable (structure) did not have an initial value.
Please confirm that there are no missing members in the parameter settings below in this function.
*/
I2C_InitTypeDef I2C_InitStructure;
I2C_DeInit(HT_I2C0);
I2C_InitStructure.I2C_GeneralCall = I2C_GENERALCALL_ENABLE;
I2C_InitStructure.I2C_AddressingMode = I2C_ADDRESSING_7BIT;
I2C_InitStructure.I2C_Acknowledge = I2C_ACK_DISABLE;
I2C_InitStructure.I2C_OwnAddress = 0x00;
I2C_InitStructure.I2C_Speed = 1000000;
I2C_InitStructure.I2C_SpeedOffset = 0;
I2C_Init(HT_I2C0, &I2C_InitStructure);
/* Enable I2C */
I2C_Cmd(HT_I2C0, ENABLE);
}
//发送器件地址
void I2C_Ackpolling(void)
{
while(I2C_GetFlagStatus(HT_I2C0, I2C_FLAG_BUSBUSY));
I2C_TargetAddressConfig(HT_I2C0, 7708_ADDR, I2C_MASTER_WRITE);
while(!I2C_GetFlagStatus(HT_I2C0, I2C_FLAG_ADRS));
}
void I2C_Send(uint8_t reg_addr, uint8_t *data, uint8_t length)
{
I2C_Ackpolling();
while(!I2C_CheckStatus(HT_I2C0, I2C_MASTER_TX_EMPTY));
I2C_SendData(HT_I2C0, reg_addr);
while(0 != length)
{
while(I2C_CheckStatus(HT_I2C0, I2C_MASTER_TX_EMPTY));
I2C_SendData(HT_I2C0, *data);
length--;
}
I2C_GenerateSTOP(HT_I2C0);
}
uint8_t I2C_Recv(void)
{
uint8_t data;
I2C_TargetAddressConfig(HT_I2C0, 7708_ADDR, I2C_MASTER_READ);
while(!I2C_CheckStatus(HT_I2C0, I2C_MASTER_RECEIVER_MODE));
while (!I2C_CheckStatus(HT_I2C0, I2C_MASTER_TX_EMPTY));
I2C_SendData(HT_I2C0, REG_ADDR1);
I2C_TargetAddressConfig(HT_I2C0, 7708_ADDR, I2C_MASTER_READ);
while(!I2C_CheckStatus(HT_I2C0, I2C_MASTER_RECEIVER_MODE));
I2C_AckCmd(HT_I2C0, ENABLE);
while(!I2C_CheckStatus(HT_I2C0, I2C_MASTER_RX_NOT_EMPTY));
data = I2C_ReceiveData(HT_I2C0);
return data;
}
|
上一篇:HOLTEK推出HT32F49365高性能Arm Cortex M4 32-bit单片机下一篇:有单片机教程吗?
|