单片机串口通信实验 实现串口接收字符:如果接收到A,led1闪烁,如果接收到B,led2闪烁,如果接收到D,两个一起闪烁。如果接收到P,蜂鸣器蜂鸣,如果接收到M蜂鸣器停止。 当按下按键KEY0时发送你的学号到上位机。 定时器实验: 利用通用定时器6,定时2秒钟,时间到,通过串口向上位机发送发送你的学号,同时,led0亮灭一次。的详细代码 其中波特率为115200
时间: 2023-07-16 19:14:10 浏览: 40
以下是基于STM32F103单片机的代码实现,使用USART1作为串口通信接口,PA0作为按键输入,PC13、PC14作为LED输出,PB8作为蜂鸣器输出,使用TIM6作为定时器。
```
#include "stm32f10x.h"
#include "stdio.h"
#define USART1_DR_Base 0x40013804 //USART1数据寄存器地址
void USART1_Config(void);
void GPIO_Config(void);
void TIM6_Config(void);
int main(void)
{
USART1_Config(); //配置USART1串口
GPIO_Config(); //配置GPIO口
TIM6_Config(); //配置定时器6
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) //如果接收到数据
{
char data = USART_ReceiveData(USART1); //读取接收到的数据
switch (data)
{
case 'A':
GPIO_SetBits(GPIOC, GPIO_Pin_13); //LED1亮
GPIO_ResetBits(GPIOC, GPIO_Pin_14); //LED2灭
GPIO_SetBits(GPIOB, GPIO_Pin_8); //蜂鸣器亮
break;
case 'B':
GPIO_ResetBits(GPIOC, GPIO_Pin_13); //LED1灭
GPIO_SetBits(GPIOC, GPIO_Pin_14); //LED2亮
GPIO_SetBits(GPIOB, GPIO_Pin_8); //蜂鸣器亮
break;
case 'D':
GPIO_SetBits(GPIOC, GPIO_Pin_13); //LED1亮
GPIO_SetBits(GPIOC, GPIO_Pin_14); //LED2亮
GPIO_SetBits(GPIOB, GPIO_Pin_8); //蜂鸣器亮
break;
case 'P':
GPIO_SetBits(GPIOB, GPIO_Pin_8); //蜂鸣器亮
break;
case 'M':
GPIO_ResetBits(GPIOB, GPIO_Pin_8); //蜂鸣器灭
break;
default:
break;
}
}
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0) //如果按键被按下
{
USART_SendData(USART1, '1'); //发送学号
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); //等待发送完成
}
}
}
void USART1_Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能USART1、GPIOA、AFIO时钟
//USART1 Tx(PA9)、Rx(PA10)引脚配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1配置
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
//USART1中断配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //打开接收中断
USART_Cmd(USART1, ENABLE); //使能USART1
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOC、GPIOB时钟
//LED1(PC13)、LED2(PC14)、蜂鸣器(PB8)引脚配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOC, GPIO_Pin_13 | GPIO_Pin_14); //LED1、LED2灭
GPIO_ResetBits(GPIOB, GPIO_Pin_8); //蜂鸣器灭
//按键PA0引脚配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM6_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //使能定时器6时钟
TIM_TimeBaseStructure.TIM_Period = 2000 - 1; //2s定时
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1; //时钟预分频为72,计数频率为10kHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); //打开更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM6, ENABLE); //使能定时器6
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除中断标志位
}
}
void TIM6_IRQHandler(void)
{
if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) //更新中断
{
TIM_ClearITPendingBit(TIM6, TIM_IT_Update); //清除中断标志位
GPIO_SetBits(GPIOC, GPIO_Pin_0); //LED0亮
GPIO_ResetBits(GPIOC, GPIO_Pin_0); //LED0灭
USART_SendData(USART1, '2'); //发送学号
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); //等待发送完成
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)