手柄板是F1的,主控板是F4的,然后我按下一个手柄的按键后,主控应该是接收到一个数据,但是它却接受到了多个数据(这几个数据相同而且应该是正确的)并且在接收这几个数据后,恢复按键不按的时候返回的数据(我自己写的success)。。。。
个人分析,CAN的配置是没有问题的!我觉得发送的问题比较大!
下面我把我的程序贴出来,希望大家给看看~~~
*****************手柄处理*************
static int E=0x7f,F=0x7f,G=0x01,H=0x01;
int q=0x01;
int p=0x80;//首位设为1
A = DBUF[0]; //3 4
B = DBUF[1];
C = DBUF[5]&0xf1;
D = DBUF[6]|0x01;
if((A!=E)||(B!=F)||(C!=G)||(D!=H))
{
delay_ms(500);
if((A!=E)||(B!=F)||(C!=G)||(D!=H))
{
if((A==0x7f)&&(B==0x7f)&&(C==0x01)&&(D==0x01)) //默认 1001
{
data[0]=((p&0xffff)<<8)+(q&0xffff);
printf("%x\r\n",data[0]);
}
if(((A!=0x7f)||(B!=0x7f))&&((C==0x01)&&(D==0x01)))//方向键
{
data[1]=(((A&0xffff)<<8)+(B&0xffff))|0x1001;
printf("%x\r\n",data[1]);
}
if(( C!=0x01)&&(D==0x01)) //1 2 3 4
{
data[2]=((p&0xffff)<<8)+(C&0xffff);
printf("%x\r\n",data[2]);
}
if(( D!=0x01)) //组合键
{
if((D==0x21)||(D==0x11)) //组合键1
{
if((A!=0x7f)||(B!=0x7f)) // 方向键
{
data[3]=(((A&0xffff)<<8)+(((B&0x01)+D)&0xffff))|0x1001;
printf("%x\r\n",data[3]);
}
if(C!=0x01) //组合键 +1 2 3 4
{
data[4]=((p&0xffff)<<8)+((C+D)&0xffff);
printf("%x\r\n",data[4]);
}
if((D==0x02)||(D==0x03)||(D==0x05)||(D==0x09)) //功能键+ L 1 2 R 1 2
{
data[5]=((p&0xffff)<<8)+(D&0xffff);
printf("%x\r\n",data[5]);
}
}
else
{
data[6]=((p&0xffff)<<8)+(D&0xffff); //L 1 2 R 1 2
printf("%x\r\n",data[6]);
}
}
}
}
E = A;
F = B;
G = C;
H = D;
TestTx = CAN_MessageSend();
if (TestTx == 1)
{
printf("success\r\n");
for(j=0;j<7;j++) //用于数组清零
data[j] = 0x00;
}
**************CAN的发送函数*************
unsigned int CAN_MessageSend(void)
{
CAN_InitTypeDef CAN_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
CanTxMsg TxMessage;
u32 i = 0;
u32 j = 0;
u8 TransmitMailbox = 0;
/* Configure CAN pin: RX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure CAN pin: TX */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//只能工作在此速度下
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* CAN register init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
/* CAN cell init */
CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间触发通讯模式
CAN_InitStructure.CAN_ABOM=DISABLE; //禁止自动离线模式
CAN_InitStructure.CAN_AWUM=DISABLE; //禁止自动唤醒模式
CAN_InitStructure.CAN_NART=DISABLE; //禁止非自动重传模式
CAN_InitStructure.CAN_RFLM=DISABLE; //禁止接收FIFO锁定模式
CAN_InitStructure.CAN_TXFP=DISABLE; //禁止发送FIFO优先级
CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_3tq; //已经折算好了 1M
CAN_InitStructure.CAN_BS2=CAN_BS2_5tq;
CAN_InitStructure.CAN_Prescaler=4;
CAN_Init(CAN1,&CAN_InitStructure);
/* transmit */
TxMessage.StdId = 0xa3; // 0x321
TxMessage.ExtId = 0x00; // 0x01
TxMessage.RTR=CAN_RTR_DATA;//数据帧
TxMessage.IDE=CAN_ID_STD;//标准帧格式
TxMessage.DLC=7; //帧长度,单位:字节
for(i=0;i<7;i++)
TxMessage.Data[i]= data[i];
/*数据发送*/
TransmitMailbox=CAN_Transmit(CAN1,&TxMessage);
i = 0;
while((CAN_TransmitStatus(CAN1,TransmitMailbox) != CANTXOK) && (i != 0xFF)) //等待CAN总线传输数据成功
{
i++;
}
if(i==0xFF)//发送超时,但发送不成功
{
return 0;
}
for(j=0;j<7;j++)//数组清零
data[j] = 0x00;
return 1; /* Test Passed */
}
|