当前位置:网站首页>Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
2022-07-06 01:07:00 【lzya.】
The real topic of the 8th provincial competition


Main function part of the code
#include "main.h"
#include "rcc.h"
#include "led_key.h"
#include "lcd.h"
#include "rtc.h"
#include "tim.h"
//*** Execute speed control variable
__IO uint32_t uwTick_LED_Speed_Ctrl;
__IO uint32_t uwTick_KEY_Speed_Ctrl;
__IO uint32_t uwTick_LCD_Speed_Ctrl;
//*** Global variable area
uint8_t ucLED;
uint8_t key_value,key_up,key_down;
static uint8_t key_old; // Key three lines of code
uint8_t LCD_String_Disp[21];
RTC_TimeTypeDef T;
RTC_DateTypeDef D;
uint8_t Current_platform = 1; // The current platform defaults to 1
uint8_t Set_platform; // Use only the lower four digits of the variable Key B1-B4 Corresponding to the platform you need to reach LD1-LD4
__IO uint32_t uwTick_Time_Count; // For time counting
uint8_t State_Num; // Status number variable , The default is 0
uint8_t Direction; //1 Indicates the uplink 2 It means down
uint8_t Flow_LED = 0x10; // Running water lamp Only use the upper four digits
//*** Subfunction declaration area
void SystemClock_Config(void);
void LED_Proc(void);
void KEY_Proc(void);
void LCD_Proc(void);
void Lift_State_Proc(void);
//*** Main function area
int main(void)
{
HAL_Init();
SystemClock_Config();
//* Function initialization area
LED_KEY_Init();
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(Magenta);
RTC_Init();
TIM3_Init();
//__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1,800);
HAL_TIM_PWM_Start(&htim3 , TIM_CHANNEL_1);
TIM17_Init();
HAL_TIM_PWM_Start(&htim3 , TIM_CHANNEL_1);
while (1)
{
Lift_State_Proc();
LED_Proc();
KEY_Proc();
LCD_Proc();
}
}
void LED_Proc(void)
{
if((uwTick - uwTick_LED_Speed_Ctrl)<100) return;
uwTick_LED_Speed_Ctrl = uwTick; //100ms Do it once
LED_Disp(ucLED);
}
void KEY_Proc(void)
{
if((uwTick - uwTick_KEY_Speed_Ctrl)<100) return;
uwTick_KEY_Speed_Ctrl = uwTick; //100ms Do it once
key_value = KEY_Scan();
key_up = key_value & (key_value ^ key_old);
key_down = ~key_value & (key_value ^ key_old);
key_old = key_value;
if(State_Num == 0)
{
if(key_down == 1) //B1 Press down
{
if(Current_platform != 1) Set_platform |= 0x01; // Pressing the current floor does not work
}
else if(key_down == 2) //B2 Press down
{
if(Current_platform != 2) Set_platform |= 0x02;
}
else if(key_down == 3) //B3 Press down
{
if(Current_platform != 3) Set_platform |= 0x04;
}
else if(key_down == 4) //B4 Press down
{
if(Current_platform != 4) Set_platform |= 0x08;
}
}
ucLED &= 0xF0; // Use and equal Clear the lower four digits The top four keep
ucLED |= Set_platform; // Use or equal to Keep the top four unchanged
if(key_down != 0) // When no key is pressed , Start counting
{
uwTick_Time_Count = uwTick; // Update timing initial time , Start timing
}
}
void LCD_Proc(void)
{
if((uwTick - uwTick_LCD_Speed_Ctrl)<100) return;
uwTick_LCD_Speed_Ctrl = uwTick; //100ms Do it once
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Current platform ");
LCD_DisplayStringLine(Line1 , LCD_String_Disp);
if(State_Num != 5)
{
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " %1d ",(unsigned int)Current_platform);
LCD_DisplayStringLine(Line4 , LCD_String_Disp);
}
HAL_RTC_GetTime(&hrtc , &T ,RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc , &D ,RTC_FORMAT_BIN);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " %02d-%02d-%02d ",T.Hours,T.Minutes,T.Seconds);
LCD_DisplayStringLine(Line6 , LCD_String_Disp);
if(State_Num == 0)
{
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Open door "); // The elevator is on by default
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
}
}
void Lift_State_Proc(void) // Elevator state processing function
{
if(Set_platform) // When a key is pressed and the platform to be reached is set, execute this program , Otherwise, do not execute
{
switch(State_Num)
{
case 0:
if((uwTick - uwTick_Time_Count)>=1000) // If the waiting time exceeds 1 second , Go to the next state - state 1
State_Num = 1;
else
break;
case 1: // Control the elevator to close the door , To configure PA7 close PWM The signal PA7 ------> TIM17_CH1
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); //PA5 = 0
__HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1,250); // PA7 close - 2khz Duty cycle 50% - 250
HAL_TIM_PWM_Start(&htim17 , TIM_CHANNEL_1);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Closing door ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
uwTick_Time_Count = uwTick; // Update timing initial time , Start timing
State_Num = 2;
case 2: // wait for 4s Whether the closing time has arrived ,
if((uwTick - uwTick_Time_Count)>4000) // If the closing time is greater than 4s, Stop closing the door PWM Go to the next state
{
HAL_TIM_PWM_Stop(&htim17 , TIM_CHANNEL_1);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Close door ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
HAL_Delay(1000);
State_Num = 3;
}
else
break;
case 3: // Judge whether the lift is up or down , To configure PA6 Up and down PWM The signal PA6 ------> TIM3_CH1
if(Set_platform > ( 1<<(Current_platform-1) ) ) // If the set platform is larger than the current platform —— The upside
{
Direction = 1;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); //PA4 = 1
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1,800); // PA6 The upside - 1khz Duty cycle 80% - 800
HAL_TIM_PWM_Start(&htim3 , TIM_CHANNEL_1);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Lift Up ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
}
else if(Set_platform < ( 1<<(Current_platform-1) ) ) // If the set platform is smaller than the current platform —— The downside
{
Direction = 2;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); //PA4 = 0
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1,600); // PA6 The downside - 1khz Duty cycle 60%- 600
HAL_TIM_PWM_Start(&htim3 , TIM_CHANNEL_1);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Lift Down ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
}
uwTick_Time_Count = uwTick; // Update timing initial time , Start timing
State_Num = 4;
case 4: // wait for 6s Whether the ascending or descending time of the lift has arrived
if((uwTick - uwTick_Time_Count)>=6000) // If time arrives Control the current platform variable plus or minus one , The running water lamp is turned off and restored to its initial state , Go to the next state
{
if(Direction == 1) // The upside Add one unit to the current platform
{
Current_platform += 1;
}
else if(Direction == 2) // The downside Subtract one unit from the current platform
{
Current_platform -= 1;
}
ucLED &= 0x0F;
Flow_LED = 0x10;
State_Num = 5;
}
else // Produce the effect of running water lamp , Left to right The upside Right to left The downside , sign out , Continue to wait for 6 The arrival of seconds
{
if(Direction == 1) // The upside Water lamp from left to right
{
Flow_LED = Flow_LED>>1; // Move right 1 position 0001 0000 -> 0000 1000
if(Flow_LED == 0x08) //0001 0000 - 0000 1000
Flow_LED = 0x80;
ucLED &= 0x0F;
ucLED |= Flow_LED;
}
else if(Direction == 2) // The downside Water lamp from right to left
{
ucLED &= 0x0F;
ucLED |= Flow_LED;
Flow_LED = Flow_LED<<1;
if(Flow_LED == 0x00) //1000 0000 - 0001 0000
Flow_LED = 0x10;
}
HAL_Delay(300);
break;
}
case 5:// Judge whether the currently arrived platform is the corresponding platform to arrive
if(( 1<<(Current_platform-1) ) == Set_platform) // yes , Stop uplink and downlink PWM The signal , Open door (open door) To configure PA7
{
HAL_TIM_PWM_Stop(&htim3 , TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); //PA5 = 1
__HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1,300); // PA7 Open door - 2khz Duty cycle 60% - 300
HAL_TIM_PWM_Start(&htim17 , TIM_CHANNEL_1);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Opening door ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " ");
LCD_DisplayStringLine(Line4 , LCD_String_Disp);
HAL_Delay(250);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " %1d ",(unsigned int)Current_platform);
LCD_DisplayStringLine(Line4 , LCD_String_Disp);
HAL_Delay(250);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " ");
LCD_DisplayStringLine(Line4 , LCD_String_Disp);
HAL_Delay(250);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " %1d ",(unsigned int)Current_platform);
LCD_DisplayStringLine(Line4 , LCD_String_Disp);
HAL_Delay(250);
uwTick_Time_Count = uwTick; // Update timing initial time , Start timing
State_Num = 6;
}
else
{
State_Num = 3;
break;
}
case 6 : // Wait for the opening time 4s arrival
if((uwTick - uwTick_Time_Count)>=4000) // wait for 4s Open door , Close the door PWM The signal
{
HAL_TIM_PWM_Stop(&htim17 , TIM_CHANNEL_1);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Open door ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
Set_platform &= (~(1<<(Current_platform-1))); // Reach the set target platform , Clear this target platform
ucLED &= 0xF0;
ucLED |= Set_platform;
LED_Disp(ucLED);
State_Num = 7;
}
else
break;
case 7: // Determine whether there are other target platforms
if( Set_platform != 0 ) // There are other setting platforms , Enter the state of 8
{
uwTick_Time_Count = uwTick; // Update timing initial time , Start timing
State_Num = 8;
}
else // There is no other setting platform , Enter the state of 0
{
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Get it %1d floor ",(unsigned int)Current_platform);
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
HAL_Delay(1000);
State_Num = 0;
break;
}
case 8: //
if((uwTick - uwTick_Time_Count)>=2000) // wait for 2s
{
State_Num = 1;
break;
}
else
break;
}
}
}
边栏推荐
猜你喜欢

