完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
管脚配置 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_SCL PB7 ------> 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_SCL PB7 ------> 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 __cplusplus extern "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 0xB8 uint8_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:%.1fnr", a,b); printf("nr"); } 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库模板工程就行了。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1758 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1613 浏览 1 评论
1055 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
721 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1670 浏览 2 评论
1932浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
723浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
564浏览 3评论
590浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
548浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 09:37 , Processed in 4.213732 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号