打印
[其他]

【MM32 eMiniBoard测评报告】+CAN通讯的问题

[复制链接]
1445|25
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
今天捣鼓了一下CAN通讯,没搞好。
一开始用示波器测波形,CAN发送腿上有波形,后来没了。
程序:
int main(void)
{
  u8 i = 0;
  delay_init();
  uart_initwBaudRate(115200);                 //串口初始化为115200
  CANM_Init();
  CAN_Config(CAN_250K,ExtendedFrame_DoubleFilter,0x172,0x325,0xFF,0xFF);                //CAN验收屏蔽寄存器所有位被置为相关,即只可接受验收代码寄存器中的两个CAN_ID
        //CAN_Config(CAN_250K,ExtendedFrame_DoubleFilter,0x172,0x325,0xFF,0xFF);                //CAN验收屏蔽寄存器所有位被置为相关,即只可接受验收代码寄存器中的两个CAN_ID
       
  while(1)
  {
//    if(flag)                                                                                                                                       
//    {
//      flag = 0;
//      printf("CANID:0x%x  Data",CanPeliRxMsgStructure.ID);        //串口打印
//      for(i=0;i<8;i++)                                                                                                                       
//      {
//        printf("%x",CanPeliRxMsgStructure.Data[i]);                //打印CAN报文内容
//      }
//      printf("\r\n");                                                                                                                                                //换行
//    }
                Send_CANFrame(&CAN_TX_Config);                                                                //发送一帧CAN数据
    delay_ms(30);
  }
}

