找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2493|回復(fù): 1
收起左側(cè)

VRS51L3074單片機(jī)AD7799參考代碼

[復(fù)制鏈接]
ID:330976 發(fā)表于 2021-4-21 20:46 | 顯示全部樓層 |閱讀模式
通過,可以使用

單片機(jī)源程序如下:
  1. #pragma CODE DEBUG SYMBOLS OBJECTEXTEND     //SRC 當(dāng)與匯編混合編譯時(shí)需要
  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{     //無符號(hào)字符型,整形,長(zhǎng)整形數(shù)據(jù)聯(lián)合體
  8.               uchar uc[2];
  9.               uint  ui;
  10.              }UCI;
  11. typedef union{     //無符號(hào)字符型,整形,長(zhǎng)整形數(shù)據(jù)聯(lián)合體
  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  同上是一個(gè)引腳
  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后,內(nèi)部自動(dòng)上拉到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  算術(shù)單元、IO口使能*/
  57.   P0=P1=P2=P3=P6=0x00; P4=0x00; P5=0x04;
  58.   AD7799_CS=1;
  59.     UART0CFG=0x90;  // 0x09:38400 BRADJ=0x02(19200)/波特率發(fā)生器使用內(nèi)部晶振/9th不使用/1位停止位
  60.     UART0INT=0x00;  //使能接收中斷,使能發(fā)送完成中斷,允許接收 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寫寄存器函數(shù)
  78.     WriteData:要寫的數(shù)據(jù)*/
  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讀寄存器函數(shù)*/
  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關(guān)連續(xù)讀00留用
  144.     WriteByteToAd7799(0x37);//增益為128  B0011 0111   00留用1開電流源1單端  0留用111是128倍增益
  145.     WriteByteToAd7799(0x00);  //通道號(hào) 0 B0011 0000   00留用1基準(zhǔn)默認(rèn)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連續(xù)模式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.,F(xiàn)adc=16.7HZ;*/

  170. }
  171. ulong ReadAd7799ConversionData(void)
  172. {
  173.     ulong ConverData ;
  174.     unsigned char ADSAT ;
  175.     unsigned char ErrNUM=0;
  176.     WaiteRDY();              //等待數(shù)據(jù)READY
  177.     WriteByteToAd7799(0x40);  //0100 0000 配置下一操作為:讀狀態(tài)寄存器
  178.     ADSAT=ReadByteFromAd7799();   //讀出狀態(tài) 8位
  179.     while((ADSAT&0x40)||(!(ADSAT&0x08)))    //出錯(cuò)或者讀寫異常
  180.     {
  181.         WriteByteToAd7799(0xff);         //復(fù)位
  182.         WriteByteToAd7799(0xff);
  183.         WriteByteToAd7799(0xff);
  184.         WriteByteToAd7799(0xff);
  185.         Ad7799_Ini();                   //初始化
  186.         WaiteRDY();                     //讀狀態(tài)
  187.         WriteByteToAd7799(0x40);        //下一操作:讀狀態(tài)
  188.         ADSAT=ReadByteFromAd7799();     //讀狀態(tài)
  189.         
  190.         ErrNUM++;
  191.         if(ErrNUM>5)return(0xffffff);//if(ErrNUM>5)break;              //連續(xù)5次都讀出錯(cuò)
  192.     }
  193.    
  194.     WriteByteToAd7799(0x58);  //0101 1000 配置下一操作:讀數(shù)據(jù)寄存器,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個(gè)高電平,復(fù)位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. }
復(fù)制代碼
注意:需要VRS51L3074_keil.h頭文件才能編譯,請(qǐng)從單片機(jī)官方獲取

所有程序51hei提供下載:
Keil_C_AD7799.rar (38.08 KB, 下載次數(shù): 5)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:915548 發(fā)表于 2021-5-4 15:52 | 顯示全部樓層
我想問下,VRS51L3074這款芯片用什么下載器下載程序?窮學(xué)生一個(gè),有沒有便宜點(diǎn)的下載器或者仿真器
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表