int main(void)
{
u16 x,y,z;
u8 button;
u8 Buffer[9];
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init_all(115200);
LED_Init();
stepperMotor_Init();
Encoder_Init();
while(1)
{
for(u8 i=0;i<USART_REC_LEN;i++)
{
if(USART_RX_BUF[i]==0xff)
{
//memcpy(&Buffer[0],&USART_RX_BUF[i],9);
Buffer[0]=USART_RX_BUF[i];
Buffer[1]=USART_RX_BUF[1+i];
Buffer[2]=USART_RX_BUF[2+i];
Buffer[3]=USART_RX_BUF[3+i];
Buffer[4]=USART_RX_BUF[4+i];
Buffer[5]=USART_RX_BUF[5+i];
Buffer[6]=USART_RX_BUF[6+i];
Buffer[7]=USART_RX_BUF[7+i];
Buffer[8]=USART_RX_BUF[8+i];
u8 m;
u8 sum_check=0;
for(m=1;m<8;m++)
{
sum_check=sum_check+Buffer[m];
}
if(sum_check==Buffer[8])
{
x=(Buffer[1]<<8)|(Buffer[2]);
y=(Buffer[3]<<8)|(Buffer[4]);
z=(Buffer[5]<<8)|(Buffer[6]);
button=Buffer[7];
if(x<0x0200)printf("Left
");
else if((x>0x0200)&(x<=0x3ff))printf("Right
");
else if(x==0x0200)printf("No LR
");
if(y<0x0200)printf("Up
");
else if((y>0x0200)&(y<=0x3ff))printf("Down
");
else if(y==0x0200)printf("No UD
");
if(z<0x0200)printf("Wide
");
else if((z>0x0200)&(z<=0x3ff))printf("Tele
");
else if(z==0x0200)printf("No WT
");
if(button==0x02)printf("Burron
");
else if(button==0x00)printf("No Button
");
USART_RX_STA=0;
sum_check=0;
}
else
{printf("
");
USART_RX_STA=0;
sum_check=0;
}
printf("
");
USART_RX_STA=0;
sum_check=0; }
}
LED0=!LED0;//DS0·-×a
delay_ms(400);//Ñóê±200ms
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
//GPIO¶Ë¿úéèÖÃ
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //ê1ÄüGPIOAê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//ê1ÄüUSART1ê±Öó
//′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); //GPIOA9¸′óÃÎaUSART1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); //GPIOA10¸′óÃÎaUSART1
//USART1¶Ë¿úÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; //GPIOA9óëGPIOA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Ëù¶è50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA9£¬A10
//USART1 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = bound;//2¨ìØÂêéèÖÃ
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART1, USART_FLAG_TC);
#endif
#if EN_USART1_RX
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆôÏà1ØÖD¶Ï
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//′®¿ú1ÖD¶Ïí¨μà
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇàÕ¼óÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×óóÅÏ輶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷¡¢
#endif
}
void USART1_IRQHandler(void) //′®¿ú1ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART_RX_STA|=0x8000; //½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
}
}
}
}
一周热门 更多>