⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.lst

📁 ad转换的程序
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V6.12  SPI                                                                    06/09/2003 19:14:26 PAGE 1   


C51 COMPILER V6.12, COMPILATION OF MODULE SPI
OBJECT MODULE PLACED IN spi.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE spi.c DB OE

stmt level    source

   1          /*================================================================================================
   2          
   3          						AD7705 analogy serialport driver
   4          
   5          ==================================================================================================
   6          		AD7705------W77E58		AD7705------W77E58		AD7705------W77E58
   7          		AD_DOUT-------P0.7 		AD_DIN----------P0.4	AD_SCLK-------P2.2 	
   8          [note] CYGANAL at 6MHz, AD7705BN at 2.576MHz
   9          ================================================================================================*/
  10          #include <C8051F020.H>
  11          //#include <reg51.h>
  12          //#include <math.h>
  13          //#include <intrins.h>
  14          
  15          /*--------------------global define-------------------------------------------------------------*/
  16          #define	 uchar unsigned char
  17          #define  STORE_BUF_LEN  12                 	    /*size of storing buffer    					*/
  18          uchar idata ucidStoreBuf[STORE_BUF_LEN];  		/*storage for storing ADC results				*/     
  19          uchar idata *inlast=ucidStoreBuf;  				/*the positon of last ADC result				*/
  20          long int transfer_ad;
  21          extern unsigned int xdata canmodify_package[];
  22          extern int cycle_times;
  23          extern xdata int Parameter_transfer;
  24          /*-------------------interface define-------------------------*/
  25          sbit AD_SCLK 	= P0^6;							/*analogize TX in serial mode 0					*/
  26          sbit AD_DIN 	= P0^4;							/*												*/
  27          sbit AD_DOUT 	= P0^7;	
  28          sbit CS         = P1^7;						/*												*/
  29          sfr16 DAC0=0Xd2;
  30          /*---------------function prototype define--------------------*/
  31          extern void 	writeCmd (uchar);						/*write command code to AD7705's register		*/
  32          extern uchar	readStatus (void);						/*read the communication register's status		*/
  33          extern void 	readData (void);						/*read ADC result from AD7705					*/
  34          extern uchar 	readByte (void);						/* read a byte from AD7705						*/
  35          extern void delay(int a );
  36          extern  init3();
  37          void ad1 (void)				  
  38          {  
  39   1      
  40   1      
  41   1      		readData ();							/*read ADC result								*/
  42   1      }
  43          
  44          /* -----------------------------------------------------------------------------------------------
  45          Function:		void writeCmd (uchar)
  46          PURPOSE: 		write command code to AD7705's regester
  47          PARAMETERS:
  48          RETURN:
  49          COMMENTS:
  50          HISTORY:   
  51          	Date:		2002-7-18  		2002-7-23
  52          	Author:		xiaolan   		xiaolan
  53          	Comment:	Wrote it		Revise it
  54          ------------------------------------------------------------------------------------------------*/
  55          void writeCmd (uchar command)
