本文主要是介绍STM32HAL库【G431】--【蓝桥杯嵌入式第十一届国赛题】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
完整工程(百度网盘免费下载,提取码:0403)和演示视频在文章末尾,需要请移步至文章末尾。
目录
- 第十一届届国赛题目
- 涉及模块主要代码
- 主要变量与结构体
- 按键扫描
- 按键处理
- ADC双通道采集
- LCD显示
- LED处理
- Puls1与Puls2输入捕获
- Frequency跟随->输出比较模式
- 完整工程文件
- 总结
第十一届届国赛题目
涉及模块主要代码
主要变量与结构体
struct keys key[4]={0,0,0};float volt[2]={0};
bool sence_flag=0;//界面模式
uchar lcd_buffer[30]={0};uint fre[2]={0};
uchar volt_led=0;
uchar fre_led=7;bool pulse_flag=0;uchar oc_duty=50;
uint oc_pulse=500;uint32_t OC_Count = 0;
bool outpulse_flag=0;
uint crr2_raise=0;
uint crr3_raise=0;
按键扫描
void Key_scan(void)//按键扫描
{key[0].sta = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0);key[1].sta = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1);key[2].sta = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2);key[3].sta = HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0);for(uchar i=0; i<4; i++){switch(key[i].judge){case 0:{if(key[i].sta==0)key[i].judge=1;}break;case 1:{if(key[i].sta==0)key[i].judge=2;else key[i].judge=0;}break;case 2:{if(key[i].sta==1){key[i].signle=1;key[i].judge=0;}}break;}}
}
按键处理
void Key_proc(void)//按键处理
{if(key[0].signle==1)//界面切换0:显示 1:参数{sence_flag=!sence_flag;key[0].signle=0;key[1].signle=0;key[2].signle=0;key[3].signle=0;}else if(key[1].signle==1 && sence_flag==1)//电压led指示{volt_led = (volt_led + 1) % 8;if(volt_led == fre_led)volt_led = (volt_led + 1) % 8;key[0].signle=0;key[1].signle=0;key[2].signle=0;key[3].signle=0;}else if(key[2].signle==1 && sence_flag==1)//频率led指示{fre_led = (fre_led + 1) % 8;if(fre_led == volt_led)fre_led = (fre_led + 1) % 8;key[0].signle=0;key[1].signle=0;key[2].signle=0;key[3].signle=0;}else if(key[3].signle==1){outpulse_flag =!outpulse_flag;key[0].signle=0;key[1].signle=0;key[2].signle=0;key[3].signle=0;}
}
ADC双通道采集
void ADC_proc(void)
{for(uchar i=0; i<2; i++){HAL_ADC_Start(&hadc2);HAL_ADC_PollForConversion(&hadc2,50);volt[i] = HAL_ADC_GetValue(&hadc2)*3.3/4096;}HAL_ADC_Stop(&hadc2);
}
LCD显示
void LCD_view(void)
{ADC_proc();if(sence_flag==0)//数据显示{sprintf((char *)lcd_buffer," DATA ");LCD_DisplayStringLine(Line1,lcd_buffer);sprintf((char *)lcd_buffer," V1:%.1fV ",volt[0]);LCD_DisplayStringLine(Line3,lcd_buffer);sprintf((char *)lcd_buffer," V2:%.1fV ",volt[1]);LCD_DisplayStringLine(Line4,lcd_buffer);sprintf((char *)lcd_buffer," F1:%dHz ",fre[0]);LCD_DisplayStringLine(Line5,lcd_buffer);sprintf((char *)lcd_buffer," F2:%dHz ",fre[1]);LCD_DisplayStringLine(Line6,lcd_buffer);}else {sprintf((char *)lcd_buffer," PARA ");LCD_DisplayStringLine(Line1,lcd_buffer);sprintf((char *)lcd_buffer," VD:LD%d ",volt_led+1);LCD_DisplayStringLine(Line3,lcd_buffer);sprintf((char *)lcd_buffer," FD:LD%d ",fre_led+1);LCD_DisplayStringLine(Line4,lcd_buffer);sprintf((char *)lcd_buffer," ");LCD_DisplayStringLine(Line5,lcd_buffer);sprintf((char *)lcd_buffer," ");LCD_DisplayStringLine(Line6,lcd_buffer);}}
LED处理
void LED_dis(uchar dis)//LED
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_8 |GPIO_PIN_10 |GPIO_PIN_12 |GPIO_PIN_14 |GPIO_PIN_9 |GPIO_PIN_11 |GPIO_PIN_13 |GPIO_PIN_15 ,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2 ,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2 ,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOC,dis << 8 ,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2 ,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2 ,GPIO_PIN_RESET);
}
void LED_proc(void)
{uchar led=0x00;if(volt[0] > volt[1]) led = (0x01 << volt_led);else led = (0x00 << (volt_led -1));if(fre[0] > fre[1]) led |= (0x01 << fre_led);else led |= (0x00 << fre_led);LED_dis(led);
}
Puls1与Puls2输入捕获
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{if(htim->Instance==TIM2){if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_2){crr2_raise = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_2);__HAL_TIM_SetCounter(&htim2,0);fre[0] = (80000000/80)/crr2_raise;HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);}}else if(htim->Instance==TIM15){if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1){crr3_raise = HAL_TIM_ReadCapturedValue(&htim15,TIM_CHANNEL_1);__HAL_TIM_SetCounter(&htim15,0);fre[1] = (80000000/80)/crr3_raise;HAL_TIM_IC_Start_IT(&htim15,TIM_CHANNEL_1);}}
}
Frequency跟随->输出比较模式
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim)//输出比较
{OC_Count = __HAL_TIM_GET_COUNTER(&htim3);if(htim->Instance == TIM3){if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2){if(outpulse_flag==0)oc_pulse=(80000000/80)/fre[0];else oc_pulse=(80000000/80)/fre[1];if(pulse_flag==1)//低电平所占时间{TIM3->CCR2 = OC_Count + oc_pulse - oc_duty*oc_pulse/100;pulse_flag=!pulse_flag;}else //高电平所占时间{pulse_flag=!pulse_flag;TIM3->CCR2 = OC_Count + oc_duty*oc_pulse/100;}}}
}
完整工程文件
总结
自己写的代码,不喜勿喷,欢迎参考呀
以上就是本次的全部内容了,笔者水平有限,仅供参考。
想联系笔者请私信就好。
这篇关于STM32HAL库【G431】--【蓝桥杯嵌入式第十一届国赛题】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!