main.c
#include "stm32f10x.h"
#include"uart.h"
int main(void)
{
uart_init(115200);
while(1)
{
}
}
uart.c
#include"uart.h"
void uart_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=bound;//比特率
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//硬件流
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//发送接收使能
USART_InitStructure.USART_Parity=USART_Parity_No;//奇偶校验
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//数据位长度(字长)
USART_Init(USART1,&USART_InitStructure);
//使用中断---------------------------
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//优先级分组在misc.c里,2位抢占,2位响应
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收缓冲区非空,即接收中断
//----------------------------------
USART_Cmd(USART1,ENABLE);
}
void USART1_IRQHandler(void)//中断服务函数,启动文件中找
{
u8 rec;
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//接收中断,状态检测stm32f10x_usart.h中找
{
rec=USART_ReceiveData(USART1);
// if(rec==1)
// {
USART_SendData(USART1,rec);//(DATA) <= 0x1FF
// }
}
}
uart.h
#ifndef __uart_H
#define __uart_H
#include"sys.h"
void uart_init(u32 bound);
#endif
评论(0)
您还未登录,请登录后发表或查看评论