第八届省赛真题
主函数部分代码
#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;
uint8_t ucLED;
uint8_t key_value,key_up,key_down;
static uint8_t key_old;
uint8_t LCD_String_Disp[21];
RTC_TimeTypeDef T;
RTC_DateTypeDef D;
uint8_t Current_platform = 1;
uint8_t Set_platform;
__IO uint32_t uwTick_Time_Count;
uint8_t State_Num;
uint8_t Direction;
uint8_t Flow_LED = 0x10;
void SystemClock_Config(void);
void LED_Proc(void);
void KEY_Proc(void);
void LCD_Proc(void);
void Lift_State_Proc(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
LED_KEY_Init();
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(Magenta);
RTC_Init();
TIM3_Init();
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;
LED_Disp(ucLED);
}
void KEY_Proc(void)
{
if((uwTick - uwTick_KEY_Speed_Ctrl)<100) return;
uwTick_KEY_Speed_Ctrl = uwTick;
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)
{
if(Current_platform != 1) Set_platform |= 0x01;
}
else if(key_down == 2)
{
if(Current_platform != 2) Set_platform |= 0x02;
}
else if(key_down == 3)
{
if(Current_platform != 3) Set_platform |= 0x04;
}
else if(key_down == 4)
{
if(Current_platform != 4) Set_platform |= 0x08;
}
}
ucLED &= 0xF0;
ucLED |= Set_platform;
if(key_down != 0)
{
uwTick_Time_Count = uwTick;
}
}
void LCD_Proc(void)
{
if((uwTick - uwTick_LCD_Speed_Ctrl)<100) return;
uwTick_LCD_Speed_Ctrl = uwTick;
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 ");
LCD_DisplayStringLine(Line8 , LCD_String_Disp);
}
}
void Lift_State_Proc(void)
{
if(Set_platform)
{
switch(State_Num)
{
case 0:
if((uwTick - uwTick_Time_Count)>=1000)
State_Num = 1;
else
break;
case 1:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
__HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1,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;
State_Num = 2;
case 2:
if((uwTick - uwTick_Time_Count)>4000)
{
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:
if(Set_platform > ( 1<<(Current_platform-1) ) )
{
Direction = 1;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1,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) ) )
{
Direction = 2;
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1,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;
State_Num = 4;
case 4:
if((uwTick - uwTick_Time_Count)>=6000)
{
if(Direction == 1)
{
Current_platform += 1;
}
else if(Direction == 2)
{
Current_platform -= 1;
}
ucLED &= 0x0F;
Flow_LED = 0x10;
State_Num = 5;
}
else
{
if(Direction == 1)
{
Flow_LED = Flow_LED>>1;
if(Flow_LED == 0x08)
Flow_LED = 0x80;
ucLED &= 0x0F;
ucLED |= Flow_LED;
}
else if(Direction == 2)
{
ucLED &= 0x0F;
ucLED |= Flow_LED;
Flow_LED = Flow_LED<<1;
if(Flow_LED == 0x00)
Flow_LED = 0x10;
}
HAL_Delay(300);
break;
}
case 5:
if(( 1<<(Current_platform-1) ) == Set_platform)
{
HAL_TIM_PWM_Stop(&htim3 , TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
__HAL_TIM_SET_COMPARE(&htim17, TIM_CHANNEL_1,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;
State_Num = 6;
}
else
{
State_Num = 3;
break;
}
case 6 :
if((uwTick - uwTick_Time_Count)>=4000)
{
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)));
ucLED &= 0xF0;
ucLED |= Set_platform;
LED_Disp(ucLED);
State_Num = 7;
}
else
break;
case 7:
if( Set_platform != 0 )
{
uwTick_Time_Count = uwTick;
State_Num = 8;
}
else
{
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)
{
State_Num = 1;
break;
}
else
break;
}
}
}
|