当前位置:网站首页>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

 Insert picture description here
 Insert picture description here

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;
		}
	}
	
}
原网站

版权声明
本文为[lzya.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140147105835.html