[技术问答] 新唐NUC131OBD与汽车模拟器之间的can通信交互

[复制链接]
3627|13
 楼主| 嵌入式初涉江湖 发表于 2015-12-2 15:19 | 显示全部楼层 |阅读模式
本人嵌入式小白一个,目前在弄一个OBD与汽车模拟器之间的通信。基于新唐给的BSP弄了一个交互的程序,发送数据OK,模拟器也有反应,正常情况下模拟器应该有反馈的数据可以接收的,可是我接收到的数据包是空的?不知道问题到底在哪里?希望有懂的大神帮我排解一下问题。我把主程序的代码传一下,大家帮我看看。谢谢。
  1. #include <stdio.h>
  2. #include "NUC131.h"


  3. #define PLL_CLOCK       48000000

  4. /*---------------------------------------------------------------------------*/
  5. /*  Function Declare                                                         */
  6. /*---------------------------------------------------------------------------*/
  7. extern char GetChar(void);
  8. void CAN_ShowMsg(STR_CANMSG_T* Msg);

  9. /* Declare a CAN message structure */
  10. STR_CANMSG_T tMsg;                //transmit data message structure
  11. STR_CANMSG_T rrMsg;   //recieve data message structure
  12. CAN_T *tCAN;       

  13. /*---------------------------------------------------------------------------------------------------------*/
  14. /* ISR to handle CAN interrupt event                                                                       */
  15. /*---------------------------------------------------------------------------------------------------------*/
  16. void CAN_MsgInterrupt(CAN_T *tCAN, uint32_t u32IIDR)
  17. {
  18.                 printf("\nThe recieved data is:\n");
  19.        
  20.     if(u32IIDR == 1)
  21.     {
  22.         CAN_Receive(tCAN, 0, &rrMsg);  
  23.         CAN_ShowMsg(&rrMsg);
  24.     }
  25.     if(u32IIDR == 5 + 1)
  26.     {
  27.         CAN_Receive(tCAN, 5, &rrMsg);
  28.         CAN_ShowMsg(&rrMsg);
  29.     }
  30.     if(u32IIDR == 31 + 1)
  31.     {
  32.         CAN_Receive(tCAN, 31, &rrMsg);
  33.         CAN_ShowMsg(&rrMsg);
  34.     }
  35. }

  36. /*---------------------------------------------------------------------------------------------------------*/
  37. /* CAN0 interrupt handler                                                                                  */
  38. /*---------------------------------------------------------------------------------------------------------*/
  39. void CAN0_IRQHandler(void)
  40. {
  41.     uint32_t u8IIDRstatus;

  42.     u8IIDRstatus = CAN0->IIDR;

  43.     if(u8IIDRstatus == 0x00008000)        /* Check Status Interrupt Flag (Error status Int and Status change Int) */
  44.     {
  45.         /**************************/
  46.         /* Status Change interrupt*/
  47.         /**************************/
  48.         if(CAN0->STATUS & CAN_STATUS_RXOK_Msk)
  49.         {
  50.             CAN0->STATUS &= ~CAN_STATUS_RXOK_Msk;   /* Clear RxOK status*/
  51.         }

  52.         if(CAN0->STATUS & CAN_STATUS_TXOK_Msk)
  53.         {
  54.             CAN0->STATUS &= ~CAN_STATUS_TXOK_Msk;    /* Clear TxOK status*/
  55.         }

  56.         /**************************/
  57.         /* Error Status interrupt */
  58.         /**************************/
  59.         if(CAN0->STATUS & CAN_STATUS_BOFF_Msk)
  60.         {
  61.             printf("BOFF INT\n") ;
  62.         }
  63.         else if(CAN0->STATUS & CAN_STATUS_EWARN_Msk)
  64.         {
  65.             printf("EWARN INT\n") ;
  66.         }
  67.         else if((CAN0->ERR & CAN_ERR_TEC_Msk) != 0)
  68.         {
  69.             printf("Transmit error!\n") ;
  70.         }
  71.         else if((CAN0->ERR & CAN_ERR_REC_Msk) != 0)
  72.         {
  73.             printf("Receive error!\n") ;
  74.         }

  75.     }
  76.     else if((u8IIDRstatus >= 0x1) || (u8IIDRstatus <= 0x20))
  77.     {
  78.                                 CAN_MsgInterrupt(CAN0, u8IIDRstatus);
  79.                        
  80.         CAN_CLR_INT_PENDING_BIT(CAN0, (u8IIDRstatus - 1)); /* Clear Interrupt Pending */
  81.     }
  82.     else if(CAN0->WU_STATUS == 1)
  83.     {
  84.         printf("Wake up\n");

  85.         CAN0->WU_STATUS = 0;    /* Write '0' to clear */
  86.     }

  87. }



  88. /*---------------------------------------------------------------------------*/
  89. /*  Show Message Function                                                    */
  90. /*---------------------------------------------------------------------------*/
  91. void CAN_ShowMsg(STR_CANMSG_T* Msg)
  92. {
  93.     uint8_t i;

  94.     /* Show the message information */
  95.     printf("Read ID=0x%X, Type=%s, DLC=%d, Data=", Msg->Id, Msg->IdType ? "EXT" : "STD", Msg->DLC);
  96.     for(i = 0; i < Msg->DLC; i++)
  97.         printf("%X,", Msg->Data[i]);
  98.     printf("\n\n");
  99. }

  100. void SYS_Init(void)
  101. {
  102.     /*---------------------------------------------------------------------------------------------------------*/
  103.     /* Init System Clock                                                                                       */
  104.     /*---------------------------------------------------------------------------------------------------------*/

  105.     /* Enable Internal RC 22.1184MHz clock */
  106.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  107.     /* Waiting for Internal RC clock ready */
  108.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  109.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  110.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  111.     /* Enable external XTAL 12MHz clock */
  112.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  113.     /* Waiting for external XTAL clock ready */
  114.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  115.     /* Set core clock as PLL_CLOCK from PLL */
  116.     CLK_SetCoreClock(PLL_CLOCK);

  117.     /* Enable UART module clock */
  118.     CLK_EnableModuleClock(UART0_MODULE);

  119.     /* Enable CAN module clock */
  120.     CLK_EnableModuleClock(CAN0_MODULE);
  121.     //CLK_EnableModuleClock(CAN1_MODULE);

  122.     /* Select UART module clock source */
  123.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));

  124.     /*---------------------------------------------------------------------------------------------------------*/
  125.     /* Init I/O Multi-function                                                                                 */
  126.     /*---------------------------------------------------------------------------------------------------------*/

  127.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  128.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
  129.     SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;

  130.     /* Set PD multi-function pins for CANTX0, CANRX0 */
  131.     SYS->GPD_MFP &= ~(SYS_GPD_MFP_PD6_Msk | SYS_GPD_MFP_PD7_Msk);
  132.     SYS->GPD_MFP = SYS_GPD_MFP_PD6_CAN0_RXD | SYS_GPD_MFP_PD7_CAN0_TXD;

  133. }

  134. /*---------------------------------------------------------------------------------------------------------*/
  135. /* Init UART                                                                                               */
  136. /*---------------------------------------------------------------------------------------------------------*/
  137. void UART0_Init()
  138. {
  139.     /* Reset IP */
  140.     SYS_ResetModule(UART0_RST);

  141.     /* Configure UART0 and set UART0 Baudrate */
  142.     UART_Open(UART0, 115200);
  143. }



  144. /*----------------------------------------------------------------------------*/
  145. /*  Check the real baud-rate                                                  */
  146. /*----------------------------------------------------------------------------*/
  147. void BaudRateCheck(uint32_t u32BaudRate, uint32_t u32RealBaudRate)
  148. {
  149.     /* Get Core Clock Frequency */
  150.     SystemCoreClockUpdate();

  151.     if(u32BaudRate != u32RealBaudRate)
  152.     {
  153.         printf("\nSetting CAN baud-rate failed.\n");
  154.         printf("Real baud-rate value(bps): %d\n", u32RealBaudRate);
  155.         printf("CAN baud-rate calculation equation as below:\n");
  156.         printf("CAN baud-rate(bps) = Fin/(BPR+1)*(Tseg1+Tseg2+3)\n");
  157.         printf("where: Fin: System clock freq.(Hz)\n");
  158.         printf("       BRP: The baud rate prescale. It is composed of BRP (CAN_BTIME[5:0]) and BRPE (CAN_BRPE[3:0]).\n");
  159.         printf("       Tseg1: Time Segment before the sample point. You can set tseg1 (CAN_BTIME[11:8]).\n");
  160.         printf("       Tseg2: Time Segment after the sample point. You can set tseg2 (CAN_BTIME[14:12]).\n");

  161.         if(SystemCoreClock % u32BaudRate != 0)
  162.             printf("\nThe BPR does not calculate, the Fin must be a multiple of the CAN baud-rate.\n");

  163.         else
  164.             printf("\nThe BPR does not calculate, the (Fin/(CAN baud-rate)) must be a multiple of the (Tseg1+Tseg1+3).\n");
  165.     }
  166.     else
  167.         printf("\nReal baud-rate value(bps): %d\n", u32RealBaudRate);
  168. }

  169. /*----------------------------------------------------------------------------*/
  170. /*  Set the CAN speed                                                         */
  171. /*----------------------------------------------------------------------------*/
  172. void SetCANSpeed(CAN_T  *tCAN)
  173. {
  174.     uint32_t BaudRate = 0, RealBaudRate = 0;
  175.                 BaudRate = 250000;
  176.                 RealBaudRate = CAN_Open(tCAN,  BaudRate, CAN_NORMAL_MODE);
  177.        
  178.     /* Check the real baud-rate is OK */
  179.     BaudRateCheck(BaudRate, RealBaudRate);
  180. }

  181. //set the param of the corresponding mode
  182. void Set_NormalMode_Tx(CAN_T *tCAN)
  183. {

  184.     CAN_EnableInt(tCAN, CAN_CON_IE_Msk | CAN_CON_SIE_Msk);      /* Enable CAN interrupt and corresponding NVIC of CAN */
  185.     NVIC_SetPriority(CAN0_IRQn, (1 << __NVIC_PRIO_BITS) - 2);   /* Install CAN call back functions */
  186.     NVIC_EnableIRQ(CAN0_IRQn);
  187.        
  188.     printf("Use Message Object No.0 to send STD_ID:0x7DF, Data[02,01]\n\n\n");
  189.        
  190.                 tMsg.FrameType = CAN_DATA_FRAME;                        //接收帧的类型
  191.     tMsg.IdType    = CAN_STD_ID;                                   //标准帧报文
  192.     tMsg.Id        = 0x7DF;                                                                //目的地址
  193.     tMsg.DLC       = 2;                                                                                //发送报文字节数
  194.                 tMsg.Data[0]   = 0X02;                        
  195.                 tMsg.Data[1]   = 0x01;       

  196. }


  197. /*---------------------------------------------------------------------------------------------------------*/
  198. /* MAIN function                                                                                           */
  199. /*---------------------------------------------------------------------------------------------------------*/
  200. int main(void)
  201. {

  202.     tCAN = (CAN_T *) CAN0;

  203.     /* Unlock protected registers */
  204.     SYS_UnlockReg();

  205.     /* Init System, IP clock and multi-function I/O */
  206.     SYS_Init();

  207.     /* Lock protected registers */
  208.     SYS_LockReg();

  209.     /* Init UART0 for printf */
  210.     UART0_Init();

  211.     /* Configuring the Bit Timing */
  212.     SetCANSpeed(tCAN);
  213.                
  214.                 Set_NormalMode_Tx(tCAN);
  215.                
  216.                 while(1)
  217.                 {
  218.                         CLK_SysTickDelay(100000);
  219.                        
  220.                         if(CAN_Transmit(tCAN, MSG(0), &tMsg)==TRUE)                    
  221.                                         printf("Transmit succeeded.\n");
  222.                         else
  223.                                         printf("Transmit failed.\n");                       
  224.                        
  225.                         if(CAN_SetRxMsg(tCAN, MSG(0), rrMsg.IdType, rrMsg.Id) == FALSE)                        //配置接收数据的报文对象
  226.                                         printf("Set Rx Msg Object failed\n");
  227.                 }
  228. }





 楼主| 嵌入式初涉江湖 发表于 2015-12-2 15:20 | 显示全部楼层
