STM32F205ZET6 单片机CAN1通讯程序无法通讯

2019-03-25 14:47发布

STM32F205ZET6单片机,使用PA11和PA12进行CAN通信,收发器无论是ISO1050还是TJA1050,都不成功,发送信息时,CAN_TransmitStatus函数一直返回Pending,请问是为什么啊?




另外,使用CAN分析仪——收发器——PA11,PA12连接时,单片机的PA11和PA12引脚是有波形的,,两个引脚都有方波,TXD上的波形密集些,RXD上面的好像只有一个低脉冲,占空比很高,


主要是使用ISO1050芯片通讯,但是就无法发送数据,也没有办法进入到接收终端之中,中断函数我自认为配置好了,和库函数的配置是一样的,


ISO1050 电路为:有120欧终端电阻,CAN_TX和CAN_RX接上拉电阻472,CANH和CANL之间有120欧终端电阻,并且各串接了一个零欧电阻,


大家交流交流?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
16条回答
mo_正_pei
1楼-- · 2019-03-27 16:56
  1. int main(void)
  2. {
  3.   RCC_ClocksTypeDef RCC_Clocks;
  4. //&**** SysTick end of count event each 10ms *****&/
  5.   RCC_GetClocksFreq(&RCC_Clocks);
  6.   SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);
  7.   NVIC_Config();
  8.   LED_Config();
  9.   CAN_Config();
  10.         while(1)
  11.         {
  12.                 CAN_Trans();
  13.                 Delay(60);
  14.         }
  15. }


  16. void NVIC_Config(void)
  17. {
  18.   NVIC_InitTypeDef  NVIC_InitStructure;
  19. //NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  20.   NVIC_InitStructure.NVIC_IRQChannel=CAN1_RX0_IRQn;   //中断请求 CAN1_RX0_IRQn
  21.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  22.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
  23.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  24.   NVIC_Init(&NVIC_InitStructure);
  25. }


  26. void CAN_Config(void)
  27. {
  28.   GPIO_InitTypeDef  GPIO_InitStructure;
  29.        
  30.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  31. /* Connect CAN pins to AF9 */
  32.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
  33.   GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);

  34. /* Configure CAN RX and TX pins */
  35. /* PA11 RX-上拉输入,PA12 TX-复用推挽输出*/
  36.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 |GPIO_Pin_12;
  37.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  38.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  39.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽输出
  40.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP; //上拉输入
  41.   GPIO_Init(GPIOA, &GPIO_InitStructure);

  42.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  43.        
  44.   CAN_DeInit(CAN1);

  45.   CAN_InitStructure.CAN_TTCM = DISABLE; //time triggered communication mode
  46.   CAN_InitStructure.CAN_ABOM = DISABLE; //automatic bus-off management
  47.   CAN_InitStructure.CAN_AWUM = DISABLE; //automatic wake-up mode
  48.   CAN_InitStructure.CAN_NART = DISABLE; //non-automatic retransmission mode
  49.   CAN_InitStructure.CAN_RFLM = DISABLE; //Receive FIFO Locked mode
  50.   CAN_InitStructure.CAN_TXFP = DISABLE;  //transmit FIFO priority
  51.   CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;

  52.        
  53. /*主板的时钟使用外部告诉时钟,25MHz,CAN clocked(APB1) at 30 MHz, set CAN Baudrate = 125KBps
  54.           than, how to calculate the Tq*/  
  55. /* 主板的时钟使用外部高速时钟,25MHz Baudrate = 500 Kbps ,CAN clocked (APB1) at 30MHz*/
  56.   CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;  //Specifies the maximum number of time quanta
  57.   CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  58.   CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  59.   CAN_InitStructure.CAN_Prescaler = 4;   //30 000000/(1+6+8)/4= 30 000000/(1+2+3)/10=
  60.   CAN_Init(CAN1, &CAN_InitStructure);


  61.   CAN_FilterInitStructure.CAN_FilterNumber = 0;
  62.   CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;// CAN_FilterMode_IdList
  63.   CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
  64.   CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
  65.   CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
  66.   CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
  67.   CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
  68.   CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0; //FIFO0
  69.   CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  70.   CAN_FilterInit(&CAN_FilterInitStructure);
  71.        
  72.        
  73.   TxMessage.StdId = 0x04;  //standard ID
  74.   TxMessage.ExtId = 0x00;   //extend ID
  75.   TxMessage.RTR = CAN_RTR_DATA;  //data frame
  76.   TxMessage.IDE = CAN_ID_STD;   //standard ID
  77.   TxMessage.DLC = 1;  //length of data
  78.   TxMessage.Data[0] = 0xDE;
  79.        
  80.   CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);  // finish sending Interrupt CAN_IT_TME, Enable FIFO 0 message pending Interrupt
  81.        
  82. }

  83. void CAN_Trans(void)
  84. {
  85.   uint32_t i=0;
  86.   uint8_t TransmitMailbox=0;
  87. /* Transmit Structure preparation */
  88.   TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
  89. //CAN_Transmit(CAN1, &TxMessage);

  90.   i = 0;
  91. //一定时间内不断发送,直到:1、发送成功==CANTXOK,2、i !=0xFFFF
  92.   while((CAN_TransmitStatus(CAN1, TransmitMailbox)  != CANTXOK ) && (i  !=  0xFFFF)) // CAN_TxStatus_Failed
  93.   {
  94.     i++;  //发送失败
  95.   }
  96.   i = 0;
  97.   while((CAN_MessagePending(CAN1, CAN_FIFO0) <1 ) && (i  !=  0xFFFF))
  98.   {
  99.     i++;  //MessagePending 不是FIFO 1,也不是FIFO 0
  100.   }
  101. }
复制代码
mo_正_pei
2楼-- · 2019-03-27 19:53
解决了!!!!!!!!!!!!!!!我的天天老爷,收发器ISO1050,CANH,CANL那端没供电!!!!!!!!!!!!!!我真是猪到家了,因为底板电源种类比较多,没有认真检查,浪费了将近一周的时间,天啊!!!!!!!!!!!!!程序是没有问题的!!!!!!!!!!
mo_正_pei
3楼-- · 2019-03-27 20:56
谢谢各位大哥
huaiqiao
4楼-- · 2019-03-27 22:40
mo_正_pei 发表于 2017-3-16 18:26
解决了!!!!!!!!!!!!!!!我的天天老爷,收发器ISO1050,CANH,CANL那端没供电!!!!!!! ...

你这样说让我有点想骂人。。。。。,我之前就让你排除硬件问题,你告诉我的硬件没有问题。。。。。。。
我们这种控制,硬件是软件的基础,硬件都有问题,你的软件写的再牛逼,也没有用。。。。

一周热门 更多>