一、程序设计题
A.CubeMx
1.选用80MHZ时钟、SW线
2.GPIO配置
3.TIM2_Channel2
?4.串口USART1
? ? ? ? 选择PA9、PA10引脚,DAM传输、使能空闲中断
?
?5.中断优先级配置
B.代码部分
1.点灯、按键驱动部分2
//gpio.c
//点灯、按键检测
void LED_Toggle(uint16_t pin)
{
switch(pin){
case 1: pin =LED1_Pin;break;
case 2: pin =LED2_Pin;break;
case 3: pin =LED3_Pin;break;
case 4: pin =LED4_Pin;break;
case 5: pin =LED5_Pin;break;
case 6: pin =LED6_Pin;break;
case 7: pin =LED7_Pin;break;
case 8: pin =LED8_Pin;break;
}
HAL_GPIO_TogglePin(LED1_GPIO_Port,pin);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
void LED_Set(uint16_t pin,uint16_t state)
{
switch(pin){
case 1: pin =LED1_Pin;break;
case 2: pin =LED2_Pin;break;
case 3: pin =LED3_Pin;break;
case 4: pin =LED4_Pin;break;
case 5: pin =LED5_Pin;break;
case 6: pin =LED6_Pin;break;
case 7: pin =LED7_Pin;break;
case 8: pin =LED8_Pin;break;
}
HAL_GPIO_WritePin(LED1_GPIO_Port,pin,state);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
}
uint16_t KEY_Scan(void)
{
uint16_t val=0;
if(HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin)==RESET)
val=1;
if(HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin)==RESET)
val=2;
if(HAL_GPIO_ReadPin(KEY3_GPIO_Port,KEY3_Pin)==RESET)
val=3;
if(HAL_GPIO_ReadPin(KEY4_GPIO_Port,KEY4_Pin)==RESET)
val=4;
return val;
}
2.PWM初始化
//tim.c
//添加一行代码
/* USER CODE BEGIN TIM2_Init 2 */
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
/* USER CODE END TIM2_Init 2 */
3.串口部分初始化
//usart.h
//声明外部变量
/* USER CODE BEGIN Prototypes */
#define RXBUFFERSIZE 256
extern uint8_t RxBuffer[RXBUFFERSIZE];
extern uint16_t RxNum ;
/* USER CODE END Prototypes */
//usart.c
//定义全局变量
/* USER CODE BEGIN 0 */
uint8_t RxBuffer[RXBUFFERSIZE]= {0};
uint16_t RxNum = 0;
/* USER CODE END 0 */
//使能串口空闲中断
/* USER CODE BEGIN USART1_Init 2 */
__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);
/* USER CODE END USART1_Init 2 */
3.中断部分
//滴答计时器
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
usLed++;
usTim++;
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
//串口中断部分
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
if(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)==SET){
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
HAL_UART_DMAStop(&huart1);
static int init_falg =0;
if(!init_falg){
init_falg = 1;
RxNum = 0;
memset(RxBuffer,0,RXBUFFERSIZE);
HAL_UART_Receive_DMA(&huart1,RxBuffer,RXBUFFERSIZE);
return;
}
RxNum = RXBUFFERSIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);
RxFlag = 1;
}
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
5.主函数部分
/* USER CODE BEGIN 2 */
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
LED_Set(GPIO_PIN_All, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// sprintf((char *)ucLcd,"B1:%d B2:%d B3:%d",keyword_1,keyword_2,keyword_3);
// LCD_DisplayStringLine(Line0,ucLcd);
LED_Proc();
KEY_Proc();
TIM_Proc();
USART_Proc();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
6.函数的实现
/* USER CODE BEGIN PV */
enum sys_state{
state_psd,
state_sta
}lcd_state = state_psd;
unsigned char ucLcd[21];
uint16_t usLed,led_error_falling=0,led_start=0;
uint16_t usTim,pwm_change_flag=0;
int RxFlag=0,Rx_read_keyword=0,Rx_set_keyword=0;
unsigned char key_val,key_old,key_down,key_up;
int set_keyword_1=0,set_keyword_2=0,set_keyword_3=0;
int keyword_1=1,keyword_2=2,keyword_3=3,key_set_flag_1=0,key_set_flag_2=0,key_set_flag_3=0,keyword_great=0,keyword_error=0;
float duty;
uint16_t freq;
/* USER CODE END PV */
//printf重定向函数定义在main函数之前
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,2);
return ch;
}
/* USER CODE END 0 */
/* USER CODE BEGIN 4 */
void USART_Proc(void)
{
if(RxFlag){
RxFlag = 0;
Rx_read_keyword=0;
Rx_set_keyword=0;
if(RxNum == 7 && RxBuffer[0] >= '0' && RxBuffer [0]<='9' && RxBuffer[1] >= '0' && RxBuffer [1]<='9' && RxBuffer[2] >= '0' && RxBuffer [2]<='9'\
&& RxBuffer[3] == '-' && RxBuffer[4] >= '0' && RxBuffer [4]<='9' && RxBuffer[5] >= '0' && RxBuffer [5]<='9'&& RxBuffer[6] >= '0' && RxBuffer [6]<='9'){
sscanf((char *)RxBuffer,"%d-%d",&Rx_read_keyword,&Rx_set_keyword);
if(keyword_1*100+keyword_2*10+keyword_3 == Rx_read_keyword){
keyword_1 = Rx_set_keyword/100;
keyword_2 = Rx_set_keyword/10%10;
keyword_3 = Rx_set_keyword%10;
keyword_great=1;
keyword_error=0;
pwm_change_flag=1;
LCD_ClearLine(Line6);
lcd_state = state_sta;
__HAL_TIM_SET_AUTORELOAD(&htim2,500-1);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,50);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
usTim = 0;
usLed = 0;
}else{
keyword_error++;
keyword_great=0;
}
}else{
keyword_error++;
keyword_great=0;
}
// LCD_ClearLine(Line8);
// LCD_ClearLine(Line9);
// sprintf((char *)ucLcd,"o:%3d n:%3d R:%d",Rx_read_keyword,Rx_set_keyword,RxNum);
// LCD_DisplayStringLine(Line9,ucLcd);
// sprintf((char *)ucLcd,"RE:%s",RxBuffer);
// LCD_DisplayStringLine(Line8,ucLcd);
RxNum = 0;
memset(RxBuffer,0,RXBUFFERSIZE);
HAL_UART_Receive_DMA(&huart1,RxBuffer,RXBUFFERSIZE);
}
}
void LED_Proc(void)
{
if(keyword_great){
LED_Set(LED1_Pin,GPIO_PIN_RESET);
if(usLed < 5000)
return;
LED_Set(LED1_Pin,GPIO_PIN_SET);
}else{
if(keyword_error == 3){
if(usLed < 100)
return;
usLed=0;
led_error_falling++;
if(led_error_falling == 40){
led_error_falling=0;
LED_Set(LED2_Pin,GPIO_PIN_SET);
keyword_error=2;
return;
}
LED_Toggle(LED2_Pin);
}
}
}
void TIM_Proc(void)
{
//&htim2,TIM_CHANNEL_2
freq =1000000/( __HAL_TIM_GetAutoreload(&htim2)+1);
duty = __HAL_TIM_GetCompare(&htim2,TIM_CHANNEL_2)*100/( __HAL_TIM_GetAutoreload(&htim2)+1);
if(pwm_change_flag){
if(usTim < 5000)
return;
lcd_state = state_psd;
pwm_change_flag=0;
}else{
__HAL_TIM_SET_AUTORELOAD(&htim2,1000-1);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,500);
}
}
void KEY_Proc(void)
{
key_val=KEY_Scan();
key_down=key_val&(key_old ^ key_val);
key_up=~key_val&(key_old ^ key_val);
key_old=key_val;
switch(key_up){
case 1:
if(lcd_state == state_psd){
if(!key_set_flag_1){
set_keyword_1=-1;
key_set_flag_1=1;
}
set_keyword_1++;
if(set_keyword_1 ==10)
set_keyword_1=0;
}
break;
case 2:
if(lcd_state == state_psd){
if(!key_set_flag_2){
set_keyword_2=-1;
key_set_flag_2=1;
}
set_keyword_2++;
if(set_keyword_2 ==10)
set_keyword_2=0;
}
break;
case 3:
if(lcd_state == state_psd){
if(!key_set_flag_3){
set_keyword_3=-1;
key_set_flag_3=1;
}
set_keyword_3++;
if(set_keyword_3 ==10)
set_keyword_3=0;
}
break;
case 4:
if(lcd_state == state_psd){
if(key_set_flag_1&&key_set_flag_2&&key_set_flag_3){
if(set_keyword_1==keyword_1 &&set_keyword_2==keyword_2&&set_keyword_3==keyword_3){
lcd_state = state_sta;
keyword_great=1;
keyword_error=0;
key_set_flag_1=0;
key_set_flag_2=0;
key_set_flag_3=0;
keyword_1=set_keyword_1;
keyword_2=set_keyword_2;
keyword_3=set_keyword_3;
pwm_change_flag=1;
LCD_ClearLine(Line6);
__HAL_TIM_SET_AUTORELOAD(&htim2,500-1);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,50);
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);
usTim = 0;
usLed = 0;
}
else{
goto ee;
}
}
else{
ee: keyword_error++;
keyword_great=0;
key_set_flag_1=0;
key_set_flag_2=0;
key_set_flag_3=0;
if(keyword_error >= 3){
keyword_error=3;
led_error_falling=0;
usLed = 0;
}
}
}
break;
default:
if(lcd_state == state_psd) LCD_PSD_Proc();
else LCD_STA_Proc();
}
}
void LCD_PSD_Proc(void)
{
sprintf((char *)ucLcd," PSD");
LCD_DisplayStringLine(Line2,ucLcd);
if(key_set_flag_1){
sprintf((char *)ucLcd," B1:%d",set_keyword_1);
LCD_DisplayStringLine(Line4,ucLcd);
}
else{
sprintf((char *)ucLcd," B1:@ ");
LCD_DisplayStringLine(Line4,ucLcd);
}
if(key_set_flag_2){
sprintf((char *)ucLcd," B2:%d",set_keyword_2);
LCD_DisplayStringLine(Line5,ucLcd);
}else{
sprintf((char *)ucLcd," B2:@ ");
LCD_DisplayStringLine(Line5,ucLcd);
}
if(key_set_flag_3){
sprintf((char *)ucLcd," B3:%d",set_keyword_3);
LCD_DisplayStringLine(Line6,ucLcd);
}else{
sprintf((char *)ucLcd," B3:@ ");
LCD_DisplayStringLine(Line6,ucLcd);
}
}
void LCD_STA_Proc(void)
{
sprintf((char *)ucLcd," STA");
LCD_DisplayStringLine(Line2,ucLcd);
sprintf((char *)ucLcd," F:%4dHZ",freq);
LCD_DisplayStringLine(Line4,ucLcd);
sprintf((char *)ucLcd," D:%02d%%",(int)duty);
LCD_DisplayStringLine(Line5,ucLcd);
}
/* USER CODE END 4 */
二、选择题部分
|