logo
carrot

太阳当空照,花儿对我笑

stm32uart

7/7/2023, 10:33:27 PM
  1. 首页
  2. /
  3. 正文
#include "delay.h"
#include "usart.h"
#include "rtc.h"
#include "led.h"
#include "key.h"
#include "buzzer.h"

int main() {
	u8 a;
	//初始化程序
	RCC_Configuration(); //时钟设置
	LED_Init(); //LED初始化
	KEY_Init(); //按键初始化
	BUZZER_Init(); //蜂鸣器初始化
	USART1_Init(); //串口初始化(参数是波特率)

	//主循环
	while (1) {

		//查询方式接收
		if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) {
			//查询串口待处理标志位
			a = USART_ReceiveData(USART1); //读取接收到的数据
			switch (a) {
			case '0':
				GPIO_WriteBit(LEDPORT, LED1, (BitAction)(0)); //LED控制
				USART1_printf("%c:LED1 OFF ", a); //
				break;
			case '1':
				GPIO_WriteBit(LEDPORT, LED1, (BitAction)(1)); //LED控制
				USART1_printf("%c:LED1 ON ", a); //
				break;
			case '2':
				BUZZER_BEEP1(); //蜂鸣一声
				USART1_printf("%c:BUZZER ", a); //把收到的数据发送回电脑
				break;
			default:
				break;
			}		  
		}

		//按键控制
		if (!GPIO_ReadInputDataBit(KEYPORT, KEY1)) {
			//读按键接口的电平
			delay_ms(20); //延时20ms去抖动
			if (!GPIO_ReadInputDataBit(KEYPORT, KEY1)) {
				//读按键接口的电平
				while (!GPIO_ReadInputDataBit(KEYPORT, KEY1)) ; //等待按键松开 
				USART1_printf("KEY1 "); //
			}
		}		 
		if (!GPIO_ReadInputDataBit(KEYPORT, KEY2)) {
			//读按键接口的电平
			delay_ms(20); //延时20ms去抖动
			if (!GPIO_ReadInputDataBit(KEYPORT, KEY2)) {
				//读按键接口的电平
				while (!GPIO_ReadInputDataBit(KEYPORT, KEY2)) ; //等待按键松开 
				USART1_printf("KEY2");
			}
		}
	}
}
热门文章
标签云
© 2021 Copyright 本站由 upyun 提供储存服务