亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? sja_bcanfunc.c

?? 8051單片機(jī)訪問外圍器件的代碼
?? C
字號:
/**
 * @file
 * Driver for the CAN chip SJA1000 in Basic CAN mode
 * 
 * SJA_BCANFUNC.c
 * Copyright(C) 2007 Agate Logic Corporation. All rights reserved.
 * This file is licensed under the terms of Agate Logic SDK License Agreement.
 * 
 * @author bxu@agatelogic.com.cn
*/

#define CAN_GLOBALS	///< define macro "CAN_GLOBALS" as empty

#include <ag1f1.h>
#include "SJA_BCANCONF.H"
#include <intrins.h>
#define ALLDELAY _nop_();_nop_();_nop_();_nop_();

/**
 * @brief Write data to the specified register.
 * 
 * The Function write the specified data to the specified register of CAN chip.
 * 
 * @param Adr The address of the register to write to.
 * @param Value The data to write.
 * 
 * @return N/A
*/
void    WriteSjaReg(uchar Adr, uchar Value)
{
	CAN_CS=1;
	ALLDELAY
	CAN_RD=1;
	ALLDELAY
	CAN_WR=1;
	ALLDELAY
	CAN_DIR=0;//output direction
	ALLDELAY
	CAN_ALE=1;
	ALLDELAY
	CAN_ADDA=Adr;//addr output
	ALLDELAY
	CAN_ALE=0;//latch addr
	ALLDELAY
	CAN_CS=0;//chip select
	ALLDELAY
	CAN_ADDA=Value;//value output
	ALLDELAY
	CAN_WR=0;//WRITE start
	ALLDELAY
	CAN_WR=1;//WRITE end
	ALLDELAY
	CAN_CS=1;//chip unselect
	ALLDELAY
}

/**
 * @brief Read the specified register.
 * 
 * The Function read the value of the specified register of CAN chip.
 * 
 * @param Adr The address of the register to read from.
 * 
 * @return The value of the specified register
*/
uchar   ReadSjaReg(uchar Adr)
{
	uchar uctmp=0;
	CAN_CS=1;
	ALLDELAY
	CAN_RD=1;
	ALLDELAY
	CAN_WR=1;
	ALLDELAY
	CAN_DIR=0;//output direction
	ALLDELAY
	CAN_ALE=1;
	ALLDELAY
	CAN_ADDA=Adr;//addr output
	ALLDELAY
	CAN_ALE=0;//latch addr
	ALLDELAY
	CAN_DIR=1;//input direction
	ALLDELAY
	CAN_CS=0;//chip select
	ALLDELAY
	CAN_RD=0;//READ start
	ALLDELAY
	uctmp=CAN_ADDA;//value input
	ALLDELAY
	CAN_RD=1;//READ end
	ALLDELAY
	CAN_CS=1;//chip unselect
	ALLDELAY
	return uctmp;
}

/**
 * @brief Test the interface of CAN chip.
 * 
 * The Function first write the test register and then read back
 * the value of the test register. If the value is equal the value written, 
 * that means the communication with CAN chip is right.
 * 
 * @return The result of test.
 * @retval 0 means the interface work right
 * @retval 1 means the interface work wrong
*/
 bit   BCAN_CREATE_COMMUNATION(void)
 {
    WriteSjaReg(REG_TEST,0xaa);
    if(ReadSjaReg(REG_TEST) == 0xaa)
    {
        return    0;
    }
    else
    {    
       return    1;
    } 
 }

/**
 * @brief Drive the CAN chip enter the reset mode.
 * 
 * The Function set the control register to drive the chip to enter the reset mode,
 * then read back the value of the control register to check whether the chip has 
 * entered the reset mode.
 * 
 * @return The result of the operation.
 * @retval 0 means enter the reset mode successfully
 * @retval 1 means enter the reset mode falsely
*/
 bit   BCAN_ENTER_RETMODEL(void)
 {
    unsigned   char   TempData;
    TempData=  ReadSjaReg(REG_CONTROL);
    WriteSjaReg(REG_CONTROL, (TempData|0x01));
    if((ReadSjaReg(REG_CONTROL)&0x01) == 1)
    {
       return   0;
    }
    else
    {
       return   1;
    }
    
 }

