码迷,mamicode.com
首页 > 其他好文 > 详细

STM32下调试CAN通信

时间:2017-06-03 17:23:44      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:接收   class   normal   rcc   ant   amp   启用   过滤   enable   

基本流程:
1、初始化CAN模块:

    启用CAN时钟

    配置CAN功能寄存器(包括位定时,分频等等)

    配置CAN过滤器

    打开中断

2、初始化I/O口

    启用GPIO时钟

    打开CAN对应的GPIO引脚

    配置重映射(根据实际情况)

3、定义CAN发送结构体,接收结构体

4、开始发送并准备接收总线上信号

5、检查接收邮箱收的数据是否正确

 1 void    CAN_Config()
 2 {
 3     CAN_InitTypeDef         CAN_InitStructure;
 4     CAN_FilterInitTypeDef     CAN_FilterInitStructure;
 5     GPIO_InitTypeDef         GPIO_InitStructure;
 6     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
 7     /**Configure the PA11 PA12 to CAN1*/
 8     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
 9     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
10     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
11     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
12     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
13     GPIO_Init(GPIOA,&GPIO_InitStructure);
14     GPIO_PinAFConfig(GPIOA,GPIO_PinSource11,GPIO_AF_CAN1);
15     GPIO_PinAFConfig(GPIOA,GPIO_PinSource12,GPIO_AF_CAN1);
16 
17     CAN_DeInit(CAN1);
18     CAN_InitStructure.CAN_ABOM = DISABLE;        //自动离线
19     CAN_InitStructure.CAN_AWUM = DISABLE;        //自动唤醒
20     CAN_InitStructure.CAN_TTCM = DISABLE;        //触发通讯模式
21     CAN_InitStructure.CAN_NART = ENABLE;        //禁止报文自动重传
22     CAN_InitStructure.CAN_RFLM = DISABLE;        //报文自动覆盖¨
23     CAN_InitStructure.CAN_TXFP = DISABLE;        //优先级判定
24     CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;    //工作模式
25     /**configure baudrate 1M */
26     CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;            
27     CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
28     CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
29     CAN_InitStructure.CAN_Prescaler =20;
30     CAN_Init(CAN1, &CAN_InitStructure);
31     
32     CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;                //启用过滤
33     CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FilterFIFO0;    //过滤邮箱0
34     CAN_FilterInitStructure.CAN_FilterIdHigh= 0x0000;                    //过滤ID
35     CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
36     CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;                //过滤ID掩码
37     CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
38     CAN_FilterInitStructure.CAN_FilterMode =CAN_FilterMode_IdMask;
39     CAN_FilterInitStructure.CAN_FilterNumber = 0;                    
40     CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
41     CAN_FilterInit(&CAN_FilterInitStructure);
42     
43     CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);
44 }

 

定义CAN结构体:

1 CanTxMsg msg;
2 CanRxMsg RxMsg;
3 
4     msg.StdId = 0x09;
5     msg.IDE = CAN_Id_Standard;
6 msg.Data[0]=0x01;
7     msg.DLC=1;
 1 void CAN1_RX0_IRQHandler(void)
 2 {
 3       if(CAN_MessagePending(CAN1,CAN_FIFO0) != RESET)
 4         {
 5             CAN_ClearITPendingBit(CAN1,CAN_IT_FMP0);
 6             CAN_Receive(CAN1,CAN_FIFO0,&RxMsg);
 7           msg.Data[0] = RxMsg.Data[2]+1;
 8             msg.DLC =1;
 9             msg.StdId = 0x02;
10             msg.IDE = CAN_Id_Standard;
11             if(RxMsg.Data[0] ==0x01)
12             {
13                 GPIO_ToggleBits(GPIOD,GPIO_Pin_14);
14             }
15             CAN_Transmit(CAN1,&msg);
16         }
17 }

经过实验,在通信波特率为20K下可以正常工作。建议在低速下测试,若通信正常,可以逐步提高通信速率。

 

STM32下调试CAN通信

标签:接收   class   normal   rcc   ant   amp   启用   过滤   enable   

原文地址:http://www.cnblogs.com/zh666888/p/6937521.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!