接收数据为:BB 97 11 30 00 11 22 33 44 55 66 77 88 99 00 11 22 FD 6F 03 1A 0D 0A
发送端一直不定时发此类数据
但是我用
STM32F103RCT6
单片机接收丢包挺多,丢包的频率很规律 应该是我程序代码没写严谨,求大神指点
void Init_NVIC(void)
{
NVIC_InitTypeDef NVIC_InitStructure; //¶¨ÒåÒ»¸öNVICÏòÁ¿±í½á¹¹Ìå±äÁ¿
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃÖжÏ×é Ϊ2
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´
NVIC_InitStructure.NVIC_IRQChannelPreemp
tionPriority = 3; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //ÉèÖø±ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÉèÖø±ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //ÉèÖø±ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //ÅäÖô®¿Ú1ΪÖжÏÔ´
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÉèÖÃÕ¼ÏÈÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //ÉèÖø±ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ʹÄÜ´®¿Ú1ÖжÏ
NVIC_Init(&NVIC_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯ÖжϼĴæÆ÷
}
void Init_Usart2(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //¶¨ÒåÒ»¸öGPIO½á¹¹Ìå±äÁ¿
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO ,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE);
//ʹÄܸ÷¸ö¶Ë¿ÚʱÖÓ£¬ÖØÒª£¡£¡£¡
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //ÅäÖô®¿Ú·¢ËͶ˿ڹҽӵ½9¶Ë¿Ú
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´Óù¦ÄÜÊä³ö¿ªÂ©
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ÅäÖö˿ÚËÙ¶ÈΪ50M
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯GPIOA¼Ä´æÆ÷
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //½ÓÊÕ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿ÕÊäÈë(¸´Î»×´Ì¬);
GPIO_Init(GPIOA, &GPIO_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯GPIOA¼Ä´æÆ÷
}
void Usart2_Configuration(uint32_t BaudRate)
{
USART_InitTypeDef USART_InitStructure; //¶¨ÒåÒ»¸ö´®¿Ú½á¹¹Ìå
USART_InitStructure.USART_BaudRate = BaudRate ; //²¨ÌØÂÊ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_Init(USART2, &USART_InitStructure); //¸ù¾Ý²ÎÊý³õʼ»¯´®¿Ú¼Ä´æÆ÷
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //ʹÄÜ´®¿ÚÖжϽÓÊÕ
USART_Cmd(USART2, ENABLE); //ʹÄÜ´®¿ÚÍâÉè
}
void USART2_IRQHandler()
{
int i=0;
u8 a=0;
extern u8 UART2_Save_data_box[UART2_Save_data_box_number]; //·ÅÖÃUART2½ÓÊÕµ½µÄ×Ö½Ú ÐèÒªÒç³ö¹é0
extern u16 UART2_Save_data_box_pointer; //·ÅÖÃUART2½ÓÊÕµ½µÄ×Ö½ÚºÐ×ÓÏÂÒ»¸öÊý¾Ý·ÅÖõÄλÖÃÖ¸Ïò±êÖ¾ ÐèÒªÒç³ö¹é0
extern u32 EPC_box[EPC_number];
extern u16 EPC_box_pointer;
extern u8 back_correct;
extern u32 error1;
extern u32 error2;
extern u32 error3;
extern u32 EPC_count_l;
extern u32 EPC_count_h;
extern u32 timer1;
extern u8 zisuo3;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //¶ÁÈ¡½ÓÊÕÖжϱê־λUSART_IT_RXNE
//USART_FLAG_RXNE:½ÓÊÕÊý¾Ý¼Ä´æÆ÷·Ç¿Õ±ê־λ
//1£ºÃ¦×´Ì¬ 0£º¿ÕÏÐ(ûÊÕµ½Êý¾Ý£¬µÈ´ý¡£¡£¡£)
{
if(UART2_Save_data_box[0]==0xBB) //ÅжÏÊý¾Ý°üÍ·
{
UART2_Save_data_box[UART2_Save_data_box_pointer]=USART_ReceiveData(USART2); //¼ÌÐø´æ´¢Êý¾Ý
UART2_Save_data_box_pointer++;
/*
if(UART2_Save_data_box[1]==0x98&&UART2_Save_data_box[5]==0x0D && UART2_Save_data_box[6]==0x0A) //ÅжÏÊý¾Ý°üβ
{
for(i=1;i<4;i++) //¼ÆËãУÑéÖµ
{
a+=(u8)UART2_Save_data_box
;
}
if(a==UART2_Save_data_box[4]) //УÑé³É¹¦
{
if(UART2_Save_data_box[3]==0x01)
{
back_correct=1;
OUT2_LED=0;
}
else
{
back_correct=2;
}
}
for(i=0;i<UART2_Save_data_box_number;i++)
{
UART2_Save_data_box=0; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÊý×é³õʼ»¯
}
UART2_Save_data_box_pointer=1; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÖÃ1
}
else */if(UART2_Save_data_box[1]==0x97&&UART2_Save_data_box[21]==0x0D && UART2_Save_data_box[22]==0x0A) //ÅжÏÊý¾Ý°üβ
{
for(i=1;i<20;i++) //¼ÆËãУÑéÖµ
{
a+=(u8)UART2_Save_data_box;
}
if(a==UART2_Save_data_box[20]) //УÑé³É¹¦
{
for(i=0;i<3;i++)
{
EPC_box[EPC_box_pointer]=(UART2_Save_data_box[i*4+5]<<24)+(UART2_Save_data_box[i*4+1+5]<<16)+(UART2_Save_data_box[i*4+2+5]<<8)+(UART2_Save_data_box[i*4+3+5]<<0); //´æ´¢EPC
EPC_box_pointer++;
}
zisuo3=0;
OUT1_jingbao=1; //Êä³ö±¨¾¯
timer1=0; //¼ÆʱÇå0
EPC_count_l++; //±êÇ©×ÜÊý+1
if(EPC_count_l>=1000000000)
{
EPC_count_l=0;
EPC_count_h++;
}
}
else
{
error1++; //żȻ´íÎó ´íÎó±êÖ¾¼Ó1
}
for(i=0;i<UART2_Save_data_box_number;i++)
{
UART2_Save_data_box=0; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÊý×é³õʼ»¯
}
UART2_Save_data_box_pointer=1; //Ò»µ©Åжϵ½Êý¾Ý°üµÄβ²¿ÔòÖÃ1
}
else if(UART2_Save_data_box_pointer>=23) //ż¶ûÐÔµÄÊý¾Ý´íÎó ¿ªÊ¼Çå0
{
for(i=0;i<UART2_Save_data_box_number;i++)
{
UART2_Save_data_box=0;
}
UART2_Save_data_box_pointer=1;
error2++; //żȻ´íÎó ´íÎó±êÖ¾¼Ó1
}
if(EPC_box_pointer>=EPC_number)
{
EPC_box_pointer=0;
}
}
else if(UART2_Save_data_box[0]==0x00) //Èç¹ûÉϸöÊý¾Ý²»ÊÇÊý¾Ý°üÍ·
{
UART2_Save_data_box[0]=USART_ReceiveData(USART2); //´¢´æÊý¾Ýµ½UART2
}
else
{
error3++;
UART2_Save_data_box[0]=USART_ReceiveData(USART2); //´¢´æÊý¾Ýµ½UART2
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //Çå³ýÖжϱê־λ
}
}
你的程序虽然有对一些数据包的特定数据进行判断处理,但这些都是基于接收完数据包的操作,并没有看到接收数据包过程的操作。
一周热门 更多>