当前位置:网站首页>Embedded design and development project - liquid level detection and alarm system
Embedded design and development project - liquid level detection and alarm system
2022-06-28 13:26:00 【Procedural deer】
Embedded design and development project - Liquid level detection alarm system
One 、 Implemented function
- ① Liquid level detection , adopt ADC A voltage analog liquid level sensor with a peripheral detection potentiometer , And calculate the liquid level height through the formula ;
- ② Threshold settings , Set the threshold value of liquid level through key peripherals ;
- ③ Threshold save , adopt IIC Peripherals save data to AT12C02 In the memory ;
- ④ Serial port query and output , Send specific characters through serial port peripherals ‘C’、‘S’ Query the liquid level height, level and threshold respectively . When the liquid level level level changes, the serial port sends real-time liquid level data ;
- ⑤ Status indication , adopt LED Peripheral LD1 With 1S The interval flashing indicates the operation status indicator ,LD2 With 0.2S Blinking at intervals 5 Times indicates that the liquid level level sends a change ,LD3 With 0.2S Blinking at intervals 5 Times indicates that the serial port has received information ;
Two 、 According to code implementation
1、 Master file main.c
#include "stm32f10x.h"
#include "lcd.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#include "i2c.h"
#include "adc.h"
typedef unsigned char uchar;
typedef unsigned int uint;
uint uiAdc_Val; //R37 Analog liquid level sensor ADC value
uchar ucHeight,ucLevel,ucLevel1; // Liquid level height 、 Liquid level level level
uchar ucState,pucStr[21],pucTh[3],pucRcv[1]; //LCD Show buffer 、 Level threshold 、 Serial buffer
uchar ucLed,ucLd2,ucLd3,ucNum = 10; // Lighten up LED
uchar ucSec,ucSec1,ucKey_Long; // preservation ms、s, Long press the flag bit
unsigned long ulTick_ms; // The count of the tick timer
void KEY_Proc(void);
void LCD_Proc(void);
void ADC_Proc(void);
void UART_Proc(void);
//Main Body
int main(void)
{
SysTick_Config(SystemCoreClock/1000); // Set the system tick clock 1ms And can interrupt
KEY_Init(); // Key initialization
LED_Init(); //LED initialization
STM3210B_LCD_Init(); //LCD initialization
LCD_Clear(Blue); // Refresh the screen to blue
LCD_SetBackColor(Blue); // Set the font background to blue
LCD_SetTextColor(White); // Font set to white
USART2_Init(9600); // Serial initialization
i2c_init(); //IIC initialization
ADC1_Init(); //ADC initialization
i2c_read(pucTh,0,3); //AT24C02 Read from address 0 At the beginning 3 Bytes of data
if(pucTh[0] > 100)
pucTh[0] = 10;
if(pucTh[1] > 100)
pucTh[0] = 20;
if(pucTh[2] > 100)
pucTh[0] = 30;
while(1)
{
KEY_Proc(); // The function corresponding to the key
LCD_Proc(); //LCD Corresponding functions
ADC_Proc(); //ADC Collect corresponding functions
UART_Proc(); // Functions corresponding to serial port
}
}
/********************************************************************************************** * function :void KEY_Proc(void) * function : Key B1 Set the key ,B2 Toggle key ,B3 Add the key ,B4 Minus the key * Input : nothing * Output : nothing * Special instructions : Each key does not affect each other **********************************************************************************************/
void KEY_Proc(void)
{
uchar ucKey_Val;
ucKey_Val = KEY_Scan(); // Check that the key is pressed
if(ucKey_Val != ucKey_Long)
ucKey_Long = ucKey_Val;
else
ucKey_Val = 0; // Prevent long press
switch(ucKey_Val)
{
case 1: //B1: Set the key
if(!ucState)
ucState = 1;
else
{
if((pucTh[0]) < pucTh[1] && pucTh[1] < pucTh[2])
{
LCD_DisplayStringLine(Line3,(u8*)" ");
i2c_write(pucTh , 0 , 3); //pucTh Threshold save to AT24C02
ucState = 0;
}
else
{
LCD_SetTextColor(Red);
LCD_DisplayStringLine(Line3,(u8*)" Threshold Error "); // Error in setting the threshold
LCD_SetTextColor(White);
}
}
break;
case 2: //B2: Toggle key
if(ucState)
if(++ucState == 4)
ucState = 1;
break;
case 3: //B3: Threshold plus
if(ucState)
if(pucTh[ucState-1] < 95)
pucTh[ucState-1] +=5;
break;
case 4: //B3: The threshold is reduced
if(ucState)
if(pucTh[ucState-1] > 5)
pucTh[ucState-1] -=5;
break;
}
}
/********************************************************************************************** * function :void LCD_Proc(void) * function :LCD Screen display function * Input : nothing * Output : nothing * Special instructions : nothing **********************************************************************************************/
void LCD_Proc(void)
{
float temp;
if(!ucState) //0 Display the status interface , It's not equal to 0 Display the setting interface
{
LCD_DisplayStringLine(Line1,(u8*)" Liquid Level ");
sprintf((char*)pucStr," Height:%03ucm ",ucHeight);
LCD_DisplayStringLine(Line4,pucStr);
temp = (float)uiAdc_Val * 3.3 /4095;
sprintf((char*)pucStr," VR37: %4.3fV ",temp);
LCD_DisplayStringLine(Line6,pucStr);
sprintf((char*)pucStr," Level: %01u ",ucLevel);
LCD_DisplayStringLine(Line8,pucStr);
}
else
{
LCD_DisplayStringLine(Line1,(u8*)" Threshold Setup ");
// The index is on page 1 Threshold settings
sprintf((char*)pucStr," Threshold 1: %02ucm ",pucTh[0]);
if(ucState == 1)
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line4,pucStr);
LCD_SetBackColor(Blue);
// The index is on page 2 Threshold settings
sprintf((char*)pucStr," Threshold 2: %02ucm ",pucTh[1]);
if(ucState == 2)
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line6,pucStr);
LCD_SetBackColor(Blue);
// The index is on page 3 Threshold settings
sprintf((char*)pucStr," Threshold 3: %02ucm ",pucTh[2]);
if(ucState == 3)
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line8,pucStr);
LCD_SetBackColor(Blue);
}
}
/********************************************************************************************** * function :void ADC_Proc(void) * function :ADC Acquisition potentiometer R37 The voltage signal of * Input : nothing * Output : nothing * Special instructions : nothing **********************************************************************************************/
void ADC_Proc(void)
{
if(!ucState && ucSec != ucSec1)
{
ucSec1 = ucSec;
uiAdc_Val = ADC1_Conv(); // Obtain the replacement value
ucHeight = uiAdc_Val*100/4095; // Calculate the liquid level ADC_Val = V * 4095 / 3.3
if(ucHeight < pucTh[0])
ucLevel = 0;
else if(ucHeight < pucTh[1])
ucLevel = 1;
else if(ucHeight < pucTh[2])
ucLevel = 2;
else
ucLevel = 3;
if(ucLevel != ucLevel1)
{
if(ucLevel > ucLevel1) // The liquid level rises
printf("A:H%02u+L%01u+U\r\n",ucHeight,ucLevel);
else // The liquid level decreases
printf("A:H%02u+L%01u+D\r\n",ucHeight,ucLevel);
ucLevel1 = ucLevel;
ucLd2 = 1; // Grade change
}
}
}
/********************************************************************************************** * function :void UART_Proc(void) * function : Serial port query and sending function * Input : nothing * Output : nothing * Special instructions : nothing **********************************************************************************************/
void UART_Proc(void)
{
if(pucRcv[0] == 'C')
printf("C:H%02u+L%01u\r\n",ucHeight,ucLevel);
if(pucRcv[0] == 'S')
printf("S:TL%02u+TM%02u+TH%02u\r\n",pucTh[0],pucTh[1],pucTh[2]);
if(pucRcv[0] == 'C' || pucRcv[0] == 'S')
ucLd3 = 1; // Serial port query
pucRcv[0] = 0;
}
/********************************************************************************************** * function :void SysTick_Handler(void) * function : Tick timer interrupt function * Input : nothing * Output : nothing * Special instructions : Every time 1 Mode enters once **********************************************************************************************/
void SysTick_Handler(void)
{
ulTick_ms++;
if(ulTick_ms%1000 == 0)
{
ucSec++;
ucLed ^=1; //LD1 flashing
}
if(ucLd2)
{
if(ulTick_ms%200 == 0)
{
if(ucNum--)
ucLed ^= 2; //LD2 flashing 5 Time
else
{
ucLd2 = 0;
ucNum = 10;
}
}
}
if(ucLd3)
{
if(ulTick_ms%200 == 0)
{
if(ucNum--)
ucLed ^= 4; //LD3 flashing 5 Time
else
{
ucLd3 = 0;
ucNum = 10;
}
}
}
LED_Disp(ucLed);
}
/********************************************************************************************** * function :void USART2_IRQHandler( void ) * function : Receiving serial port 2 The data of * Input : nothing * Output : nothing * Special instructions : Receiving serial port 2 The interrupt service function of **********************************************************************************************/
void USART2_IRQHandler(void)
{
pucRcv[0] = USART_ReceiveData(USART2);
}
Principal function analysis :️ ️ ️
- Define global variables , Functions in the current source file can be cross assigned and used .
- Each peripheral of the main control chip shall be modularized as much as possible , Each function is responsible for the function of a peripheral .
- Three are defined ucLevel1、ucSec1、ucKey_Long Variable , Save the last level level respectively 、 Time 、 Key value , thus Judge the liquid level level in real time 、 Time delay 1S、 Prevent long press The function of .
2、void KEY_Proc(void) Key processing program design
/********************************************************************************************** * function :void KEY_Proc(void) * function : Key B1 Set the key ,B2 Toggle key ,B3 Add the key ,B4 Minus the key * Input : nothing * Output : nothing * Special instructions : Each key does not affect each other **********************************************************************************************/
void KEY_Proc(void)
{
uchar ucKey_Val;
ucKey_Val = KEY_Scan(); // Check that the key is pressed
if(ucKey_Val != ucKey_Long)
ucKey_Long = ucKey_Val;
else
ucKey_Val = 0; // Prevent long press
switch(ucKey_Val)
{
case 1: //B1: Set the key
if(!ucState)
ucState = 1;
else
{
if((pucTh[0]) < pucTh[1] && pucTh[1] < pucTh[2])
{
LCD_DisplayStringLine(Line3,(u8*)" ");
i2c_write(pucTh , 0 , 3); //pucTh Threshold save to AT24C02
ucState = 0;
}
else
{
LCD_SetTextColor(Red);
LCD_DisplayStringLine(Line3,(u8*)" Threshold Error "); // Error in setting the threshold
LCD_SetTextColor(White);
}
}
break;
case 2: //B2: Toggle key
if(ucState)
if(++ucState == 4)
ucState = 1;
break;
case 3: //B3: Threshold plus
if(ucState)
if(pucTh[ucState-1] < 95)
pucTh[ucState-1] +=5;
break;
case 4: //B3: The threshold is reduced
if(ucState)
if(pucTh[ucState-1] > 5)
pucTh[ucState-1] -=5;
break;
}
}
Key KEY Brief analysis :️ ️
- Definition ucKey_Long Sign a , Prevent long press to execute the function of the key ;
- Definition pucTh[3] Data can be used as a function i2c_write Of (unsigned char*) The ginseng , Save the data in order AT24C02 In the memory ;
- If ucState Greater than 0, Judgment is the setting interface , Thus, the toggle key can be executed 、 Threshold plus 、 Operation of threshold subtraction .
- Use... Cleverly ucState Judge the threshold setting of the current switching level , And can use the threshold array pucTh Set up .
3、LCD Handler design
/********************************************************************************************** * function :void LCD_Proc(void) * function :LCD Screen display function * Input : nothing * Output : nothing * Special instructions : nothing **********************************************************************************************/
void LCD_Proc(void)
{
float temp;
if(!ucState) //0 Display the status interface , It's not equal to 0 Display the setting interface
{
LCD_DisplayStringLine(Line1,(u8*)" Liquid Level ");
sprintf((char*)pucStr," Height:%03ucm ",ucHeight);
LCD_DisplayStringLine(Line4,pucStr);
temp = (float)uiAdc_Val * 3.3 /4095;
sprintf((char*)pucStr," VR37: %4.3fV ",temp);
LCD_DisplayStringLine(Line6,pucStr);
sprintf((char*)pucStr," Level: %01u ",ucLevel);
LCD_DisplayStringLine(Line8,pucStr);
}
else
{
LCD_DisplayStringLine(Line1,(u8*)" Threshold Setup ");
// The index is on page 1 Threshold settings
sprintf((char*)pucStr," Threshold 1: %02ucm ",pucTh[0]);
if(ucState == 1)
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line4,pucStr);
LCD_SetBackColor(Blue);
// The index is on page 2 Threshold settings
sprintf((char*)pucStr," Threshold 2: %02ucm ",pucTh[1]);
if(ucState == 2)
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line6,pucStr);
LCD_SetBackColor(Blue);
// The index is on page 3 Threshold settings
sprintf((char*)pucStr," Threshold 3: %02ucm ",pucTh[2]);
if(ucState == 3)
LCD_SetBackColor(Red);
LCD_DisplayStringLine(Line8,pucStr);
LCD_SetBackColor(Blue);
}
}
LCD Screen brief analysis :️ ️
- If ucState by 0, yes Liquid level status display Interface , It is Set up Interface .
- Clever use of Space Overlap another interface to display data ;
5、ADC Handler design
/********************************************************************************************** * function :void ADC_Proc(void) * function :ADC Acquisition potentiometer R37 The voltage signal of * Input : nothing * Output : nothing * Special instructions : nothing **********************************************************************************************/
void ADC_Proc(void)
{
if(!ucState && ucSec != ucSec1)
{
ucSec1 = ucSec;
uiAdc_Val = ADC1_Conv(); // Obtain the replacement value
ucHeight = uiAdc_Val*100/4095; // Calculate the liquid level ADC_Val = V * 4095 / 3.3
if(ucHeight < pucTh[0])
ucLevel = 0;
else if(ucHeight < pucTh[1])
ucLevel = 1;
else if(ucHeight < pucTh[2])
ucLevel = 2;
else
ucLevel = 3;
if(ucLevel != ucLevel1)
{
if(ucLevel > ucLevel1) // The liquid level rises
printf("A:H%02u+L%01u+U\r\n",ucHeight,ucLevel);
else // The liquid level decreases
printf("A:H%02u+L%01u+D\r\n",ucHeight,ucLevel);
ucLevel1 = ucLevel;
ucLd2 = 1; // Grade change
}
}
}
ADC Brief analysis of analog liquid level voltage :️ ️
- On the liquid level display interface , Conduct 1S Collect once Analog liquid level voltage potentiometer ;
- According to the obtained AD Value for Formula conversion For height , Judge whether the height is within the threshold range of liquid level , So as to know the current liquid level level ;
- adopt ucLevel1 Judge the level level Send changes , Send the corresponding message to the serial port according to the liquid level change , And carry on Ld2 Of 5 Secondary flicker .
6、UART Handler design
/********************************************************************************************** * function :void UART_Proc(void) * function : Serial port query and sending function * Input : nothing * Output : nothing * Special instructions : nothing **********************************************************************************************/
void UART_Proc(void)
{
if(pucRcv[0] == 'C')
printf("C:H%02u+L%01u\r\n",ucHeight,ucLevel);
if(pucRcv[0] == 'S')
printf("S:TL%02u+TM%02u+TH%02u\r\n",pucTh[0],pucTh[1],pucTh[2]);
if(pucRcv[0] == 'C' || pucRcv[0] == 'S')
ucLd3 = 1; // Serial port query
pucRcv[0] = 0;
}
A serial port UART Brief analysis :️ ️
- Judge the characters received by the serial port , Whether it is consistent with the system setting parameters , Perform corresponding functions .
- After receiving , Put the pucRcv Buffer restore defaults .
7、LED Handler design
/********************************************************************************************** * function :void SysTick_Handler(void) * function : Tick timer interrupt function * Input : nothing * Output : nothing * Special instructions : Every time 1 Mode enters once **********************************************************************************************/
void SysTick_Handler(void)
{
ulTick_ms++;
if(ulTick_ms%1000 == 0)
{
ucSec++;
ucLed ^=1; //LD1 flashing
}
if(ucLd2)
{
if(ulTick_ms%200 == 0)
{
if(ucNum--)
ucLed ^= 2; //LD2 flashing 5 Time
else
{
ucLd2 = 0;
ucNum = 10;
}
}
}
if(ucLd3)
{
if(ulTick_ms%200 == 0)
{
if(ucNum--)
ucLed ^= 4; //LD3 flashing 5 Time
else
{
ucLd3 = 0;
ucNum = 10;
}
}
}
LED_Disp(ucLed);
}
Brief analysis of the system tick timer :️ ️
- Set up 1ms, Interrupt entering this function once , Achieve accurate timing .
- If the level level level sends a change , be LD2 flashing 5 Time ; If you receive a command from the serial port , be LD3 Flashing 5 Time , Clear the flag bit after flashing .
8、USART2 Handler design
/********************************************************************************************** * function :void USART2_IRQHandler( void ) * function : Receiving serial port 2 The data of * Input : nothing * Output : nothing * Special instructions : Receiving serial port 2 The interrupt service function of **********************************************************************************************/
void USART2_IRQHandler(void)
{
pucRcv[0] = USART_ReceiveData(USART2);
}
Brief analysis :️ ️
- Serial interrupt function , The serial port receives the data entry interrupt and saves the data to pucRcv Array .
3、 ... and 、 Attention and learning points in the process of realizing functions
1、 Be careful
① How to define global variables , Coupling various peripheral functions together !
2、 What to learn
- ① Realize modular programming , The function of each peripheral defines a function separately ;
- ② Cleverly define an array , Save the thresholds for each level of the liquid level , It also realizes the addition and subtraction of the threshold ;
- ③ Learn to define flags , Determine whether the current state has changed , Prevent long press of key 、 Level level change detection and other functions ;
- ④ Realize the interrupt receiving service function of the serial port ;
️ ️ ️ ️ ️ ️
边栏推荐
- 简历模板百度网盘自取
- Centos7: switch MySQL users and log in to MySQL
- 2022年中国运维安全产品市场规模及发展趋势预测分析
- 1015. picking flowers
- The difference between align items and align content
- Hubble database x a joint-stock commercial bank: upgrade the number management system of Guanzi, so that every RMB has an "ID card"
- PHP抓取网页获取特定信息
- 2. 01背包问题
- Commonly used "redmine" for # test bug
- Arduino-ESP32闪存文件插件程序搭建和上传
猜你喜欢

