当前位置:网站首页>The 10th Landbridge cup embedded electronic provincial competition
The 10th Landbridge cup embedded electronic provincial competition
2022-07-28 10:49:00 【[email protected]】
List of articles
One 、 Hardware design
Schematic diagram of analog output

LED Lamp schematic diagram

LCD Schematic diagram

Schematic diagram of keys

The module used
8 individual LED
1 individual LCD
4 A button
1 Analog inputs R37
Pins used
LCD
PC0~PC7 --------- LCD_D0~D7 Low data 8 position
PC8~PC15 --------- LCD_D8~D15 High data 8 position
PA8 --------- LCD_RD Read strobe
PB5 --------- LCD_WR Write strobe
PB8 --------- LCD_RS Register selection
PB9 --------- LCD_CS Chip selection
LED
LED Lamp pin :PC8~PC15 ( They correspond to each other G4 On board LED Of a lamp LD1~LD8)
Latch
PD2: Connect latch
Key
PA0 --------- B4( Key 4)
PB0~PB2: Corresponding keys respectively 1 — Key 3 namely B1、B2、B3
Analog input
PB15 -------- R37
Two 、STM32CubeMX To configure
1、 Choose the clock source
External high-speed clock source (HSE) choice crystal/ceramic resonator( Crystal / Ceramic crystal oscillator ). Pictured 1.1 Shown 
chart 1.1
2、 Set the clock frequency
2.1 Select the external crystal oscillator 8M
2.2 HSE It's a high-speed external clock , It can be connected to quartz / Ceramic resonator , Or
External clock source , The frequency range is 4MHz~48MHz. This time I chose 8M
2.3 The system clock SYSCLK( commonly SYSCLK=PLLCLK=80MHz)
2.4 stay HCLK(AHB Bus clock ) Enter... In one column 80, You can enter. . Pictured 2.1 Shown .
chart 2.1
3、 Set up GPIO
notes :“ All pins are set by default ” On the right “Pinout view” After the interface is set, it is not necessary to “GPIO Mode and Configuration” Interface changes GPIO Set up . Pictured 3.1 and 3.2 Shown .
chart 3.1

chart 3.2
LCD Pin :
“ All pins are set by default ”
GPIO Output level : Low level
GPIO Pattern : Push pull output
GPIO Up and down : No up and down
GPIO Maximum output speed : low speed
User defined labels : Set it according to your needs
for example :PA8 The setting is as shown in the figure 3.3 Shown .
chart 3.3
LED Pin :
All pins are set by default Same as LCD
User defined labels : Set up PD2(LED Latch ) Its alias is LEDLOCK
Key pin B1~B4:
“ All pins are set by default ”
GPIO Pattern : Input
GPIO Up and down : No up and down
User defined labels : The alias of the key is set to KEY1~KEY4
Analog input R37:
GPIO Pattern : Analog input
GPIO Up and down : No up and down
4、 Set up ADC
1、 Check “IN15 Single-ended”(single-ended It is a single ended signal ). Pictured 4.1 Shown .
chart 4.1
After selection PB15 Of ADC2_IN15(ADC2 The passage of 15) Turn green ( That means the selection is successful ). Pictured 4.2 Shown .
chart 4.2
It can be seen in the picture 4.3 Set in ADC2 Parameters of , The default settings are used here .
chart 4.3
5、 Set up TIM
5.1 The prescaled coefficients (Prescaler) Set to 1599
5.2 Master counter (Counter Period) Set to 9999
Knowledge supplement :
a. APB2 be responsible for AD,I/O, senior TIM, A serial port 1.
APB1 be responsible for DA,USB,SPI,I2C,CAN, A serial port 2345, Ordinary TIM
TIM1 It's the advanced timer , It includes all functions of basic timer and general timer .
b. HZ It's Hertz , Unit of frequency ,1MHZ( Megahertz )=1000KHZ( Qianhe )=1000000HZ
c. Calculation method of timer interrupt time :
Timing time = (Prescaler+1 ) X (Counter Period+1) X 1/ Timer clock frequency ( The period of the signal )
Clock signal 1KHz,Prescaler by 9,Counter Period by 999, Timing time ?
Timing time =10x1000x1/1000=10s=10000ms
Therefore, in order to meet the meaning of the topic “ With 0.2s Blink at intervals ”, Can be Prescaler Set to 1599,Counter Period Set to 9999. Timing time =1600x10000x1/80000000=0.2s
Of course Prescaler and Counter Period The value of can be changed to other values , Just meet the meaning of the question and it can be realized .
From the figure 5.1 You know , The timer clock frequency is 80MHZ, namely 80000000HZ

