我用STM32103RE与红牛开发板(STM32103ZE)进行CAN总线2点通信,一块应用例程程序,另一块应用修改程序,各自LoopBack模式,没问题。CAN总线正常模式下,可以检测到H与L引脚有周期的波形,但是收不到正确的数据请问是怎么回事。
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //CAN_RX,上拉输入
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //CAN_TX,复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
144脚STM32103ZE
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE); /* 使能相关CAN引脚时钟 */
GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE); /* 重映射CAN引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
CAN配置部分
void CAN_Configuration(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
CAN_DeInit();
CAN_FilterInitStructure.CAN_FilterNumber=0;//选择过滤器0
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定过滤器被设置为标识符屏蔽模式
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;//给出过滤器位宽为32位
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; // 0x0020
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; // 0x0002
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;//为过滤器分配缓存 此处到//fifo 因为 CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
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;
CAN_InitStructure.CAN_TXFP = DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack ; // CAN_Mode_LoopBack,CAN_Mode_Normal
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //重新同步跳跃宽度1个时间单位,在每位中可以延长或缩短多少个时间单位的上限
CAN_InitStructure.CAN_BS1=CAN_BS1_5tq; //时间段1为4个时间单位
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq; // 时间段2为3个时间单位
CAN_InitStructure.CAN_Prescaler=4.5; //CAN_Prescaler设定了一个时间
CAN_Init(&CAN_InitStructure); //CAN_APS=PCLK1/((1+5+2)*3)=1MBPS
CAN_ITConfig(CAN_IT_FMP0, ENABLE);
}
CAN接受中断
void UserCAN_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN_RX0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USB_LP_CAN_RX0_IRQHandler(void)
{
INT32U i;
i=CAN_GetITStatus(CAN_IT_FMP0);
if(CAN_GetITStatus(CAN_IT_FMP0)!= RESET)
{
INT16U ch;
CanRxMsg RxMessage;
CAN_ClearITPendingBit(CAN_IT_FMP0);
CAN_Receive(CAN_FIFO0,&RxMessage);
ch=(RxMessage.Data[1]<<8)|RxMessage.Data[0];
}
}
CAN发送部分:
while(DEF_TRUE)
{
CanTxMsg TxMessage;
CanRxMsg RxMessage;
CAN_TypeDef CAN_Type;
u8 TransmitMailbox;
TxMessage.StdId=0x11; //标识符
TxMessage.IDE=CAN_ID_STD; //标准标识符
TxMessage.RTR=CAN_RTR_DATA; //数据帧
TxMessage.DLC=8; //传输消息的帧长度
TxMessage.Data[0]=0X09;
TxMessage.Data[1]=0X11;
TxMessage.Data[2]=0X42;
TxMessage.Data[3]=0X86;
TxMessage.Data[4]=0X33;
TxMessage.Data[5]=0X91;
TxMessage.Data[6]=0XF6;
TxMessage.Data[7]=0X77;
TransmitMailbox=CAN_Transmit(&TxMessage);
while((CAN_TransmitStatus(TransmitMailbox) != CANTXOK));
OSTimeDlyHMSM(0,0,0,200);
} |