Cve-2017-11882 reappearance
![[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)](/img/bf/18ef41a8f30523b7ce57d03f93892f.jpg)
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)

MCU realizes OTA online upgrade process through UART

Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?

After 95, the CV engineer posted the payroll and made up this. It's really fragrant

有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀

程序员搞开源,读什么书最合适?

Browser reflow and redraw

Convert binary search tree into cumulative tree (reverse middle order traversal)

Xunrui CMS plug-in automatically collects fake original free plug-ins
随机推荐
curlpost-php
Distributed base theory
China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
[pat (basic level) practice] - [simple mathematics] 1062 simplest fraction
面试必刷算法TOP101之回溯篇 TOP34
curlpost-php
282. Stone consolidation (interval DP)
Fibonacci number
小程序容器可以发挥的价值
View class diagram in idea
Cf:d. insert a progression [about the insert in the array + the nature of absolute value + greedy top-down]
Finding the nearest common ancestor of binary search tree by recursion
WordPress collection plug-in automatically collects fake original free plug-ins
Finding the nearest common ancestor of binary tree by recursion
Construction plan of Zhuhai food physical and chemical testing laboratory
Cf:c. the third problem
Leetcode study - day 35
猿桌派第三季开播在即,打开出海浪潮下的开发者新视野
Live broadcast system code, custom soft keyboard style: three kinds of switching: letters, numbers and punctuation
SSH login is stuck and disconnected