当前位置:网站首页>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;
}
}
}
边栏推荐
- servlet(1)
- Cglib dynamic agent -- example / principle
- MIT doctoral thesis | robust and reliable intelligent system using neural symbol learning
- Illustrated network: the principle behind TCP three-time handshake, why can't two-time handshake?
- Obstacle detection
- Is chaozhaojin safe? Will it lose its principal
- Why can't mathematics give machine consciousness
- For a deadline, the IT fellow graduated from Tsinghua suddenly died on the toilet
- Zhuhai's waste gas treatment scheme was exposed
- cf:D. Insert a Progression【关于数组中的插入 + 绝对值的性质 + 贪心一头一尾最值】
猜你喜欢
激动人心,2022开放原子全球开源峰会报名火热开启
After Luke zettlemoyer, head of meta AI Seattle research | trillion parameters, will the large model continue to grow?
程序员搞开源,读什么书最合适?
MobileNet系列(5):使用pytorch搭建MobileNetV3并基于迁移学习训练
看抖音直播Beyond演唱会有感
What is the most suitable book for programmers to engage in open source?
Mobilenet series (5): use pytorch to build mobilenetv3 and learn and train based on migration
Five challenges of ads-npu chip architecture design
SAP Spartacus home 页面读取 product 数据的请求的 population 逻辑
Dede collection plug-in free collection release push plug-in
随机推荐
cf:H. Maximal AND【位运算练习 + k次操作 + 最大And】
FFT learning notes (I think it is detailed)
Logstash clear sincedb_ Path upload records and retransmit log data
C language programming (Chapter 6 functions)
Beginner redis
In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
Cf:c. the third problem
FFT 学习笔记(自认为详细)
MCU通过UART实现OTA在线升级流程
[simple implementation of file IO]
Live video source code, realize local storage of search history
Spark DF adds a column
Curlpost PHP
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
[Arduino syntax - structure]
A preliminary study of geojson
BiShe - College Student Association Management System Based on SSM
Spark SQL null value, Nan judgment and processing
Cve-2017-11882 reappearance
Use of crawler manual 02 requests