Chinaunix首页 | 论坛 | 博客
  • 博客访问: 692190
  • 博文数量: 192
  • 博客积分: 1875
  • 博客等级: 上尉
  • 技术积分: 2177
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-23 23:21
个人简介

有时候,就是想窥视一下不知道的东东,因为好奇!

文章分类

全部博文(192)

文章存档

2024年(8)

2023年(3)

2020年(1)

2019年(1)

2018年(1)

2017年(2)

2016年(69)

2015年(53)

2014年(14)

2013年(1)

2012年(5)

2011年(25)

2010年(9)

分类: 其他平台

2014-09-28 21:12:04

STC89C51RC+sja1000的canbus通信方案代码。记录备忘。

1、初始化sja1000配置,代码如下:

点击(此处)折叠或打开

  1. /************************************************************************
  2. ;*函数原型: bit BCAN_SET_BANDRATE(unsigned char CAN_ByteRate) *
  3. ;*返回值: *
  4. ;* 0 ;波特率设置成功 *
  5. ;* 1 ;波特率设置失败 *
  6. ;* *
  7. ;*说明:设置CAN控制器SJA1000通讯波特率.SJA1000的晶振必须为16MHZ, *
  8. ;* 其它晶体的频率的值的波特率,需自己计算 。该子程序只能用于 *
  9. ;* 复位模式 *
  10. ;************************************************************************/
  11. bit BCAN_SET_BANDRATE(unsigned char CAN_ByteRate) //波特率选择
  12. {
  13.      unsigned char BR_Num= CAN_ByteRate,BTR0_num,BTR1_num;
  14.        switch (BR_Num)
  15.         {
  16.           case ByteRate_5k:
  17.                BTR0_num=0xBF;
  18.                BTR1_num=0xFF;
  19.                break;
  20.           case ByteRate_10k:
  21.                BTR0_num=0x31;
  22.                BTR1_num=0x1C;
  23.                break;
  24.           case ByteRate_20k:
  25.                BTR0_num=0x18;
  26.                BTR1_num=0x1C;
  27.                break;
  28.           case ByteRate_40k :
  29.                BTR0_num=0x87;
  30.                BTR1_num=0xff;
  31.                break;
  32.           case ByteRate_50k:
  33.                //BTR0_num=0x47;
  34.                //BTR1_num=0x2f;
  35.                BTR0_num=0x0e;
  36.                BTR1_num=0x1c;
  37.                break;
  38.           case ByteRate_80k :
  39.                //BTR0_num=0x83;
  40.                //BTR1_num=0xff;
  41.                BTR0_num=0x49; //ok
  42.                BTR1_num=0x1b;
  43.                break;
  44.           case ByteRate_100k :
  45.                //BTR0_num=0x43;
  46.                //BTR1_num=0x2f;
  47.                BTR0_num=0x04; //ok
  48.                BTR1_num=0x1C;
  49.                break;
  50.           case ByteRate_125k : //SJA1000的晶振为必须为16MHZ,波特率设置为125kpbs
  51.                BTR0_num=0x03;
  52.                BTR1_num=0x1c;
  53.                //BTR0_num=0x44; //ok
  54.                //BTR1_num=0x1f;
  55.                break;
  56.           case ByteRate_200k ://24MHZ
  57.             // BTR0_num=0xc5; //
  58.              // BTR1_num=0xa5;
  59.                BTR0_num=0x43; //ok
  60.                BTR1_num=0x1b;
  61.                break;
  62.           /* case ByteRate_200k ://24MHZ
  63.                BTR0_num=0x81;
  64.                BTR1_num=0xFA;
  65.                break;*/
  66.           case ByteRate_400k :
  67.                BTR0_num=0x80;
  68.                BTR1_num=0xfa;
  69.                break;
  70.           case ByteRate_500k :
  71.                BTR0_num=0x01;
  72.                BTR1_num=0x1c;
  73.                break;
  74.           case ByteRate_800k :
  75.                BTR0_num=0x00;
  76.                BTR1_num=0x16;
  77.                break;
  78.           case ByteRate_1000k :
  79.                BTR0_num=0x00;
  80.                BTR1_num=0x14;
  81.                break;
  82.           default :
  83.                return 1;
  84.                break;
  85.         }

  86.     SJA_BCANAdr=REG_BTR0;
  87.     *SJA_BCANAdr=BTR0_num;
  88.     if(*SJA_BCANAdr!=BTR0_num)
  89.       {return 1;}
  90.     SJA_BCANAdr=REG_BTR1;
  91.     *SJA_BCANAdr=BTR1_num;
  92.     if(*SJA_BCANAdr!=BTR1_num)
  93.       {return 1;}
  94.     return 0;
  95. }

  96. /************************************************************************
  97. *函数原型: bit BCAN_SET_OUTCLK( unsigned char Clock_Out) *
  98. *参数说明: *
  99. * Clock_Out:存放时钟分频寄存器(CDR)的参数设置 *
  100. *返回值: *
  101. * 0 ;设置成功 *
  102. * 1 ;设置失败 *
  103. *说明:设置SJA1000的时钟分频 。该子程序只能用于复位模式
  104. ************************************************************************/
  105. bit BCAN_SET_OUTCLK(unsigned char Clock_Out)
  106. {
  107.   SJA_BCANAdr=REG_CDR;

  108.   *SJA_BCANAdr=Clock_Out;
  109.   if(*SJA_BCANAdr != Clock_Out)
  110.      return 1;
  111.    else
  112.      return 0;
  113. }

  114. /************************************************************************
  115. *函数原型: bit BCAN_SET_OBJECT(unsigned char BCAN_ACR0,BCAN_ACR1,BCAN_ACR2,BCAN_ACR3
  116.                                               BCAN_AMR0, BCAN_AMR1, BCAN_AMR2, BCAN_AMR3
  117. *参数说明: *
  118. * BCAN_ACR(0-3):存放验收代码寄存器(ACR)的参数设置 *
  119. * BCAN_AMR(0-3):存放接收屏蔽寄存器(AMR)的参数设置 *
  120. *返回值: *
  121. * 0 ;设置成功 *
  122. * 1 ;设置失败 *
  123. *说明:设置CAN节点的通讯对象,允许接收的报文,是由AMR和ACR共同决定的. *
  124. *************************************************************************/
  125. bit BCAN_SET_OBJECT(unsigned char BCAN_ACR0,BCAN_ACR1,BCAN_ACR2,BCAN_ACR3,BCAN_AMR0,BCAN_AMR1,BCAN_AMR2,BCAN_AMR3)
  126. {
  127.   SJA_BCANAdr=REG_TXBuffer1;

  128.   *SJA_BCANAdr=BCAN_ACR0; //写入参数
  129.    
  130.   SJA_BCANAdr=REG_TXBuffer2;
  131.   *SJA_BCANAdr=BCAN_ACR1;

  132.   SJA_BCANAdr=REG_TXBuffer3;
  133.   *SJA_BCANAdr=BCAN_ACR2;
  134.    
  135.   SJA_BCANAdr=REG_TXBuffer4;
  136.   *SJA_BCANAdr=BCAN_ACR3;
  137.  
  138.   //校验写入值
  139.   if(*SJA_BCANAdr != BCAN_ACR3) return 1;
  140.   
  141.   SJA_BCANAdr=REG_TXBuffer5;

  142.   *SJA_BCANAdr=BCAN_AMR0; //写入参数
  143.   
  144.   SJA_BCANAdr=REG_TXBuffer6;
  145.   *SJA_BCANAdr=BCAN_AMR1;

  146.   SJA_BCANAdr=REG_TXBuffer7;
  147.   *SJA_BCANAdr=BCAN_AMR2;

  148.   SJA_BCANAdr=REG_TXBuffer8;
  149.   *SJA_BCANAdr=BCAN_AMR3;
  150.   //校验写入值
  151.   if(*SJA_BCANAdr != BCAN_AMR3) return 1;

  152.   return 0;
  153. }

  154. /************************************************************************
  155.  *函数原型: bit BCAN_CREATE_COMMUNATION(void) *
  156.  *参数说明:*
  157.  *返回值: *
  158.  * 0 ; 表示SJA1000接口正常 *
  159.  * 1 ; 表示SJA1000与处理器接口不正常 *
  160.  *说明:该函数用于检测CAN控制器的接口是否正常 *
  161.  ************************************************************************/
  162.  bit BCAN_CREATE_COMMUNATION(void)
  163.  {
  164.     SJA_BCANAdr=REG_TEST;
  165.     *SJA_BCANAdr=0xaa; //写入测试值
  166.     if(*SJA_BCANAdr == 0xaa)
  167.        return 0; //读测试正确
  168.     else
  169.        return 1;
  170.      
  171.  }

  172.  /************************************************************************
  173.  *函数原型: bit BCAN_ENTER_RETMODEL(void) *
  174.  *参数说明:*
  175.  *返回值: *
  176.  * 0 ; 表示成功进入复位工作模式 *
  177.  * 1 ; 表示不能进入复位工作模式 *
  178.  * *
  179.  *说明: CAN控制器进入复位工作模式 *
  180.  ************************************************************************/
  181.  bit BCAN_ENTER_RETMODEL(void) //置位复位请求
  182.  {
  183.     unsigned char TempData;
  184.     SJA_BCANAdr = REG_CONTROL;

  185.   TempData= *SJA_BCANAdr;
  186.     *SJA_BCANAdr=0x09; //置位复位请求 和单滤波模式
  187.     if((*SJA_BCANAdr&0x01) == 1)
  188.      return 0;
  189.     else
  190.       return 1;
  191.  }

  192. /****************************************************
  193. **函数原型: bit Sja_1000_Init(void)
  194. **功 能: 初始化SJA10000
  195. **入口参数:
  196. **返 回 值:
  197.       0: 初始化成功
  198.       1: 复位失败
  199.       2: 测试sja1000失败
  200.       3: 设置失败
  201.       4: 设置验收滤波器失败
  202.       5: 设置波特率失败
  203. *****************************************************/

  204. unsigned char Sja_1000_Init(unsigned char intMode)
  205. {
  206.  bit s;
  207.  //EA=0; //关总中断
  208.  //WDT_CONTR=0x3d; //喂狗 1.13s;
  209.    s=BCAN_ENTER_RETMODEL();
  210.  if (s==1) return 1;
  211.  if(intMode==1)
  212.  {
  213.     s=BCAN_CREATE_COMMUNATION(); //建立通信
  214.     if (s==1) return 2;
  215.  }
  216.  s=BCAN_SET_OUTCLK(0x88); //Pelican,关闭clkout
  217.  if (s==1) return 3;

  218.  s=BCAN_SET_OBJECT(key_temp,0x34,0x16,0x00,0x00,0xff,0xff,0xf0);//屏蔽寄存器,都设为无关,接收所有报文
  219.                                                             //当屏蔽位为1,不滤波,0就滤波必须相等
  220.  //s=BCAN_SET_OBJECT(0x55,0xe0,0xaa,0xa1,0x00,0x00,0xff,0xff);//验收码&屏蔽码

  221.  if (s==1) return 4;

  222.  s=BCAN_SET_BANDRATE(ByteRate_100k); //设置波特率125K 16MHZ
  223.     if (s==1) return 5;
  224.     
  225.     SJA_BCANAdr=REG_OCR ; //输出控制寄存器
  226.     *SJA_BCANAdr=0x1a; // 0001 1010 正常输出模式,TX1悬空,TX0上拉,正常输出模式
  227.     
  228.     SJA_BCANAdr=REG_INTENABLE;
  229.    *SJA_BCANAdr=0x1D; //设置中断,接收和发送中断
  230.     
  231.     // s=BCAN_SET_CONTROL(0x08);
  232.     SJA_BCANAdr=REG_CONTROL; //退出 复位模式
  233.     *SJA_BCANAdr=*SJA_BCANAdr&0xfe;
  234.      if(*SJA_BCANAdr!=0x00)
  235.      return 6;

  236.  //if (s==1) return 6;

  237.     //EA=1;
  238.  return 0;
  239. }

