-
Notifications
You must be signed in to change notification settings - Fork 0
/
Honeywell_DustSensor.c
153 lines (133 loc) · 3.87 KB
/
Honeywell_DustSensor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
******************************************************************************
* File Name : Honeywell_DustSensor.c
* Description : This file contains the common defines of the application
******************************************************************************
*
* COPYRIGHT(c) 2017 Akash kapashia
* Created by Akash kapashia
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l0xx_hal.h"
#include "Honeywell_DustSensor.h"
UART_HandleTypeDef huart1_DustSensor;
uint8_t Particle_Measure_Data[8];
uint16_t PM2_5=0;;
uint16_t PM10 =0;
/**
* @brief MX_USART_DUSTSENSOR_UART_Init.
* @param NONE.
* @retval None
*/
void MX_USART_DUSTSENSOR_UART_Init(void)
{
huart1_DustSensor.Instance = USART_DUSTSENSOR;
huart1_DustSensor.Init.BaudRate = 9600;
huart1_DustSensor.Init.WordLength = UART_WORDLENGTH_8B;
huart1_DustSensor.Init.StopBits = UART_STOPBITS_1;
huart1_DustSensor.Init.Parity = UART_PARITY_NONE;
huart1_DustSensor.Init.Mode = UART_MODE_TX_RX;
huart1_DustSensor.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1_DustSensor.Init.OverSampling = UART_OVERSAMPLING_16;
huart1_DustSensor.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1_DustSensor.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart1_DustSensor) != HAL_OK)
{
while(1);
}
}
/**
* @brief start_Particle_Measure.
* @param NONE.
* @retval None.
*/
void start_Particle_Measure(void){
uint8_t meas_start_data[]={0x68,0x01,0x01,0x96};
while(HAL_UART_GetState(&huart1_DustSensor)!= HAL_UART_STATE_READY);
if( HAL_UART_Transmit(&huart1_DustSensor, meas_start_data, 4,500)!= HAL_OK)
{
while(1);
}
uint8_t ackn[2];
if(HAL_UART_Receive(&huart1_DustSensor,ackn,2,500)!=HAL_OK)
{
while(1);
}
}
/**
* @brief Read_Particle_Measuring.
* @param NONE.
* @retval None.
*/
void Read_Particle_Measuring(){
uint8_t Particle_Measure[]={0x68,0x01,0x4,0x93};
while(HAL_UART_GetState(&huart1_DustSensor)!= HAL_UART_STATE_READY);
if( HAL_UART_Transmit(&huart1_DustSensor, Particle_Measure, 4,500)!= HAL_OK)
{
while(1);
}
while(HAL_UART_GetState(&huart1_DustSensor)!= HAL_UART_STATE_READY);
if(HAL_UART_Receive(&huart1_DustSensor,Particle_Measure_Data,8,1000)!=HAL_OK)
{
while(1);
}
PM2_5 = Particle_Measure_Data[3] * 256 + Particle_Measure_Data[4];
PM10 = Particle_Measure_Data[5] * 256 + Particle_Measure_Data[6];
}
/**
* @brief Stop_Particle_Measurement.
* @param NONE.
* @retval None.
*/
void Stop_Particle_Measurement(){
uint8_t meas_start_data[]={0x68,0x01,0x02,0x95};
while(HAL_UART_GetState(&huart1_DustSensor)!= HAL_UART_STATE_READY);
if( HAL_UART_Transmit(&huart1_DustSensor, meas_start_data, 4,500)!= HAL_OK)
{
while(1);
}
uint8_t ackn[2];
if(HAL_UART_Receive(&huart1_DustSensor,ackn,2,500)!=HAL_OK)
{
while(1);
}
}
/**
* @brief Stop_Auto_Send.
* @param NONE.
* @retval None.
*/
void Stop_Auto_Send()
{
uint8_t meas_start_data[]={0x68,0x01,0x20,0x77};
while(HAL_UART_GetState(&huart1_DustSensor)!= HAL_UART_STATE_READY);
if( HAL_UART_Transmit(&huart1_DustSensor, meas_start_data, 4,500)!= HAL_OK)
{
while(1);
}
uint8_t ackn[2];
if(HAL_UART_Receive(&huart1_DustSensor,ackn,2,500)!=HAL_OK)
{
while(1);
}
}
/**
* @brief Enable_Auto_Send.
* @param NONE.
* @retval None.
*/
void Enable_Auto_Send()
{
uint8_t meas_start_data[]={0x68,0x01,0x40,0x77};
while(HAL_UART_GetState(&huart1_DustSensor)!= HAL_UART_STATE_READY);
if( HAL_UART_Transmit(&huart1_DustSensor, meas_start_data, 4,500)!= HAL_OK)
{
while(1);
}
uint8_t ackn[2];
if(HAL_UART_Receive(&huart1_DustSensor,ackn,2,500)!=HAL_OK)
{
while(1);
}
}