#include "stm32f10x.h"
void usart_init(uint32_t bound);
void USART_IRQHandler(void);
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
usart_init(9600);
while(1);
return 0;
}
//串口初始化函数
void usart_init(uint32_t bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA,ENABLE);
//串口复位
USART_DeInit(USART1);
//初始化PA9    A10
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);
//初始化串口参数
USART_InitStructure.USART_BaudRate=bound;
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);
//开启中断 接收数据中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//初始化NVIC
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//串口使能
USART_Cmd(USART1,ENABLE);
}
//串口中断函数
void USART_IRQHandler(void)
{
uint8_t res;
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)!=RESET)
{
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);//这里简单的写了一下 不知道为什么实现不了呢
}
}
|