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