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

📄 ad7799.lst

📁 Keil C写的ADI的24位A/DAD7799使用SPI接口程序
💻 LST
📖 第 1 页 / 共 4 页
字号:
C51 COMPILER V8.02   AD7799                                                                03/21/2008 20:01:02 PAGE 1   


C51 COMPILER V8.02, COMPILATION OF MODULE AD7799
OBJECT MODULE PLACED IN AD7799.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE AD7799.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #pragma CODE DEBUG SYMBOLS OBJECTEXTEND     //SRC 当与汇编混合编译时需要
   2          #include"VRS51L3074_keil.h"
   3          #include<intrins.h>
   4          #define uchar   unsigned char
   5          #define uint    unsigned int
   6          #define ulong   unsigned long
   7          typedef union{     //无符号字符型,整形,长整形数据联合体
   8                        uchar uc[2];
   9                        uint  ui;
  10                       }UCI;
  11          typedef union{     //无符号字符型,整形,长整形数据联合体
  12                        uchar uc[4];
  13                        uint  ui[2];
  14                        ulong ul;
  15                       }UCIL;
  16                                       
  17          /************FIR数字滤波系数表***************/
  18          //DFTAB
  19          //FIR滤波级数
  20          unsigned char code DFNUMBER[]={19,27,35,43,51,59,67,75,83,91};
  21          //各级滤波系数表
  22          unsigned int  code DFTAB0[]={76,163,256,351,443,527,597,651,684,696,684,651,597,527,443,351,256,163,76};
  23                                      /*6*/
  24          unsigned int  code DFTAB1[]={13,57,105,156,209,263,315,364,409,447,479,502,516,522,
  25                                          516,502,479,447,409,364,315,263,209,156,105,57,13};
  26                                          /*4.5*/
  27          unsigned int  code DFTAB2[]={4,29,56,86,117,149,182,214,245,275,303,328,351,370,385,
  28                                      396,403,406,403,396,385,370,351,328,303,275,245,214,182,
  29                                      149,117,86,56,29,4};
  30                                      /*3.50*/
  31          unsigned int  code DFTAB3[]={6,22,41,59,79,99,119,140,161,181,201,220,238,255,270,
  32                                       284,296,306,314,320,323,324,323,320,314,306,296,284,
  33                                       270,255,238,220,201,181,161,140,119,99,79,59,41,22,6};
  34                                       /*2.8*/
  35          unsigned int  code DFTAB4[]={1,12,25,38,52,66,81,95,110,126,141,155,170,184,197,210,
  36                                       222,233,243,252,259,265,270,274,276,278,276,274,270,265,
  37                                       259,252,243,233,222,210,197,184,170,155,141,126,110,95,
  38                                       81,66,52,38,25,12,1};
  39                                       /*2.39*/
  40          unsigned int  code DFTAB5[]={1,9,19,28,38,48,59,70,81,92,103,115,126,137,147,158,
  41                                                           168,177,186,195,203,210,217,222,227,231,234,237,238,240,
  42                                                           238,237,234,231,227,222,217,210,203,195,186,177,168,158,
  43                                                           147,137,126,115,103,92,81,70,59,48,38,28,19,9,1};
  44                                                           /*2.06*/
  45          unsigned int  code DFTAB6[]={1,8,15,23,30,38,46,54,63,71,80,88,97,105,114,122,130,138,
  46                                       146,153,160,167,173,179,184,189,194,198,201,204,206,207,
  47                                       208,208,208,207,206,204,201,198,194,189,184,179,173,167,
  48                                       160,153,146,138,130,122,114,105,97,88,80,71,63,54,46,38,
  49                                       30,23,15,8,1};
  50                                       /*1.8*/
  51          unsigned int  code DFTAB7[]={1,7,12,18,24,30,36,42,49,56,62,69,76,83,90,97,103,110,116,
  52                                       123,129,135,141,146,151,156,161,165,169,173,176,179,181,
  53                                       182,184,185,186,186,186,185,184,182,181,179,176,173,169,
  54                                       165,161,156,151,146,141,135,129,123,116,110,103,97,90,83,
  55                                       76,69,62,56,49,42,36,30,24,18,12,7,1};
