[8/16-bit MCU] 寻求MC9S12G系列的CAN模块的源代码,对我的代码差错

[复制链接]
2586|8
 楼主| longlianqing 发表于 2015-10-20 19:59 | 显示全部楼层 |阅读模式
RT,求MC9S12G的初始化和发送接收的源代码!自己写了一个但是不能成功发送出数据!以下是我自己的源代码,包含初始化和发送模块,更希望有个大大帮我找出其中的!!!红色段是复制的别人的,不太懂那两个寄存器!
  1. /******************************************************************************
  2. Function Name:    CAN_Init()
  3. Engineer:                  created by Huanglongjie
  4. Date:                  9/24/2015
  5. Arguments:              none  
  6. Return:           none
  7. MCU:              MC9S12G48
  8. function:         CAN Init               
  9. caution:          baud rate = 125khz;
  10.                   CANE which is write once in normal,(now I only turn on rreceive interrupt)
  11.            
  12. ******************************************************************************/
  13. void MSCAN_Init(void)
  14. {
  15.     CANCTL1_CANE = 1;//enable MSCAN
  16.    
  17.     CANCTL0 = 0b00101100;//Enable internal MSCAN timer,The MSCAN is able to restart,
  18.                          //The module ceases to be clocked during wait mode                     
  19.                         
  20.     CANCTL0_INITRQ = 1;//gointo  initialization mode
  21.                         
  22.     while(CANCTL1_INITAK != 1)
  23.     {
  24.         ;//wait for going into CAN initialization mode
  25.     }
  26.    
  27.                         
  28.     CANCTL1 = 0b11000100;//clock source is the bus clock
  29.                          //Automatic bus-off recovery ,MSCAN wakes up only in case of
  30.                          //a dominant pulse on the CAN bus that has a length of Twup
  31.                         
  32.     CANBTR0 = 0b01001111;//Synchronization Jump Width max = 2 Tq clock cycles and  Tq = 1us
  33.    
  34.     CANBTR1 = 0b00100011;//Time Segment 2 = 3tq,Time Segment 1 = 4tq;=>baud rate = 125khz
  35.    
  36.     CANRIER = 0b00000001;//enable receive buffer full interrupt
  37.    
  38.     CANIDAC = 0b00010000;//Four 16-bit acceptance filters
  39.    
  40.    [color=Red] ////////////////////////////////////////////////////////////////////////
  41.     CANIDAR0 = 0x20;                    /* Filter 0, ID=0x100 Standard Identifier */
  42.     CANIDMR0 = 0x00;
  43.     CANIDAR1 = 0x00;
  44.     CANIDMR1 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

  45.     CANIDAR2 = 0x00;                    /* Filter 1, ID=0x0000 */
  46.     CANIDMR2 = 0x00;
  47.     CANIDAR3 = 0x00;
  48.     CANIDMR3 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

  49.     CANIDAR4 = 0x00;                        /* Filter 2, ID=0x0000 */
  50.     CANIDMR4 = 0x00;
  51.     CANIDAR5 = 0x00;
  52.     CANIDMR5 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */

  53.     CANIDAR6 = 0x00;                          /* Filter 3, ID=0x0000 */
  54.     CANIDMR6 = 0x00;
  55.     CANIDAR7 = 0x00;
  56.     CANIDMR7 = 0x07;                    /* AM[2:0] = 7 to receive standard identifiers */[/color]
  57.    
  58.     ////////////////////////////////////////////////////////////////////////
  59.     CANCTL0_INITRQ = 0;//MSCAN restarts and then tries to synchronize to the CAN bus,gointo normol mode
  60.    
  61.     while(CANCTL1_INITAK);//wait gointo normol mode
  62.     {
  63.         ;
  64.     }
  65.    
  66.     while(CANCTL0_SYNCH == 0)
  67.     {
  68.         ;//wait that MSCAN is synchronized to the CAN bus
  69.     }
  70.    
  71.    
  72.     CANR标志寄存器 = 0xc3;                                            /*reset some flags */
  73.    
  74.    
  75. }




  76. /******************************************************************************
  77. Function Name:    CAN_Send_Data_Frame()
  78. Engineer:                  created by Huanglongjie
  79. Date:                  9/24/2015
  80. Arguments:              none  
  81. Return:           none
  82. MCU:              MC9S12G48
  83. function:         CAN_Send_Data_Frame
  84. caution:         

  85. *******************************************************************************/
  86. UINT MSCAN_Send_Data_Frame(UINT id,UCHAR can_send_msg[])
  87. {
  88.     UCHAR id_low7bit = 0,id_high4bit = 0,buffer_select_flag = 0,send_success_flag = 0;
  89.     UINT  overflow_cnt = 0;
  90.    
  91.     /*do
  92.     {
  93.         buffer_empty_flag = CANT标志寄存器;   
  94.     }while(buffer_empty_flag == 0x00);//show all tx buffer is full,wait tx buffer is empty*/
  95.    
  96.    
  97.     if(CANT标志寄存器 != 0x00)//exist empty tx buffer?
  98.     {
  99.         CANTBSEL = CANT标志寄存器;//selecet corresponding buffer
  100.         
  101.         buffer_select_flag = CANTBSEL;//return value of selected tx buffer id
  102.         
  103.         id_low7bit = id&0x007f;
  104.         id_high4bit = (id&0x0780)/64;//calculate id
  105.         
  106.         CANTXDSR0 = can_send_msg[0];
  107.         CANTXDSR1 = can_send_msg[1];
  108.         CANTXDSR2 = can_send_msg[2];
  109.         CANTXDSR3 = can_send_msg[3];
  110.         CANTXDSR4 = can_send_msg[4];
  111.         CANTXDSR5 = can_send_msg[5];
  112.         CANTXDSR6 = can_send_msg[6];
  113.         CANTXDSR7 = can_send_msg[7];
  114.         
  115.    
  116.         CANTXIDR0 = 0x00;               
  117.         CANTXIDR1 = 0x00;
  118.         CANTXIDR2 = id_high4bit;
  119.         CANTXIDR3 = (id_low7bit*2)+0;//+0 show is data frame not is Remote frame
  120.         CANTXDLR = 0x08;//DLC = 8;
  121.    
  122.         CANTXIDR1_IDE = 0;//frame is Standard format
  123.         
  124.         CANTXTBPR = 0x08;
  125.         
  126.         CANT标志寄存器 = buffer_select_flag;//start transmission send data

  127.         
  128.         do
  129.         {
  130.             send_success_flag = buffer_select_flag & CANT标志寄存器;//not is 0 show successful
  131.             overflow_cnt ++;   
  132.         }while((!send_success_flag)&&overflow_cnt < 5000);//wait send data successlly
  133.         
  134.         if(overflow_cnt < 5000)
  135.         {
  136.             return CAN_SEND_SUCCESS_FLAG;
  137.         }
  138.         else
  139.         {
  140.             return CAN_SEND_FAIL_FLAG;
  141.         }
  142.         
  143.         
  144.     }
  145.     else
  146.     {
  147.         return CAN_SEND_FAIL_FLAG;
  148.     }
  149.   
  150. }
 楼主| longlianqing 发表于 2015-10-20 20:03 | 显示全部楼层