在线等大神解答。
 楼主| 嵌入式初涉江湖 发表于 2015-12-2 15:24 | 显示全部楼层
拜托啦,有没有大神愿意帮忙的啊。
 楼主| 嵌入式初涉江湖 发表于 2015-12-2 16:20 | 显示全部楼层
这是串口调试助手显示的内容,就是没有接收数据。
80da885c-87e4-4462-a8aa-edfdbf7ab20f.png
wangwang2015 发表于 2015-12-2 17:56 | 显示全部楼层
楼主,如果比较急,需要技术支持,可加FAE QQ2355898184
cowboy2014 发表于 2015-12-2 20:52 | 显示全部楼层
can是不是和uart是一样的吗
小猫爱吃鱼 发表于 2015-12-5 20:35 | 显示全部楼层
嵌入式初涉江湖 发表于 2015-12-2 16:20
这是串口调试助手显示的内容,就是没有接收数据。

得先查查有没有初始化成功啊
DBZ_OBD 发表于 2015-12-7 14:19 | 显示全部楼层
加我QQ 1932861944
DBZ_OBD 发表于 2015-12-7 14:20 | 显示全部楼层
我们是专业OBD技术提供商,可以开放源码供你参考加我QQ 1932861944
 楼主| 嵌入式初涉江湖 发表于 2015-12-7 14:57 | 显示全部楼层