/**
 * @brief Drive the CAN chip quit the reset mode.
 * 
 * The Function set the control register to drive the chip to quit the reset mode,
 * then read back the value of the control register to check whether the chip has 
 * quit the reset mode.
 * 
 * @return The result of the operation.
 * @retval 0 means quit the reset mode successfully
 * @retval 1 means quit the reset mode falsely
*/
 bit   BCAN_QUIT_RETMODEL(void)
 {
    unsigned   char   TempData;
    TempData=  ReadSjaReg(REG_CONTROL);
    WriteSjaReg(REG_CONTROL, (TempData&0xfe));
    if((ReadSjaReg(REG_CONTROL)&0x01) == 0)
    {
       return   0;
    }
    else
    {
       return   1;
    }
 }

/// The present value to set the baud rate
unsigned  char	code	SJA_BTR_CODETAB[]={
    0xbf,0xff,                     //;5KBPS
    0x67,0x2f,                     //;10KBPS
    0x53,0x2F,                     //;20KBPS
    0x87,0xFF,                     //;40KBPS
    0x47,0x2F,                     //;50KBPS
    0x83,0xFF,                     //;80KBPS
    0x43,0x2f,                     //;100KBPS
    0x03,0x1c,                     //;125KBPS
    0x81,0xfa,                     //;200KBPS
    0x01,0x1c,                     //;250KBPS
    0x80,0xfa,                     //;400KBPS
    0x00,0x1c,                     //;500KBPS
    0x80,0xb6,                     //;666KBPS
    0x00,0x16,                     //;800KBPS
    0x00,0x14                      //;1000KBPS
  };

/**
 * @brief Set the baud rate.
 * 
 * The Function write the present value to the bus time register to set the baud rate as specified,
 * then read back the value of the bus time register to check whether the register have set properly,
 * if the register value is wrong then do the write and check operation again till 20 loops.
 * This function can only run under reset mode.
 * 
 * @param CAN_ByteRate The constant of the baud rate to set, refer to the header file.
 * 
 * @return The result of the operation.
 * @retval 0 means baud rate is set successfully
 * @retval 1 means baud rate is set falsely
*/
bit	BCAN_SET_BANDRATE(unsigned char CAN_ByteRate)
{
	bit ErrorFlag = 1;		
	unsigned char ErrorCount = 0x20;
	if(CAN_ByteRate>14)
	{
		ErrorFlag =1;
	}
    else
    {
		while(--ErrorCount)
		{
			WriteSjaReg(REG_BTR0,SJA_BTR_CODETAB[CAN_ByteRate*2]);
			WriteSjaReg(REG_BTR1,SJA_BTR_CODETAB[CAN_ByteRate*2+1]);
			if(ReadSjaReg(REG_BTR0) != SJA_BTR_CODETAB[CAN_ByteRate*2])continue;
			if(ReadSjaReg(REG_BTR1) != SJA_BTR_CODETAB[CAN_ByteRate*2+1])continue;
			ErrorFlag=0;
			break;
		}
	}
	return	ErrorFlag ;
}

/**
 * @brief Set the communication object.
 * 
 * The Function set the acceptance code register and the acceptance mask register,
 * then read back the value of the registers to check whether the register have set properly,
 * This function can only run under reset mode.
 * 
 * @param BCAN_ACR The value to write to the acceptance code register.
 * @param BCAN_AMR The value to write to the acceptance mask register.
 * 
 * @return The result of the operation.
 * @retval 0 means set successfully
 * @retval 1 means set falsely
*/
bit    BCAN_SET_OBJECT(unsigned char  BCAN_ACR,unsigned char  BCAN_AMR)
{
  WriteSjaReg(REG_ACR,BCAN_ACR);
  if(ReadSjaReg(REG_ACR) != BCAN_ACR)
  {
    return  1; 
  }
  WriteSjaReg(REG_AMR,BCAN_AMR);
  if(ReadSjaReg(REG_AMR) != BCAN_AMR)
  {
    return  1; 
  }
  return    0;
}