代码里面“CAN标志寄存器” = “CANT标志寄存器”,莫名的被替换了。
 楼主| longlianqing 发表于 2015-10-20 20:04 | 显示全部楼层
初始化应该没太大问题,请大大主要帮看下发送程序块!求成功发送的源代码!!!
攻城狮 发表于 2015-10-21 09:13 | 显示全部楼层
楼主可以参考一下分享的资料
https://bbs.21ic.com/icview-654618-1-1.html
史迪威将军 发表于 2015-10-21 20:56 | 显示全部楼层
longlianqing 发表于 2015-10-20 20:03
代码里面“CAN标志寄存器” = “CANT标志寄存器”,莫名的被替换了。

看看你的发送的时序和要求的时序一致吗?
Luis德华 发表于 2015-10-22 08:46 | 显示全部楼层
能确定初始化有没有问题?
 楼主| longlianqing 发表于 2015-10-24 20:45 | 显示全部楼层
问题已经解决,初始化没问题,是发送那块的问题!谢谢!
 楼主| longlianqing 发表于 2015-10-24 20:46 | 显示全部楼层
史迪威将军 发表于 2015-10-21 20:56
看看你的发送的时序和要求的时序一致吗?

一致的,发送那块问题!
Luis德华 发表于 2015-10-24 21:16 | 显示全部楼层
硬件有没有错误啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

33

帖子

3

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