2020-06-14 08:44 PM
Hi Sir ,
I have set a project using CAN .Now I have found a problem , the max value is 0x80 ,in the datasheet it can be up to 255.Please help me to find why ,the following is my configurition code:
void CanInit(uint32_t gateID)
{
//CAN1_Mode_Init(CAN_SJW_1tq,CAN_BS2_3tq,CAN_BS1_11tq,24,CAN_Mode_Normal);
CAN1_Mode_Set(CAN_SJW_1TQ,CAN_BS2_3TQ,CAN_BS1_11TQ,24,CAN_MODE_NORMAL,gateID);//CAN�?始化,波特率50Kbps,smaple=(1+CAN_BS1)/(1+CAN_BS1+CAN_BS2)
}
////CAN1 �?始化�?�?�滤波,便于上�?机广播
void CAN1_Mode_Set(uint32_t tsjw,uint32_t tbs2,uint32_t tbs1,uint16_t brp,uint32_t mode,uint32_t GateID)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN1_InitConf;
CAN_FilterTypeDef sFilterConfig;
__HAL_RCC_CAN1_CLK_ENABLE(); //时钟使能 ,�?�独使用CAN2,时一定先打开CAN1时钟
__HAL_RCC_GPIOA_CLK_ENABLE();
/*PA11 ------> CAN_RX
PA12 ------> CAN_TX
*/
GPIO_InitStructure.Pin = GPIO_PIN_11;
GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
GPIO_InitStructure.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_12;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
CAN1_Handler.Instance=CAN1;
CAN1_Handler.Init = CAN1_InitConf;
CAN1_Handler.Init.Prescaler=brp; //分频系数(Fdiv)为brp+1
CAN1_Handler.Init.Mode=mode; //模�?设置
CAN1_Handler.Init.SyncJumpWidth=tsjw; //�?新�?�步跳跃宽度(Tsjw)为tsjw+1个时间�?��? CAN_SJW_1TQ~CAN_SJW_4TQ
CAN1_Handler.Init.TimeSeg1=tbs1; //tbs1范围CAN_BS1_1TQ~CAN_BS1_16TQ
CAN1_Handler.Init.TimeSeg2=tbs2; //tbs2范围CAN_BS2_1TQ~CAN_BS2_8TQ
CAN1_Handler.Init.TimeTriggeredMode=DISABLE; //�?�时间触�?�通信模�?
CAN1_Handler.Init.AutoBusOff=DISABLE; //软件自动离线管�?�
CAN1_Handler.Init.AutoWakeUp=DISABLE; //�?�眠模�?通过软件唤醒(清除CAN->MCR的SLEEP�?)
CAN1_Handler.Init.AutoRetransmission=ENABLE; //�?止报文自动传�?
CAN1_Handler.Init.ReceiveFifoLocked=DISABLE; //报文�?�?定,新的覆盖旧的
CAN1_Handler.Init.TransmitFifoPriority=DISABLE; //优先级由报文标识符决定
if(HAL_CAN_Init(&CAN1_Handler)!=HAL_OK) //�?始化
{
NVIC_SystemReset(); //�?始化失败则�?�?
}
sFilterConfig.FilterBank = 0;
sFilterConfig.FilterMode = CAN_FILTERMODE_IDLIST; //采用32�? 列表方�? xxxx0000 或0000 0000 的地�?��?能被接收
sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
sFilterConfig.FilterIdHigh = 0x0000|(GateID>>16);
sFilterConfig.FilterIdLow = 0x0000|CAN_ID_EXT;
sFilterConfig.FilterMaskIdHigh = 0x0000;
sFilterConfig.FilterMaskIdLow = 0x0000|CAN_ID_EXT;
sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO1;
sFilterConfig.FilterActivation = ENABLE;
//sFilterConfig.SlaveStartFilterBank = 14;
if (HAL_CAN_ConfigFilter(&CAN1_Handler, &sFilterConfig) != HAL_OK)
{
NVIC_SystemReset(); //�?始化失败则�?�?
}
__HAL_CAN_ENABLE_IT(&CAN1_Handler, CAN_IT_RX_FIFO1_MSG_PENDING);
HAL_NVIC_SetPriority(CAN1_RX1_IRQn,1,0); //CAN2中断,先�?�优先级1级,从优先级0级
HAL_NVIC_EnableIRQ(CAN1_RX1_IRQn); //IRQ通�?�被使能
if (HAL_CAN_Start(&CAN1_Handler) != HAL_OK)
{
NVIC_SystemReset(); //�?始化失败则�?�?
}
}
Thanks .
2020-06-15 10:45 AM
A high TEC count typically indicates a transmit problem. Do you have at least two node on the bus? CAN will not work with a single node. Make sure you are using twisted-pair wiring with termination resistors at each end. Check the error registers to see what is causing the transmit errors. Check that both nodes have the same bit rate and timing.
Jack Peacock
2020-06-16 02:08 AM
Hi Jack,
Thank you very much! I'll try it later!