被这个程序整的没脾气了!大家来看看吧。串口接收中断总是进不去 , 不知道哪里设置错误,
我这个程序是我在 USART1的基础上改过来的,现在接收不了串口调试助手发来的数据,串口调试助手能收到USART3发出去的数据,
程序的意思是这样: STM32 main()里初始化外设,等待接收到数据的标志位flag,然后STM32 串口3中断接受这个数据,然后置位接收到数据的标志flag,把收到的数据存入temp 变量, STM32主程序就一直判断的这个标志位,如果置位了 就把这个收到的数据temp 回传到串口调试助手,并复位flag,进入下次接受
中断处理如下
#include "stm32f10x_it.h"
extern volatile unsigned char temp;
extern volatile unsigned char flag;
void USART3_IRQHandler(void) //串口1中断服务程序
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
{
temp =USART_ReceiveData(USART3);
flag=1;
}
}
//主程序
volatile unsigned char flag =0;
volatile unsigned char temp =0;
void NVIC_Configuration(void);
void GPIO_Configuration(void);
voidUSART_Configuration(void);
int main(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
NVIC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
if(flag) // 如果保留这行,注释掉下一行 ,调试助手就收不到数据了
//if(1) //不注释这行 调试助手能一直收到数据
{
flag=0;
USART_SendData(USART3, temp);
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET);
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //设置优先级分组:先占优先级0位,从优先级4位
//设置向量表的位置和偏移
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //向量表位于RAM
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //向量表位于FLASH
#endif
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //USART1中断
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate= 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_Mode= USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
} |