在G6U6封装中,IIC引脚为PB6、PB7;需求驱动AS5600磁编码器;
IIC初始化代码如下:
void AS5600_Init(void) { GPIO_InitTypeDef GPIO_InitStructure={0}; I2C_InitTypeDef I2C_InitTSturcture={0}; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//按手册要求应该是AF_OD,但是设置成AF_OD就一点输出都没有 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOB, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOB, &GPIO_InitStructure ); I2C_InitTSturcture.I2C_ClockSpeed = 100 * 1000; I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C; I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9; I2C_InitTSturcture.I2C_OwnAddress1 = 0x00; I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable; I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_Init( I2C1, &I2C_InitTSturcture ); I2C_Cmd( I2C1, ENABLE ); }
但是使用逻辑分析仪并不能采集到完整的IIC信号波形;在V307和V203的示例程序中,IIC的使用均是PB8、PB9的重映射,而G6U6刚好没有PB9引脚;
以下是逻辑分析仪100MHZ采集到的数据,从RESET开始:SDA信号并不完整
以下是获取数据代码:
uint16_t AS5600_ReadRawAngle(void) { uint16_t Data; I2C_GenerateSTART(I2C1, ENABLE); //Start_Event AS5600_WaitEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); //Event_5 I2C_Send7bitAddress(I2C1, AS5600_ADDR, I2C_Direction_Transmitter); //Fram_Head AS5600_WaitEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED); //Event_9 I2C_SendData(I2C1, 0x0C); //addr AS5600_WaitEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED); //Event_6 I2C_GenerateSTART(I2C1, ENABLE); //ReStart AS5600_WaitEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT); //Event_5 I2C_Send7bitAddress(I2C1, AS5600_ADDR, I2C_Direction_Receiver); //Fram_Head AS5600_WaitEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED); //Event_6 AS5600_WaitEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED); //Event_7 Data = I2C_ReceiveData(I2C1); //Read_Data Data = Data << 8; I2C_AcknowledgeConfig(I2C1, ENABLE); //Non_ACK I2C_GenerateSTOP(I2C1, ENABLE); //Stop_Event Data |= I2C_ReceiveData(I2C1); //Read_Data I2C_AcknowledgeConfig(I2C1, ENABLE); //ACK return Data;
热门产品 :
CH32L103: 32位PDUSB低功耗单片机