前面上传了一个HAL库版本的,现在上传一个标准库版本的,同样是使用STM32测量甲醛。用到的模块有STM32C8T6最小系统板,一个LCD1602液晶显示屏,一个ZE08-CH2O甲醛模组,还有蜂鸣器、按键、指示灯等。可以通过按键调整甲醛超标的阈值,当测量的甲醛值超过设置的阈值时,蜂鸣器会报警,相应的指示灯也会闪烁。
下面是main.c文件的代码
#include "stm32f10x.h"
#include "lcd.h"
#include <stdio.h>
#include "led.h"
#include "deputy.h"
#include "sys.h"
#include "delay.h"
#include "stmflash.h"
#define beep PBout(0)
void KEY_Scan(void);
u8 KEY_scan(u8 mode);
unsigned char Uart1_Buff[1024]; //串口1缓冲数组
unsigned char Uart1_Count=0; //串口1累加变量
u8 state=0,s0=0,right=0,count=0;
float result=0.0000;
float table=0.70;
int pm25Value;
u8 str[] = " CH2O_H ";
u8 showBuffer[9] = {0};
uint8_t key1_flag=0,key2_flag=0,key3_flag=0,memory_flag=0;
uint8_t sec1=0,sec2=0;
uint32_t Tim_ms=0;
int main(void)
{
delay_init();
GPIO_init();
LCD1602_Init();
TIM2_Int_Init(4999,7199); //10Khz,计数到5000是500ms
TIM3_Int_Init(499,7199); //定时器3 50ms中断一次
sprintf((char*)showBuffer,"CH2O:%1.4fmg/m3",result);
LCD1602_Show_Str(0, 0, (unsigned char *)showBuffer);
sprintf((char*)str,"CH2O_H:%1.2fmg/m3",table);
LCD1602_Show_Str(0, 1, (unsigned char *)str);
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
GPIO_SetBits(GPIOB,GPIO_Pin_11);
//Test_Write(0x08009106,(float)0.55);
table=STMFLASH_ReadHalfWord(0x08009106)/256.;
while(1)
{
if(right==1)
{
right=0;
pm25Value = Uart1_Buff[4]*256 + Uart1_Buff[5];
result = pm25Value / 66.64 * 0.08;
sprintf((char*)showBuffer,"CH2O:%1.4fmg/m3",result);
LCD1602_Show_Str(0, 0, (unsigned char *)showBuffer);
}
if(memory_flag)
{
memory_flag=0;
Test_Write(0x08009106,table*256.);
}
}
}
void KEY_Scan(void)
{
if(KEY1==0)
{
state=(state+1)%2;
}while(!KEY1);
if(KEY2==0)
{
if(key2_flag==1)
{
key2_flag=0;
if(state==1)
{
table+=0.01;
}
}
if(sec1==0)
{
if(state==1)
{
table+=0.01;
}
}
}
else
{
sec1=3;
if(key2_flag==0)
{
key2_flag=1;
memory_flag=1;
}
}
if(KEY3==0)
{
if(key3_flag==1)
{
key3_flag=0;
if(state==1)
{
if(table>0) table-=0.01;
}
}
if(sec2==0)
{
if(state==1)
{
if(table>0) table-=0.01;
}
}
}
else
{
sec2=3;
if(key3_flag==0)
{
key3_flag=1;
memory_flag=1;
}
}
}
void TIM2_IRQHandler(void) //TIM2中断
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查TIM2更新中断发生与否
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除TIM2更新中断标志
if(result > table)
{
beep=!beep;
GPIO_SetBits(GPIOB,GPIO_Pin_10);//红灯灭,绿灯亮
GPIO_ResetBits(GPIOB,GPIO_Pin_11);
}else
{
GPIO_ResetBits(GPIOB,GPIO_Pin_0);
GPIO_ResetBits(GPIOB,GPIO_Pin_10);
GPIO_SetBits(GPIOB,GPIO_Pin_11);
}
if(sec1!=0) sec1--;
if(sec2!=0) sec2--;
}
}
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
KEY_Scan();
Tim_ms++;
if(Tim_ms%10==0)
{
if(s0==1)
s0=0;
else
s0=1;
}
if(state==1&&s0)
{
sprintf((char*)str,"CH2O_H: mg/m3");
LCD1602_Show_Str(0, 1, (unsigned char *)str);
} else{
sprintf((char*)str,"CH2O_H:%1.2fmg/m3",table);
LCD1602_Show_Str(0, 1, (unsigned char *)str);
}
}
}
需要原理图和全部源码的朋友可以通过下面的链接下载:
程序源码