小猫爱吃鱼 发表于 2015-12-5 20:35
得先查查有没有初始化成功啊

你说的初始化是指什么呢?发送成功了啊
huangcunxiake 发表于 2015-12-7 20:48 | 显示全部楼层
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * $Revision: 4 $
  5. * $Date: 14/09/11 7:15p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate Smartcard UART mode by connecting PA.8 and PA.9 pins.
  7. *
  8. * @note
  9. * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #include "Nano100Series.h"

  13. uint8_t au8TxBuf[] = "Hello World!";


  14. /**
  15.   * @brief  The interrupt services routine of smartcard port 0
  16.   * @param  None
  17.   * @retval None
  18.   */
  19. void SC0_IRQHandler(void)
  20. {
  21.     // Print SCUART received data to UART port
  22.     // Data length here is short, so we're not care about UART FIFO over flow.
  23.     UART_WRITE(UART0, SCUART_READ(SC0));

  24.     // RDA is the only interrupt enabled in this sample, this status bit
  25.     // automatically cleared after Rx FIFO empty. So no need to clear interrupt
  26.     // status here.

  27.     return;
  28. }

  29. void SYS_Init(void)
  30. {
  31.     /*---------------------------------------------------------------------------------------------------------*/
  32.     /* Init System Clock                                                                                       */
  33.     /*---------------------------------------------------------------------------------------------------------*/
  34.     /* Unlock protected registers */
  35.     SYS_UnlockReg();

  36.     /* Enable External XTAL (4~24 MHz) */
  37.     CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk);

  38.     /* Waiting for 12MHz clock ready */
  39.     CLK_WaitClockReady( CLK_CLKSTATUS_HXT_STB_Msk);

  40.     /* Switch HCLK clock source to HXT */
  41.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT,CLK_HCLK_CLK_DIVIDER(1));

  42.     /* Enable IP clock */
  43.     CLK_EnableModuleClock(UART0_MODULE);
  44.     CLK_EnableModuleClock(SC0_MODULE);


  45.     /* Select IP clock source */
  46.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_UART_CLK_DIVIDER(1));
  47.     CLK_SetModuleClock(SC0_MODULE, CLK_CLKSEL2_SC_S_HXT, CLK_SC0_CLK_DIVIDER(1));

  48.     /* Update System Core Clock */
  49.     /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock. */
  50.     SystemCoreClockUpdate();


  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Init I/O Multi-function                                                                                 */
  53.     /*---------------------------------------------------------------------------------------------------------*/
  54.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  55.     SYS->PB_L_MFP &= ~(SYS_PB_L_MFP_PB0_MFP_Msk | SYS_PB_L_MFP_PB1_MFP_Msk);
  56.     SYS->PB_L_MFP |= (SYS_PB_L_MFP_PB1_MFP_UART0_TX | SYS_PB_L_MFP_PB0_MFP_UART0_RX);
  57.     /* Set PA.8 and PA.9 pin for SC UART mode */
  58.     SYS->PA_H_MFP &= ~(SYS_PA_H_MFP_PA8_MFP_Msk | SYS_PA_H_MFP_PA9_MFP_Msk);
  59.     SYS->PA_H_MFP |= (SYS_PA_H_MFP_PA8_MFP_SC0_CLK | SYS_PA_H_MFP_PA9_MFP_SC0_DAT);

  60.     /* Lock protected registers */
  61.     SYS_LockReg();
  62. }

  63. int main(void)
  64. {
  65.     /* Init System, IP clock and multi-function I/O
  66.        In the end of SYS_Init() will issue SYS_LockReg()
  67.        to lock protected register. If user want to write
  68.        protected register, please issue SYS_UnlockReg()
  69.        to unlock protected register if necessary */
  70.     SYS_Init();

  71.     /* Init UART to 115200-8n1 for print message */
  72.     UART_Open(UART0, 115200);

  73.     printf("This sample code demos smartcard interface UART mode\n");
  74.     printf("Please connect SC0 CLK pin(PA.8) with SC0 I/O pin(PA.9)\n");
  75.     printf("Hit any key to continue\n");
  76.     getchar();

  77.     // Open smartcard interface 0 in UART mode.
  78.     SCUART_Open(SC0, 115200);
  79.     // Enable smartcard receive interrupt
  80.     SCUART_ENABLE_INT(SC0, SC_IER_RDA_IE_Msk);
  81.     NVIC_EnableIRQ(SC0_IRQn);

  82.     // Write output buffer to smartcard tx FIFO
  83.     SCUART_Write(SC0, au8TxBuf, sizeof(au8TxBuf));



  84.     while(1);
  85. }

  86. /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/



史迪威将军 发表于 2015-12-7 20:54 | 显示全部楼层
落叶行健ywm 发表于 2015-12-15 10:36 | 显示全部楼层
目前有个项目就是用CAN的,在学习呢
21mengnan 发表于 2017-9-30 20:14 | 显示全部楼层
怎么实现双向同时发送接收啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

16

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部