27,508
社区成员




u8 *usart_buff;
u32 count=0;
int main(void)
{
u16 i=0,j=0,icnt=0;
RCC_Configuration(); //系统时钟初始化
GPIO_Configuration();//端口初始化
USART_Configuration();
NVIC_Configuration();
usart_buff=(u8*)malloc(PC_MSG_LEN);
BuffSet(usart_buff,0x0,PC_MSG_LEN);
while(1)
{
if(!USART_GetFlagStatus(USART1,USART_FLAG_RXNE)&&count>7)
{
MessageProcess();
}
}
}
extern unsigned int count;
extern u8* usart_buff;
void MessageProcess(void)
{
MSGINFO* msg_info=(MSGINFO*)malloc(sizeof(MSGINFO));
msg_info=GetMsgInfo(usart_buff);
switch(msg_info->command)
{
case 'o':
printf("o\n");
break;
default:
break;
}
free(msg_info);
count=0;
BuffSet(usart_buff,0x0,PC_MSG_LEN);
}
void BuffSet(unsigned char* buff,unsigned char vaule, unsigned int length)
{
unsigned char* temp=buff;
int i=0;
for(i=0;i<length;i++)
{
*temp=vaule;
temp++;
}
}
MSGINFO* GetMsgInfo(u8 * buff)
{
u32 i =0,j=0,temp=0;
MSGINFO* msginfo= (MSGINFO*)malloc(sizeof(MSGINFO));
unsigned int sum;
//printf("MessageProcess\r\n");
msginfo->command=0;
msginfo->data_start_addr=0;
msginfo->length=0;
while(buff[i++]==0xaa && i<count)
{
temp=i-1;
//printf("length = 0x%x\r\n",length);
sum =GetChecksum(&buff[temp],msginfo->length+1+1+2);
// printf("sum = 0x%x\r\n",sum);
if(VerifyChksum(sum,&buff[msginfo->length+1+1+2]))
{
msginfo->length =GetLength(&buff[temp+2]);
msginfo->command = buff[temp+1];
return msginfo;
}
else
{
return 0;
}
}
return 0;
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
usart_buff[count++]=USART_ReceiveData(USART1);
}
}