chart 5.1
Select the internal clock ,Prescaler Set to 1599,Counter Period Set to 9999. Pictured 5.2 Shown .
chart 5.2
5.3 Set up TIM interrupt
Check “TIM1 update interrupt and TIM16 global interrupt”, Namely select TIM1 The update of is interrupted and TIM16 Global interrupt . Pictured 5.3 Shown .
chart 5.3
6、 Project management settings
6.1 Name yourself , Such as num101, It means the 10th provincial competition question embedded in the electronic category of the Blue Bridge Cup . Pictured 6.1 Shown .
6.2 Select corresponding IDE, Such as MDK-ARM. Pictured 6.1 Shown .
chart 6.1
6.3 Code generation column , Check the in the figure “Generate peripheral initialization as a pair of '.c/.h files per peripheral”, That is, generate the corresponding peripheral .c and .h file . Pictured 6.2 Shown .
chart 6.2
7、 The generated code
Click on the “ Building code ”. Pictured 7.1 Shown .
chart 7.1
3、 ... and 、 add to LCD Relevant documents into the project
1、 take fonts.h and lcd.h Add to the folder of the following project . Pictured 1.1 Shown .
chart 1.1
notes :inc Folders are generally placed .h file
1、 take lcd.c Add to the folder of the following project . Pictured 2.1 Shown .
chart 2.1
Four 、Keil MDK 5 To configure
4.1 Add file path
notes :( This time, you can omit Add file path Steps for , Because the original project has been added with LCD Path to related files , Add other files later , Be sure to add the file path )
1、 open Keil5, Click on the magic wand . Pictured 1.1 Shown .
chart 1.1
2、 Click on “C/C++”. Pictured 2.1 Shown .
chart 2.1
3、 Click on “Include Paths” One column Three points , Click again “ Yellow Icon ” To add a path . Pictured 3.1 Shown .
chart 3.1
4.2 Set up Debug
According to the configuration in the figure .
4.3 Add to the project file
1、 Right click “User/Core”, Click on “Manage Project Items…”. Pictured 1.1 Shown .
chart 1.1
2、 Click on “User/Core” That column , Click on the “Add Files…”. Pictured 2.1 Shown .
chart 2.1
3、 find lcd.c file , Click once “Add” that will do ( Clicking many times will add repeatedly , Error will be reported in compilation ). Pictured 3.1 Shown .
notes : Try to choose “All files” To display the file , Otherwise, some files exist but cannot be seen , Such as .s Postfix file 
chart 3.1
4、 After successful addition , Click on “OK” that will do .
5、 ... and 、 software design
Kamimiao Big guy's code warehouse
notes : stay Kamimiao Big guy's code , Added some comments , The code has changed , The overall logical structure remains unchanged
1、 Reference header file
main.c Add header file to .( notes : Put all the code in BEGIN and END Between , Prevent overwriting the original code when regenerating the code )
#include "lcd.h"
#include "stdio.h"
2、 Define global variables

char lcdbuf[20]; // Used to store the display to LCD String on
double maxv=2.4; // Upper limit of voltage
double minv=1.2; // Lower limit of voltage
uint16_t upper=1; // The label of the warning indicator lamp when the voltage exceeds the upper limit
uint16_t lower=2; // The label of the warning indicator lamp whose voltage is lower than the lower limit
uint8_t setmode=0; // Label of parameter item ( Parameter item : Upper limit of voltage 、 Lower limit of voltage 、 The warning indicator lamp whose voltage exceeds the upper limit and the warning indicator lamp whose voltage is lower than the lower limit )
uint8_t yemian=0; // Page no.
char status[10]; // Used to store the voltage status collected in real time
uint8_t ledmode=0; //led Flashing selection
uint16_t tim_ok=0; // adopt tim_ok Change in value , Realization 0.2s flashing
//uint8_t voltclear=0; // The voltage returns to normal and is cleared LED sign
3、 Function declaration

