h 关闭黄色
l 关闭蓝色
g 关闭绿色
a 打开全部
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();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) //ËÀÑ»· ²é¿´Ö´ÐÐЧ¹û
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
uint8_t ch[10]="";
HAL_UART_Receive(&huart1, ch, 10, 200);
if(ch[0] == 'l')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
HAL_UART_Transmit(&huart1, " blue off ", 10, 200);
HAL_Delay(500);
}
else if(ch[0]=='g')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
HAL_UART_Transmit(&huart1, " grean off ", 10, 200);
HAL_Delay(500);
}
else if(ch[0]=='h')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
HAL_UART_Transmit(&huart1, " yellow off ", 10, 200);
HAL_Delay(500);
}
else if(ch[0]=='a')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_RESET);
HAL_UART_Transmit(&huart1, " all led on ", 10, 200);
HAL_Delay(500);
}
}
/* USER CODE END 3 */
}
简单开关控制
#include "main.h"
#include "stm32f0xx_hal.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart1;
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
*
* @retval None
*/
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();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
uint8_t data = 0;
HAL_UART_Receive(&huart1, &data, 1, 0);
if(data == 'l')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_RESET);
HAL_UART_Transmit(&huart1, &data, 1, 0);
}
else if(data == 'h')
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_SET);
HAL_UART_Transmit(&huart1, &data, 1, 0);
}
}
/* USER CODE END 3 */
}
上一篇:STM32F407ZET6 用串口通过键盘控制LED灯的亮灭
下一篇:STM32延时消除抖动个人理解
推荐阅读最新更新时间:2024-03-16 16:22
设计资源 培训 开发板 精华推荐
- 学习赢京东卡 | 从12V电池及供电网络优化的角度分析电动汽车E/E架构的趋势
- 似兰斯馨,一个老电子工程师的点滴足迹
- 瑞萨电子颠覆传统理念的嵌入式开发平台 Renesas Synergy™ 评估套件震撼来袭!答题即可免费申请!
- 双11之单片机狂欢:11.11元包邮拇指板STM32L011,抢完为止
- 直播已结束【借助 TI DLP 技术,开始您的下一代4K显示应用设计】
- 有奖直播:NXP 汽车系统电源管理开讲啦,从功能安全架构到新品FS26,预约有礼~
- ADI 新一期《模拟对话》上线,免费下载ing!
- 6月4日上午10:00直播:英飞凌栅极驱动芯片的应用以及安富利对应的解决方案
- 端午节芯币兑换礼品专场