最近在弄单片机的串口,想使用单片机的中断接收字符串,在网上查阅资料,汇总了一下,单片机型号为stm32f103rct6,IDE为keil5版本。
代码示例如下:
首先是串口初始化:里面包含了GPIO、NVIC和USART初始化的结构体及设置。
void MyUSART_Init()
{
/* 定义GPIO、NVIC和USART初始化的结构体 */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 使能GPIO和USART的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 将USART TX(A9)的GPIO设置为推挽复用模式 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 将USART RX(A10)的GPIO设置为浮空输入模式 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 配置串口 */
USART_InitStructure.USART_BaudRate=115200; //波特率了设置为115200
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //不使用硬件流控制
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //使能接收和发送
USART_InitStructure.USART_Parity=USART_Parity_No; //不使用奇偶校验位
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //字长设置为8位
USART_Init(USART1, &USART_InitStructure);
/* Usart1 NVIC配置 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
/*初始化串口,开启串口接收中断 */
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
/* 使能串口1 */
USART_Cmd(USART1,ENABLE);
}
接下来是USART1中断服务函数
/* USART1中断函数 */
void USART1_IRQHandler(void)
{
uint8_t ucTemp; //接收数据
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
ucTemp = USART_ReceiveData(USART1);
USART_SendData(USART1,ucTemp);
}
}
主函数main.c代码如下:
#include "stm32f10x.h"
#include "usart.h"
int main(void)
{
MyUSART_Init();
while(1)
{}
}
全部代码如下:
usart.c
#include "usart.h"
//重定向C库函数printf到串口,重定向后可使用printf函数
int fputc(int ch,FILE *f)
{
/* 发送一个字节数据到串口 */
USART_SendData(USART1,(uint8_t) ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return (ch);
}
//重定向C库函数scanf到串口,重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
/* 等待串口输入数据 */
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return (int)USART_ReceiveData(USART1);
}
void MyUSART_Init()
{
/* 定义GPIO、NVIC和USART初始化的结构体 */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* 使能GPIO和USART的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 将USART TX(A9)的GPIO设置为推挽复用模式 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 将USART RX(A10)的GPIO设置为浮空输入模式 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/* 配置串口 */
USART_InitStructure.USART_BaudRate=115200; //波特率了设置为115200
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //不使用硬件流控制
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx; //使能接收和发送
USART_InitStructure.USART_Parity=USART_Parity_No; //不使用奇偶校验位
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //字长设置为8位
USART_Init(USART1, &USART_InitStructure);
/* Usart1 NVIC配置 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
/*初始化串口,开启串口接收中断 */
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
/* 使能串口1 */
USART_Cmd(USART1,ENABLE);
}
/* USART1中断函数 */
void USART1_IRQHandler(void)
{
uint8_t ucTemp; //接收数据
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
ucTemp = USART_ReceiveData(USART1);
USART_SendData(USART1,ucTemp);
}
}
/* 发送一个字节 */
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
{
/* 发送一个字节数据到USART */
USART_SendData(pUSARTx,ch);
/* 等待发送数据寄存器为空 */
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
/* 发送字符串 */
void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
{
unsigned int k=0;
do
{
Usart_SendByte( pUSARTx, *(str + k) );
k++;
} while(*(str + k)!='\0');
/* 等待发送完成 */
while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
{}
}
usart.h
#ifndef __USART_H
#define __USART_H
#include "stm32f10x.h"
#include
void MyUSART_Init(void);
void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch);
void Usart_SendString( USART_TypeDef * pUSARTx, char *str);
#endif
main.c
#include "stm32f10x.h"
#include "usart.h"
int main(void)
{
MyUSART_Init();
while(1)
{
}
}