[STM32G4] Preparing for the Blue Bridge Cup Embedded --- Actual Combat --- The Twelfth Embedded Simulation Competition


Preface

In order to reduce space, the initialization of each module is in the module configuration, you can read the blog at any time, and the functions in the function will not be listed anymore, just screenshots.

1. Topic

Generally speaking, the simulation contest is very simple, and it may indicate that the test questions this year are simple (purely guesses)
. You still have to study carefully.

Function brief

Insert picture description here
Insert picture description here
Insert picture description here

2. Module initialization and function analysis

1. Initialization of the module

Modules needed: LCD, four buttons, LED, ADC (PB15), TIM

2. Module function analysis

LCD: Display-----》Display();
Button: Adjust parameters-----》KEY_Handle();KEY_Scan();
LED: Prompt function-----》LED();
ADC(PB15 ): Read the signal output by the temperature sensor-----》Get_ADC();
TIM(PA1): Output square waves of different frequencies

Three, function implementation

1.void Display(void);

void Display(void)
{
    
    
	
	if(mode == 0)
	{
    
    
		LCD_DisplayStringLine(Line1,"      Data");
		sprintf((char *)arr, " V:%.2fV",Volt);
		LCD_DisplayStringLine(Line3,arr);
		sprintf((char *)arr, " A:%d",Condition);
		LCD_DisplayStringLine(Line4,arr);
	}
	else if(mode == 1)
	{
    
    
		LCD_DisplayStringLine(Line1,"      Para");
		sprintf((char *)arr, " Vmax:%.1fV",Volt_Range[1]);
		LCD_DisplayStringLine(Line3,arr);
		sprintf((char *)arr, " Vmin:%.1fV",Volt_Range[0]);
		LCD_DisplayStringLine(Line4,arr);
	}
}

2.uint8_t KEY_Scan(uint8_t mode);

uint8_t KEY_Scan(uint8_t mode)
{
    
    
	static uint8_t flag=1;
	if(mode)	flag = 1;
	if(flag &&(KEY_B1 == 0 || KEY_B2	== 0 || KEY_B3 == 0 ||	KEY_B4== 0 ))
	{
    
    
		HAL_Delay(10);
		flag = 0;
		if (KEY_B1 == 0)	return B1_Press;
		else if (KEY_B2 == 0) return B2_Press;
		else if (KEY_B3 == 0) return B3_Press;
		else if (KEY_B4 == 0) return B4_Press;
	}else if(KEY_B1 == KEY_B2 == KEY_B3 == KEY_B4 == 1)	flag = 1;
	return 0;
}

3.void KEY_Handle(uint8_t key);

For the three buttons, different buttons correspond to different operations. In
this simulation question, there is only one misoperation prevention. You only need to record the voltage range when B1 is pressed.

void KEY_Handle(uint8_t key)
{
    
    
	static float i, j;
	if(key == B1_Press)
	{
    
    
		if(mode == 0)
		{
    
    
			i = Volt_Range[0];
			j = Volt_Range[1];
			mode = 1;
		}
		else
		{
    
    
			if(Volt_Range[1] - Volt_Range[0] < 0.5f)
			{
    
    
				Volt_Range[0] = i;
				Volt_Range[1] = j;
			}
			mode = 0;
		}
		LCD_Clear(White);
	}
	else if(key == B2_Press && mode == 1)
	{
    
    
		if(Volt_Range[1] == 0)
			Volt_Range[1] = 3.3f;
		else
			Volt_Range[1] = Volt_Range[1] - 0.1f;
		
	}
	else if(key == B3_Press && mode == 1)
	{
    
    
		if(Volt_Range[0] == 3.3f)
			Volt_Range[0] = 0;
		else
			Volt_Range[0] = Volt_Range[0] + 0.1f;
	}
}

4.uint16_t Get_ADC(void);

Read the voltage value, the voltage value is Get_ADC()*3.3/4096;

uint16_t Get_ADC(void)
{
    
    
	uint16_t temp = 0 ;
	HAL_ADC_Start(&hadc2);
	temp = HAL_ADC_GetValue(&hadc2);
	HAL_ADC_Stop(&hadc2);
	
	return temp;
}

5.int main(void);

Insert picture description here

initialization;

	LCD_Init();
	LCD_Clear(White);
	LCD_SetTextColor(Black);

	HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
	HAL_ADC_Init(&hadc2);

	Volt_Range[0] = 1;
	Volt_Range[1] = 3;
	Condition = 1;
	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
                          |GPIO_PIN_9|GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);

while(1);

Insert picture description here
Here are some explanations for the timer output PWM wave.
ARR is the loaded value, PSC is the prescaler coefficient, CCR1 is the comparison value of channel 1,
that is, counts until CCR1 outputs high (low), and CCR1 to ARR are low (high) , Here depends on the configuration mode

		Display();
		key = KEY_Scan(0);
		KEY_Handle(key);
		Volt = Get_ADC()*3.3/4096;
		
		if(Volt < Volt_Range[0])
			Condition_Stay = 1;
		else if(Volt > Volt_Range[1])
			Condition_Stay = 3;
		else
			Condition_Stay = 2;
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
		/* 输出要求 */
		if(Condition_Stay == 1 && Condition_Stay != Condition)
		{
    
    
			Condition = Condition_Stay;
			TIM2->ARR = 1000-1;
			TIM2->PSC = 800-1;
			TIM2->CCR2 = 500;
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_10
                          |GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
		}
		else if(Condition_Stay == 2 && Condition_Stay != Condition)
		{
    
    
			Condition = Condition_Stay;
			TIM2->ARR = 1000-1;
			TIM2->PSC = 80-1;
			TIM2->CCR2 = 800;
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
                          |GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
		}
		else if(Condition_Stay == 3 && Condition_Stay != Condition)
		{
    
    
			Condition = Condition_Stay; 
			TIM2->ARR = 1000-1;
			TIM2->PSC = 8-1;
			TIM2->CCR2 = 200;
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_10, GPIO_PIN_RESET);
			HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_8
                          |GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_12, GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
			HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
		}
  }

to sum up

It's the first time to participate in a simulation contest, hey, I feel that jio is still different.
I finished it yesterday, I haven't had time to write, forgive me, hehe
Like it, I haven't liked it lately, it's refreshing. ┭┮﹏┭┮

Guess you like

Origin blog.csdn.net/qq_47877230/article/details/115009003