您好,欢迎访问三七文档
1.#includesysdef.h2.3.#defineMAX_MAIL_NUM34.//CAN总线调试:0=运行1=自环调试5.#defineCAN_DEBUG06.//CAN总线波特率:0=250kbps,1=500kbps,2=1Mbps7.#defineCAN1_BPS08.9.unsignedcharcan1_addr=0;10.11.unsignedshortCan1_Tx_Count=0;12.unsignedshortCan1_Rx_Count=0;13.14.unsignedshortCan1_Send_Delay=0;15.16.unsignedcharCan1_Send_Buf[10]={0xeb,0x90,0x01,0x55,0xAA};17.unsignedcharCan1_Recv_Buf[10]={0};18.externintangle_num;19.externunsignedintangle_data;20.21.staticu8CAN_msg_num[MAX_MAIL_NUM];//发送邮箱标记22.23.voidCAN1_Config_init(void)24.{25.CAN_InitTypeDefCAN_InitStructure;26.CAN_FilterInitTypeDefCAN_FilterInitStructure;27.28./*CANregisterinit*/29.CAN_DeInit(CAN1);30.CAN_StructInit(&CAN_InitStructure);31.32./*CANcellinit*///36MHz500Kbps33.CAN_InitStructure.CAN_TTCM=DISABLE;//禁止时间触发通信模式34.CAN_InitStructure.CAN_ABOM=DISABLE;//软件对CAN_MCR寄存器的INRQ位进行置1随后清0后,一旦硬件检测35.//到128次11位连续的隐性位,就退出离线状态36.CAN_InitStructure.CAN_AWUM=DISABLE;//睡眠模式通过清除CAN_MCR寄存器的SLEEP位,由软件唤醒37.CAN_InitStructure.CAN_NART=DISABLE;//CAN报文是否只发1次,不管发送的结果如何(成功/出错或仲裁丢失)38.CAN_InitStructure.CAN_RFLM=DISABLE;//在接收到溢出时FIFO未被锁定,当接收到FIFO报文未被读出,下一个收到的报文会覆盖原有的报文39.CAN_InitStructure.CAN_TXFP=DISABLE;//发送的FIFO优先级由报文的标识符来决定40.41.#ifCAN_DEBUG42.CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;43.#else44.CAN_InitStructure.CAN_Mode=CAN_Mode_Normal;45.#endif46.//传输波特率47.48.if(CAN1_BPS==0)49.{50.CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位51.CAN_InitStructure.CAN_BS1=CAN_BS1_12tq;//时间段1为9个时间单位52.CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;//时间段2为8个时间单位53.CAN_InitStructure.CAN_Prescaler=9;//36M/(1+12+3)/9=250kbps54.//36M/(1+5+2)/9=500kbps55.//36M(1+2+1)/9=1M56.}57.elseif(CAN1_BPS==1)58.{59.CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位60.CAN_InitStructure.CAN_BS1=CAN_BS1_5tq;//时间段1为9个时间单位61.CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//时间段2为8个时间单位62.CAN_InitStructure.CAN_Prescaler=9;//36M/(1+12+3)/9=250kbps63.64.}65.else66.{67.CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;//重新同步跳跃宽度1个时间单位68.CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;//时间段1为9个时间单位69.CAN_InitStructure.CAN_BS2=CAN_BS2_1tq;//时间段2为8个时间单位70.CAN_InitStructure.CAN_Prescaler=9;//36M/(1+12+3)/9=250kbps71.72.}73.CAN_Init(CAN1,&CAN_InitStructure);74.75./*CANfilterinit*/76.CAN_FilterInitStructure.CAN_FilterNumber=0;//指定了待初始化的过滤器077.CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;//指定了过滤器将被初始化到的模式为标识符屏蔽位模式78.CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;;//给出了过滤器位宽1个32位过滤器79.CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;//用来设定过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个)80.CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;;//用来设定过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个)81.CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其高段位,16位位宽时为第一个)82.CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//用来设定过滤器屏蔽标识符或者过滤器标识符(32位位宽时为其低段位,16位位宽时为第二个)83.CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;;//设定了指向过滤器的FIFO084.CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//使能过滤器85.CAN_FilterInit(&CAN_FilterInitStructure);86.87.}88.89.//***************************************************************90.//BaudRate=1/NominalBitTime91.//NominalBitTime=1tq+tBS1+tBS292.//tq=(BRP[9:0]+1)xtPCLK93.//tPCLK=CAN'sclock=APB1'sclock94.//1Mbps速率下,采用点的位置在6tq位置处,BS1=5,BS2=295.//500kbps速率下,采用点的位置在8tq位置处,BS1=7,BS2=396.//250kbps速率下,采用点的位置在14tq位置处,BS1=13,BS2=297.//125k,100k,50k,20k,10k的采用点位置与250K相同98.//****************************************************************99.100.voidCAN1_Com_init(void)101.{102.GPIO_InitTypeDefGPIO_InitStruct;103.NVIC_InitTypeDefNVIC_InitStructure;104.105./*EnableCANRX0interruptIRQchannel*/106.NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN1_RX0_IRQn;107.NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;108.NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;109.NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;110.NVIC_Init(&NVIC_InitStructure);111.112.NVIC_InitStructure.NVIC_IRQChannel=USB_HP_CAN1_TX_IRQn;113.NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;114.NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;115.NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;116.NVIC_Init(&NVIC_InitStructure);117.118.NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX1_IRQn;119.NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;120.NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;121.NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;122.NVIC_Init(&NVIC_InitStructure);123.124.125.RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);126.RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);127.//CanRx128.GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11;129.GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;130.GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;131.GPIO_Init(GPIOA,&GPIO_InitStruct);132.133.//CanTx134.GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;135.GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;136.GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;137.GPIO_Init(GPIOA,&GPIO_InitStruct);138.139.CAN1_Config_init();140.141.CAN_ITConfig(CAN1,CAN_IT_FMP0|CAN_IT_FF0|CAN_IT_FOV0,ENABLE);//fifo0中断142.CAN_ITConfig(CAN1,CAN_IT_FMP1|CAN_IT_FF1|CAN_IT_FOV1,ENABLE);//fifo1中断143.CAN_ITConfig(CAN1,CAN_IT_T
本文标题:can实例
链接地址:https://www.777doc.com/doc-2903180 .html