本帖最后由 taijing33 于 2016-5-28 09:11 编辑
小弟是这样的。stm32串口1通过485给电脑发数据正常。
但是我用串口调试助手给单片机发数据。发现一直进入串口1中断。刚上电的时候不进入。只要是我一给单片机发送就一直在串口中断里、。
下面是代码:、
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //ͨµÀÉèÖÃΪ´®¿Ú1ÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÖжÏÕ¼Ïȵȼ¶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //ÖжÏÏìÓ¦ÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //´ò¿ªÖжÏ
NVIC_Init(&NVIC_InitStructure);
}
void USART1_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1
|RCC_APB2Periph_GPIOA ,ENABLE);
// GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
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_InitStructure.USART_BaudRate = 9600;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_TXE,DISABLE); //DISABLE
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1, ENABLE);
//USART_ClearFlag(USART1,USART_FLAG_TC);
void USART1_IRQHandler(void)
{
static u8 startdata =0;
if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
rdata = USART_ReceiveData(USART1); //USART_ReceiveData(USART1)
//if((rdata!= 0x03)||(RxCounter1!=0)) //
if(RxCounter1 != 0) //
{
if(startdata!= 1) //??????????
{
RxCounter1 = 0;
return;
}
if(RxCounter1 > 8)//??????
{
RxCounter1 = 0;
startdata = 0;
return;
}
if(RxCounter1==1 && rdata!= 0X03)
{
RxCounter1 = 0;
startdata = 0;
return;
}
if((RxCounter1 == 2) &&(rdata!= 0)) //??????,???
{
RxCounter1 = 0;
startdata = 0;
return;
}
if((RxCounter1 == 3) &&(rdata!= 0)) //??????,???
{
RxCounter1 = 0;
startdata = 0;
return;
}
if((RxCounter1 == 4) &&(rdata!= 0)) //??????,???
{
RxCounter1 = 0;
startdata = 0;
return;
}
if((RxCounter1 == 5) &&(rdata!= 1)) //??????,???
{
RxCounter1 = 0;
startdata = 0;
return;
}
RX_BUFF1[RxCounter1++] =rdata;
}
else
{
//????
RX_BUFF1[0] = rdata;
RxCounter1 = 1;
startdata = 1;
}
}
}
|