C51 COMPILER V6.12  SPI                                                                    06/09/2003 19:14:26 PAGE 2   

  56          {
  57   1      	xdata uchar i;
  58   1      
  59   1      	/* AD_CS = 0; */
  60   1      	for (i = 0; i < 8; i ++)
  61   1      	{
  62   2      		AD_SCLK = 0;
  63   2      
  64   2      		if (command & 0x80)
  65   2      			AD_DIN = 1;
  66   2      		else
  67   2      			AD_DIN = 0;
  68   2      
  69   2      		command <<= 1;
  70   2      		AD_SCLK = 1;
  71   2      	}
  72   1      	/* AD_CS = 1; */
  73   1      	AD_DIN = 1; 
  74   1      	AD_SCLK = 1; 
  75   1      }
  76          
  77          /* -----------------------------------------------------------------------------------------------
  78          Function:		uchar readStatus (void);
  79          PURPOSE: 		read AD7705's regester status
  80          PARAMETERS:
  81          RETURN:
  82          COMMENTS:
  83          HISTORY:   
  84          	Date:		2002-7-18  		2002-7-23
  85          	Author:		xiaolan   		xiaolan
  86          	Comment:	Wrote it		Revise it
  87          ------------------------------------------------------------------------------------------------*/
  88          uchar readStatus (void)
  89          {
  90   1       xdata	uchar ucStatus = 0x00;
  91   1         if(canmodify_package[6]==3)
  92   1      	writeCmd (0x08);							/*write to communications register setting up 	*/
  93   1         if(canmodify_package[6]==1)
  94   1           writeCmd(0x09);												/*next operation to be a read from the communic-*/
  95   1      												/*ations register 								*/
  96   1      	ucStatus = readByte();
  97   1      	return (ucStatus);
  98   1      }
  99          
 100          /* -----------------------------------------------------------------------------------------------
 101          Function:		uchar	readData (void);
 102          PURPOSE: 		read AD7705's regester status
 103          PARAMETERS:
 104          RETURN:
 105          COMMENTS:
 106          HISTORY:   
 107          	Date:		2002-7-18  		2002-7-23
 108          	Author:		xiaolan   		xiaolan
 109          	Comment:	Wrote it		Revise it
 110          ------------------------------------------------------------------------------------------------*/
 111          void readData (void)
 112          {
 113   1       xdata	uchar ucStatus = 0x00, ucDataHigh = 0x00, ucDataLow = 0x00;
 114   1        static long  xdata jiyi[4];
 115   1       static unsigned dec=5;
 116   1       static long total=0L;
 117   1       long  xdata transfer1;
C51 COMPILER V6.12  SPI                                                                    06/09/2003 19:14:26 PAGE 3   

 118   1       long  xdata transfer2;
 119   1       long xdata   transfer3;
 120   1       long  xdata transfer;
 121   1      //	{
 122   1      //	ucStatus = readStatus();
 123   1      //	}while (ucStatus & 0x80);
 124   1      	ucStatus = readStatus();					/*wait for ADC result							*/
 125   1          if((ucStatus & 0x80)==0x00)
 126   1      {
 127   2          if(canmodify_package[6]==3)
 128   2      		  writeCmd(0x38);							/*write to communications register setting up 	*/
 129   2          if(canmodify_package[6]==1)
 130   2      	      writeCmd(0x39);												/*next operation to be a read from the communic-*/											/*ations re
             -gister 								*/
 131   2      
 132   2      	ucDataHigh = readByte();					/*first get the high 8 bits						*/
 133   2      	*inlast = ucDataHigh;            			/*store the ADC result							*/          
 134   2      	inlast++;			            			/*add 1 to the position of the last ADC result	*/  
 135   2      	if (inlast == ucidStoreBuf + STORE_BUF_LEN) 
 136   2      		inlast = ucidStoreBuf;					/*reset the pointer of the last ADC result 		*/      
 137   2      
 138   2      	ucDataLow = readByte();					/*then get the low 8 bits						*/
 139   2      	*inlast = ucDataLow;            			/*store the ADC result							*/          
 140   2      	inlast++;			            			/*add 1 to the position of the last ADC result	*/  
 141   2      	if (inlast == ucidStoreBuf + STORE_BUF_LEN) 
 142   2      		inlast = ucidStoreBuf;	
 143   2      /*------------------------------------------------------------------------------------------------	
 144   2           AD                          dealing with the data	
 145   2      ----------------------------------------------------------------------------------------------------*/
 146   2              
 147   2      		transfer=(ucDataHigh<<8)+ucDataLow;
 148   2              total+=transfer;
 149   2      		dec--;
 150   2      		if(dec==0)
 151   2      		  {
 152   3      		   jiyi[0]=jiyi[1];
 153   3      	       jiyi[1]=jiyi[2];
 154   3      	       jiyi[2]=jiyi[3];
 155   3      	       //jiyi[3]=jiyi[4];
 156   3      	       //jiyi[4]=jiyi[5];
 157   3      	       //jiyi[5]=jiyi[6];
 158   3      	       //jiyi[6]=jiyi[7];
 159   3      	       //jiyi[7]=jiyi[8];
 160   3      	       //jiyi[8]=jiyi[9]; 
 161   3                 transfer1=((total/5));
 162   3      	       if(transfer<0)transfer1=jiyi[2];
 163   3      	       jiyi[3]=transfer1;
 164   3                 transfer2=(jiyi[0]+jiyi[1]+jiyi[2]+jiyi[3])/4;
 165   3      		   
 166   3      		   Parameter_transfer=(int)transfer2;
 167   3      		   //transfer2=(jiyi[0]+jiyi[1]+jiyi[2]+jiyi[3]+jiyi[4]+jiyi[5]+jiyi[6]+jiyi[7]+jiyi[8]+jiyi[9])/10;
 168   3      		   if(canmodify_package[6]==1)
 169   3      		      {
 170   4      		          
 171   4      				 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -