Stm32F103 Hal库硬件iic 访问AM2320温湿度传感器
管脚配置 PB6 PB7
之前用库函数,一直读不到,换hal库后正常了,因为要在android板上调试,但是板卡没来,只好用stm32 先调试下。
很简单,先初始化i2C1
i2c.c
I2C_HandleTypeDef hi2c1;/* I2C2 init function */
void MX_I2C1_Init(void)
{hi2c1.Instance = I2C1;hi2c1.Init.ClockSpeed = 100000;hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;hi2c1.Init.OwnAddress1 = 0;hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;hi2c1.Init.OwnAddress2 = 0;hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;if (HAL_I2C_Init(&hi2c1) != HAL_OK){//_Error_Handler(__FILE__, __LINE__);}
}void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{GPIO_InitTypeDef GPIO_InitStruct;if(i2cHandle->Instance==I2C1){/* USER CODE BEGIN I2C2_MspInit 0 */__HAL_RCC_I2C1_CLK_ENABLE(); //默认情况下这句时钟初始化代码放在了GPIO初始化之后,需要调整到这里,即GPIO初始化前/* USER CODE END I2C2_MspInit 0 */__HAL_RCC_GPIOB_CLK_ENABLE();/**I2C2 GPIO Configuration PB6 ------> I2C2_SCLPB7 ------> I2C2_SDA */GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);/* I2C2 clock enable */// __HAL_RCC_I2C2_CLK_ENABLE();/* USER CODE BEGIN I2C2_MspInit 1 *//* USER CODE END I2C2_MspInit 1 */}
}void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{if(i2cHandle->Instance==I2C1){/* USER CODE BEGIN I2C2_MspDeInit 0 *//* USER CODE END I2C2_MspDeInit 0 *//* Peripheral clock disable */__HAL_RCC_I2C1_CLK_DISABLE();/**I2C2 GPIO Configuration PB6 ------> I2C2_SCLPB7 ------> I2C2_SDA */HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6|GPIO_PIN_7);/* USER CODE BEGIN I2C2_MspDeInit 1 *//* USER CODE END I2C2_MspDeInit 1 */}
}
i2c.h
#ifndef __i2c_H
#define __i2c_H
#ifdef __cplusplusextern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "main.h"/* USER CODE BEGIN Includes *//* USER CODE END Includes */extern I2C_HandleTypeDef hi2c1;/* USER CODE BEGIN Private defines *//* USER CODE END Private defines *///extern void _Error_Handler(char *, int);void MX_I2C1_Init(void);/* USER CODE BEGIN Prototypes *//* USER CODE END Prototypes */#ifdef __cplusplus
}
#endif
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "exti.h"
#include "i2c.h"u8 read = 0;
u8 write[8] = {0x56,0x57,0x58,0x59,0x60,0x61,0x62,0x63};#define AM2320_ADDRESS 0xB8uint8_t IIC_TX_Buffer[]={0x03,0x00,0x04};
uint8_t IIC_RX_Buffer[12];
int16_t pTemperature;
uint16_t pHumidity;void dataPrint(double a, double b)
{printf("humidity : %.1f Temp:%.1f\n\r", a,b);printf("\n\r");
}int main(void)
{HAL_Init(); Stm32_Clock_Init(RCC_PLL_MUL9); delay_init(72); uart_init(115200); LED_Init(); KEY_Init(); EXTI_Init();MX_I2C1_Init();//HAL_I2C_Mem_Write(&hi2c1,0xA0,248,I2C_MEMADD_SIZE_8BIT,write,8,1000);//delay_ms(5);//HAL_I2C_Mem_Read(&hi2c1,0xA1,255,I2C_MEMADD_SIZE_8BIT,&read,1,100);//HAL_I2C_Mem_Write(&hi2c1,0xa0,254,I2C_MEMADD_SIZE_8BIT,&write,8,100);//HAL_I2C_Mem_Read(&hi2c1,0xA1,254,I2C_MEMADD_SIZE_8BIT,&read,8,100);HAL_I2C_Master_Transmit(&hi2c1,AM2320_ADDRESS,0,0,10); //唤醒delay_ms(3);//写温度 湿度指令,对照规格书HAL_I2C_Master_Transmit(&hi2c1,AM2320_ADDRESS,IIC_TX_Buffer,3,10);delay_ms(3);//读温湿度HAL_I2C_Master_Receive(&hi2c1,AM2320_ADDRESS,IIC_RX_Buffer,8,10);//根据规格书计算温湿度值pHumidity =((IIC_RX_Buffer[2] << 8) | IIC_RX_Buffer[3] )/10.0;pTemperature =((IIC_RX_Buffer[4] << 8) | IIC_RX_Buffer[5])/10.0; while(1){delay_ms(1000); //打印 dataPrint(pHumidity,pTemperature);}
}
其他初始化,找个正点原子的hal库模板工程就行了。