采用的STM32F103RCT6开发板,之前由于失误把串口1烧坏了,后来改用串口2做发送和接收,但是调程序的时候发现能发送,接收出现问题,程序像是死在串口2的接收函数里了,不能继续进行。还望各位大神帮忙看一下有什么问题,给提点意见!下面是程序代码,实现单片机给电脑先发送八个十六进制数,然后再接收电脑发的八个十六进制数,然后把这八个数在发给电脑看是否正确。程序能完成第一步的发送,然后就没反应了,感觉像是停在接收函数没出来!求大神们帮忙看看
[mw_shl_code=cpp,true]void uart_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2_RX PA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
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(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART2, ENABLE); //使能串口
}
void USART2_IRQHandler(void) //串口2中断服务程序
{
int i=0;
while(count<8)
{if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是8个)
{
USART_RX_BUF[i++] =USART_ReceiveData(USART2);//(USART1->DR); //读取接收到的数据
count++;
}
}
}[/mw_shl_code][mw_shl_code=c,true] for(i=0;i<8;i++) //循环发送8个命令码
{
USART_SendData(USART2, USART_TX_BUF); //串口发送函数
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){} //判断是否发送结束
}
delay_ms(10); //发送结束后延时至少3.5个字节时间
USART2_IRQHandler(); //接收八个命令码
for(i=0;i<8;i++) //循环发送8个命令码
{
USART_SendData(USART2, USART_RX_BUF); //串口发送函数
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){} //判断是否发送结束
}
delay_ms(10); //接收结束后延时至少3.5个字节时间[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); //使能USART1,GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能重映射时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PA10
USART_DeInit(USART2); //复位串口1
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
//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(USART2, &USART_InitStructure); //初始化串口
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART2, ENABLE); //使能串口
}
u8 listen_angle_buf[17];
u8 st;
u8 recive_angle;
u8 USART2_FINISH;
void USART2_IRQHandler(void) //串口中断服务程序
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
Res =USART_ReceiveData(USART2);//(USART1->DR);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
// USART_SendData(USART1,Res);
if(Res == 0x57){
recive_angle = 1;
}
if(recive_angle == 1){
listen_angle_buf[st] = Res;
st++;
}
if(st == 17){
recive_angle = 0;
st = 0;
USART2_FINISH = 1;
}
USART_ClearFlag(USART2, USART_FLAG_RXNE);
}
}
老哥,可以留个邮箱吗?我把整个程序发给你看一下,麻烦给简单的改一下!
你直接复制到你代码中就行,优先级你看你用的哪个配置一下
一周热门 更多>