标题: VRS51L3074单片机AD7799参考代码 [打印本页]

作者: 飞碟来了    时间: 2021-4-21 20:46
标题: VRS51L3074单片机AD7799参考代码
通过,可以使用

单片机源程序如下:
  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. }
复制代码
注意:需要VRS51L3074_keil.h头文件才能编译,请从单片机官方获取

所有程序51hei提供下载:
Keil_C_AD7799.rar (38.08 KB, 下载次数: 3)


作者: 2376728423    时间: 2021-5-4 15:52
我想问下,VRS51L3074这款芯片用什么下载器下载程序?穷学生一个,有没有便宜点的下载器或者仿真器




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1