新手上路
- 积分
- 28
- 金钱
- 28
- 注册时间
- 2020-9-11
- 在线时间
- 7 小时
|
最近在做一个东西,需要用到串口,配置串口的时候总是出错,一个一个检查又感觉没有问题,实在找不到错误了,求大佬帮看看是哪里配置错了。
#include "sys.h"
#include "usart.h"
#include "stdio.h"
#include "string.h"
#include "stm32f10x_tim.h"
unsigned char uart1_getok;
//////////////////////////////////////////////////////////////////
//加入以下代码,支持printf函数,而不需要选择use MicroLIB
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
#endif
void UART1_send_byte(char data)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void UART2_send_byte(char data)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, data);
}
//串口1中断服务程序
//注意,读取USARTx->SR能避免莫名其妙的错误
char RxCounter,RxBuffer[100]; //接收缓冲,最大USART_REC_LEN个字节.
char RxCounter1,RxBuffer1[100]; //接收缓冲,最大USART_REC_LEN个字节.
//接收状态
//bit15, 接收完成标志
//bit14, 接收到0x0d
//bit13~0, 接收到的有效字节数目
u16 USART_RX_STA=0; //接收状态标记
extern u8 Timeout;
//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_DeInit(USART1); //复位串口1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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);//开启接收中断
USART_Cmd(USART1, ENABLE); //使能串口
}
//初始化IO 串2
//bound:波特率
//void uart2_init(u32 bound)
//{
// //GPIO端口设置
// GPIO_InitTypeDef GPIO_InitStructure;
// USART_InitTypeDef USART_InitStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
//
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能,GPIOA时钟
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//USART2
// USART_DeInit(USART2); //复位串口2
// //USART2_TX PA.2
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
// GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
//
// //USART2_RX PA.3
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
// GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3
// //Usart1 NVIC 配置
// NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0
// NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
// NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//
// //USART 初始化设置
// USART_InitStructure.USART_BaudRate = bound;//115200
// USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
// 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(USART2, &USART_InitStructure); //初始化串口
//
// USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
// USART_Cmd(USART2, ENABLE); //使能串口
//}
//初始化IO 串3
//bound:波特率
void uart3_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能,GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//USART3
USART_DeInit(USART3); //复位串口3
//USART3_TX PB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PA2
//USART3_RX PB11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化PB11
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(USART3, &USART_InitStructure); //初始化串口
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART3, ENABLE); //使能串口
}
void Uart1_SendStr(char*SendBuf)//串口1打印数据
{
while(*SendBuf)
{
while((USART1->SR&0X40)==0);//等待发送完成
USART1->DR = (u8) *SendBuf;
SendBuf++;
}
}
void Uart3_SendStr(char*SendBuf)//串口3打印数据
{
while(*SendBuf)
{
while((USART3->SR&0X40)==0);//等待发送完成
USART3->DR = (u8) *SendBuf;
SendBuf++;
}
}
void USART1_IRQHandler(void) //串口1中断服务程序
{
char Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断,可以扩展来控制
{
Res=USART_ReceiveData(USART1);//接收模块的数据;
RxBuffer[RxCounter++] =USART_ReceiveData(USART1);//接收模块的数据
if(RxCounter>99)//长度自行设定
RxCounter=0;
}
}
void USART3_IRQHandler(void) //串口2中断服务程序
{
char Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收模块返回的数据
{
Res=USART_ReceiveData(USART3);//接收模块的数据;
RxBuffer1[RxCounter1++] =USART_ReceiveData(USART3);//接收模块的数据
if(RxCounter1>99)//长度自行设定
RxCounter1=0;
while(USART_GetFlagStatus(USART3,USART_FLAG_TC) != SET);
}
USART_ClearFlag(USART3,USART_FLAG_TC);
}
|
|