void CANM_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef         NVIC_InitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
#if 1
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE);        
  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_8; //CAN RX PB8
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_9; //CAN TX PB9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_4);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_4);
#else
  RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);        
  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_11; //CAN RX PA11
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_12; //CAN TX PA12
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_4);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_4);
#endif
  //NVIC配置
  NVIC_InitStructure.NVIC_IRQChannel = CAN_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

}
/********************************************************************************************************
**函数信息 :void CAN_Config(u32 CAN_Pre,CAN_Mode ID,u32 idCode1,u32 idCode2,u32 mask1,u32 mask2)
**功能描述 :CAN配置
**输入参数 :CAN_Pre:CAN通信速率
CAN_250K  CAN_500K
fMode:CAN通信模式
StandardFrame_SingleFilter=0:标准帧,单滤波器模式
StandardFrame_SingleFilter=1:扩展帧,单滤波器模式
ExtendedFrame_SingleFilter=2:标准帧,双滤波器模式
ExtendedFrame_DoubleFilter=3:扩展帧,双滤波器模式
idCode1:验收代码1
idCode2:验收代码2
mask1:验收代码屏蔽位1
mask2:验收代码屏蔽位2
**输出参数 :None
**    备注 :
********************************************************************************************************/
void CAN_Config(u32 CAN_Pre,CAN_Mode ID,u32 idCode1,u32 idCode2,u32 mask1,u32 mask2)
{
  CAN_Peli_InitTypeDef          CAN_Peli_InitStructure;
  RCC_ClocksTypeDef             RCC_Clocks;
  u32 idCodeTemp1,idMaskTemp1;
  u32 idCodeTemp2,idMaskTemp2;
  CAN_Peli_FilterInitTypeDef    CAN_Peli_FilterInitStructure;

  CAN_ResetMode_Cmd(CAN1,ENABLE);// 进入复位模式
  CAN_Mode_Cmd(CAN1, CAN_PELIMode);//CAN进入Peli模式

  RCC_GetClocksFreq(&RCC_Clocks);

  CAN_Peli_StructInit(&CAN_Peli_InitStructure);

  CAN_AutoCfg_BaudParam( &CAN_Peli_InitStructure, RCC_Clocks.PCLK1_Frequency, CAN_Pre);

  CAN_Peli_InitStructure.SAM = RESET;//采样点
  //CAN_Peli_InitStructure.STM = DISABLE;//DISABLE;//自测模式
        CAN_Peli_InitStructure.STM = ENABLE;//DISABLE;//自测模式
  //CAN_Peli_InitStructure.LOM = DISABLE;//只听模式
        CAN_Peli_InitStructure.LOM = ENABLE;//只听模式
  CAN_Peli_InitStructure.EWLR = 0x96;
  CAN_Peli_Init(&CAN_Peli_InitStructure);

  switch(ID)
  {
  case StandardFrame_SingleFilter:
    idCodeTemp1 = idCode1<<(3 + 18);
    idMaskTemp1 = mask1<<(3 + 18);
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Singal;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp1>>8)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = (idCodeTemp1>>0)&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = ((idMaskTemp1>>16)&0xff)|0x1f;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = ((idMaskTemp1>>8)&0xff)|0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = ((idMaskTemp1>>0)&0xff)|0xff;
    break;
  case ExtendedFrame_SingleFilter:
    idCodeTemp1 = idCode1<<3;
    idMaskTemp1 = mask1<<3;
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Singal;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp1>>8)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = idCodeTemp1&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = ((idMaskTemp1>>16)&0xff);
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = ((idMaskTemp1>>8)&0xff);
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = ((idMaskTemp1>>0)&0xff);
    break;        
  case StandardFrame_DoubleFilter:
    idCodeTemp1 = idCode1<<(3 + 18);
    idMaskTemp1 = mask1<<(3 + 18);
    idCodeTemp2 = idCode2<<(3 + 18);
    idMaskTemp2 = mask2<<(3 + 18);
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Double;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = (idCodeTemp2>>16)&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = ((idMaskTemp1>>16)&0xff)|0x1f;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = (idMaskTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = ((idMaskTemp2>>16)&0xff)|0x1f;
    break;
  case ExtendedFrame_DoubleFilter:
    idCodeTemp1 = idCode1<<(3);
    idMaskTemp1 = mask1<<(3);
    idCodeTemp2 = idCode2<<(3);
    idMaskTemp2 = mask2<<(3);
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Double;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = (idCodeTemp2>>16)&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = (idMaskTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = (idMaskTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = (idMaskTemp2>>16)&0xff;
    break;
  default:
    break;
  }
  CAN_Peli_FilterInit(&CAN_Peli_FilterInitStructure);
  //CAN_Peli_ITConfig(CAN_IT_RI,ENABLE);
        //CAN_Peli_ITConfig(CAN_IT_TI,ENABLE);
        CAN_Peli_ITConfig(CAN_IT_ALL,ENABLE);
  CAN_ResetMode_Cmd(CAN1,DISABLE);//退出复位模式,进入工作模式
}

各位看看,有什么错误?
另外,发送接收腿都倒换着接了。谢谢!

使用特权

评论回复
沙发
coslight| | 2020-5-8 08:24 | 只看该作者
SW2转换开关注意一下

使用特权

评论回复
板凳
Cjy_JDxy|  楼主 | 2020-5-8 08:26 | 只看该作者
coslight 发表于 2020-5-8 08:24
SW2转换开关注意一下

拨到正确位置了

使用特权

评论回复
地板
coslight| | 2020-5-8 09:44 | 只看该作者
Cjy_JDxy 发表于 2020-5-8 08:26
拨到正确位置了

我的板子是拨到位置也连不上,后来干脆焊死,就好了

使用特权

评论回复
5
Cjy_JDxy|  楼主 | 2020-5-8 16:41 | 只看该作者

到断点了。

接收不成功。
配置如下:
void CAN_Config(u32 CAN_Pre,CAN_Mode ID,u32 idCode1,u32 idCode2,u32 mask1,u32 mask2)
{
  CAN_Peli_InitTypeDef          CAN_Peli_InitStructure;
  RCC_ClocksTypeDef             RCC_Clocks;
  u32 idCodeTemp1,idMaskTemp1;
  u32 idCodeTemp2,idMaskTemp2;
  CAN_Peli_FilterInitTypeDef    CAN_Peli_FilterInitStructure;

  CAN_ResetMode_Cmd(CAN1,ENABLE);// ½øÈ븴λģʽ
  CAN_Mode_Cmd(CAN1, CAN_PELIMode);//CAN½øÈëPeliģʽ

  RCC_GetClocksFreq(&RCC_Clocks);

  CAN_Peli_StructInit(&CAN_Peli_InitStructure);

  CAN_AutoCfg_BaudParam( &CAN_Peli_InitStructure, RCC_Clocks.PCLK1_Frequency, CAN_Pre);

  CAN_Peli_InitStructure.SAM = RESET;//²ÉÑùµã
  CAN_Peli_InitStructure.STM = ENABLE;//DISABLE;//×Ô²âģʽ
  CAN_Peli_InitStructure.LOM = DISABLE;//Ö»Ìýģʽ
  //        CAN_Peli_InitStructure.EWLR = 0x96;
  CAN_Peli_Init(&CAN_Peli_InitStructure);

  switch(ID)
  {
  case StandardFrame_SingleFilter:
    idCodeTemp1 = idCode1<<(3 + 18);
    idMaskTemp1 = mask1<<(3 + 18);
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Singal;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp1>>8)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = (idCodeTemp1>>0)&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = ((idMaskTemp1>>16)&0xff)|0x1f;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = ((idMaskTemp1>>8)&0xff)|0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = ((idMaskTemp1>>0)&0xff)|0xff;
    break;
  case ExtendedFrame_SingleFilter:
    idCodeTemp1 = idCode1<<3;
    idMaskTemp1 = mask1<<3;
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Singal;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp1>>8)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = idCodeTemp1&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = ((idMaskTemp1>>16)&0xff);
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = ((idMaskTemp1>>8)&0xff);
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = ((idMaskTemp1>>0)&0xff);
    break;        
  case StandardFrame_DoubleFilter:
    idCodeTemp1 = idCode1<<(3 + 18);
    idMaskTemp1 = mask1<<(3 + 18);
    idCodeTemp2 = idCode2<<(3 + 18);
    idMaskTemp2 = mask2<<(3 + 18);
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Double;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = (idCodeTemp2>>16)&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = ((idMaskTemp1>>16)&0xff)|0x1f;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = (idMaskTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = ((idMaskTemp2>>16)&0xff)|0x1f;
    break;
  case ExtendedFrame_DoubleFilter:
    idCodeTemp1 = idCode1<<(3);
    idMaskTemp1 = mask1<<(3);
    idCodeTemp2 = idCode2<<(3);
    idMaskTemp2 = mask2<<(3);
    CAN_Peli_FilterInitStructure.AFM = CAN_FilterMode_Double;
    CAN_Peli_FilterInitStructure.CAN_FilterId0 = (idCodeTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId1 = (idCodeTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId2 = (idCodeTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterId3 = (idCodeTemp2>>16)&0xff;

    CAN_Peli_FilterInitStructure.CAN_FilterMaskId0 = (idMaskTemp1>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId1 = (idMaskTemp1>>16)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId2 = (idMaskTemp2>>24)&0xff;
    CAN_Peli_FilterInitStructure.CAN_FilterMaskId3 = (idMaskTemp2>>16)&0xff;
    break;
  default:
    break;
  }
  CAN_Peli_FilterInit(&CAN_Peli_FilterInitStructure);
  CAN_Peli_ITConfig(CAN_IT_ALL,ENABLE);
  CAN_ResetMode_Cmd(CAN1,DISABLE);//Í˳ö¸´Î»Ä£Ê½,½øÈ빤×÷ģʽ
}



使用特权

评论回复
6
ningling_21| | 2020-5-8 22:22 | 只看该作者
Cjy_JDxy 发表于 2020-5-8 16:41
到断点了。

接收不成功。

回环模式呢

使用特权

评论回复
7
Cjy_JDxy|  楼主 | 2020-5-9 07:59 | 只看该作者

回环模式也不行

使用特权

评论回复
8
ningling_21| | 2020-5-9 08:11 | 只看该作者
Cjy_JDxy 发表于 2020-5-9 07:59
回环模式也不行

应该是程序问题

使用特权

评论回复
9
Cjy_JDxy|  楼主 | 2020-5-9 08:12 | 只看该作者

可是我不知道哪儿有问题

使用特权

评论回复
10
夜神荣| | 2022-9-28 16:50 | 只看该作者
我用得mm32 官方例程,也接收不到数据,can接收中断也不行,没反应。   你们有解决么

使用特权

评论回复
11
xld0932| | 2022-9-29 09:38 | 只看该作者
CAN接口与CAN工具连接的CAN_H和CAN_L调换一下试试呢

使用特权

评论回复
12
bestwell| | 2022-10-2 21:30 | 只看该作者
can总线的可靠通信是依靠什么机制来实现的?

使用特权

评论回复
13
sesefadou| | 2022-10-2 21:51 | 只看该作者
怎么实现一对一通讯的               

使用特权

评论回复
14
pixhw| | 2022-10-2 22:17 | 只看该作者
can通讯电路上面的电压值范围是多少呢?

使用特权

评论回复
15
everyrobin| | 2022-10-2 22:44 | 只看该作者
canopen通讯和CAN通讯有什么不同吗?

使用特权

评论回复
16
AloneKaven| | 2022-10-4 22:16 | 只看该作者
试试回环

使用特权

评论回复
17
elsaflower| | 2022-10-5 17:19 | 只看该作者
canbus的通讯波特率可以被设为是多少呢?   

使用特权

评论回复
18
pentruman| | 2022-10-5 18:29 | 只看该作者
can线的通讯速率范围是多少     

使用特权

评论回复
19
robertesth| | 2022-10-5 19:54 | 只看该作者
can通讯可以并联吗              

使用特权

评论回复
20
mollylawrence| | 2022-10-13 15:04 | 只看该作者
can总线的可靠通信是依靠什么机制来实现的?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:绿水本无忧因风皱面,青山原不老为雪白头。

553

主题

3530

帖子

19

粉丝