/**
 * @brief Set the output mode and the output clock of CAN chip.
 * 
 * The Function set the output control register and the clock divider register,
 * then read back the value of the registers to check whether the register have set properly,
 * This function can only run under reset mode.
 * 
 * @param Out_Control The value to write to the output control register.
 * @param Clock_Out The value to write to the clock divider register.
 * 
 * @return The result of the operation.
 * @retval 0 means set successfully
 * @retval 1 means set falsely
*/
bit  BCAN_SET_OUTCLK (unsigned char Out_Control, unsigned char Clock_Out)
{
  WriteSjaReg(REG_OCR,Out_Control);
  if(ReadSjaReg(REG_OCR) != Out_Control)
  {
    return 1;
  } 
  WriteSjaReg(REG_CDR,Clock_Out);
  return 0;
}


/**
 * @brief Initialize the CAN chip.
 * 
 * The Function write the specified value to the corresponding register
 * to initialize the CAN chip.
 * This function can only run under reset mode.
 * 
 * @param BCAN_ACR The value to write to the acceptance code register.
 * @param BCAN_AMR The value to write to the acceptance mask register.
 * @param Bus_Timing0 The value to write to the bus time register 0.
 * @param Bus_Timing1 The value to write to the bus time register 1.
 * @param Out_Control The value to write to the output control register.
 * @param Clock_Out The value to write to the clock divider register.
 * 
 * @return The result of the operation.
 * @retval 0 means the chip is initialized successfully
 * @retval 1 means the chip is initialized falsely
*/
bit             BCAN_HW_INIT(unsigned char  BCAN_ACR,       
                             unsigned char  BCAN_AMR,      
                             unsigned char  Bus_Timing0,   
                             unsigned char  Bus_Timing1,   
                             unsigned char  Out_Control,    
                             unsigned char  Clock_Out      
                             ) 
{
  WriteSjaReg(REG_ACR,BCAN_ACR);
  if(ReadSjaReg(REG_ACR) != BCAN_ACR)
  {
    return  1; 
  }
  WriteSjaReg(REG_AMR,BCAN_AMR);
  if(ReadSjaReg(REG_AMR) != BCAN_AMR)
  {
    return  1; 
  }
  WriteSjaReg(REG_BTR0,Bus_Timing0);
  if(ReadSjaReg(REG_BTR0) != Bus_Timing0)
  {
    return  1; 
  }
  WriteSjaReg(REG_BTR1,Bus_Timing1);
  if(ReadSjaReg(REG_BTR1) != Bus_Timing1)
  {
    return  1; 
  }
  WriteSjaReg(REG_OCR,Out_Control);
  if(ReadSjaReg(REG_OCR) != Out_Control)
  {
    return  1; 
  } 
  WriteSjaReg(REG_CDR,Clock_Out);
  return    0;
}                         

/**
 * @brief Write the data array to the transmit buffer.
 * 
 * The Function write the specified data array to the transmit buffer.
 * 
 * @param SendDataBuf The data array to write to the buffer.
 * 
 * @return The result of the operation.
 * @retval 0 means the data array is written successfully
 * @retval 1 means the data array is written falsely
*/
 bit   BCAN_DATA_WRITE(unsigned char *SendDataBuf)
 {
	uchar  TempCount,uctmp;
	uctmp = ReadSjaReg(REG_STATUS);
	if((uctmp & 0x08) == 0)	//Check whether the last transmit operation has finished or not.
	{
		return	1;
	}

	if((uctmp & 0x04) == 0)	//Check whether the transmit buffer is locked or not.
	{
		return	1;
	}

   if((SendDataBuf[1]&0x10)==0)        //check the RTR bit
   { 
     TempCount =(SendDataBuf[1]&0x0f)+2;	//calculate the length of the data array
   }
   else
   {
     TempCount =2;	//remote frame
   }
   for(uctmp=0;uctmp<TempCount;uctmp++)
   {
     WriteSjaReg(REG_TxBuffer1+uctmp,SendDataBuf[uctmp]);
   }
   return 0;
}

