最近在弄有关于can的东西,大体上能通信了,现在就剩下一个能进中断,但是解码的东西进不了,这是什么情况,困扰了好久,同样的原理写的为什么,求助,感激不尽,下面相关代码
这是 can配置的
u8 CAN1_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN1_RX0_INT_ENABLE
NVIC_InitTypeDef NVIC_InitStructure;
#endif
//使能相关时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);//使能PORTB时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//使能CAN1时钟
//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化PB8,PB9
//引脚复用映射配置
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_CAN1); //GPIOB8复用为CAN1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_CAN1); //GPIOB9复用为CAN1
//CAN单元设置
CAN_InitStructure.CAN_TTCM=DISABLE; //非时间触发通信模式
CAN_InitStructure.CAN_ABOM=DISABLE; //软件自动离线管理
CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠模式通过软件唤醒(清除CAN->MCR的SLEEP位)
CAN_InitStructure.CAN_NART=DISABLE; //开启报文自动传送
CAN_InitStructure.CAN_RFLM=DISABLE; //报文不锁定,新的覆盖旧的
CAN_InitStructure.CAN_TXFP=DISABLE; //优先级由报文标识符决定
CAN_InitStructure.CAN_Mode= mode; //模式设置
CAN_InitStructure.CAN_SJW=tsjw; //重新同步跳跃宽度(Tsjw)为tsjw+1个时间单位 CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1=tbs1; //Tbs1范围CAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2=tbs2; //Tbs2范围CAN_BS2_1tq ~ CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler=brp; //分频系数(Fdiv)为brp+1
CAN_Init(CAN1, &CAN_InitStructure); // 初始化CAN1
//配置过滤器
for(Fliter_i=0;Fliter_i<3;Fliter_i++)
{
CAN_FilterInitStructure.CAN_FilterNumber=Fliter_i; //过滤器0、1
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32位
CAN_FilterInitStructure.CAN_FilterIdHigh=std_id[2*Fliter_i]<<5; //32位ID
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=std_id[2*Fliter_i+1]<<5; //32位MASK
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0; //过滤器0、1关联到FIFO0
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //激活过滤器0、1
CAN_FilterInit(&CAN_FilterInitStructure); //滤波器初始化
}
#if CAN1_RX0_INT_ENABLE
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0消息挂号中断允许.
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; // 主优先级为1 优先级联调的时候要小心
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; // 次优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
return 0;
}
下面是发送部分的,现在是地磁的能通,但是gps能进中断,但是进不了解码 通信连不上
uint8_t TxData_CAN[100] = {0};
void CAN1_Send_Mag_Msg(void)
{
u8 mbox;
u16 i=0;
u16 Mag_data_Size =0;
u8 Send_times =0;
u8 Send_times_count =0;
u8 Residual_byte;
CanTxMsg TxMessage;
TxMessage.StdId=0x003;
TxMessage.IDE=CAN_ID_STD;
TxMessage.RTR=CAN_RTR_DATA;
TxMessage.DLC=8;
Mag_data_Size = chose_Mag_CAN();
Send_times=Mag_data_Size/8;
for(Send_times_count=0;Send_times_count<Send_times;Send_times_count++)
{
for(i=0;i<8;i++)
TxMessage.Data[i]=TxData_CAN[i+(Send_times_count)*8]; //
mbox= CAN_Transmit(CAN1, &TxMessage);
// while(!(CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP0)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP1)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP2)==1));
while(CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed){};
}
Residual_byte=Mag_data_Size%8;
if(Residual_byte != 0)
{
TxMessage.DLC = Residual_byte; // ????????
for(i=0;i<Residual_byte;i++)
TxMessage.Data[i] = TxData_CAN[i+(Send_times)*8]; // ?????
mbox= CAN_Transmit(CAN1, &TxMessage);
// while(!(CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP0)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP1)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP2)==1));
}
//i=0;
while(CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed){};
}
void CAN1_Send_GPS_Msg(void)
{
u8 mbox;
u16 i=0;
u16 GPS_data_Size =0; //?????? ???
u8 Send_times =0; //?????? ???
u8 Send_times_count =0; //?????? ???
u8 Residual_byte; //?????? ???
CanTxMsg TxMessage;
TxMessage.StdId=0x004; // ??????0
//TxMessage.ExtId=0x12; // ???????(29?)
TxMessage.IDE=CAN_ID_STD; // ???????
TxMessage.RTR=CAN_RTR_DATA; // ????????,??8?
TxMessage.DLC=8; // ??????
GPS_data_Size = chose_GPS_data();
Send_times=GPS_data_Size/8;
for(Send_times_count=0;Send_times_count<Send_times;Send_times_count++)
{
for(i=0;i<8;i++)
TxMessage.Data[i]=TxData_CAN[i+(Send_times_count)*8]; // ?????
mbox= CAN_Transmit(CAN1, &TxMessage);
//while(!(CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP0)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP1)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP2)==1));
while(CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed){};
}
Residual_byte=GPS_data_Size%8;
if(Residual_byte != 0)
{
TxMessage.DLC = Residual_byte; // ????????
for(i=0;i<Residual_byte;i++)
TxMessage.Data[i] = TxData_CAN[i+(Send_times)*8]; // ?????
mbox= CAN_Transmit(CAN1, &TxMessage);
// while(!(CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP0)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP1)==1|CAN_GetFlagStatus(CAN1, CAN_FLAG_RQCP2)==1));
}
//i=0;
while(CAN_TransmitStatus(CAN1, mbox)==CAN_TxStatus_Failed){};
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
是的,004就是
一周热门 更多>