基于STM32_ZPH01粉尘传感器使用笔记

基于STM32_ZPH01粉尘传感器使用笔记


第一次发布博客,有很多不懂.见笑了

数据手册

链接:https://pan.baidu.com/s/1kJvWSDfan5lyPg0boz-1ZQ
提取码:fyzy

小重点

  1. ZPH01 每一秒会发送一次数据
  2. 初次使用的时候建议预热5分钟以上.
  3. ZPH01 每一秒会发送一次数据 初次使用的时候建议预热5分钟以上.
  4. 建议拿胶布or遮光的东西遮挡住,硬件上方的三角口这样可以保持数值的精准(红外线的也准不到哪去,除非激光的)

引脚&模式的定义

我这里选择的是,使用串口接收。
引脚的接线
TX(接RX)
RX(接不接都无所谓,在串口模式下ZPH01只发数据,不接受命令)
GND(当然是接GND啦)
模式脚(浮空为PMW输出模式,接GND为选择串口模式)
注意!!! 串口模式下 GND和模式脚都要 接地 接地 接地
重要事情说三遍

数据格式

串口模式下从机发送过来的数据模式
在这里插入图片描述

代码

我接入的是串口2,然后使用串口1printf输出.
没有使用到串口中断.
.c的

#include "Zph01.h"








void Zph01_Init(void){
	
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);//映射到PA2
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//映射到PA3
	
	
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;       //配置:复用
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     	//配置:推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//配置:速度
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;		//配置:无上下拉
	GPIO_Init(GPIOA,&GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;       //配置:复用
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;     	//配置:推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 	//配置:速度
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;		//配置:无上下拉
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate=9600;//设置:波特率
	USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;设置:硬件流
	USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;设置:模式
	USART_InitStructure.USART_Parity=USART_Parity_No;//设置:奇偶校验位
	USART_InitStructure.USART_StopBits=USART_StopBits_1;//设置:停止位
	USART_InitStructure.USART_WordLength=USART_WordLength_8b;设置:字长
	
	USART_Init(USART2,&USART_InitStructure);//串口服务库函数
	USART_Cmd(USART2,ENABLE);//使能串口库函数
	
	
}






u8 Buf_Cnt;//数据大小
unsigned char Buf[9];//用于存放接收到的数据
unsigned char flag[5]={0xff,0x18,0x00,0x00,0x01};
void Zph01_Rend_buf(void)
{
	uint16_t value;
	u8 i;
	
				while(USART2->SR &(1<<5)){//判断Zph01是否把数据是否已经写入DR寄存器 
						value = USART_ReceiveData(USART2);
						if(Buf_Cnt<9)
						{
							Buf[Buf_Cnt++] = value;
						}
						else{
							for(i=0;i<9;i++)	
							printf("%c",Buf[i]);
						Buf_Cnt=0;
						}
					
			 }

				
}




.h的

#ifndef _ZPH01_H
#define _ZPH01_H

#include "stm32f4xx.h"
#include "io_bit.h"
#include "stdlib.h"
#include "string.h"
#include "delay.h"
#include "stdio.h"


void Zph01_Init(void);
void Zph01_Rend_buf(void);//发送接收到的数据






#endif




总结

第一次发自己的代码出来,如果有什么问题或者更好的解决方法,希望告知,共同进步谢谢大家.

评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值