void led(int mode); //led Set up
double adcread(ADC_HandleTypeDef *hadc); // Obtain voltage value
void ledboom(void); //led State control
void ledclear(void); // Extinguish all LED The lamp
void yemianmode(void); // Page switching
void setting(void); // Key function
void lcdlive(void); // Page shows
4、 Function definition
Extinguish all LED The lamp
// Extinguish all LED The lamp
void ledclear(void)
{
HAL_GPIO_WritePin(LEDLOCK_GPIO_Port, LEDLOCK_Pin, GPIO_PIN_SET); // Latch on
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); //LED Extinguish all
HAL_GPIO_WritePin(LEDLOCK_GPIO_Port, LEDLOCK_Pin, GPIO_PIN_RESET); // Latch off
}
Page switching
// Page switching
// When yemian=0 when , Switch to the data display interface ; When yemian=1 when , Switch to the parameter configuration interface
void yemianmode(void)
{
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET) // Page switch button B1
{
while(HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin) == GPIO_PIN_RESET);
yemian++;
if(yemian==2)
{
yemian=0;
}
LCD_Clear(White); // The screen is white
ledclear(); // Extinguish all LED The lamp
}
}
Key function
// Key function
// In the parameter configuration interface , By pressing the key B2 Switch and select parameter items , By pressing the key B3( Add the key ) And the buttons B4( Minus the key ) Upper limit of voltage 、 Lower limit of voltage 、 The label of the warning indicator lamp when the voltage exceeds the upper limit 、 When the voltage is lower than the lower limit, increase or decrease the label of the warning indicator
void setting(void)
{
if(yemian==1) // This judgment guarantees the key B2~B4 It only takes effect in the parameter configuration interface
{
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET) // Switch and select the parameter item key B2
{
while(HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin) == GPIO_PIN_RESET); // Prevent long press of key B2, Release the button , The level becomes high , Jump out of while loop
setmode++;
if(setmode==5) // The label value of the parameter item is 1~4
{
setmode=1;
}
}
else if(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_RESET) // Add the key B3
{
while(HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_RESET);
if(setmode==1) {
maxv += 0.3; }
else if(setmode==2) {
minv += 0.3; }
else if(setmode==3) {
upper++; }
else if(setmode==4) {
lower++; }
}
else if(HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == GPIO_PIN_RESET) // Minus the key B3
{
while(HAL_GPIO_ReadPin(KEY4_GPIO_Port, KEY4_Pin) == GPIO_PIN_RESET);
if(setmode==1) {
maxv -= 0.3; }
else if(setmode==2) {
minv -= 0.3; }
else if(setmode==3) {
upper--; }
else if(setmode==4) {
lower--; }
}
// Error setting protection function
if(maxv > 3.3) {
maxv = 0; } if(maxv < 0) {
maxv = 3.3; } // Ensure that the upper voltage limit range is 0~3.3V
if(minv > 3.3) {
minv = 0; } if(minv < 0) {
minv = 3.3; } // Ensure that the lower voltage limit range is 0~3.3V
if(upper > 8) {
upper = 1; } if(upper < 1) {
upper = 8; } // Ensure that the range of the upper voltage limit reminder indicator is 1~8
if(lower > 8) {
lower = 1; } if(lower < 1) {
lower = 8; } // Ensure that the range of the low voltage reminder indicator is 1~8
if(upper == lower) {
lower -=1; } // It is forbidden to set the upper and lower limit indicators to the same indicator
}
}
Obtain voltage value
// Obtain voltage value
double adcread(ADC_HandleTypeDef *hadc)
{
double adc = 0;
HAL_ADC_Start(&hadc2);
adc = (HAL_ADC_GetValue(&hadc2) *3.3)/4096;
if(adc<maxv && adc >minv) {
sprintf(status,"Normal");led(0);} //0 = Normal 1 = Upper 2 = Lower Compare the voltage with the set value
if(adc>maxv) {
sprintf(status,"Upper"); led(1);}
if(adc<minv) {
sprintf(status,"Lower"); led(2);}
return adc;
}
Page shows
// Page shows
void lcdlive(void)
{
if(yemian == 0) // Data display interface
{
LCD_DisplayStringLine(Line1, (u8 *)" Main ");
sprintf(lcdbuf," Volt:%.2lfV ",adcread(&hadc2));
LCD_DisplayStringLine(Line3, (u8 *)lcdbuf);
sprintf(lcdbuf," Status:%s ",status);
LCD_DisplayStringLine(Line5, (u8 *)lcdbuf);
// sprintf(lcdbuf, " tim_ok:%d ", tim_ok); // Timer test
// LCD_DisplayStringLine(Line7, (uint8_t *)lcdbuf);
}
if(yemian == 1) // Parameter configuration interface
{
HAL_TIM_Base_Stop_IT(&htim1); // off timer 1 interrupt Make sure that in the parameter configuration interface , All the lights are off Another operation //ledclear(); // Extinguish all LED The lamp
LCD_DisplayStringLine(Line1, (u8 *)" Setting ");
sprintf(lcdbuf," Max Volt:%.1lfV",maxv);
if(setmode != 1) {
LCD_DisplayStringLine(Line3, (u8 *)lcdbuf); }
sprintf(lcdbuf," Min Volt:%.1lfV",minv);
if(setmode != 2) {
LCD_DisplayStringLine(Line5, (u8 *)lcdbuf); }
sprintf(lcdbuf," Upper:LD%d",upper);
if(setmode != 3) {
LCD_DisplayStringLine(Line7, (u8 *)lcdbuf); }
sprintf(lcdbuf," Lower:LD%d",lower);
if(setmode != 4) {
LCD_DisplayStringLine(Line9, (u8 *)lcdbuf); }
// Selected parameter items “ The highlighted ” Show
LCD_SetBackColor(Blue); // Blue background
LCD_SetTextColor(Red); // The scarlet letter
if(setmode == 1) {
sprintf(lcdbuf," Max Volt:%.1lfV",maxv); LCD_DisplayStringLine(Line3, (u8 *)lcdbuf); }
else if(setmode == 2) {
sprintf(lcdbuf," Min Volt:%.1lfV",minv); LCD_DisplayStringLine(Line5, (u8 *)lcdbuf); }
else if(setmode == 3) {
sprintf(lcdbuf," Upper:LD%d",upper); LCD_DisplayStringLine(Line7, (u8 *)lcdbuf); }
else if(setmode == 4) {
sprintf(lcdbuf," Lower:LD%d",lower); LCD_DisplayStringLine(Line9, (u8 *)lcdbuf); }
LCD_SetBackColor(White); // white background
LCD_SetTextColor(Blue); // The blue words
}
}
led Set up
//led Set up
void led(int mode)
{
if(mode == 0) //0 = Normal The voltage is normal
{
HAL_TIM_Base_Stop_IT(&htim1); // off timer 1 interrupt Make sure the lights are all off
__HAL_TIM_CLEAR_IT(&htim1, TIM_IT_UPDATE); // Clear timer 1 Update interrupt flag of 1492 742
//ledmode = 0; //led No blinking
ledclear(); // Extinguish all LED The lamp
}
else if(mode == 1) //1 = Upper The upper voltage limit is exceeded
{
HAL_TIM_Base_Start_IT(&htim1); // Turn on timer 1 interrupt
ledmode=1; // The flashing flag bit of the reminder indicator when the voltage exceeds the upper limit
}
else if(mode == 2) // 2 = Lower Below the lower voltage limit
{
HAL_TIM_Base_Start_IT(&htim1); // Turn on timer 1 interrupt
ledmode=2; // When the voltage is lower than the lower limit, the warning indicator flashes
}
}
led State control
//led State control
void ledboom(void)
{
uint16_t ledpc = 0;
if(tim_ok == 0)
{
ledclear(); // Extinguish all LED The lamp
}
else if(tim_ok == 1)
{
if(ledmode == 1) // The flashing flag bit of the reminder indicator when the voltage exceeds the upper limit
{
ledpc = upper+7; // The flashing voltage exceeds the upper limit of the warning indicator label
}
else if(ledmode == 2) // When the voltage is lower than the lower limit, the warning indicator flashes
{
ledpc = lower+7; // The flashing voltage is lower than the lower limit of the warning indicator label
}
HAL_GPIO_WritePin(LEDLOCK_GPIO_Port, LEDLOCK_Pin, GPIO_PIN_SET); // Latch on
// The corresponding light is on , All other lights are off
if(ledpc == 8 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 9 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 10 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 11 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_11, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 12 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_12, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 13 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 14 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_15, GPIO_PIN_SET); }
else if(ledpc == 15 ) {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_15, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14, GPIO_PIN_SET); }
HAL_GPIO_WritePin(LEDLOCK_GPIO_Port, LEDLOCK_Pin, GPIO_PIN_RESET); // Latch off
}
}
while Before circulation

