生活随笔
收集整理的这篇文章主要介绍了
第十四周温湿度传感器采集
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
目录
- 一、I2C总述
- 二、软件IIC与硬件IIC的区别
- 三、实现I2CAHT20温湿度传感器的数据采集
一、I2C总述
I2C通讯协议(Inter—Integrated Circuit)是由Philps公司开发的,由于它引脚少,硬件实现简单,可扩展性强,不需要USART、CAN等通讯协议的外部收发设备,现在被广泛地使用在系统内多个集成电路(IC)间的通讯。
在计算机科学里,大部分复杂的问题都可以通过分层来简化。如芯片被分为内核层和片上外设;STM32标准库则是在寄存器与用户代码之间的软件层。对于通讯协议,我们也以分层的方式来理解,最基本的是把它分为物理层和协议层。物理层规定通讯系统中具有机械、电子功能部分的特性,确保原始数据在物理媒体的传输。协议层主要规定通讯逻辑,统一收发双方的数据打包、解包标准。简单来说物理层规定我们用嘴巴还是用肢体来交流,协议层则规定我们用中文还是英文来交流。
二、软件IIC与硬件IIC的区别
硬件IIC:硬件I2C对应芯片上的I2C外设,有相应I2C驱动电路,其所使用的I2C管脚也是专用的
软件IIC:软件I2C一般是用GPIO管脚,用软件控制管脚状态以模拟I2C通信波形
区别在于,硬件IIC用法比较复杂,模拟IIC的流程更清楚一些;且硬件IIC速度比模拟快;但模拟IIC可以在任何管脚上,而硬件只能在固定管脚上
三、实现I2CAHT20温湿度传感器的数据采集
1、编写主函数
(1)检测AHT20是否采集到数据,如果采到就处理数据
(2)根据如下两个公式
c1 = AHT20.HT[0]10010/1024/1024; //湿度
t1 = AHT20.HT[1]20010/1024/1024-500;//温度计算公式
来计算得出温度。
(3)通过指示灯翻转标识正常工作
(4)AHT20函数中,有定义结构体,根据结构体来得到相关值
main函数如下
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include "stdio.h"
#include "delay.h"
#include "bsp_i2c.h"
#include "ATH20.h"void RCC_Configuration(void);
void GPIO_Configuration(void);GPIO_InitTypeDef GPIO_InitStructure
;#pragma import(__use_no_semihosting)
struct __FILE
{int handle
;};
FILE __stdout
;
_sys_exit(int x
)
{x
= x
;
}
int fputc(int ch
, FILE
*f
)
{while(USART_GetFlagStatus(USART1
,USART_FLAG_TC
)==RESET
);USART_SendData(USART1
,(uint8_t)ch
);return ch
;
}void uart_init(u32 bound
)
{GPIO_InitTypeDef GPIO_InitStructure
;USART_InitTypeDef USART_InitStructure
;RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
|RCC_APB2Periph_GPIOA
, ENABLE
); USART_DeInit(USART1
); GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_9
; GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
;GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_AF_PP
; GPIO_Init(GPIOA
, &GPIO_InitStructure
); GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_10
;GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_IN_FLOATING
;GPIO_Init(GPIOA
, &GPIO_InitStructure
); USART_InitStructure
.USART_BaudRate
= bound
;USART_InitStructure
.USART_WordLength
= USART_WordLength_8b
;USART_InitStructure
.USART_StopBits
= USART_StopBits_1
;USART_InitStructure
.USART_Parity
= USART_Parity_No
;USART_InitStructure
.USART_HardwareFlowControl
= USART_HardwareFlowControl_None
;USART_InitStructure
.USART_Mode
= USART_Mode_Rx
| USART_Mode_Tx
; USART_Init(USART1
, &USART_InitStructure
); USART_Cmd(USART1
, ENABLE
);
}int main(void)
{uint8_t ret
= 0;float P
,T
,ALT
;uint32_t CT_data
[2];int c1
,t1
;uint8_t LED_Stat
= 0;RCC_Configuration(); GPIO_Configuration(); I2C_Bus_Init();uart_init(115200);ret
= ATH20_Init();if(ret
== 0){printf("ATH20´«¸ÐÆ÷³õʼ»¯´íÎó\n");while(1);}while(1){while(ATH20_Read_Cal_Enable() == 0){ATH20_Init();SoftDelay_ms(30);}ATH20_Read_CTdata(CT_data
); c1
= CT_data
[0] * 1000 / 1024 / 1024; t1
= CT_data
[1] * 200 *10 / 1024 / 1024 - 500;printf("AHT20ÎÂʪ¶È¶ÁȡʵÑé:\n");printf("ζÈ: %d.%d ¡æ\n",(t1
/10),(t1
%10));printf("ʪ¶È: %d.%d %%\n",(c1
/10),(c1
%10));printf("\n\n");SoftDelay_ms(1000);}
}void RCC_Configuration(void)
{SystemInit();RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO
, ENABLE
);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
| RCC_APB2Periph_GPIOB
| RCC_APB2Periph_GPIOC
| RCC_APB2Periph_GPIOD
| RCC_APB2Periph_GPIOE
, ENABLE
);
}void GPIO_Configuration(void)
{GPIO_InitTypeDef GPIO_InitStructure
;GPIO_InitStructure
.GPIO_Pin
= GPIO_Pin_2
| GPIO_Pin_4
| GPIO_Pin_5
| GPIO_Pin_7
; GPIO_InitStructure
.GPIO_Mode
= GPIO_Mode_Out_PP
; GPIO_InitStructure
.GPIO_Speed
= GPIO_Speed_50MHz
; GPIO_Init(GPIOC
, &GPIO_InitStructure
);
}
然后烧入程序,结果如下
总结
以上是生活随笔为你收集整理的第十四周温湿度传感器采集的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。