一、题目介绍
按键输入:短按
模拟电压输出:ADC
LCD显示
PWM输出:PA6,PA7
二、usr.c
#include "usr.h"
#include "lcd.h"
#include "stdio.h"
#include "tim.h"
#include "adc.h"
/*value-------------------------------------------------*/
uint8_t menu = 0;
uint8_t mode = 0;
float V;/*define------------------------------------------------*/
#define MENU_DATA 0
#define MENU_PARA 1#define MODE_AUTO 0
#define MODE_MANU 1
/*define */
#define B4_Pin GPIO_PIN_0
#define B4_GPIO_Port GPIOA
#define B1_Pin GPIO_PIN_0
#define B1_GPIO_Port GPIOB
#define B2_Pin GPIO_PIN_1
#define B2_GPIO_Port GPIOB
#define B3_Pin GPIO_PIN_2
#define B3_GPIO_Port GPIOB
/*define */
#define KEY_LONG_TIME 200
#define KEY_SHORT_TIME 50
/*define */
#define PA7_BASE (500-1)
#define PA6_BASE (1000-1)
#define PA6_BUJIN 1000
#define PA7_BUJIN 500struct keys key[4]={0,0,0,0};
float PA6_duty = 10;
float PA7_duty = 10;
//led
void led_set(uint8_t display)
{HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC,display<<8,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
//display
void display_init(void)
{LCD_Clear(Black);LCD_SetBackColor(Black);LCD_SetTextColor(White);
}
void menu_display(void)
{uint8_t text[25];if(menu == MENU_DATA){sprintf((char*)text," Data ");LCD_DisplayStringLine(Line0,text);sprintf((char*)text," V:%.2f",V);LCD_DisplayStringLine(Line2,text);if(mode == MODE_MANU) {sprintf((char*)text," Mode:MANU ");}else {sprintf((char*)text," Mode:AUTO ");}LCD_DisplayStringLine(Line4,text);}else if(menu == MENU_PARA){sprintf((char*)text," Para ");LCD_DisplayStringLine(Line0,text);if(mode == MODE_AUTO){sprintf((char*)text," PA6:%.f%% ",PA6_duty*100);LCD_DisplayStringLine(Line2,text);sprintf((char*)text," PA7:%.f%% ",PA7_duty*100);LCD_DisplayStringLine(Line4,text);}else{sprintf((char*)text," PA6:%.f%% ",PA6_duty);LCD_DisplayStringLine(Line2,text);sprintf((char*)text," PA7:%.f%% ",PA7_duty);LCD_DisplayStringLine(Line4,text);}} if((mode == MODE_AUTO)&&(menu == MENU_DATA)){led_set(0x03);}else if((mode != MODE_AUTO)&&(menu == MENU_DATA)){led_set(0x02);}else if((mode == MODE_AUTO)&&(menu == MENU_PARA)){led_set(0x01);}else if((mode == MODE_MANU)&&(menu == MENU_PARA)){led_set(0x00);} if(mode == MODE_AUTO){PA6_duty = V/3.3;PA7_duty = V/3.3; if(V <= 0){__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,0); __HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,0);}else if(V >= 3.3){__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,5000-1);__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,10000-1);}else{__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,10000*PA6_duty - 1);__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,5000*PA7_duty - 1);}}
}
void get_ADCvalue()
{HAL_ADC_Start(&hadc2);uint32_t val = HAL_ADC_GetValue(&hadc2);V = val*3.3/(4096.0-1.0);
}//按键控制函数
void key_proc(void)
{get_ADCvalue(); //adc获取电压值if(key[0].single_flag == 1){key[0].single_flag = 0;menu ++;if(menu > 1){menu = MENU_DATA;}}else if(key[1].single_flag == 1){key[1].single_flag = 0;//PA6占空比调整static uint8_t num_pa6 = 0;num_pa6 ++;num_pa6 %= 9;PA6_duty = (num_pa6 + 1)*10;__HAL_TIM_SetCompare(&htim3,TIM_CHANNEL_1,PA6_BASE+num_pa6*PA6_BUJIN);}else if(key[2].single_flag == 1){key[2].single_flag = 0;//PA7占空比调整static uint8_t num_pa7 = 0;num_pa7 ++;num_pa7 %= 9;PA7_duty = (num_pa7 + 1)*10;__HAL_TIM_SetCompare(&htim17,TIM_CHANNEL_1,PA7_BASE+num_pa7*PA7_BUJIN);}else if(key[3].single_flag == 1){key[3].single_flag = 0;mode ++;if(mode > MODE_MANU){mode = MODE_AUTO;}}
}
//定时器中断服务回调函数
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{if(htim -> Instance == TIM6){key[0].key_sta = HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin);key[1].key_sta = HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin);key[2].key_sta = HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin);key[3].key_sta = HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin);for(uint8_t i = 0;i < 4;i++){switch(key[i].key_judge_sta){case 0:{if(key[i].key_sta == 0){key[i].key_judge_sta = 1;}else{key[i].key_judge_sta = 0;} }break;case 1:{if(key[i].key_sta == 0){key[i].key_judge_sta = 2;}else{key[i].key_judge_sta = 0;}}break;case 2:{if(key[i].key_sta == 1){key[i].key_judge_sta = 0;key[i].single_flag = 1;}}break;}}}}
三、usr.h
#ifndef __USR_H__
#define __USR_H__#ifdef __cplusplus
extern "C" {
#endif/* Includes ------------------------------------------------------------------*/
#include "main.h"/* struct --------------------------------------------------------------------*/
struct keys{uint8_t single_flag;// uint8_t long_flag;uint8_t key_sta;uint8_t key_judge_sta;uint8_t key_time;
};void led_set(uint8_t display);
//display
void display_init(void);
void menu_display(void);
void display_init(void);
//按键控制函数
void key_proc(void); #ifdef __cplusplus
}
#endif
#endif
四、 开启函数
LCD_Init();display_init();led_set(0x00);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); //PA6--PWM输出HAL_TIM_PWM_Start(&htim17,TIM_CHANNEL_1); //PA7--PWM输出HAL_TIM_Base_Start_IT(&htim6); //开启按键扫描函数
五、while内容
menu_display();
key_proc();
六、总结
虽然只有PWM输出,但是在比赛中也最好也写输入捕获,来查看pwm输出是否频率和占空比是否正确