/**
 * @brief Read the data array from the receive buffer.
 * 
 * The Function read the data from the receive buffer and save them to the specified data array.
 * 
 * @param RcvDataBuf The data array to save the data read from the recv buffer.
 * 
 * @return The result of the operation.
 * @retval 0 means receive successfully
 * @retval 1 means receive falsely
*/
 bit    BCAN_DATA_RECEIVE(unsigned char *RcvDataBuf)
 {
	uchar  TempCount,uctmp;
	if((ReadSjaReg(REG_STATUS)&0x01)==0)	//check the receive buffer is valid or not
	{
		return 1;
	}

	uctmp=ReadSjaReg(REG_RxBuffer2);	//read recv buffer 2
	if((uctmp&0x10)==0)	// check the RTR bit
	{
		TempCount=(uctmp&0x0f)+2;	//calculate the length of the data array
	}
	else
	{
		TempCount=2;
	}
	for(uctmp=0;uctmp<TempCount;uctmp++)
	{
		RcvDataBuf[uctmp]=ReadSjaReg(REG_RxBuffer1+uctmp);
	}
	return  0;
 }

/**
 * @brief Drive the CAN chip to do a command.
 * 
 * The Function write the command to the command register to drive the chip to do this command.
 * 
 * @param cmd The command to do.
 * 
 * @return The result of the operation.
 * @retval 0 means the command is done successfully
 * @retval 1 means the command is done falsely
*/
 bit  BCAN_CMD_PRG(unsigned char cmd)
 {
   WriteSjaReg(REG_COMMAND,cmd);
   switch(cmd)
   {
     case  TR_CMD:
           return    0;
           break;
     case  AT_CMD:
           if((ReadSjaReg(REG_STATUS) & 0x20)==0)
           {
              return  0;
           }
           else
           {
              return  1;
           }   
           break; 
     case  RRB_CMD:
           if((ReadSjaReg(REG_STATUS) & 0x01)==1)
           {
             return  1;
           }
           else
           {
              return  0;
           }   
           break; 
  
     case  COS_CMD:
           if((ReadSjaReg(REG_STATUS) & 0x02)==0)
           {
             return  0;
           }
           else
           {
              return  1;
           }   
           break; 
     
     case  GTS_CMD:
           return    0;
           break; 
     default:
             return  1;
             break; 
   }
 }

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
白白色 亚洲乱淫| 国产老女人精品毛片久久| 精品久久久久久久久久久久久久久| 国产乱码精品一区二区三区av| 一区二区国产视频| 国产女同互慰高潮91漫画| 欧美在线观看18| 懂色av一区二区在线播放| 免费成人在线网站| 亚洲丰满少妇videoshd| 亚洲日本在线观看| 国产色综合一区| 日韩一级免费一区| 欧美日韩国产片| 一本大道久久a久久精品综合| 国内偷窥港台综合视频在线播放| 亚洲免费在线视频| 91网站最新地址| 中文欧美字幕免费| 国产剧情一区在线| 久久精品国产第一区二区三区| 亚洲激情欧美激情| 中文字幕一区二区三区不卡在线| 精品国产乱子伦一区| 欧美精品电影在线播放| 在线亚洲人成电影网站色www| 在线视频你懂得一区| 毛片av中文字幕一区二区| 国产午夜精品久久久久久免费视| 欧美不卡在线视频| 日韩美女视频在线| 精品国精品自拍自在线| 日韩欧美一级精品久久| 欧美精选一区二区| 91精品福利在线一区二区三区| 在线观看日产精品| 91高清在线观看| 在线一区二区三区做爰视频网站| 91色在线porny| 白白色亚洲国产精品| 国产麻豆成人传媒免费观看| 欧美日韩国产大片| 亚洲成年人网站在线观看| 亚洲一区视频在线| 亚洲一区二区三区美女| 亚洲高清三级视频| 爽好多水快深点欧美视频| 偷拍与自拍一区| 日韩—二三区免费观看av| 麻豆91在线播放免费| 久久精品国内一区二区三区| 国产一区二区成人久久免费影院| 国产精品资源网站| 成a人片国产精品| 91免费观看国产| 欧美日韩一区二区三区四区五区 | 91蝌蚪porny| 99精品一区二区| 91福利社在线观看| 日韩一区二区三区四区| 久久综合久久久久88| 中文字幕精品—区二区四季| 亚洲免费在线视频一区 二区| 中文字幕乱码久久午夜不卡| www久久精品| 久久一夜天堂av一区二区三区| 国产拍揄自揄精品视频麻豆| 国产精品久久久久久妇女6080 | 91精品国产一区二区三区香蕉| 日韩欧美一级精品久久| 亚洲国产精品精华液ab| 亚洲综合色丁香婷婷六月图片| 日本不卡的三区四区五区| 国产精品正在播放| 91成人在线免费观看| 欧美一区二区成人| 国产精品久久久久毛片软件| 五月激情六月综合| 成人亚洲精品久久久久软件| 欧美视频一区在线观看| 久久这里只有精品6| 亚洲精品欧美激情| 久久精品国产成人一区二区三区 | 国产在线视频一区二区三区| 亚洲午夜影视影院在线观看| 国产二区国产一区在线观看| 亚洲青青青在线视频| 日韩区在线观看| 欧美国产激情二区三区 | 久久久久久综合| 亚洲女同ⅹxx女同tv| 另类小说图片综合网| www.欧美日韩| 日韩欧美综合一区| 日韩理论片一区二区| 精品一区二区日韩| 欧美系列在线观看| 亚洲国产精品成人综合| 秋霞午夜av一区二区三区| 91一区二区三区在线观看| 精品久久五月天| 五月天亚洲精品| 99久久99久久久精品齐齐| 欧美大白屁股肥臀xxxxxx| 夜夜精品视频一区二区| 成人在线综合网| 欧美电影免费观看高清完整版在线观看 | 精品久久久久av影院| 一区二区三区日韩欧美精品 | 国产成人综合亚洲91猫咪| 日本精品裸体写真集在线观看| 亚洲精品一区二区精华| 午夜电影网一区| 91在线小视频| 国产精品入口麻豆九色| 国产一区二区三区国产| 日韩欧美成人一区| 午夜精品在线视频一区| 在线观看视频一区| 最新国产成人在线观看| 高清在线不卡av| 久久精品亚洲精品国产欧美| 激情av综合网| 欧美电影免费观看高清完整版在| 午夜久久电影网| 欧美日本不卡视频| 亚洲国产wwwccc36天堂| 欧美性生活影院| 亚洲精品视频免费观看| 97se亚洲国产综合自在线| 国产视频亚洲色图| 粉嫩绯色av一区二区在线观看| 精品国产凹凸成av人网站| 国产精品视频一二| 成人涩涩免费视频| 成人h动漫精品一区二区| 91福利视频网站| 伊人一区二区三区| 在线看日本不卡| 亚洲永久精品国产| 欧美日韩国产一级片| 日韩av网站免费在线| 欧美一区二区视频免费观看| 蜜桃视频一区二区| 久久综合色8888| 国产suv精品一区二区三区| 欧美国产精品久久| 91色乱码一区二区三区| 亚洲线精品一区二区三区| 777奇米四色成人影色区| 美国欧美日韩国产在线播放| 精品国产凹凸成av人网站| 国产成人在线观看免费网站| 自拍偷拍亚洲欧美日韩| 色婷婷激情一区二区三区| 婷婷丁香激情综合| 日韩欧美一二三| 国产91精品精华液一区二区三区| 国产精品国产三级国产aⅴ原创 | 日韩一区和二区| 午夜欧美大尺度福利影院在线看| 欧美国产精品久久| 日韩免费高清电影| 久久免费国产精品| 亚洲成av人影院在线观看网| 欧美α欧美αv大片| 中文字幕一区二区三区乱码在线| 2021中文字幕一区亚洲| 精品黑人一区二区三区久久| 国产欧美日韩麻豆91| 日韩亚洲欧美在线| 亚洲免费观看高清完整版在线观看熊 | 94-欧美-setu| 亚洲二区在线视频| 久久久亚洲午夜电影| 色婷婷av一区二区三区大白胸| 日韩精品乱码免费| 久久精品欧美一区二区三区不卡 | 日本欧美肥老太交大片| 日韩亚洲电影在线| 国产福利视频一区二区三区| 亚洲猫色日本管| 欧美一区三区四区| 成人av电影在线网| 日本欧美肥老太交大片| ...av二区三区久久精品| 欧美性色黄大片| 国产精品99久久久久久久vr| 一区二区欧美国产| 久久久久国产精品麻豆| 欧美日韩一级二级三级| 高清久久久久久| 日本亚洲天堂网| 亚洲精品在线观| 另类小说视频一区二区| 亚洲免费毛片网站| 国产亚洲欧洲997久久综合 | 欧美日韩国产综合一区二区三区| 国产不卡在线播放| 玖玖九九国产精品|