在线时间954 小时
UID336767
注册时间2011-12-29
NXP金币725
TA的每日心情 | 开心 2018-7-23 21:04 |
---|
签到天数: 103 天 连续签到: 1 天 [LV.6]常住居民II
金牌会员
 
- 积分
- 16706
- 最后登录
- 1970-1-1
|
去年花钱淘了一个粉尘传感器PMS1003,攀藤的产品。
这几天又拿出来玩玩,使用LPC824读取一下数据,检测PM1.0、PM2.5和PM10的值。
这次先上个效果
这个激光PM检测器精度比几十元的要好
PMS1003的结构,采用激光传感器
引脚8个
攀藤公司除了一些列的多个产品
PMS1003的参数,通过这个表也可以看到精度
这个传感器一点好处就是不用A/D之类的,直接输出串口数据
起协议如下:只需要配合该协议串口读取数据就可以了
看看我的测试硬件,由于UART1作为串口调试接口,所以选择UART0读取传感器数据,通过开关矩阵接到P0.18和P0.19上
定义需要用到的变量
串口0初始化,启动接收中断,9600bps
中断函数,按照上面的协议读取数据
主函数输出检测数据
下载调试即可
下面分享一下我的程序,有这个模块的朋友也可以试试:
- /*
- ===============================================================================
- Name : 005_lpc_pms1003.c
- Author : $(lkl0305)
- Version : 1.0
- Copyright : $(lkl0305)
- Description : main definition
- ===============================================================================
- */
- #if defined (__USE_LPCOPEN)
- #if defined(NO_BOARD_LIB)
- #include "chip.h"
- #else
- #include "board.h"
- #endif
- #endif
- #include <cr_section_macros.h>
- // TODO: insert other include files here
- // TODO: insert other definitions and declarations here
- #define PMS1003_BUFFER_SIZE 32
- typedef struct pms1003_data
- {
- uint16_t pm1_0_Value;
- uint16_t pm2_5_Value;
- uint16_t pm10_Value;
- } pms1003_data_t;
- volatile uint8_t pms1003_Flag = 0;
- pms1003_data_t pms1003_Data = {0};
- void Uart0_Init(void)
- {
- Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM);
- Chip_SWM_DisableFixedPin(SWM_FIXED_ADC7);
- Chip_SWM_DisableFixedPin(SWM_FIXED_ADC8);
- Chip_Clock_SetUARTClockDiv(1);
- Chip_SWM_MovablePinAssign(SWM_U0_TXD_O, 18);
- Chip_SWM_MovablePinAssign(SWM_U0_RXD_I, 19);
- Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM);
- Chip_UART_Init(LPC_USART0);
- Chip_UART_ConfigData(LPC_USART0, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1);
- Chip_Clock_SetUSARTNBaseClockRate((115200 * 6 * 16), true);
- Chip_UART_SetBaud(LPC_USART0, 9600);
- Chip_UART_Enable(LPC_USART0);
- Chip_UART_TXEnable(LPC_USART0);
- Chip_UART_IntEnable(LPC_USART0, UART_INTEN_RXRDY);
- NVIC_EnableIRQ(UART0_IRQn);
- }
- void UART0_IRQHandler(void)
- {
- static uint8_t pms1003_Value[PMS1003_BUFFER_SIZE] = {0};
- static volatile uint8_t pms1003_Status = 0;
- uint8_t data;
- if ((Chip_UART_GetStatus(LPC_USART0) & UART_STAT_RXRDY) != 0)
- {
- data = Chip_UART_ReadByte(LPC_USART0);
- switch (pms1003_Status)
- {
- case 0:
- if (data == 0x42)
- {
- pms1003_Value[pms1003_Status] = data;
- pms1003_Status++;
- }
- break;
- case 1:
- if (data == 0x4d)
- {
- pms1003_Value[pms1003_Status] = data;
- pms1003_Status++;
- }
- else
- {
- pms1003_Status = 0;
- }
- break;
- default:
- pms1003_Value[pms1003_Status] = data;
- pms1003_Status++;
- if (pms1003_Status == PMS1003_BUFFER_SIZE)
- {
- pms1003_Status = 0;
- uint16_t crc_Value = 0;
- for (uint8_t i = 0; i < 30; i++)
- {
- crc_Value += pms1003_Value[i];
- }
- if (crc_Value == ((uint16_t)pms1003_Value[30] << 8) + pms1003_Value[31])
- {
- pms1003_Data.pm1_0_Value = ((uint16_t)pms1003_Value[10] << 8) + pms1003_Value[11];
- pms1003_Data.pm2_5_Value = ((uint16_t)pms1003_Value[12] << 8) + pms1003_Value[13];
- pms1003_Data.pm10_Value = ((uint16_t)pms1003_Value[14] << 8) + pms1003_Value[15];
- pms1003_Flag = 1;
- }
- }
- break;
- }
- }
- }
- int main(void) {
- #if defined (__USE_LPCOPEN)
- // Read clock settings and update SystemCoreClock variable
- SystemCoreClockUpdate();
- #if !defined(NO_BOARD_LIB)
- // Set up and initialize all required blocks and
- // functions related to the board hardware
- Board_Init();
- // Set the LED to the state of "On"
- Board_LED_Set(0, true);
- #endif
- #endif
- // TODO: insert code here
- Uart0_Init();
- // Force the counter to be placed into memory
- // Enter an infinite loop, just incrementing a counter
- while(1) {
- if (pms1003_Flag == 1)
- {
- pms1003_Flag = 0;
- DEBUGOUT("\r\n=======================\r\n");
- DEBUGOUT("PM1.0 = %d ug/m3\r\n", pms1003_Data.pm1_0_Value);
- DEBUGOUT("PM2.5 = %d ug/m3\r\n", pms1003_Data.pm2_5_Value);
- DEBUGOUT("PM10 = %d ug/m3\r\n", pms1003_Data.pm10_Value);
- DEBUGOUT("=======================\r\n");
- }
- }
- return 0 ;
- }
复制代码
|
|