Google Earth engine (GEE) - Global organic soil area of FAO (1992-2018)

2. 01背包问题

pytorch模型

公司领导说,个人代码超10个Bug就开除,是什么体验?

Shareit a une force exceptionnelle et se connecte au top 7 de la liste mondiale des forces IAP

全志V853芯片 如何在Tina V85x平台切换sensor?

初识exception

PostgreSQL超越MySQL

The $980000 SaaS project failed

How vscade sets auto save code
随机推荐
Fh511+tp4333 form an outdoor mobile power lighting camping lamp scheme.
The difference between align items and align content
(original) [Maui] realize "floating action button" step by step
PHP抓取网页获取特定信息
Introduction to PWN (1) binary Basics
Why do more and more users give up swagger and choose apifox
恒生电子:金融分布式数据库LightDB通过中国信通院多项测评
Unit test ci/cd
电脑无线网络不显示网络列表应该如何解决
Centos7: switch MySQL users and log in to MySQL
SHAREit實力出眾,登陸全球 IAP 實力榜 Top7
Pytorch Foundation
895. 最长上升子序列
FS7022方案系列FS4059A双节两节锂电池串联充电IC和保护IC
You must configure either the server or JDBC driver (via the ‘serverTimezone‘ configuration property
简历模板百度网盘自取
Align content attribute in flex layout
Oracle 云基础设施扩展分布式云服务,为组织提供更高的灵活性和可控性
How does Quanzhi v853 chip switch sensors on Tina v85x platform?
MySQL多表联合查询