单片机
返回首页

stm32 通过串口进行信息交流 并控制led亮灭

2019-01-14 来源:eefocus

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 */

 

}

进入单片机查看更多内容>>
相关视频
  • RISC-V嵌入式系统开发

  • SOC系统级芯片设计实验

  • 云龙51单片机实训视频教程(王云,字幕版)

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

精选电路图
  • 光控音效发生器电路

  • 非常简单的150W功放电路图

  • 一个简单的警笛电路图

  • 一个简单的立体声平衡指示器电路

  • 分享一个电网倾角计电路

  • 使用NE555和磁簧开关的橱柜照明电路

    相关电子头条文章