2、发送数据,代码如下:

点击(此处)折叠或打开

  1. /************************************************************************
  2.  *函数原型: BCAN_CMD_PRG(unsigned char cmd) *
  3.  *参数说明: unsigned char cmd *
  4.  *返回值: *
  5.  * 0 ; 请求成功 *
  6.  * 1 ; 请求失败 *
  7.  * *
  8.  *说明: 启动命令字 *
  9.  ************************************************************************/
  10. bit BCAN_CMD_PRG(unsigned char cmd)
  11.  {
  12.    SJA_BCANAdr=REG_COMMAND; //访问地址指向命令寄存器
  13.    *SJA_BCANAdr=cmd; //启动命令字
  14.  
  15.    switch(cmd)
  16.    { case TR_CMD:

  17.            return 0;
  18.            break;

  19.   case SRR_CMD:

  20.       return 0;
  21.      break;
  22.     
  23.   case AT_CMD:

  24.             SJA_BCANAdr = REG_STATUS; //访问地址指向状态寄存器
  25.            if((*SJA_BCANAdr & 0x20)==0) //判断是否正在发送
  26.              return 0;
  27.            else
  28.              return 1;
  29.            break;
  30.      case RRB_CMD:
  31.            SJA_BCANAdr = REG_STATUS; //访问地址指向状态寄存器
  32.            if((*SJA_BCANAdr & 0x01)==1)
  33.               return 1;
  34.            else
  35.               return 0;
  36.            break;
  37.      case COS_CMD:

  38.            SJA_BCANAdr = REG_STATUS;
  39.            if((*SJA_BCANAdr & 0x02)==0)//判断清除超载是否成功
  40.              return 0;
  41.            else
  42.              return 1;
  43.            break;
  44.      default:
  45.              return 1;
  46.              break;
  47.    }
  48. }
  49. void CAN_Send_Small(unsigned char *CAN_TX_Buf,unsigned char length1,unsigned char packLength,unsigned char packNum) //小于或者等于8字节数据发送
  50. {
  51.     unsigned char temptt;
  52.     CntCanTxTimeout=0;
  53.     flag_CanTxTimeout=0;
  54.     do
  55.     {
  56.         if(flag_CanTxTimeout)return;
  57.         SJA_BCANAdr = REG_STATUS;
  58.     temptt=*SJA_BCANAdr;
  59.   }while((temptt&TBS_Bit)!=TBS_Bit); //循环检测等待
  60.     //可以向发送缓冲器写数据
  61.     SJA_BCANAdr = REG_TXBuffer1; //访问地址指向发送缓冲区1,修改成头文件
  62.     *SJA_BCANAdr=length1|0x80; //扩展帧
  63.     
  64.     SJA_BCANAdr = REG_TXBuffer2; //ID28-21
  65.     *SJA_BCANAdr=packAcr0;
  66.     SJA_BCANAdr = REG_TXBuffer3; //ID20-13
  67.     *SJA_BCANAdr=packAcr1;
  68.     SJA_BCANAdr = REG_TXBuffer4; //ID12-5
  69.     *SJA_BCANAdr=((packLength<<4)&0xf0)+(packNum&0x0f); //包序列号
  70.     SJA_BCANAdr = REG_TXBuffer5; //ID12-5
  71.     *SJA_BCANAdr=0xf0;
  72.     
  73.     SJA_BCANAdr = REG_TXBuffer6;
  74.     //*SJA_BCANAdr=PackNum;
  75.     //SJA_BCANAdr = REG_TXBuffer7;
  76.     memcpy(SJA_BCANAdr,CAN_TX_Buf,length1);
  77.     //数据发送请求
  78.     BCAN_CMD_PRG(TR_CMD); //请求发送
  79. }

  80. void CAN_Send_Large(unsigned char *CAN_TX_Buf,unsigned char length1) //大于8字节发送
  81. {
  82.     unsigned char bufi,i,j;
  83.     unsigned char bufj[8];
  84.     for(i=0;i<8;i++)
  85.     {
  86.         bufj[i]=0;
  87.     }
  88.     i=length1/8;
  89.     j=length1%8;
  90.     if(j==0)
  91.     {
  92.         i--;
  93.         for(bufi=0;bufi<=i;bufi++)
  94.      {
  95.          CAN_Send_Small(&CAN_TX_Buf[8*bufi],8,i,bufi);
  96.      }
  97.     }
  98.     else
  99.     {
  100.         for(bufi=0;bufi<i;bufi++)CAN_Send_Small(&CAN_TX_Buf[8*bufi],8,i,bufi);
  101.         CAN_Send_Small(&CAN_TX_Buf[8*i],j,i,i);
  102.     }
  103.     //CAN_Send_Small(bufj,1,0xff);
  104. }

