久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

標題: Keil AD7799 AD轉換程序 [打印本頁]

作者: LINWEI1208    時間: 2016-4-16 17:38
標題: Keil AD7799 AD轉換程序

ad7798,ad7799的詳細中文資料請看: http://www.zg4o1577.cn/chip/4000.html
  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. #define DELAY5  delay()   //_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
  18. sbit    P1_5    =   P1^5;      //1
  19. sbit    P1_6    =   P1^6;      //2
  20. sbit    P1_7    =   P1^7;      //3
  21. sbit    P4_5    =   P4^5;     //6  pin
  22. sbit    P5_0    =   P5^0;     //7
  23. sbit    P5_1    =   P5^1;     //8
  24. sbit    P5_2    =   P5^2;     //9
  25. sbit    P5_3    =   P5^3;     //10
  26. sbit    P5_4    =   P5^4;     //20
  27. sbit    P5_5    =   P5^5;     //21
  28. sbit    P5_6    =   P5^6;     //22
  29. sbit    P5_7    =   P5^7;     //23

  30. #define AD7799_CS    P4_5           //輸出   6pin
  31. #define AD7799_SCLK  P5_1           //輸出   8pin
  32. #define AD7799_DOUT  P5_2           //輸入   9pin
  33. #define AD7799_RDY   P5_2           //輸入   9pin  同上是一個引腳
  34. #define AD7799_DIN   P5_0           //輸出   7pin

  35. UCIL MainData,demoul;
  36. ulong xdata XMainData,xdemoul;
  37. void Ad7799_Ini();

  38. #define DELAYS10    _nop_();_nop_();_nop_();_nop_()
  39. void delay(char i)
  40. {uchar j;
  41. for(;i>0;i--); for(j=0;j<10;j++)_nop_();
  42. }

  43. void CPUInit(void)
  44. {
  45.   /*I/O口配置:輸入配置為1(配置為1后,內部自動上拉到2.5V,輸出配置為0;空腳要配置為0*/   
  46.   PERIPHEN1=0x18;  //BIT7:SPICSEN 6:SPIEN 5:I2CEN 4:U1EN 3:U0EN 2:T2EN 1;T1EN 0:T0EN  //TIMER2和TIMER0使能
  47.   P0PINCFG=0x00;
  48.   P1PINCFG=0x04;
  49.   P2PINCFG=0x00;   //00100100
  50.   P3PINCFG=0x01;
  51.   P4PINCFG=0x00;
  52.   P5PINCFG=0x04;  //00000100
  53.   P6PINCFG=0x00;
  54.   PORTINEN=0xff; //0xff全部I/O口均不使用
  55.   P0=P1=P2=P3=P6=0x00;  P4=0x00;P5=0x04;
  56.   PERIPHEN2=0x28;//BIN7:PWC1EN 6:PWC0EN 5:AUEN 4:XRAM2CODE 3:IOPORTEN 2:WDTEN 1:PWMSFREN 0:FPIEN  算術單元、IO口使能*/
  57.   P0=P1=P2=P3=P6=0x00; P4=0x00; P5=0x04;
  58.   AD7799_CS=1;
  59.     UART0CFG=0x90;  // 0x09:38400 BRADJ=0x02(19200)/波特率發生器使用內部晶振/9th不使用/1位停止位
  60.     UART0INT=0x00;  //使能接收中斷,使能發送完成中斷,允許接收 Enable RX AV + RXOVint + Enable Reception
  61.     UART0EXT=0x00;  //Not using UART0 Extensions
  62.     UART0BRL=0x1f;   //0040=19200  001f=38400
  63.     UART0BRH=0x00;  //波特率19200
  64.     U0IEN=1;
  65.    GENINTEN = 0x01;      //Enable Global interrupt
  66.    UART0BUF=0xff;
  67.    while(!(UART0INT & 0x01));
  68.    UART0BUF=0xff;
  69.    while(!(UART0INT & 0x01));
  70.    UART0BUF=0xff;
  71.    while(!(UART0INT & 0x01));
  72.    UART0BUF=0xff;
  73.    while(!(UART0INT & 0x01));
  74.    UART0BUF=0x0d;
  75.    while(!(UART0INT & 0x01));
  76. }

  77. /*  AD7799寫寄存器函數
  78.     WriteData:要寫的數據*/
  79. void WriteByteToAd7799(unsigned char WriteData)
  80. {
  81.     unsigned char i ;
  82.     AD7799_CS=0;
  83.     for(i=0;i<8;i++)
  84.     {
  85.         DELAYS10;
  86.         AD7799_SCLK=0 ;
  87.         DELAYS10;
  88.         if(WriteData&0x80)AD7799_DIN=1 ;
  89.         else AD7799_DIN=0 ;
  90.         WriteData=WriteData<<1 ;
  91.         DELAYS10;
  92.         AD7799_SCLK=1 ;
  93.         DELAYS10;
  94.     }
  95.     AD7799_CS=1;
  96. }

  97. /*AD7799讀寄存器函數*/
  98. unsigned char ReadByteFromAd7799(void)
  99. {
  100.     unsigned char i ;
  101.     unsigned char ReadData ;
  102.     AD7799_CS=0;
  103.     ReadData=0 ;
  104.     for(i=0;i<8;i++)
  105.     {
  106.         DELAYS10;
  107.         AD7799_SCLK=0 ;
  108.         DELAYS10;
  109.         ReadData=ReadData<<1 ;
  110.         if(AD7799_DOUT)ReadData+=1 ;
  111.         DELAYS10;
  112.         AD7799_SCLK=1 ;
  113.         DELAYS10;
  114.     }
  115.     AD7799_DOUT=1 ;
  116.     AD7799_CS=1;
  117.     return(ReadData);
  118. }
  119. void WaiteRDY(void)
  120. {
  121.     unsigned int iint ;
  122.     iint=0 ;
  123.     while(AD7799_RDY)
  124.     {
  125.         iint++;
  126.         if(iint>65530)
  127.         {
  128.             //reset ad7799
  129.             WriteByteToAd7799(0xff);
  130.             WriteByteToAd7799(0xff);
  131.             WriteByteToAd7799(0xff);
  132.             WriteByteToAd7799(0xff);
  133.             Ad7799_Ini();
  134.             break ;
  135.         }
  136.     }
  137. }
  138. void Ad7799_Ini()
  139. {
  140.     WriteByteToAd7799(0x10);
  141.     //b0001 0000
  142.     /* Writes to Communications Register Setting Next Operation as Write to CONFIGURATION Register*/
  143.     //寫通訊寄存器為:下一操作寫配置寄存器 WriteByteToAd7799(0x10)b0001 0000    0寫通訊0下操作寫010配置寄存器0關連續讀00留用
  144.     WriteByteToAd7799(0x37);//增益為128  B0011 0111   00留用1開電流源1單端  0留用111是128倍增益
  145.     WriteByteToAd7799(0x00);  //通道號 0 B0011 0000   00留用1基準默認1開緩沖 0留用000通道0
  146.     //1通道
  147.     /*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)]*/
  148.     //WriteByteToAd7799(0x08);
  149.     //b0000 1000
  150.     /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/
  151.     //WriteByteToAd7799(0x80);
  152.     //WriteByteToAd7799(0x0a);
  153.     /* Writes to Mode Register Initiating Internal Zero-Scale Calibration*/
  154.     //WaiteRDY();
  155.     /* Wait for RDY pin to go low to indicate end of calibration cycle*/
  156.     //WriteByteToAd7799(0x08);
  157.     /* Writes to Communications Register Setting Next Operation as Write to
  158.         Mode Register*/
  159.     //WriteByteToAd7799(0xa0);
  160.     //WriteByteToAd7799(0x0a);
  161.     /* Writes to Mode Register Initiating Internal Full-Scale Calibration*/
  162.     //WaiteRDY();
  163.     /* Wait for RDY pin to go low to indicate end of calibration cycle*/
  164.     WriteByteToAd7799(0x08);//b0000 1000
  165.     /* Writes to Communications Register Setting Next Operation as Write to Mode Register*/
  166.     WriteByteToAd7799(0x00);   //000連續模式0斷PSW0000留用
  167.     WriteByteToAd7799(0x09);   //0000留用0011(123Hz)1010(16.7HZ65dB)
  168.     /* 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)]*/
  169.     /*模式0 Continuous-Conversion Mode.,Fadc=16.7HZ;*/

  170. }
  171. ulong ReadAd7799ConversionData(void)
  172. {
  173.     ulong ConverData ;
  174.     unsigned char ADSAT ;
  175.     unsigned char ErrNUM=0;
  176.     WaiteRDY();              //等待數據READY
  177.     WriteByteToAd7799(0x40);  //0100 0000 配置下一操作為:讀狀態寄存器
  178.     ADSAT=ReadByteFromAd7799();   //讀出狀態 8位
  179.     while((ADSAT&0x40)||(!(ADSAT&0x08)))    //出錯或者讀寫異常
  180.     {
  181.         WriteByteToAd7799(0xff);         //復位
  182.         WriteByteToAd7799(0xff);
  183.         WriteByteToAd7799(0xff);
  184.         WriteByteToAd7799(0xff);
  185.         Ad7799_Ini();                   //初始化
  186.         WaiteRDY();                     //讀狀態
  187.         WriteByteToAd7799(0x40);        //下一操作:讀狀態
  188.         ADSAT=ReadByteFromAd7799();     //讀狀態
  189.         
  190.         ErrNUM++;
  191.         if(ErrNUM>5)return(0xffffff);//if(ErrNUM>5)break;              //連續5次都讀出錯
  192.     }
  193.    
  194.     WriteByteToAd7799(0x58);  //0101 1000 配置下一操作:讀數據寄存器,000
  195.     /* Writes to Communications Register Setting Next Operation as Continuous Read From Data Register*/
  196.     WaiteRDY();
  197.     /* Wait for RDY pin to go low to indicate end of calibration cycle*/
  198.     if(!AD7799_RDY)
  199.     {
  200.         ConverData=0 ;
  201.         ConverData=ReadByteFromAd7799();
  202.         ConverData=ConverData<<8 ;
  203.         ConverData=ReadByteFromAd7799()+ConverData;
  204.         ConverData=ConverData<<8 ;
  205.         ConverData=ReadByteFromAd7799()+ConverData;
  206.     }
  207.     /* Read Conversion Result from AD7799's Data Register*/
  208.     return(ConverData);
  209. }
  210. UCIL temp;
  211. void main(void)
  212. {
  213.    uint code *adtpoint;   //
  214.    uchar    i=0,j=0;


  215. delay(100);
  216. CPUInit();
  217. //AD7799_CS=0;
  218. delay(10);
  219.         WriteByteToAd7799(0xff);       //寫入32個高電平,復位ADC
  220.         WriteByteToAd7799(0xff);
  221.         WriteByteToAd7799(0xff);
  222.         WriteByteToAd7799(0xff);
  223.         Ad7799_Ini();
  224. while(1)
  225. {
  226.    temp.ul=ReadAd7799ConversionData();
  227.    UART0BUF=temp.uc[0];
  228.    while(!(UART0INT & 0x01));
  229.    UART0BUF=temp.uc[1];
  230.    while(!(UART0INT & 0x01));
  231.    UART0BUF=temp.uc[2];
  232.    while(!(UART0INT & 0x01));
  233.    UART0BUF=temp.uc[3];
  234.    while(!(UART0INT & 0x01));
  235. }
  236. }
