当前位置: 代码迷 >> 单片机 >> 关于STM32F103的USART中断程序解决方案
  详细解决方案

关于STM32F103的USART中断程序解决方案

热度:319   发布时间:2016-04-28 14:46:02.0
关于STM32F103的USART中断程序
以下代码实现用PC上串口调试工具发送数据后中断服务程序将接收到的数据发送到PC串口调试工具上。可是我的板子却没有反应,我有用到MAX485芯片,UART接收端RX连接ARM的PA10,UART发送端TX连接PA9,控制发送接收的使能位DE/RE对应PA8,要怎么改啊?求大神指教,感激不胜!!!!部分代码如下:
void Usart_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO库函数结构体
USART_InitTypeDef USART_InitStructure;//USART库函数结构体
USART_ClockInitTypeDef USART_ClockInitStructure;
//使能串口1,GPIOA,AFIO总线
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9时钟速度50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate =115200; //波特率115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;//空闲时钟为低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据的时钟脉冲不从SCLK输出
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1,&USART_InitStructure); //初始化结构体
//使能串口1接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
USART_Cmd(USART1, ENABLE); //使能串口1
}

void USART1_IRQHandler(void)
{

USART_ClearITPendingBit(USART1,USART1_IRQn);//清除中断标志位
if(USART1->SR&(1<<5)) //如果是接收中断
{
USART1_Putc(USART1->DR);  //发送串口1接收到的数据
}
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //采用组别2  
 
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//配置串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占先式优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
}
/**********************************************************
** 函数名: main
** 功能描述: 串口接收中断操作,在中断服务程序中将串口接收到数据发送出去
** 输入参数: 无
** 输出参数: 无
** 说明:
***********************************************************/
int main(void)
{
    SystemInit();  //系统时钟初始化
Usart_Configuration();//串口初始化配置
NVIC_Config(); //中断优先级配置 
    while(1);
}

------解决思路----------------------
我最近刚用STM32  ,   你的485和我一样,我修改了你的, 你参考下。  



void Usart_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO库函数结构体
USART_InitTypeDef USART_InitStructure;//USART库函数结构体
USART_ClockInitTypeDef USART_ClockInitStructure;
//使能串口1,GPIOA,AFIO总线
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
------解决思路----------------------
RCC_APB2Periph_AFIO
------解决思路----------------------
RCC_APB2Periph_USART1,ENABLE);

////////485使能脚配置////////////
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // RS5485E=0为接收状态  RS5485E=1为发送
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//普通推挽输出(GPIO_Mode_Out_PP)
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
////////////////////
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//PA9时钟速度50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate =115200; //波特率115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;//空闲时钟为低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;//时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;//最后一位数据的时钟脉冲不从SCLK输出
USART_ClockInit(USART1, &USART_ClockInitStructure);
USART_Init(USART1,&USART_InitStructure); //初始化结构体
//使能串口1接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); 
USART_Cmd(USART1, ENABLE); //使能串口1
}

void USART1_IRQHandler(void)
{

// USART_ClearITPendingBit(USART1,USART1_IRQn);//清除中断标志位
// if(USART1->SR&(1<<5)) //如果是接收中断
// {
// USART1_Putc(USART1->DR);  //发送串口1接收到的数据
// }

if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET) 
{
////////////////////////////////////////////////////////////////////
GPIO_SetBits(GPIOA , GPIO_Pin_8); //485发送
USART_SendData(USART1 ,USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
GPIO_ResetBits(GPIOA , GPIO_Pin_8);// 设置为485接收
////////////////////////////////////////////////////////////////////
}
}
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);  //采用组别2  
 
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//配置串口中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//占先式优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //副优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断使能
NVIC_Init(&NVIC_InitStructure);//中断初始化
}
/**********************************************************
** 函数名: main
** 功能描述: 串口接收中断操作,在中断服务程序中将串口接收到数据发送出去
** 输入参数: 无
** 输出参数: 无
** 说明:
***********************************************************/
int main(void)
{
    SystemInit();   //系统时钟初始化
Usart_Configuration();//串口初始化配置
NVIC_Config();  //中断优先级配置
/////////////////////////////////////////////////////////// 
GPIO_ResetBits(GPIOA , GPIO_Pin_8);// 设置默认为485接收 -0
    while(1);
}
------解决思路----------------------
1、GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
改成GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
2、检查收发两端485的控制部分
------解决思路----------------------
1006904594加我,我这有USART中断程序 
  相关解决方案