LCD_Init(); //LCD initialization
LCD_Clear(White); // The screen is white
LCD_SetBackColor(White); // white background
LCD_SetTextColor(Blue); // The blue words
ledclear(); // Extinguish all LED The lamp
__HAL_TIM_CLEAR_IT(&htim1,TIM_IT_UPDATE); // Clear timer 1 Update interrupt flag of Make sure while The timer interrupt is not repeated before the cycle
while In circulation

yemianmode(); // Page switching
setting(); // Key function
lcdlive(); // Page shows
while After the cycle
notes : The callback function does not need to be in main Declare before function 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) // Periodic run callback , Configure timed access interrupt
{
if(htim->Instance == TIM1)
{
// adopt tim_ok Change in value , Realization 0.2s flashing
if(tim_ok == 1) {
tim_ok = 0; }
else tim_ok =1;
// Flip the level state of the corresponding lamp
ledboom(); //led State control
}
}
6、 ... and 、 Function position
TIM





#define __HAL_TIM_CLEAR_IT(__HANDLE__, __INTERRUPT__) ((__HANDLE__)->Instance->SR = ~(__INTERRUPT__))
HAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim);
HAL_StatusTypeDef HAL_TIM_Base_Stop_IT(TIM_HandleTypeDef *htim);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
7、 ... and 、 Experimental phenomena
Initial interface . Voltage too low , The warning indicator light with voltage lower than the lower limit flashes .( notes : Initial interface and LED Whether the light is on or off will change with the initial state of the potentiometer )
Turn the potentiometer R37, After adjusting the voltage . The voltage is normal , All the lights are off .
Press the button 2, Switch to the parameter setting interface , The default settings are shown in the following figure .
In the parameter setting interface, click B2~B4 After the key is set , The parameters are shown in the figure below .
8、 ... and 、 The code download
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/209/202207281025238982.html
边栏推荐
猜你喜欢

Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter

