当前位置:网站首页>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;
}
}
}
边栏推荐
- The value of applet containers
- In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
- [groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
- Leetcode study - day 35
- Spark SQL null value, Nan judgment and processing
- What is the most suitable book for programmers to engage in open source?
- golang mqtt/stomp/nats/amqp
- MYSQL GROUP_ The concat function realizes the content merging of the same ID
- [simple implementation of file IO]
- 直播系统代码,自定义软键盘样式:字母、数字、标点三种切换
猜你喜欢
How to extract MP3 audio from MP4 video files?
cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
Browser reflow and redraw
Vulhub vulnerability recurrence 74_ Wordpress
测试/开发程序员的成长路线,全局思考问题的问题......
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
VSphere implements virtual machine migration
I'm interested in watching Tiktok live beyond concert
95后CV工程师晒出工资单,狠补了这个,真香...
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
随机推荐
WordPress collection plug-in automatically collects fake original free plug-ins
Starting from 1.5, build a micro Service Framework - call chain tracking traceid
毕设-基于SSM高校学生社团管理系统
Finding the nearest common ancestor of binary search tree by recursion
Browser reflow and redraw
SSH login is stuck and disconnected
Finding the nearest common ancestor of binary tree by recursion
RAID disk redundancy queue
A preliminary study of geojson
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
Modify the ssh server access port number
Arduino hexapod robot
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
The inconsistency between the versions of dynamic library and static library will lead to bugs
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
Five challenges of ads-npu chip architecture design
Convert binary search tree into cumulative tree (reverse middle order traversal)
In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
China Taiwan strategy - Chapter 8: digital marketing assisted by China Taiwan
Zhuhai's waste gas treatment scheme was exposed