|
由于網(wǎng)上的PM2.5測試儀質(zhì)量不一,多數(shù)測試儀傳感器只能定性,而不能精確定量,而且價格還很貴,所以在家沒事做了個PM2.5測試儀,帶溫度顯示
配件:
1、空氣質(zhì)量傳感器為攀騰第七代PMS7003傳感器
2、溫濕度傳感器為SHT20
3、單片機STM32F103C8T6
屏幕各界面顯示數(shù)據(jù):空氣質(zhì)量指數(shù),PM2.5.PM0.3.PM0.5.PM1.0.PM2.5.PM10顆粒數(shù),含量以及溫濕度
特別提示:代碼我只上傳了攀藤7003傳感器的官方測試代碼,希望各位電子愛好者勇于探索,功到才能自然成。
TB2ArtEiAqvpuFjSZFhXXaOgXXa_!!0-fleamarket.jpg_728x728.jpg (47.23 KB, 下載次數(shù): 155)
下載附件
2017-7-20 13:39 上傳
TB2FJ_Kg3NlpuFjy0FfXXX3CpXa_!!0-fleamarket.jpg_728x728.jpg (23.77 KB, 下載次數(shù): 146)
下載附件
2017-7-20 13:39 上傳
TB2MjdXXr_0UKFjy1XaXXbKfXXa_!!0-fleamarket.jpg_728x728.jpg (27.91 KB, 下載次數(shù): 124)
下載附件
2017-7-20 13:39 上傳
TB2VW9gistnpuFjSZFvXXbcTpXa_!!0-fleamarket.jpg_728x728.jpg (59.44 KB, 下載次數(shù): 150)
下載附件
2017-7-20 13:39 上傳
TB2z7DYgYJkpuFjy1zcXXa5FFXa_!!0-fleamarket.jpg_728x728.jpg (45.7 KB, 下載次數(shù): 160)
下載附件
2017-7-20 13:39 上傳
單片機源程序如下:
- #include "global_includes.h"
- #include <stdio.h>
- #include <string.h>
- #include "stm32f10x_rcc.h"
- typedef struct
- {
- uint16_t Buffer_Len;
- uint16_t PM1_0_CF;
- uint16_t PM2_5_CF;
- uint16_t PM10_CF;
- uint16_t PM1_0;
- uint16_t PM2_5;
- uint16_t PM10;
- uint16_t Count0_3nm;
- uint16_t Count0_5nm;
- uint16_t Count1_0nm;
- uint16_t Count2_5nm;
- uint16_t Count5_0nm;
- uint16_t Count10nm;
- }PM_Sensor_DataStruct;
- typedef enum {RESET = 0, SET = !RESET} FlagStatus;
- typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
- PM_Sensor_DataStruct PM_Sensor_Data;
- uint8_t PM_Sensor_RxBuffer[50];
- uint16_t PM_Sensor_RxTimeOut = 0;
- uint16_t PM_Sensor_RxCount = 0;
- FlagStatus PM_Sensor_RxFinish = RESET;
- void SysTick_Handler(void) // SYS timer interrupt function
- {
- //OS_TimeMS ++; // ++1us for os timer
- //==========================================================================
- if(PM_Sensor_RxTimeOut != 0x00) // timeout for PM data receive
- {
- PM_Sensor_RxTimeOut--;
- }
- else
- {
- if((PM_Sensor_RxCount)&&(PM_Sensor_RxBuffer[0] == 'B')&&(PM_Sensor_RxBuffer[1] == 'M'))
- {
- PM_Sensor_RxCount = 0;
- PM_Sensor_RxFinish = SET;
- USART_ITConfig(USART1,USART_IT_RXNE,DISABLE);
- }
- else
- {
- PM_Sensor_RxCount = 0;
- }
- }
- }
- void USART1_IRQHandler(void) // USART1 interrupt
- {
- static uint8_t USART1_ByteData = 0;
- if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
- {
- USART1_ByteData = USART_ReceiveData(USART1);
- if(PM_Sensor_RxFinish == RESET)
- {
- PM_Sensor_RxBuffer[PM_Sensor_RxCount++] = USART1_ByteData;
- PM_Sensor_RxTimeOut = 20;
- }
- }
- //============================================================================
- if(USART_GetFlagStatus(USART1, USART_FLAG_ORE) != RESET)
- {
- USART1_ByteData = USART_ReceiveData(USART1);
- USART_ClearFlag(USART1, USART_FLAG_ORE);
- }
- if(USART_GetFlagStatus(USART1, USART_FLAG_NE) != RESET)
- {
- USART_ClearFlag(USART1, USART_FLAG_NE);
- }
- if(USART_GetFlagStatus(USART1, USART_FLAG_FE) != RESET)
- {
- USART_ClearFlag(USART1, USART_FLAG_FE);
- }
- if(USART_GetFlagStatus(USART1, USART_FLAG_PE) != RESET)
- {
- USART_ClearFlag(USART1, USART_FLAG_PE);
- }
- }
- //=============================================================================
- //PM_USART1_Configuartion USART1 configuration
- //=============================================================================
- void PM_USART1_Configuartion(void)
- {
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA |
- RCC_APB2Periph_GPIOC, ENABLE);
-
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE);
-
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- /* PA9 USART1_Tx */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽輸出-TX
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* PA10 USART1_Rx */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入-RX
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- USART_InitStructure.USART_BaudRate = 9600; //波特率
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //設(shè)置數(shù)據(jù)長度為8bit
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位為1
- USART_InitStructure.USART_Parity = USART_Parity_No; //無校驗位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//數(shù)據(jù)流控制為none
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //接收和發(fā)送模式都打開
- USART_Init(USART1, &USART_InitStructure); //初始化串口1
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); // 接收接收中斷
- USART_ITConfig(USART1, USART_IT_ERR | USART_IT_PE, ENABLE);
- /* Enable the USART1 */
- USART_Cmd(USART1, ENABLE); //使能USART1
- }
- //=============================================================================
- //Check_PMSensor_DataValid //
- //=============================================================================
- ErrorStatus Check_PMSensor_DataValid(void)
- {
- uint16_t Cal_CheckSum;
- uint16_t Buffer_CheckSum;
- uint16_t Buffer_Len;
- uint8_t i;
- ErrorStatus Result = ERROR;
- if((PM_Sensor_RxBuffer[0] == 'B')&&(PM_Sensor_RxBuffer[1] == 'M'))
- {
- Buffer_Len = (uint16_t)((PM_Sensor_RxBuffer[2] << 8) | PM_Sensor_RxBuffer[3]);
- Buffer_CheckSum = (uint16_t)((PM_Sensor_RxBuffer[Buffer_Len + 2] << 8) | PM_Sensor_RxBuffer[Buffer_Len + 3]);
- Cal_CheckSum = 0;
- for(i=0;i<(Buffer_Len + 2);i++)
- {
- Cal_CheckSum += PM_Sensor_RxBuffer[i];
- }
- if(Cal_CheckSum == Buffer_CheckSum)
- Result = SUCCESS;
- }
- return Result;
- }
- //=============================================================================
- //PMSensor_DataReflash //
- //=============================================================================
- void PMSensor_DataReflash(void)
- {
- uint16_t Buffer_Len;
- memset(&PM_Sensor_Data,0,(sizeof(PM_Sensor_Data) - 2)); //PM_Sensor_Data.PM2_5_Old should not set to zero
-
- Buffer_Len = (uint16_t)((PM_Sensor_RxBuffer[2] << 8) | PM_Sensor_RxBuffer[3]);
- if(Buffer_Len == 28) //PMS1003/5003
- {
- PM_Sensor_Data.Buffer_Len = 28;
- PM_Sensor_Data.PM1_0_CF = (uint16_t)((PM_Sensor_RxBuffer[4]<<8) | PM_Sensor_RxBuffer[5]);
- PM_Sensor_Data.PM2_5_CF = (uint16_t)((PM_Sensor_RxBuffer[6]<<8) | PM_Sensor_RxBuffer[7]);
- PM_Sensor_Data.PM10_CF = (uint16_t)((PM_Sensor_RxBuffer[8]<<8) | PM_Sensor_RxBuffer[9]);
- PM_Sensor_Data.PM1_0 = (uint16_t)((PM_Sensor_RxBuffer[10]<<8) | PM_Sensor_RxBuffer[11]);
- PM_Sensor_Data.PM2_5 = (uint16_t)((PM_Sensor_RxBuffer[12]<<8) | PM_Sensor_RxBuffer[13]);
- PM_Sensor_Data.PM10 = (uint16_t)((PM_Sensor_RxBuffer[14]<<8) | PM_Sensor_RxBuffer[15]);
- PM_Sensor_Data.Count0_3nm = (uint16_t)((PM_Sensor_RxBuffer[16]<<8) | PM_Sensor_RxBuffer[17]);
- PM_Sensor_Data.Count0_5nm = (uint16_t)((PM_Sensor_RxBuffer[18]<<8) | PM_Sensor_RxBuffer[19]);
- PM_Sensor_Data.Count1_0nm = (uint16_t)((PM_Sensor_RxBuffer[20]<<8) | PM_Sensor_RxBuffer[21]);
- PM_Sensor_Data.Count2_5nm = (uint16_t)((PM_Sensor_RxBuffer[22]<<8) | PM_Sensor_RxBuffer[23]);
- PM_Sensor_Data.Count5_0nm = (uint16_t)((PM_Sensor_RxBuffer[24]<<8) | PM_Sensor_RxBuffer[25]);
- PM_Sensor_Data.Count10nm = (uint16_t)((PM_Sensor_RxBuffer[26]<<8) | PM_Sensor_RxBuffer[27]);
-
- }
- else if(Buffer_Len == 20)// PMS3003
- {
- PM_Sensor_Data.Buffer_Len = 20;
- PM_Sensor_Data.PM1_0_CF = (uint16_t)((PM_Sensor_RxBuffer[4]<<8) | PM_Sensor_RxBuffer[5]);
- PM_Sensor_Data.PM2_5_CF = (uint16_t)((PM_Sensor_RxBuffer[6]<<8) | PM_Sensor_RxBuffer[7]);
- PM_Sensor_Data.PM10_CF = (uint16_t)((PM_Sensor_RxBuffer[8]<<8) | PM_Sensor_RxBuffer[9]);
- PM_Sensor_Data.PM1_0 = (uint16_t)((PM_Sensor_RxBuffer[10]<<8) | PM_Sensor_RxBuffer[11]);
- PM_Sensor_Data.PM2_5 = (uint16_t)((PM_Sensor_RxBuffer[12]<<8) | PM_Sensor_RxBuffer[13]);
- PM_Sensor_Data.PM10 = (uint16_t)((PM_Sensor_RxBuffer[14]<<8) | PM_Sensor_RxBuffer[15]);
- PM_Sensor_Data.Count0_3nm = 0;
- PM_Sensor_Data.Count0_5nm = 0;
- PM_Sensor_Data.Count1_0nm = 0;
- PM_Sensor_Data.Count2_5nm = 0;
- PM_Sensor_Data.Count5_0nm = 0;
- PM_Sensor_Data.Count10nm = 0;
- }
- }
- int main(void)
- {
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- file (startup_stm32f10x_xx.s) before to branch to application main.
- To reconfigure the default setting of SystemInit() function, refer to
- system_stm32f10x.c file
- */
- SetSysClockInternal(); // sysclock for internal RC
- /**/
- SysTick_Init();
- /**/
- NVIC_Configuration();
- /**/
- PM_USART1_Configuartion();
-
- /**/
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
PMS7003測試代碼.rar
(2.24 KB, 下載次數(shù): 316)
2017-7-20 13:59 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|