<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于SJA1000的CAN總線測(cè)試程序

          基于SJA1000的CAN總線測(cè)試程序

          作者: 時(shí)間:2016-11-10 來(lái)源:網(wǎng)絡(luò) 收藏

          代碼如下:

          本文引用地址:http://www.ex-cimer.com/article/201611/317523.htm

          /*************定義地址指針,指向基址***********/
          unsigned char xdata *SJA_base_Adr = SJA_BaseAdr;

          /*************定義SJA1000讀寫(xiě)緩沖區(qū)的數(shù)據(jù)結(jié)構(gòu)***********/
          struct BASICCAN_BUFStruct{
          unsigned char IDH8;
          unsigned char IDL_R_DLC;
          unsigned char Frame_Data[2];
          }receive_BUF,send_BUF;

          /****************************************************************
          *函數(shù)功能:寫(xiě)SJA1000的寄存器
          *入口參數(shù):地址 SJAREG_ADR,數(shù)據(jù) setting
          *出口參數(shù):無(wú)
          ****************************************************************/
          void Write_SJA1000(unsigned char SJAREG_ADR, unsigned char setting)
          {
          *(SJA_base_Adr+SJAREG_ADR)=setting;
          }

          /****************************************************************
          *函數(shù)功能:讀SJA1000的寄存器
          *入口參數(shù):地址 SJAREG_ADR,數(shù)據(jù) setting
          *出口參數(shù):無(wú)
          ****************************************************************/
          unsigned char Read_SJA1000(unsigned char SJAREG_ADR)
          {
          unsigned char SJAREG_data;

          SJAREG_data=*(SJA_base_Adr+SJAREG_ADR);

          return(SJAREG_data);
          }

          /****************************************************************
          *函數(shù)功能:測(cè)試SJA1000是否正常連接
          *入口參數(shù):無(wú)
          *出口參數(shù):connect_OK
          ****************************************************************/
          bit Judge_SJA1000_connect(void)
          {
          bit connect_OK=0;

          Write_SJA1000(REG_TEST,0xAA); //寫(xiě)AA到測(cè)試寄存器(地址09)

          if(Read_SJA1000(0x09)==0xAA)
          {
          connect_OK=1; //連接正常
          }
          else
          {
          connect_OK=0; //連接故障
          }
          return(connect_OK);
          }

          /****************************************************************
          *函數(shù)功能:設(shè)置SJA1000為工作模式
          *入口參數(shù):無(wú)
          *出口參數(shù):SJA_mode
          ****************************************************************/
          bit Setting_SJA1000_workingmode(void)
          {
          bit SJA_mode;
          unsigned char CONTROL_REGdata;

          CONTROL_REGdata=Read_SJA1000(REG_CONTROL);
          CONTROL_REGdata&=0xFE;

          Write_SJA1000(REG_CONTROL,CONTROL_REGdata);

          if((Read_SJA1000(REG_CONTROL)&0x01)!=0x01)
          {
          SJA_mode=1; //置工作模式成功
          }
          else
          {
          SJA_mode=0; //置工作模式失敗
          }

          return(SJA_mode);
          }

          /****************************************************************
          *函數(shù)功能:設(shè)置SJA1000為復(fù)位模式
          *入口參數(shù):無(wú)
          *出口參數(shù):SJA_mode
          ****************************************************************/
          bit Setting_SJA1000_resetmode(void)
          {
          bit SJA_mode;
          unsigned char CONTROL_REGdata;

          CONTROL_REGdata=Read_SJA1000(REG_CONTROL);
          CONTROL_REGdata|=0x01;

          Write_SJA1000(REG_CONTROL,CONTROL_REGdata);

          if((Read_SJA1000(REG_CONTROL)&0x01)==0x01)
          {
          SJA_mode=1; //置復(fù)位模式成功
          }
          else
          {
          SJA_mode=0; //置復(fù)位模式失敗
          }

          return(SJA_mode);
          }

          /****************************************************************
          *函數(shù)功能:設(shè)置SJA1000波特率
          *入口參數(shù):無(wú)
          *出口參數(shù):setting_success
          ****************************************************************/
          bit Setting_SJA1000_rate(void)
          {
          bit setting_success;

          while(Setting_SJA1000_resetmode()==0)
          {
          Setting_SJA1000_resetmode(); //設(shè)置SJA工作在復(fù)位模式
          }

          Write_SJA1000(REG_BTR0,SJA_BTR0);
          Write_SJA1000(REG_BTR1,SJA_BTR1);

          if((Read_SJA1000(REG_BTR0)==SJA_BTR0)&(Read_SJA1000(REG_BTR1)==SJA_BTR1))
          {
          setting_success=1; //波特率設(shè)置成功
          }
          else
          {
          setting_success=0; //波特率設(shè)置失敗
          }

          return(setting_success);
          }

          /****************************************************************
          *函數(shù)功能:設(shè)置SJA1000的濾波寄存器和屏蔽寄存器
          *入口參數(shù):無(wú)
          *出口參數(shù):setting_success
          ****************************************************************/
          bit Setting_SJA1000_dataselect(void)
          {
          bit setting_success;

          while(Setting_SJA1000_resetmode()==0)
          {
          Setting_SJA1000_resetmode(); //設(shè)置SJA工作在復(fù)位模式
          }

          Write_SJA1000(REG_ACR,SJA_ACR);
          Write_SJA1000(REG_AMR,SJA_AMR);

          if((Read_SJA1000(REG_ACR)==SJA_ACR)&(Read_SJA1000(REG_AMR)==SJA_AMR))
          {
          setting_success=1; //濾波器設(shè)置成功
          }
          else
          {
          setting_success=0; //濾波器設(shè)置失敗
          }

          return(setting_success);
          }

          /****************************************************************
          *函數(shù)功能:設(shè)置SJA1000的時(shí)鐘分頻器
          *入口參數(shù):無(wú)
          *出口參數(shù):setting_success
          ****************************************************************/
          bit Setting_SJA1000_CDR(void)
          {
          bit setting_success;

          while(Setting_SJA1000_resetmode()==0)
          {
          Setting_SJA1000_resetmode(); //設(shè)置SJA工作在復(fù)位模式
          }

          Write_SJA1000(REG_CDR,SJA_CDR);

          if(Read_SJA1000(REG_CDR)==SJA_CDR)
          {
          setting_success=1; //濾波器設(shè)置成功
          }
          else
          {
          setting_success=0; //濾波器設(shè)置失敗
          }

          return(setting_success);
          }

          /****************************************************************
          *函數(shù)功能:設(shè)置SJA1000的輸出控制寄存器
          *入口參數(shù):無(wú)
          *出口參數(shù):setting_success
          ****************************************************************/
          bit Setting_SJA1000_OCR(void)
          {
          bit setting_success;

          while(Setting_SJA1000_resetmode()==0)
          {
          Setting_SJA1000_resetmode(); //設(shè)置SJA工作在復(fù)位模式
          }

          Write_SJA1000(REG_OCR,SJA_OCR);

          if(Read_SJA1000(REG_OCR)==SJA_OCR)
          {
          setting_success=1; //濾波器設(shè)置成功
          }
          else
          {
          setting_success=0; //濾波器設(shè)置失敗
          }

          return(setting_success);
          }

          /****************************************************************
          *函數(shù)功能:初始化SJA1000
          *入口參數(shù):無(wú)
          *出口參數(shù):setting_success
          ****************************************************************/
          void Initial_SJA1000(void)
          {
          while(Judge_SJA1000_connect==0);

          do
          {
          Setting_SJA1000_resetmode();
          }
          while(Setting_SJA1000_resetmode()==0);

          while(Setting_SJA1000_CDR()==0);
          while(Setting_SJA1000_rate()==0);
          while(Setting_SJA1000_OCR()==0);
          while(Setting_SJA1000_dataselect()==0)

          do
          {
          Setting_SJA1000_workingmode();
          }
          while(Setting_SJA1000_workingmode()==0);

          }

          /****************************************************************
          *函數(shù)功能:初始化發(fā)送數(shù)據(jù)
          *入口參數(shù):無(wú)
          *出口參數(shù):setting_success,指示初始化是否成功
          ****************************************************************/
          bit Initial_transmitter_BUF(void)
          {
          bit setting_success=0;

          send_BUF.IDH8=0x30;
          send_BUF.IDL_R_DLC=0x22; //IDL_R_DLC。4即為RTR位,數(shù)據(jù)=0,遠(yuǎn)程=1;
          send_BUF.Frame_Data[0]=0x10;
          send_BUF.Frame_Data[1]=0x00;

          setting_success = 1;

          return(setting_success);
          }

          /****************************************************************
          *函數(shù)功能:對(duì)SJA1000發(fā)送數(shù)據(jù)
          *入口參數(shù):無(wú)
          *出口參數(shù):無(wú)
          ****************************************************************/
          void Transmitter_SJA1000(void)
          {
          unsigned char state;
          unsigned char i;

          do
          {
          Setting_SJA1000_workingmode();
          }
          while(Setting_SJA1000_workingmode()==0);

          while(Initial_transmitter_BUF()==0); //初始化發(fā)送數(shù)據(jù)

          do
          {
          state = Read_SJA1000(REG_STATUS);//REG_STATUS為SJA1000的狀態(tài)寄存器
          }
          //查詢SJA1000是否處于接收狀態(tài),發(fā)送完畢狀態(tài),或者發(fā)送緩存器被鎖;
          while( ((state&0x10)==0x10) & ((state&0x08)!=0x08) & ((state&0x04)!=0x04) );

          Write_SJA1000(REG_TxBuffer1,send_BUF.IDH8);
          Write_SJA1000(REG_TxBuffer2,send_BUF.IDL_R_DLC);
          for(i=0;i<2;i++)
          {
          Write_SJA1000(REG_TxBuffer3+i,send_BUF.Frame_Data[i]);
          }

          Write_SJA1000(REG_COMMAND,TR_CMD);

          do
          {
          state = Read_SJA1000(REG_STATUS);//REG_STATUS為SJA1000的狀態(tài)寄存器
          }
          while( (state&0x08)!=0x08);//查詢SJA1000是否發(fā)送完畢狀態(tài)
          }

          /***************************************************
          ***************************************************/

          void main(void)
          {
          unsigned char i;
          Initial_SJA1000();
          delay1ms(1);

          while(1)
          {
          if((P1_0==0)|(P1_1==0)|(P1_2==0))
          {
          delay1ms(5);

          if((P1_0==0)|(P1_1==0)|(P1_2==0))
          {
          while((P1_0==0)|(P1_1==0)|(P1_2==0));
          for(i=0;i<100;i++)
          {
          Transmitter_SJA1000();
          }
          }
          }
          }
          }



          關(guān)鍵詞: SJA1000CAN總線測(cè)試程

          評(píng)論


          技術(shù)專(zhuān)區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();