为了减少篇幅,各模块的初始化均在模块配置中,可以随时去翻看博客,还有function里面的函数,将不再罗列出来,只是截图展示。
总的来说此次模拟赛十分简单,可能预示着今年考题简单(纯属猜测)
大家还是要认真备考哦
需要用的模块:LCD、四个按键、LED、ADC(PB15)、TIM
LCD:显示-----》Display();
按键:对参数进行调整-----》KEY_Handle();KEY_Scan();
LED:提示作用-----》LED();
ADC(PB15):读取温度传感器输出的信号-----》Get_ADC();
TIM(PA1): 输出不同频率的方波
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:%.1lfV",Volt_Range[1]);
LCD_DisplayStringLine(Line3,arr);
sprintf((char *)arr, " Vmin:%.1lfV",Volt_Range[0]);
LCD_DisplayStringLine(Line4,arr);
}
}
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;
}
对于三个按键,不同的按键对应不同的操作
本次模拟题,仅有一个防误操作,只需要记录B1按下时的电压范围即可
void KEY_Handle(uint8_t key)
{
static double 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.5)
{
Volt_Range[0] = i;
Volt_Range[1] = j;
}
mode = 0;
}
LCD_Clear(Black);
}
else if(key == B2_Press && mode == 1)
{
if(Volt_Range[1] == 0)
Volt_Range[1] = 3.3;
else if(Volt_Range[1] <= 0.1)
Volt_Range[1] = 0;
else if(Volt_Range[1] > 0.1)
Volt_Range[1] = Volt_Range[1] - 0.1;
}
else if(key == B3_Press && mode == 1)
{
if(Volt_Range[0] >= 3.3)
Volt_Range[0] = 0;
else
Volt_Range[0] = Volt_Range[0] + 0.1;
}
}
读取电压值,电压值为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;
}
LCD_Init();
LCD_Clear(Black);
LCD_SetTextColor(White);
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);
这里对定时器输出PWM波做一些讲解
ARR即是加载的数值,PSC是预分频系数,CCR1是通道1的比较值,
即计数到CCR1输出高(低),CCR1到ARR是低(高),这里要看配置的模式
Display();
key = KEY_Scan(0);
KEY_Handle(key);
Volt = Get_ADC()*3.3/4096;
if(Volt < Volt_Range[0])
Condition_Stay = 2;
else if(Volt > Volt_Range[1])
Condition_Stay = 1;
else
Condition_Stay = 3;
/* 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);
}
}
第一次参加模拟赛,嘿嘿,感jio还是不一样。
昨天完成了,一直没时间写,见谅,嘿嘿。
点个赞吧,最近都没赞了,心头拔凉拔凉的。┭┮﹏┭┮