生活随笔
收集整理的這篇文章主要介紹了
第十四周温湿度传感器采集
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- 一、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′??D?÷3?ê??ˉ′í?ó\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??êa?è?áè?êμ?é:\n");printf("???è: %d.%d ??\n",(t1
/10),(t1
%10));printf("êa?è: %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
);
}
然后燒入程序,結果如下
總結
以上是生活随笔為你收集整理的第十四周温湿度传感器采集的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。