练习要求:
1、通过扩展板按键key1,开/关元器件;
2、通过串口选择模式(通过中断实现):
LED: 切换只控制LED;
FAN: 切换只控制FAN;
BEE: 切换只控制蜂蜜器;
3、通过底板的USER按键,切换工作模式,按一次切换一个模式,模式类型见2.
1、分析原理图得到以下结论
根据上表,通过CubeMax配置功能(略)
2、keil添加自定义my_prps.h和my_prps.c
封装以实现功能:
1)封装LED1、LED3、FAN和BEE的启动、关闭和切换
2)实现mode_set、mode_change(切换模式)
2.1 my_prps.h
#ifndef _MY_PRPS_H_
#define _MY_PRPS_H_void led_on(int ledno);
void led_off(int ledno);
void led_toggle(int ledno);void fan_on();
void fan_off();
void fan_toggle();void bee_on();
void bee_off();
void bee_toggle();void mode_set(int mm);
void mode_change();
void mode_toggle();#endif
2.2 my_prps.c
#include "my_prps.h"
#include "gpio.h"
#include <stdio.h>static int mode=1;//1-LED, 2-FAN, 3-BEEvoid led_on(int ledno){if(ledno == 1){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET);return;}if(ledno == 3){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);return;}
}void led_off(int ledno){if(ledno == 1){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_RESET);return;}if(ledno == 3){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);return;}
}
void led_toggle(int ledno){if(ledno == 1){HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_4);return;}if(ledno == 3){HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);return;}
}void fan_on(){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET);
}void fan_off(){HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_RESET);
}void fan_toggle(){HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_6);
}void bee_on(){HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
}void bee_off(){HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
}void bee_toggle(){HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_15);
}void mode_set(int mm){printf("即将设置模式为[%d]\n\r", mm);mode = mm;led_off(1);led_off(3);fan_off();bee_off();
}void mode_change(){mode++;mode=(mode%3)+1;mode_set(mode);
}void mode_toggle(){switch(mode){case 1:led_toggle(1);led_toggle(3);break;case 2:fan_toggle();break;case 3:bee_toggle();break;}
}
3、在main.c中 重写uart1、按键key1和按键User的中断回调函数
main.c
char buf[32]={0};
//int bufIndex=0;void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{printf("buf=[%s]\n\r", buf);HAL_UART_Receive_IT(&huart1, (uint8_t*)&buf, 3);if(strcmp(buf, "LED")==0){printf("即将切换至[LED]模式\n\r");mode_set(1);return;}if(strcmp(buf, "FAN")==0){printf("即将切换至[FAN]模式\n\r");mode_set(2);return;}if(strcmp(buf, "BEE")==0){printf("即将切换至[BEE]模式\n\r");mode_set(3);return;}
}int fputc(int ch, FILE * fp){HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 10);return ch;
}//KEY1和USER button interrupt
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin){printf("rising callback:[%d]\n\r", GPIO_Pin);if(GPIO_Pin == GPIO_PIN_9){//KEY1 Button pressedmode_toggle();return;}if(GPIO_Pin == GPIO_PIN_12){//User Button pressedmode_change();return;}
}int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* Configure the System Power */SystemPower_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_ICACHE_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */HAL_UART_Receive_IT(&huart1, (uint8_t*)&buf, 3);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE *//*HAL_GPIO_WritePin(GPIOC, GPIO_PIN_4, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);HAL_GPIO_WritePin(GPIOC, GPIO_PIN_6, GPIO_PIN_SET);HAL_Delay(2000);HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);*/while (1){//printf("hello world\n\r");//HAL_Delay(1000);/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}