Stm32RS232串口通信——中斷接收發(fā)送數(shù)據(jù)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
main.c配置:
/*
*說(shuō)明:
*PA0:KEY1;PA1:KEY2;
*PA2:LED1;PA3:LED2;
*PA9:USART1_TX;PA10:USART1_RX
*/
#include"stm32f10x.h"
#include"stm32f10x_rcc.h"
#include"stm32f10x_gpio.h"
#include"stm32f10x_usart.h"
#include"stm32f10x_exti.h"
#include"system_stm32f10x.h"
#include"misc.h"
voidRCC_Configuration(void);
voidGPIO_Configuration(void);
voidUSART_Configuration(void);
voidNVIC_Configuration(void);
intmain()
{
SystemInit();//72m
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();
while(1);
}
voidRCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
}
voidGPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//管腳9
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//復(fù)用推挽輸出
GPIO_Init(GPIOA,&GPIO_InitStructure);//TX初始化
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//管腳10
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空輸入
GPIO_Init(GPIOA,&GPIO_InitStructure);//RX初始化
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
voidUSART_Configuration(void)//串口初始化函數(shù)
{
//串口參數(shù)初始化
USART_InitTypeDefUSART_InitStructure;//串口設(shè)置恢復(fù)默認(rèn)參數(shù)
//初始化參數(shù)設(shè)置
USART_InitStructure.USART_BaudRate=9600;//波特率9600
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//字長(zhǎng)8位
USART_InitStructure.USART_StopBits=USART_StopBits_1;//1位停止字節(jié)
USART_InitStructure.USART_Parity=USART_Parity_No;//無(wú)奇偶校驗(yàn)
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//無(wú)流控制
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//打開(kāi)Rx接收和Tx發(fā)送功能
USART_Init(USART1,&USART_InitStructure);//初始化
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//允許串口1發(fā)送中斷。
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//允許串口1接收中斷。
USART_Cmd(USART1,ENABLE);//啟動(dòng)串口
USART_ClearFlag(USART1,USART_FLAG_TC);//發(fā)送完成標(biāo)志位
}
voidNVIC_Configuration(void)//配置中斷優(yōu)先級(jí)
{
NVIC_InitTypeDefNVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
stm32f10x_it.c的中斷函數(shù)接收發(fā)送數(shù)據(jù)(接收和發(fā)送的時(shí)候改變LED狀態(tài)):
#include"..includestm32f10x.h"
#include"..includestm32f10x_it.h"
#include"..includestm32f10x_gpio.h"
#include"..includestm32f10x_usart.h"
voidUSART1_IRQHandler(void)
{
u8RX_dat;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//USART_IT_RXNE:接收中斷
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
RX_dat=USART_ReceiveData(USART1);
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET){}//接收數(shù)據(jù)寄存器非空標(biāo)志位
GPIO_WriteBit(GPIOA,GPIO_Pin_2,(BitAction)(Bit_SET-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)));
USART_SendData(USART1,RX_dat);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET){}//發(fā)送數(shù)據(jù)寄存器空標(biāo)志位
GPIO_WriteBit(GPIOA,GPIO_Pin_3,(BitAction)(Bit_SET-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3)));
}
}