ICML 2022 | graph represents the structure aware transformer model of learning

非关系型数据库MongoDB的特点及安装
![[application of stack] - infix expression to suffix expression](/img/c1/879716342f6dd5eaa8b79c752eca16.png)
[application of stack] - infix expression to suffix expression

Redis-day01-常识补充及redis介绍

SQL Server 2016 learning records - single table query

GKVoronoiNoiseSource

Product side data analysis thinking

粒子群解决tsp的技术问题

Solving the optimal solution of particle swarm optimization
随机推荐
粒子群实现最优解的求解
c语言进阶篇:指针(一)
ACM winter vacation training 4
GKNoise
PyQt5快速开发与实战 4.11 拖曳与剪贴板
Machine learning -- handwritten English alphabet 3 -- engineering features
Attention 注意力机制流程框图
蓝桥杯嵌入式-HAL库-ADC
使用statement对象执行DDL语句创建表
markdown转成word或者pdf
Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter
10_ UE4 advanced_ Add fall and cast actions
GKBillowNoiseSource
OCR 知识 概括
7、MapReduce自定义排序实现
GKPerlinNoiseSource
GKCoherentNoiseSource
Redis-day01 common sense supplement and redis introduction
GKCheckerboardNoiseSource
Characteristics and installation of non relational database mongodb