復制代碼


Keil_C_AD7799.rar

38.08 KB, 下載次數: 22, 下載積分: 黑幣 -5


作者: linahaiou    時間: 2018-4-26 16:29
我用你的AD7799程序不好用呀,能幫幫我嗎,拜托了。QQ:715384732




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 日韩精品网站 | 亚洲影音先锋 | 亚洲一区二区电影在线观看 | 一区二区三区不卡视频 | 久久久久成人精品亚洲国产 | 国产小视频在线观看 | 九九色九九| 在线日韩中文字幕 | 国产精品xxxx | 91精品久久久久久久久中文字幕 | 国产成人jvid在线播放 | 久久久久亚洲 | 欧美一区二 | 亚洲欧洲日本国产 | 伊人网站| 日韩在线一区二区三区 | 欧美在线a | 男女网站在线观看 | 国产精品久久777777 | 国产一区在线免费观看视频 | 91av免费看| 日本男人天堂 | 91视频免费在观看 | 亚洲欧美一区二区三区国产精品 | 九色网址| 国产精品成人在线播放 | 噜久寡妇噜噜久久寡妇 | 亚洲欧美v | 欧美性大战xxxxx久久久 | 日韩精品一区二区在线 | 欧美日韩电影一区 | 欧美成人激情视频 | 成人免费看片 | 小视频你懂得 | 日日干日日 | 国产一区二区av | 久久久精品综合 | 夜夜操av | 中文字幕视频在线观看 | 中文字幕国产 | 久久99精品视频 |