3、中断部分接收数据,代码如下:

点击(此处)折叠或打开

  1. //--------------------------------------------------
  2. //函数原型: void ex0_int(void) interrupt 0 //using 1
  3. //功 能: 中断接收函数
  4. //入口参数:
  5. //出口参数: RevceData[]数组
  6. //说 明: 当sja1000 收到正确的报文时,会产生int中断
  7. //--------------------------------------------------
  8. void ex0_int(void) interrupt 0 using 1
  9. {
  10.     unsigned char tt,tt1,length,i;
  11.     i=0;
  12.     msg++;
  13.     CntMainTimeout=0;
  14.     flag_CanMainTimeout=0;
  15.     if(msg==5)
  16.     {
  17.         msg=0;
  18.     //P34=0;
  19.   }
  20.   LED2=0;
  21.   SJA_BCANAdr=REG_INTERRUPT;
  22.   if((*SJA_BCANAdr)&0x01)//产生了接收中断,并且CAN接收完成已经清零
  23.   {
  24.       SJA_BCANAdr=REG_RXBuffer1;
  25.     tt=*SJA_BCANAdr;
  26.     tt1=*SJA_BCANAdr;
  27.     length=tt1&0x0F;
  28.     if ((tt&0x40)!=0x40) //数据帧 假= 为远程帧
  29.     {
  30.         SJA_BCANAdr =REG_RXBuffer1; //宏定义的变量不能memcpy(CanRevceData,REG_RXBuffer4,8);
  31.         memcpy(RxData_Buf,SJA_BCANAdr,length+5);//功能:由src所指内存区域复制count个字节到dest所指内存区域
  32.         
  33.         tt=(RxData_Buf[3]>>4)&0x0f;
  34.         tt1=RxData_Buf[3]&0x0f;
  35.         memcpy(&CanRevceData[tt1*8],&RxData_Buf[5],length);
  36.         if(0x0 != bytemap_test_set((unsigned int)tt1))
  37.         {
  38.             ;/** warning: the frame received, but now received again **/
  39.         }
  40.         //if(tt==tt1) //判断数据包结束
  41.         if(bytemap_isfull(tt + 1)) /** 判断数据包结束. tt从0开始的序号, 加1后就是的1的个数 **/
  42.         {
  43.             flag_CanRxFinish=1;
  44.             CntCanRxLength=length+tt*8;
  45.             bytemap_clean_interrupt();
  46.         }
  47.         
  48.         CntCanRxTimeout = 0; /** CAN数据接收超时计数清0 **/
  49.         //send_string_com(CanRevceData,CntCanRxLength);
  50.     }
  51.     else //远程帧
  52.     {
  53.         flag_canRxRoll=1;
  54.     }
  55.     BCAN_CMD_PRG(RRB_CMD); //释放SJA1000接收缓冲区,****已经修改
  56.   }
  57.   LED2=1;
  58. }

