使用CH32F20這款MCU的CAN通信,例子代碼看不懂,有無大俠解釋一下:
代碼如下:
1)函數(shù)的各參數(shù)是什么含義?除了Bps =Fpclk1/((tpb1+1+tbs2+1+1)*brp)含義以外,各參數(shù)設(shè)置注意哪些事項(xiàng)。
2)函數(shù)內(nèi)的各個設(shè)置都有哪些注意事項(xiàng)
void CAN_Test_Mode_Init( u8 tsjw, u8 tbs2, u8 tbs1, u16 brp, u8 mode )
{
? ? GPIO_InitTypeDef GPIO_InitSturcture = {0};
? ? CAN_InitTypeDef CAN_InitSturcture = {0};
? ? CAN_FilterInitTypeDef CAN_FilterInitSturcture = {0};
? ? RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE );
? ? RCC_APB1PeriphClockCmd( RCC_APB1Periph_CAN1, ENABLE );
? ? GPIO_PinRemapConfig( GPIO_Remap1_CAN1, ENABLE );
? ? GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_9;
? ? GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_AF_PP;
? ? GPIO_InitSturcture.GPIO_Speed = GPIO_Speed_50MHz;
? ? GPIO_Init( GPIOB, &GPIO_InitSturcture );
? ? GPIO_InitSturcture.GPIO_Pin = GPIO_Pin_8;
? ? GPIO_InitSturcture.GPIO_Mode = GPIO_Mode_IPU;
? ? GPIO_Init( GPIOB, &GPIO_InitSturcture );
? ? CAN_InitSturcture.CAN_TTCM = DISABLE;
? ? CAN_InitSturcture.CAN_ABOM = DISABLE;
? ? CAN_InitSturcture.CAN_AWUM = DISABLE;
? ? CAN_InitSturcture.CAN_NART = ENABLE;
? ? CAN_InitSturcture.CAN_RFLM = DISABLE;
? ? CAN_InitSturcture.CAN_TXFP = DISABLE;
? ? CAN_InitSturcture.CAN_Mode = mode;
? ? CAN_InitSturcture.CAN_SJW = tsjw;
? ? CAN_InitSturcture.CAN_BS1 = tbs1;
? ? CAN_InitSturcture.CAN_BS2 = tbs2;
? ? CAN_InitSturcture.CAN_Prescaler = brp;
? ? CAN_Init( CAN1, &CAN_InitSturcture );
? ? CAN_FilterInitSturcture.CAN_FilterNumber = 0;
? ? CAN_FilterInitSturcture.CAN_FilterMode = CAN_FilterMode_IdMask;
? ? CAN_FilterInitSturcture.CAN_FilterScale = CAN_FilterScale_32bit;
? ? CAN_FilterInitSturcture.CAN_FilterIdHigh = 0;
? ? CAN_FilterInitSturcture.CAN_FilterIdLow = 0;
? ? CAN_FilterInitSturcture.CAN_FilterMaskIdHigh = 0;
? ? CAN_FilterInitSturcture.CAN_FilterMaskIdLow = 0x0006;
? ? CAN_FilterInitSturcture.CAN_FilterFIFOAssignment = CAN_Filter_FIFO1;
? ? CAN_FilterInitSturcture.CAN_FilterActivation = ENABLE;
? ? CAN_FilterInit( &CAN_FilterInitSturcture );
}