问题卡了很久 求大神帮看下
最近用做i2c温度传感器的程序 发现用标准库一切正常,但用HAL库在初始化i2c后,SR2寄存器一直读忙 BUSY位被置位 导致后面TMP_init()程序发送起始信号都不能成功
标准库参考了这份:
http://wenku.baidu.com/view/20d7b0d1c1c708a1284a44bd.html?re=view
[mw_shl_code=applescript,true]/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef I2cHandle;
/*************************************************
*************************************************/
void I2C_InitTmp(void)
{
int ret;
I2cHandle.Instance = I2C1;
I2cHandle.Init.ClockSpeed = 400000;
I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2;
I2cHandle.Init.OwnAddress1 = 0x0A;
I2cHandle.Init.OwnAddress2 = 0x0;
I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED;
I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED;
HAL_I2C_Init(&I2cHandle);
delay_ms(500);
ret = HAL_I2C_IsDeviceReady(&I2cHandle, 0x90, 5, 10000); <---- 一直读忙
printf("ret = %d
", ret);
TMP_init();
TMP_Get();
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(hi2c->Instance==I2C1)
{
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
/* Peripheral clock enable */
__HAL_RCC_I2C1_CLK_ENABLE(); <---------- 开启时钟后SR一直读忙
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}[/mw_shl_code]
而我又用标准库做了对比分析 main() 函数先后调用I2C_Configuration()和I2C_InitTmp()函数 初始化i2c都能正常
程序执行到TMP_I2C_CLK_INIT(TMP_I2C_CLK, ENABLE)时SR2判忙位置位;
执行完GPIO_Init(TMP_I2C_SDA_GPIO_PORT, &GPIO_InitStructure)后SR2的BUSY判忙位清零,后续i2c收发数据正常
[mw_shl_code=applescript,true]#include "stm32f4xx.h"
#include "./usart/bsp_usart.h"
#include "./tmp101/temp101.h"
uint8_t I2c_Buf[3]="AB0";
static __IO uint32_t I2CTimeout = I2CT_LONG_TIMEOUT;
static uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode);
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*!< TMP_I2C Periph clock enable */
TMP_I2C_CLK_INIT(TMP_I2C_CLK, ENABLE); <---- 开启时钟后读忙
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOB,ENABLE);
/*!< GPIO configuration */
/* Connect PXx to I2C_SCL*/
GPIO_PinAFConfig(TMP_I2C_SCL_GPIO_PORT, TMP_I2C_SCL_SOURCE, TMP_I2C_SCL_AF);
/* Connect PXx to I2C_SDA*/
GPIO_PinAFConfig(TMP_I2C_SDA_GPIO_PORT, TMP_I2C_SDA_SOURCE, TMP_I2C_SDA_AF);
/* PB6,7 SCL and SDA */
GPIO_InitStructure.GPIO_Pin = TMP_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(TMP_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = TMP_I2C_SDA_PIN;
GPIO_Init(TMP_I2C_SDA_GPIO_PORT, &GPIO_InitStructure); <---- 初始化IO口后读忙位清零
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x0A;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;
I2C_Init(TMP_I2C, &I2C_InitStructure);
I2C_Cmd(TMP_I2C, ENABLE);
I2C_AcknowledgeConfig(TMP_I2C, ENABLE);
}
[/mw_shl_code]
[mw_shl_code=applescript,true]#ifndef __TMP101_H
#define __TMP101_H
#define TMP_I2C I2C1
#define TMP_I2C_CLK RCC_APB1Periph_I2C1
#define TMP_I2C_CLK_INIT RCC_APB1PeriphClockCmd
#define TMP_I2C_SCL_PIN GPIO_Pin_6
#define TMP_I2C_SCL_GPIO_PORT GPIOB
#define TMP_I2C_SCL_GPIO_CLK RCC_AHB1Periph_GPIOB
#define TMP_I2C_SCL_SOURCE GPIO_PinSource6
#define TMP_I2C_SCL_AF GPIO_AF_I2C1
#define TMP_I2C_SDA_PIN GPIO_Pin_7
#define TMP_I2C_SDA_GPIO_PORT GPIOB
#define TMP_I2C_SDA_GPIO_CLK RCC_AHB1Periph_GPIOB
#define TMP_I2C_SDA_SOURCE GPIO_PinSource7
#define TMP_I2C_SDA_AF GPIO_AF_I2C1
#define I2CT_FLAG_TIMEOUT ((uint32_t)0x2000)
#define I2CT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
void I2C_Configuration(void);
void I2C_Test(void);
void I2C_InitTmp(void);
#endif[/mw_shl_code]
一周热门 更多>