本設(shè)計:
基于STM32單片機紫外線強度檢測儀器蜂鳴器報警(程序+原理圖+PCB)
原理圖:Altium Designer
程序編譯器:keil 5
編程語言:C語言
編號C0023
完整下載鏈接:
【騰訊文檔】C0023 網(wǎng)盤鏈接
https://docs.qq.com/doc/DS2JPanJXZ2pwQnBU
功能描述:
本系統(tǒng)由STM32F103C8T6單片機核心板、紫外線傳感器、LCD1602液晶顯示、蜂鳴器報警、按鍵控制及電源組成。
1、通過傳感器檢測傳感器實際值,并將傳感器實際值顯示在LCD1602液晶上。
2、感器檢測采用的是AD數(shù)據(jù)轉(zhuǎn)換,然后傳感器手冊運算獲得的。
3、三個按鍵設(shè)置閾值,分別為設(shè)置鍵、設(shè)置+、設(shè)置-,其中設(shè)置+、設(shè)置-只有在設(shè)置模式下才能進行操作。
4、在設(shè)置模式下,液晶有對應(yīng)的顯示標志,設(shè)置閾值存儲到單片機Flash中,具有掉電不丟失,無需重新設(shè)置。
5、設(shè)置值與采集值實時對比,如果出現(xiàn)異常情況,蜂鳴器報警提醒。
原理圖(提供源文件):
PCB(提供源文件):
源程序(提供源文件):
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include <stdio.h>
#include "timer.h"
#include "key.h"
#include <stdio.h>
#include "lcd1602.h"
#include "adc.h"
#include "stmflash.h"
#define BASICS_VAL 15 //ó?óúêy?Y×a??μ??ù′??μ
#define DEFAULT_SET_VAL 2 //μúò?′???????è?éè???μ
char dis0[17]; //?Y′?êy×é
char dis1[17]; //?Y′?êy×é
float sensorVolt = 0; //μ±?°2é?ˉ?μ
unsigned int adcx;
unsigned int uvIndex = 0;
unsigned int setNum = 50; //éè???μ
unsigned char setMode = 0;//??è?ê??ˉ?£ê?
unsigned char disFlag = 0;//??ê?±ê??
#define FLASH_SAVE_ADDR 0X08010000 // ×¢òa2?í?D??? éè??flashμ??·2?ò??ù éè??FLASH ±£′?μ??·(±?D??a??êy£??ò???μòa′óóú±?′ú???ù??ó?FLASHμ?′óD?+0X08000000)
//stm32f103R8T6 flashμ??··??§ 0x8010000-0x801FFFF
//stm32f103C8T6 flashμ??··??§ 0x8000000-0x807FFFF
#define SIZE 10 //êy×é3¤?è
unsigned char setFlashBuf[10];//′?′¢êy?Y'
unsigned char initFlagData; //3?ê??ˉêy?Y′??áè?
/******************°?êy?Y±£′?μ?μ¥???ú?ú2?eepom?D******************/
void write_FlashBuf(void)
{
setFlashBuf[0] = initFlagData;
setFlashBuf[1] = setNum / 256; //??D?D′è??μ
setFlashBuf[2] = setNum % 256;
STMFLASH_Write(FLASH_SAVE_ADDR,(u16*)setFlashBuf,SIZE); //?áè?êy?Y
}
/******************°?êy?Y′óμ¥???ú?ú2?eepom?D?á3?à′*****************/
void read_FlashBuf(void)
{
STMFLASH_Read(FLASH_SAVE_ADDR,(u16*)setFlashBuf,SIZE); //?áè?flash?μ
initFlagData = setFlashBuf[0]; //êy?Y3?ê??ˉ?áè?
setNum = setFlashBuf[1]; //?áè???×??ú
setNum <<= 8;
setNum |= setFlashBuf[2]; //?áè?μí×??ú 2¢o?2¢
}
/**************?a?ú3?ê??ˉ±£′?μ?êy?Y*****************/
void init_FlashBuf(void)
{
read_FlashBuf(); //?áè?flash?μ
if(initFlagData != 0x55) //D?μ?μ¥???ú3?ê?μ¥???ú?ú?êeeprom è?1?2?ê?0x55??D?D′è?
{
setNum = DEFAULT_SET_VAL;//éè??é??T
initFlagData = 0x55; //DT???μ
delay_ms(20);
write_FlashBuf(); //?áè?êy?Y
}
}
void CheckKey(void);
int main(void)
{
delay_init(); //?óê±oˉêy3?ê??ˉ
uart_init(9600); //′??ú3?ê??ˉ?a115200
TIM3_Int_Init(499,7199); //?¨ê±?÷3?ê??ˉ
LED_Init(); //3?ê??ˉ?ó?ú
KEY_Init();
Adc_Init(); //3?ê??ˉad×a??
init_FlashBuf();//3?ê??ˉ?áè?′?′¢êy?Y
buzzer = 1; //·??ù?÷±¨?ˉò???
delay_ms(200);
buzzer = 0;//·??ù?÷ í£?1±¨?ˉ
Lcd_GPIO_init(); //òo?§?ó?ú3?ê??ˉ
Lcd_Init(); //òo?§????3?ê??ˉ
delay_ms(20);
while(1)
{
CheckKey();
if(disFlag==1)//?¨ê±?áè?±ê??
{
disFlag=0;//????±ê??
adcx=Get_Adc_Average(ADC_Channel_1,5);//?áè?ad×a???μ 5′??ó???ù
sensorVolt =(float)adcx*3.3/4096;// 2é?ˉ?μ
if(sensorVolt < 0.05) {uvIndex = 0;}//?ù?Y′??D?÷±íè·?¨μ?
else if(sensorVolt < 0.227) {uvIndex = 1;}//?ù?Y′??D?÷±íè·?¨μ?
else if(sensorVolt < 0.318) {uvIndex = 2;}//?ù?Y′??D?÷±íè·?¨μ?
else if(sensorVolt < 0.408) {uvIndex = 3;}//?ù?Y′??D?÷±íè·?¨μ?
else if(sensorVolt < 0.503) {uvIndex = 4;}
else if(sensorVolt < 0.606) {uvIndex = 5;}
else if(sensorVolt < 0.696) {uvIndex = 6;}
else if(sensorVolt < 0.795) {uvIndex = 7;}
else if(sensorVolt < 0.881) {uvIndex = 8;}
else if(sensorVolt < 0.976) {uvIndex = 9;}
else if(sensorVolt < 1.079) {uvIndex = 10;}
else {uvIndex = 11;}
sprintf(dis0,"%4.2fv UVIndex:%d ",sensorVolt,uvIndex); //′òó?
Lcd_Puts(0,0,(u8 *)dis0);//??ê?
if(setMode){
sprintf(dis1,"setIndex:%02d< ",setNum); //′òó?
}
else{
sprintf(dis1,"setIndex:%02d ",setNum);
}
Lcd_Puts(0,1,dis1);//??ê?
if(uvIndex > setNum) //2é?ˉ?μ oí éè???μ ??±è
{buzzer = !buzzer;} //·??ù?÷±¨?ˉ
else {buzzer = 0;} //2?±¨?ˉ
}
}
}
void CheckKey(void)
{
static unsigned char rekey =0;
if((key1==0)||(key2==0)||(key3==0)) //?ì2aμ?°′?ü°′??
{
delay_ms(10); //D????ˉ
if(rekey==0)
{
if(key1==0) //?ì2aê?·?°′??
{
rekey=1;
if(setMode){
setMode = 0; //è???éè??
}
else {
setMode = 1; //éè??×′ì?
}
}
else if(key2==0)//éè???μ?ü
{
rekey=1;
if(setMode){ //éè??ì??t??
if(setNum < 99 ){setNum=setNum+1;} //+1
}
}
else if(key3==0)//éè???μ?ü
{
rekey=1;
if(setMode){ //éè??ì??t??
if(setNum > 0 ){setNum=setNum-1;}
}
}
write_FlashBuf();//′?′¢êy?Y
}
}
else
{
rekey=0; //·à?1???′?ì2aμ?°′?ü
}
}
資料清單如下: