第八届省赛真题模拟升降控制器
- 1、第八届省赛真题
- 2、状态机框图(供参考)
- 3、主函数代码(不包含各模块的初始化代码)
1、第八届省赛真题


2、状态机框图(供参考)

3、主函数代码(不包含各模块的初始化代码)
#include "main.h"
#include "rcc.h"
#include "led_key.h"
#include "lcd.h"
#include "rtc.h"
#include "tim.h"
__IO uint32_t uwTick_LED_Speed_Ctrl;
__IO uint32_t uwTick_KEY_Speed_Ctrl;
__IO uint32_t uwTick_LCD_Speed_Ctrl;
__IO uint32_t uwTick_Time_Count;
uint8_t ucLED;
uint8_t key_value,key_up,key_down;
static uint8_t key_old;
uint8_t LCD_String_Disp[21];
uint8_t Current_floor = 1;
uint8_t Set_floor;
uint8_t Status_Ctrl;
_Bool key_press_flag;
uint8_t lift_direction = 1;
uint8_t LED_flow_up = 4;
uint8_t LED_flow_down = 7;
RTC_TimeTypeDef T;
RTC_DateTypeDef D;
void SystemClock_Config(void);
void LED_Proc(void);
void KEY_Proc(void);
void LCD_Proc(void);
void Lift_Ctrl(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_KEY_Init();
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
RTC_Init();
TIM2_Init();
TIM3_Init();
TIM17_Init();
while (1)
{
LED_Proc();
KEY_Proc();
LCD_Proc();
Lift_Ctrl();
}
}
void LED_Proc(void)
{
if((uwTick - uwTick_LED_Speed_Ctrl)<200) return;
uwTick_LED_Speed_Ctrl = uwTick;
LED_Disp(ucLED);
}
void KEY_Proc(void)
{
if((uwTick - uwTick_KEY_Speed_Ctrl)<50) return;
uwTick_KEY_Speed_Ctrl = uwTick;
key_value = KEY_Scan();
key_down = key_value & (key_value ^ key_old);
key_up= ~key_value & (key_value ^ key_old);
key_old = key_value;
if(Status_Ctrl==0||Status_Ctrl==1)
{
switch(key_down)
{
case 1:
{
if(Current_floor != 1)
{
Set_floor |= 0x01;
ucLED |= 0x01;
key_press_flag = 1;
uwTick_Time_Count = uwTick;
}
}
break;
case 2:
if(Current_floor != 2)
{
Set_floor |= 0x02;
ucLED |= 0x02;
key_press_flag = 1;
uwTick_Time_Count = uwTick;
}
break;
case 3:
if(Current_floor != 3)
{
Set_floor |= 0x04;
ucLED |= 0x04;
key_press_flag = 1;
uwTick_Time_Count = uwTick;
}
break;
case 4:
if(Current_floor != 4)
{
Set_floor |= 0x08;
ucLED |= 0x08;
key_press_flag = 1;
uwTick_Time_Count = uwTick;
}
break;
}
}
if(key_press_flag)
{
Status_Ctrl = 1;
}
}
void Lift_Ctrl(void)
{
if(Status_Ctrl)
{
switch(Status_Ctrl)
{
case 1:
if((uwTick - uwTick_Time_Count)>=1000)
{
Status_Ctrl = 2;
key_press_flag = 0;
}
else
Status_Ctrl = 0;
break;
case 2:
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 0);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
__HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1, 250);
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
uwTick_Time_Count = uwTick;
Status_Ctrl = 3;
break;
case 3:
if((uwTick - uwTick_Time_Count)>=4000)
{
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(&htim17, TIM_CHANNEL_1);
Status_Ctrl = 4;
}
break;
case 4:
if(Set_floor > (1 << (Current_floor - 1)))
{
lift_direction = 1;
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 1000);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 800);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
}
else if(Set_floor < (1 << (Current_floor - 1)))
{
lift_direction = 0;
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, 0);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 600);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
}
uwTick_Time_Count = uwTick;
Status_Ctrl = 5;
break;
case 5:
if((uwTick - uwTick_Time_Count)>=6000)
{
if(lift_direction)
{
Current_floor += 1;
}
else
{
Current_floor -= 1;
}
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
ucLED &= 0x0f;
LED_flow_up = 4;
LED_flow_down = 7;
Status_Ctrl = 6;
}
else
{
if(lift_direction)
{
ucLED &= 0x0f;
ucLED |= (1<<LED_flow_up);
LED_flow_up ++ ;
if(LED_flow_up == 8)
LED_flow_up = 4;
}
else
{
ucLED &= 0x0f;
ucLED |= (1<<LED_flow_down);
LED_flow_down -- ;
if(LED_flow_down == 3)
LED_flow_down = 7;
}
HAL_Delay(300);
}
break;
case 6:
if((Set_floor & (1<<(Current_floor - 1))) == (1<<(Current_floor - 1)))
{
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 1000);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
__HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1, 300);
HAL_TIM_PWM_Start(&htim17, TIM_CHANNEL_1);
Set_floor &= ~(1<<(Current_floor - 1));
ucLED &= ~(1<<(Current_floor - 1));
uwTick_Time_Count = uwTick;
Status_Ctrl = 7;
}
else
{
Status_Ctrl = 4;
}
break;
case 7:
if((uwTick - uwTick_Time_Count)>=4000)
{
HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(&htim17, TIM_CHANNEL_1);
uwTick_Time_Count = uwTick;
Status_Ctrl = 8;
}
break;
case 8:
if(Set_floor)
{
if((uwTick - uwTick_Time_Count)>=2000)
Status_Ctrl = 2;
}
else
{
Status_Ctrl = 0;
}
break;
}
}
}
void LCD_Proc(void)
{
if((uwTick - uwTick_LCD_Speed_Ctrl)<300) return;
uwTick_LCD_Speed_Ctrl = uwTick;
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Current Floor");
LCD_DisplayStringLine(Line2 , LCD_String_Disp);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " %01d",Current_floor);
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);
memset(LCD_String_Disp , 0 ,sizeof(LCD_String_Disp));
sprintf((char*)LCD_String_Disp , " Status Test:%01d",Status_Ctrl);
LCD_DisplayStringLine(Line9 , LCD_String_Disp);
}