原来使用过STM32F407的USART2,收发送数据没有问题,这次使用USART6,将原来的USART2配置改成了USART6,却出现了问题,调适发现程序在while( (USART_GetFlagStatus(UsARTx,USART_FLAG_TXE) != SET));死循环了。在发送缓冲区为空的时候,TXE应该为1,USART2时没问题,USART6时却为0,导致了死循环。在将USART2改为USART6时也特别注意了,USART6是挂在APB2下的,其他的还有什么要改的吗?下面是我的USART6配置程序,为什么会导致TXE不为1呢?
[mw_shl_code=c,true]
void USART6_Config(u32 bound){
//GPIO配置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //????GPIOC?±??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//????USART6?±??
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOC6??????USART6
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //GPIOC7??????USART6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIOC6??GPIOC7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //????50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???ì????????
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //????
GPIO_Init(GPIOC,&GPIO_InitStructure); //??????PC6??PC7
//USART6 ???????è??
USART_InitStructure.USART_BaudRate = bound;//?¨?????è??
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(USART6, &USART_InitStructure); //???????®??6
USART_Cmd(USART6, ENABLE); //?????®??6
USART_ClearFlag(USART6, USART_FLAG_TC);
}
void USART6_IT_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//?????à??????
USART_ITConfig(USART6, USART_IT_TXE, ENABLE);
//Usart6 NVIC ????
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//?è????????????????·?×é2
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//?®??6?????¨??
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2; //×???????2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????NVIC?????÷??
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
#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((USART6->SR&0X40)==0);//循环发送,直到发送完毕
USART6->DR = (u8) ch;
return ch;
}
#endif
#if EN_USART6_RX //如果使能了接收
//初始化IO 串口1
//bound:波特率
void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,ENABLE);//使能USART1时钟
//串口1对应引脚复用映射
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOA9复用为USART1
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6); //GPIOA10复用为USART1
//USART1端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIOA9与GPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA9,PA10
//USART1 初始化设置
USART_InitStructure.USART_BaudRate = bound;//波特率设置
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(USART6, &USART_InitStructure); //初始化串口1
USART_Cmd(USART6, ENABLE); //使能串口1
USART_ClearFlag(USART6, USART_FLAG_TC);
#if EN_USART6_RX
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);//开启相关中断
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
#endif
}
void USART6_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //如果时钟节拍数定义了,说明要使用ucosII了.
OSIntEnter();
#endif
if(USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART6);//(USART1->DR); //读取接收到的数据
send(Res);
}
#ifdef OS_TICKS_PER_SEC //如果时钟节拍数定义了,说明要使用ucosII了.
OSIntExit();
#endif
}
#endif
void send(u8 ch)
{
while((USART6->SR&0X40)==0);//循环发送,直到发送完毕
USART6->DR = (u8) ch;
}
---------------------------------
原子哥,求指导啊。
---------------------------------
我也没用过串口6,不能给你指导了.你自己调试下看看.
一周热门 更多>