本帖最后由 john78 于 2016-9-19 10:43 编辑
void CANinit()
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE); //一定要打开RCC_APB1Periph_CAN1?|,
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE); //一定要打开RCC_APB1Periph_CAN1?|,
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// CAN GPIOs configuration *************************************************
// Enable GPIO clock
//RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE);
//Connect CAN pins to AF9
GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
// Configure CAN RX and TX pins
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
CAN_DeInit(CAN1);
// CAN cell init
CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间除法通讯方式
CAN_InitStructure.CAN_ABOM=DISABLE; //软件对CAN_MCR寄存器的INRQ位进行置1后清零
CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠迷失通过清楚CAN_MCR的SLEEP位,右软件唤醒
CAN_InitStructure.CAN_NART=DISABLE;//DISABLE; //CAN报文执法一次,不管是否成功
CAN_InitStructure.CAN_RFLM=DISABLE; //接收FIFO锁定
CAN_InitStructure.CAN_TXFP=DISABLE; //标识别符小的报文具有优先级
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//CAN_Mode_LoopBack;// CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
// CAN Baudrate = 1MBps (CAN clocked at 30 MHz)
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
//CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN1, &CAN_InitStructure);
// CAN filter init
CAN_FilterInitStructure.CAN_FilterNumber = 0;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
// Enable FIFO 0 message pending Interrupt
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
//CAN_ITConfig(CAN2, CAN_IT_TME, ENABLE); //传送中断
//CAN_ITConfig(CAN1, CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR, ENABLE);
//CN2---------------------------------------------------------------------------------------------
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// CAN GPIOs configuration *************************************************
// Enable GPIO clock
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
//Connect CAN pins to AF9
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_CAN2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2);
// Configure CAN RX and TX pins
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// CAN configuration *******************************************************
// Enable CAN clock
CAN_DeInit(CAN2);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE; //禁止时间除法通讯方式
CAN_InitStructure.CAN_ABOM=DISABLE; //软件对CAN_MCR寄存器的INRQ位进行置1后清零
CAN_InitStructure.CAN_AWUM=DISABLE; //睡眠迷失通过清楚CAN_MCR的SLEEP位,右软件唤醒
CAN_InitStructure.CAN_NART=DISABLE;//DISABLE; //CAN报文执法一次,不管是否成功
CAN_InitStructure.CAN_RFLM=DISABLE; //接收FIFO锁定
CAN_InitStructure.CAN_TXFP=DISABLE; //标识别符小的报文具有优先级
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;//CAN_Mode_LoopBack;// CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
// CAN Baudrate = 1MBps (CAN clocked at 30 MHz)
CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;
//CAN_Init(CAN1, &CAN_InitStructure);
CAN_Init(CAN2, &CAN_InitStructure);
// CAN_SlaveStartBank(14);
// CAN filter init
CAN_FilterInitStructure.CAN_FilterNumber = 27;
CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
// Enable FIFO 0 message pending Interrupt
CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
//CAN_ITConfig(CAN2, CAN_IT_TME, ENABLE); //传送中断
// CAN_ITConfig(CAN2, CAN_IT_EWG|CAN_IT_EPV|CAN_IT_BOF|CAN_IT_LEC|CAN_IT_ERR, ENABLE);
}
补充
CAN_TransmitStatus(CAN2, TransmitMailbox);-----返回值是0x2
|