使用相同的82c250接口,前三篇中的mcp2510与stm32f103RBT6可以通过canbus正常通信。此sja1000代码设定的125kbps速率canbus与前三篇中的canbus不能正确通信,但相同的sja1000之间通信正常,猜测其速率与注释中的并不一致。或者设定速率、及时序BTL、延时等参数并不匹配。考虑电路及及其它原因,放弃此STC89C51RC+sja1000的方案。

相关常数定义如下:

点击(此处)折叠或打开

  1. //定义SJA1000的基址
  2. #define SJA_BaseAdr 0X7F00

  3. //定义本机的ID
  4. #define ID 3
  5. // 控制寄存器
  6. //系统指针,指向SJA1000
  7. #define REG_CONTROL SJA_BaseAdr+0x00 //内部控制寄存器
  8. #define REG_COMMAND SJA_BaseAdr+0x01 //命令寄存器

  9. #define REG_STATUS SJA_BaseAdr+0x02 //状态寄存器
  10. #define RBS_Bit 0x01 /*接收缓冲器状态位 */
  11. #define DOS_Bit 0x02 /*数据超载状态位 */
  12. #define TBS_Bit 0x04 /*发送缓冲器状态位 */
  13. #define TCS_Bit 0x08 /*发送完成状态位 */
  14. #define RS_Bit 0x10 /*接收状态位 */
  15. #define TS_Bit 0x20 /*发送状态位 */
  16. #define ES_Bit 0x40 /*错误状态位 */
  17. #define BS_Bit 0x80 /*总线状态位 */
  18. #define TxFramInFo SJA_BaseAdr+16
  19. #define REG_INTERRUPT SJA_BaseAdr+0x03 //中断寄存器
  20. #define REG_INTENABLE SJA_BaseAdr+0x04 //中断使能寄存器
  21. #define REG_BTR0 SJA_BaseAdr+0x06 //总线定时寄存器0
  22. #define REG_BTR1 SJA_BaseAdr+0x07 //总线定时寄存器1
  23. #define REG_OCR SJA_BaseAdr+0x08 //输出控制寄存器
  24. #define REG_TEST SJA_BaseAdr+0x09 //测试寄存器

  25. #define REG_RESVER1 SJA_BaseAdr+0x0A //保留1
  26. #define REG_ARBITRATE SJA_BaseAdr+0x0B //仲裁丢失捕捉
  27. #define REG_ERRCATCH SJA_BaseAdr+0x0C //错误代码捕捉
  28. #define REG_ERRLIMIT SJA_BaseAdr+0x0D //错误报警限额

  29. #define REG_RXERR SJA_BaseAdr+0x0E //接收错误计数器
  30. #define REG_TXERR SJA_BaseAdr+0x0F //发送错误计数器

  31. #define REG_ACR1 SJA_BaseAdr+0x10 //验收代码寄存器
  32. #define REG_ACR2 SJA_BaseAdr+0x11 //验收代码寄存器
  33. #define REG_ACR3 SJA_BaseAdr+0x12 //验收代码寄存器
  34. #define REG_ACR4 SJA_BaseAdr+0x13 //验收代码寄存器
  35. #define REG_AMR1 SJA_BaseAdr+0x14 //验收屏蔽寄存器
  36. #define REG_AMR2 SJA_BaseAdr+0x15 //验收屏蔽寄存器
  37. #define REG_AMR3 SJA_BaseAdr+0x16 //验收屏蔽寄存器
  38. #define REG_AMR4 SJA_BaseAdr+0x17 //验收屏蔽寄存器

  39. // 发送缓冲区寄存器
  40. #define REG_TXBuffer1 SJA_BaseAdr+0x10 //发送缓冲区1
  41. #define REG_TXBuffer2 SJA_BaseAdr+0x11 //发送缓冲区2
  42. #define REG_TXBuffer3 SJA_BaseAdr+0x12 //发送缓冲区3
  43. #define REG_TXBuffer4 SJA_BaseAdr+0x13 //发送缓冲区4
  44. #define REG_TXBuffer5 SJA_BaseAdr+0x14 //发送缓冲区5
  45. #define REG_TXBuffer6 SJA_BaseAdr+0x15 //发送缓冲区6
  46. #define REG_TXBuffer7 SJA_BaseAdr+0x16 //发送缓冲区7
  47. #define REG_TXBuffer8 SJA_BaseAdr+0x17 //发送缓冲区8
  48. #define REG_TXBuffer9 SJA_BaseAdr+0x18 //发送缓冲区9
  49. #define REG_TXBuffer10 SJA_BaseAdr+0x19 //发送缓冲区10
  50. #define REG_TXBuffer11 SJA_BaseAdr+0x1A //发送缓冲区11
  51. #define REG_TXBuffer12 SJA_BaseAdr+0x1B //发送缓冲区12
  52. #define REG_TXBuffer13 SJA_BaseAdr+0x1C //发送缓冲区13

  53. // 接收缓冲区寄存器
  54. #define REG_RXBuffer1 SJA_BaseAdr+0x10 //接收缓冲区1
  55. #define REG_RXBuffer2 SJA_BaseAdr+0x11 //接收缓冲区2
  56. #define REG_RXBuffer3 SJA_BaseAdr+0x12 //接收缓冲区3
  57. #define REG_RXBuffer4 SJA_BaseAdr+0x13 //接收缓冲区4
  58. #define REG_RXBuffer5 SJA_BaseAdr+0x14 //接收缓冲区5
  59. #define REG_RXBuffer6 SJA_BaseAdr+0x15 //接收缓冲区6
  60. #define REG_RXBuffer7 SJA_BaseAdr+0x16 //接收缓冲区7
  61. #define REG_RXBuffer8 SJA_BaseAdr+0x17 //接收缓冲区8
  62. #define REG_RXBuffer9 SJA_BaseAdr+0x18 //接收缓冲区9
  63. #define REG_RXBuffer10 SJA_BaseAdr+0x19 //接收缓冲区10
  64. #define REG_RXBuffer11 SJA_BaseAdr+0x1A //接收缓冲区11
  65. #define REG_RXBuffer12 SJA_BaseAdr+0x1B //接收缓冲区12
  66. #define REG_RXBuffer13 SJA_BaseAdr+0x1C //接收缓冲区13

  67. #define REG_RXCOUNT SJA_BaseAdr+0x1D //RX报文计数器
  68. #define REG_RBSA SJA_BaseAdr+0x1E //接收缓冲区起始地址
  69. #define REG_CDR SJA_BaseAdr+0x1F //时钟分频寄存器

  70. /*
  71. 功能说明: CAN控制器SJA1000通讯波特率.SJA1000的晶振为必须为16MHZ*/

  72. #define BTR0_Rate_5k 0xBF //5KBPS的预设值
  73. #define BTR1_Rate_5k 0xFF //5KBPS的预设值

  74. #define BTR0_Rate_10k 0x31 //10KBPS的预设值
  75. #define BTR1_Rate_10k 0x1C //10KBPS的预设值

  76. #define BTR0_Rate_20k 0x18 //20KBPS的预设值
  77. #define BTR1_Rate_20k 0x1C //20KBPS的预设值

  78. #define BTR0_Rate_40k 0x87 //40KBPS的预设值
  79. #define BTR1_Rate_40k 0xFF //40KBPS的预设值

  80. #define BTR0_Rate_50k 0x47 //50KBPS的预设值
  81. #define BTR1_Rate_50k 0x2F //50KBPS的预设值

  82. #define BTR0_Rate_80k 0x83 //80KBPS的预设值
  83. #define BTR1_Rate_80k 0xFF //80KBPS的预设值

  84. #define BTR0_Rate_100k 0x43 //100KBPS的预设值
  85. #define BTR1_Rate_100k 0x2f //100KBPS的预设值

  86. #define BTR0_Rate_125k 0x03 //125KBPS的预设值
  87. #define BTR1_Rate_125k 0x1c //125KBPS的预设值

  88. #define BTR0_Rate_200k 0x81 //200KBPS的预设值
  89. #define BTR1_Rate_200k 0xFA //200KBPS的预设值

  90. #define BTR0_Rate_250k 0x01 //250KBPS的预设值
  91. #define BTR1_Rate_250k 0x1c //250KBPS的预设值

  92. #define BTR0_Rate_400k 0x80 //400KBPS的预设值
  93. #define BTR1_Rate_400k 0xfa //400KBPS的预设值

  94. #define BTR0_Rate_500k 0x00 //500KBPS的预设值
  95. #define BTR1_Rate_500k 0x1c //500KBPS的预设值

  96. #define BTR0_Rate_666k 0x80 //666KBPS的预设值
  97. #define BTR1_Rate_666k 0xb6 //666KBPS的预设值

  98. #define BTR0_Rate_800k 0x00 //800KBPS的预设值
  99. #define BTR1_Rate_800k 0x16 //800KBPS的预设值

  100. #define BTR0_Rate_1000k 0x00 //1000KBPS的预设值
  101. #define BTR1_Rate_1000k 0x14 //1000KBPS的预设值
  102. //BPS
  103. #define ByteRate_5k 5
  104. #define ByteRate_10k 10
  105. #define ByteRate_20k 20
  106. #define ByteRate_40k 40
  107. #define ByteRate_50k 50
  108. #define ByteRate_80k 80
  109. #define ByteRate_100k 100
  110. #define ByteRate_125k 125
  111. #define ByteRate_200k 200
  112. #define ByteRate_400k 400
  113. #define ByteRate_500k 500
  114. #define ByteRate_800k 800
  115. #define ByteRate_1000k 1000

  116. //命令字
  117. #define TR_CMD 0X01
  118. #define AT_CMD 0X02
  119. #define RRB_CMD 0X04
  120. #define COS_CMD 0X08
  121. #define SRR_CMD 0X10
  122. #define GTS_CMD 0X10
  123. //错误字

  124. #define CAN_INTERFACE_OK 0
  125. #define CAN_BUS_OK 0
  126. #define CAN_INTERFACE_ERR 0XFF
  127. #define CAN_ENTERSET_ERR 0XFE
  128. #define CAN_QUITSET_ERR 0XFD
  129. #define CAN_INITOBJECT_ERR 0XFC
  130. #define CAN_INITBTR_ERR 0XFB
  131. #define CAN_INITOUTCTL_ERR 0XFA
  132. #define CAN_INTCLKDIV_ERR 0XF9
  133. #define CAN_BUS_ERR 0XF8



阅读(2624) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~