C51 COMPILER V8.02   AD7799                                                                03/21/2008 20:01:02 PAGE 2   

  56                                       /*1.61*/
  57          unsigned int  code DFTAB8[]={2,6,10,15,20,24,29,35,40,45,51,56,62,67,73,78,84,89,
  58                                       95,100,105,110,115,120,125,130,134,138,142,146,149,152,
  59                                       155,158,160,162,164,165,166,167,168,168,168,167,166,165,
  60                                       164,162,160,158,155,152,149,146,142,138,134,130,125,120,
  61                                       115,110,105,100,95,89,84,78,73,67,62,56,51,45,40,35,29,
  62                                       24,20,15,10,6,2};
  63                                       /*1.45*/
  64          unsigned int  code DFTAB9[]={3,6,10,14,17,21,26,30,34,38,43,47,52,56,61,65,70,74,
  65                                       79,83,88,92,96,100,104,108,112,116,120,123,126,129,
  66                                       132,135,138,140,142,144,146,147,149,150,151,151,152,
  67                                       152,152,151,151,150,149,147,146,144,142,140,138,135,
  68                                       132,129,126,123,120,116,112,108,104,100,96,92,88,83,
  69                                       79,74,70,65,61,56,52,47,43,38,34,30,26,21,17,14,10,6,3}; //1.31
  70          //各级滤波系数和
  71          unsigned int  code DFCOESUM[]={8192,8192,8192,8192,8192,8192,8192,8192,8192,8192}   ;     /*coefficient su
             -m tab*/
  72          xdata ulong ADQUEUE[93];               //滤波所用的ad数据队列 原空间100实用91(0-90)
  73          xdata uint code *ADDFTABPoint;//指向当前滤波级数对应的滤波表格
  74          xdata uchar ADDFNumber;  //当前滤波级数对应的滤波表中数据的个数
  75          
  76          
  77          #define DELAY5  delay()   //_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(
             -)
  78          sbit    P1_5    =   P1^5;      //1
  79          sbit    P1_6    =   P1^6;      //2
  80          sbit    P1_7    =   P1^7;      //3
  81          sbit    P4_5    =   P4^5;     //6  pin
  82          sbit    P5_0    =   P5^0;     //7
  83          sbit    P5_1    =   P5^1;     //8
  84          sbit    P5_2    =   P5^2;     //9
  85          sbit    P5_3    =   P5^3;     //10
  86          sbit    P5_4    =   P5^4;     //20
  87          sbit    P5_5    =   P5^5;     //21
  88          sbit    P5_6    =   P5^6;     //22
  89          sbit    P5_7    =   P5^7;     //23
  90          
  91          #define AD7799_CS    P4_5           //输出   6pin
  92          #define AD7799_SCLK  P5_1           //输出   8pin
  93          #define AD7799_DOUT  P5_2           //输入   9pin
  94          #define AD7799_RDY   P5_2           //输入   9pin  同上是一个引脚
  95          #define AD7799_DIN   P5_0           //输出   7pin
  96          
  97          UCIL MainData,demoul;
  98          ulong xdata XMainData,xdemoul;
  99          void Ad7799_Ini();
 100          
 101          #define DELAYS10    _nop_();_nop_();_nop_();_nop_()
 102          void delay(char i)
 103          {uchar j;
 104   1       for(;i>0;i--); for(j=0;j<10;j++)_nop_();
 105   1      }
 106          
 107          void CPUInit(void)
 108          {
 109   1        /*I/O口配置:输入配置为1(配置为1后,内部自动上拉到2.5V,输出配置为0;空脚要配置为0*/   
 110   1        PERIPHEN1=0x18;  //BIT7:SPICSEN 6:SPIEN 5:I2CEN 4:U1EN 3:U0EN 2:T2EN 1;T1EN 0:T0EN  //TIMER2和TIMER0使能
 111   1        P0PINCFG=0x00; 
 112   1        P1PINCFG=0x04; 
 113   1        P2PINCFG=0x00;   //00100100
 114   1        P3PINCFG=0x01; 
 115   1        P4PINCFG=0x00; 
C51 COMPILER V8.02   AD7799                                                                03/21/2008 20:01:02 PAGE 3   

 116   1        P5PINCFG=0x04;  //00000100
 117   1        P6PINCFG=0x00; 
 118   1        PORTINEN=0xff; //0xff全部I/O口均不使用
 119   1        P0=P1=P2=P3=P6=0x00;  P4=0x00;P5=0x04;
 120   1        PERIPHEN2=0x28;//BIN7:PWC1EN 6:PWC0EN 5:AUEN 4:XRAM2CODE 3:IOPORTEN 2:WDTEN 1:PWMSFREN 0:FPIEN  算术单元
             -、IO口使能*/
 121   1        P0=P1=P2=P3=P6=0x00; P4=0x00; P5=0x04;
 122   1        AD7799_CS=1;
 123   1          UART0CFG=0x90;  // 0x09:38400 BRADJ=0x02(19200)/波特率发生器使用内部晶振/9th不使用/1位停止位
 124   1          UART0INT=0x00;  //使能接收中断,使能发送完成中断,允许接收 Enable RX AV + RXOVint + Enable Reception
 125   1          UART0EXT=0x00;  //Not using UART0 Extensions
 126   1          UART0BRL=0x1f;   //0040=19200  001f=38400
 127   1          UART0BRH=0x00;  //波特率19200
 128   1          U0IEN=1;
 129   1         GENINTEN = 0x01;      //Enable Global interrupt
 130   1         UART0BUF=0xff;
 131   1         while(!(UART0INT & 0x01));
 132   1         UART0BUF=0xff;
 133   1         while(!(UART0INT & 0x01));
 134   1         UART0BUF=0xff;
 135   1         while(!(UART0INT & 0x01));
 136   1         UART0BUF=0xff;
 137   1         while(!(UART0INT & 0x01));
 138   1         UART0BUF=0x0d;
 139   1         while(!(UART0INT & 0x01));
 140   1      }
 141          
 142          /*  AD7799写寄存器函数
 143              WriteData:要写的数据*/
 144          void WriteByteToAd7799(unsigned char WriteData)
 145          {
 146   1          unsigned char i ;
 147   1          AD7799_CS=0;
 148   1          for(i=0;i<8;i++)
 149   1          {
 150   2              DELAYS10;
 151   2              AD7799_SCLK=0 ;
 152   2              DELAYS10;
 153   2              if(WriteData&0x80)AD7799_DIN=1 ;
 154   2              else AD7799_DIN=0 ;
 155   2              WriteData=WriteData<<1 ;
 156   2              DELAYS10;
 157   2              AD7799_SCLK=1 ;
 158   2              DELAYS10;
 159   2          }
 160   1          AD7799_CS=1;
 161   1      }
 162          
 163          /*AD7799读寄存器函数*/
 164          unsigned char ReadByteFromAd7799(void)
 165          {
 166   1          unsigned char i ;
 167   1          unsigned char ReadData ;
 168   1          AD7799_CS=0;
 169   1          ReadData=0 ;
 170   1          for(i=0;i<8;i++)
 171   1          {
 172   2              DELAYS10;
 173   2              AD7799_SCLK=0 ;
 174   2              DELAYS10;
 175   2              ReadData=ReadData<<1 ;
 176   2              if(AD7799_DOUT)ReadData+=1 ;
C51 COMPILER V8.02   AD7799                                                                03/21/2008 20:01:02 PAGE 4   

 177   2              DELAYS10;
 178   2              AD7799_SCLK=1 ;
 179   2              DELAYS10;
 180   2          }
 181   1          AD7799_DOUT=1 ;
 182   1          AD7799_CS=1;
 183   1          return(ReadData);
 184   1      }
 185          void WaiteRDY(void)
 186          {
 187   1          unsigned int iint ;
 188   1          iint=0 ;
 189   1          while(AD7799_RDY)
 190   1          {
 191   2              iint++;
 192   2              if(iint>65530)
 193   2              {
 194   3                  //reset ad7799
 195   3                  WriteByteToAd7799(0xff);
 196   3                  WriteByteToAd7799(0xff);
 197   3                  WriteByteToAd7799(0xff);
 198   3                  WriteByteToAd7799(0xff);
 199   3                  Ad7799_Ini();
 200   3                  break ;
 201   3              }
 202   2          }
 203   1      }
 204          void Ad7799_Ini()
 205          {
 206   1          WriteByteToAd7799(0x10);
 207   1          //b0001 0000
 208   1          /* Writes to Communications Register Setting Next Operation as Write to CONFIGURATION Register*/
 209   1          //写通讯寄存器为:下一操作写配置寄存器 WriteByteToAd7799(0x10)b0001 0000    0写通讯0下操作写010配置寄
             -存器0关连续读00留用
 210   1          WriteByteToAd7799(0x37);//增益为128  B0011 0111   00留用1开电流源1单端  0留用111是128倍增益 
 211   1          WriteByteToAd7799(0x00);  //通道号 0 B0011 0000   00留用1基准默认1开缓冲 0留用000通道0
 212   1          //1通道
 213   1          /*CONFIGURATION REGISTER[00,BO(0),U/B(0),0(0),G2(1),G1(1),G0(1),0,0,REF_DET(0),BUF(1),0(0),CH2(0),CH1(
             -0),CH0(0)]*/
 214   1          //WriteByteToAd7799(0x08);
 215   1          //b0000 1000
 216   1          /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/
 217   1          //WriteByteToAd7799(0x80);
 218   1          //WriteByteToAd7799(0x0a);
 219   1          /* Writes to Mode Register Initiating Internal Zero-Scale Calibration*/
 220   1          //WaiteRDY();
 221   1          /* Wait for RDY pin to go low to indicate end of calibration cycle*/
 222   1          //WriteByteToAd7799(0x08);
 223   1          /* Writes to Communications Register Setting Next Operation as Write to
 224   1              Mode Register*/
 225   1          //WriteByteToAd7799(0xa0);
 226   1          //WriteByteToAd7799(0x0a);
 227   1          /* Writes to Mode Register Initiating Internal Full-Scale Calibration*/
 228   1          //WaiteRDY();
 229   1          /* Wait for RDY pin to go low to indicate end of calibration cycle*/
 230   1          WriteByteToAd7799(0x08);//b0000 1000
 231   1          /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/
 232   1          WriteByteToAd7799(0x00);   //000连续模式0断PSW0000留用
 233   1          WriteByteToAd7799(0x09);   //0000留用0011(123Hz)1010(16.7HZ65dB)
 234   1          /* Mode Register[MD2(0),MD1(0),MD0(0),PSW(0),0(0),0(0),0(0),0(0),(0),(0),0(0),0(0),FS3(1),FS2(0),FS1(1
             -),FS0(0)]*/
 235   1          /*模式0 Continuous-Conversion Mode.,Fadc=16.7HZ;*/
C51 COMPILER V8.02   AD7799                                                                03/21/2008 20:01:02 PAGE 5   

 236   1      
 237   1      }
 238          ulong ReadAd7799ConversionData(void)
 239          {
 240   1          ulong ConverData ;
 241   1          unsigned char ADSAT ;
 242   1          unsigned char ErrNUM=0;
 243   1          WaiteRDY();              //等待数据READY
 244   1          WriteByteToAd7799(0x40);  //0100 0000 配置下一操作为:读状态寄存器
 245   1          ADSAT=ReadByteFromAd7799();   //读出状态 8位
 246   1          while((ADSAT&0x40)||(!(ADSAT&0x08)))    //出错或者读写异常
 247   1          {
 248   2              WriteByteToAd7799(0xff);         //复位
 249   2              WriteByteToAd7799(0xff);
 250   2              WriteByteToAd7799(0xff);
 251   2              WriteByteToAd7799(0xff);
 252   2              Ad7799_Ini();                   //初始化
 253   2              WaiteRDY();                     //读状态
 254   2              WriteByteToAd7799(0x40);        //下一操作:读状态
 255   2              ADSAT=ReadByteFromAd7799();     //读状态
 256   2              
 257   2              ErrNUM++;
 258   2              if(ErrNUM>5)return(0xffffff);//if(ErrNUM>5)break;              //连续5次都读出错

⌨️ 快捷键说明

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