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

📄 ad.lst

📁 串口通信
💻 LST
字号:
C51 COMPILER V7.50   AD                                                                    05/22/2009 10:31:40 PAGE 1   


C51 COMPILER V7.50, COMPILATION OF MODULE AD
OBJECT MODULE PLACED IN ad.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE ad.c BROWSE DEBUG OBJECTEXTEND

line level    source

   1          #include<reg52.h>
   2          #include<stdio.h>
   3          #include<string.h>
   4          #define uchar unsigned char
   5          #define uint unsigned int
   6          sbit test=P1^2;
   7          
   8          //定义控制AD转换的寄存器
   9          //******************************
  10          sfr P1M0=0x91;
  11          sfr P1M1=0x92;
  12          sfr AD_Control=0xc5;
  13          sfr AD_Data=0xc6;
  14          //sfr ADC_Low2=0xbe;
  15          //******************************
  16          uchar advalue=0x01;
  17          uchar ad_T=0x32;
  18          uchar control=0x00;
  19          void delay1ms(uint f);
  20          void AD_init();
  21          void serial_init();
  22          
  23          
  24          //主程序
  25          void main()
  26          {
  27   1       AD_init();
  28   1       serial_init();  //初始化串口 
  29   1       //AD_Control=AD_Control | 0x08;                //启动AD,置START位
  30   1       while(1)
  31   1        {                     
  32   2         /*if((AD_Control & 0x10)==0x10)              //判断标志位FLAG
  33   2           {advalue=AD_Data;                        //读取转换数据
  34   2            AD_Control=AD_Control & 0xe7;           //将标志位清零
  35   2            AD_Control=AD_Control | 0x08;           //转换完成后,再次启动AD     
  36   2           }
  37   2         else
  38   2           test=0;*/
  39   2         }
  40   1      }
  41          
  42          //***************************
  43          //1ms延时函数
  44          void delay1ms(uint f)
  45          {
  46   1       uint i,j;
  47   1       for(i=0;i<f;i++)
  48   1       for(j=0;j<120;j++);
  49   1      }
  50          
  51          
  52          //***************************
  53          //串口初始化函数,定时器0初始化 
  54          void serial_init()
  55          {
C51 COMPILER V7.50   AD                                                                    05/22/2009 10:31:40 PAGE 2   

  56   1       SCON=0x50;               //方式1,10位异步收发,波特率由定时器控制,单机对单机,允许接收      
  57   1       PCON=0x00;               //SMOD=0,方式1、2、3的波特率不加倍
  58   1       TMOD=0x21;               //定时器1工作于方式2,自动装载方式,定时器0工作于方式1,16位加1
  59   1       TH0=0x00;
  60   1       TH1=0xb8;                //设置定时器初值,定时20ms
  61   1       TL1=0xfd;                //设置定时器装载值,来设置波特率
  62   1       TH1=0xfd;
  63   1       TR0=1;                 //启动定时器T0
  64   1       TR1=1;                  //启动定时器T1
  65   1      //TI=1;                  //有发送中断,发送中断标志
  66   1      //RI=1;                  //有接收中断,接收中断标志
  67   1       ET0=1;                 //开T0中断
  68   1       ES=1;                                   //     开串口中断
  69   1       EA=1;                   //开总中断
  70   1      }
  71          
  72          
  73          //**************************
  74          //AD初始化函数
  75          void AD_init()
  76          {P1M0=0x01;              //set the P1 working moduel
  77   1       P1M1=0x01;              //set P10 as AD convert channel   
  78   1       AD_Control=0xc0;
  79   1       //启动AD前必须确认AD电源已经打开,初次打开AD电源,需适当延时,等电源稳定后再启动AD
  80   1       //AD转换完成后,ADC_FLAG=1,需软件清零
  81   1       //ADC_START设置为1时,开始转换,转换结束后为0
  82   1       delay1ms(2);          //delay to besure the AD power is OK 
  83   1      } 
  84          
  85          
  86          //**************************
  87          //AD convert function (start ad out of this function)
  88          void adconvert()
  89          {if((AD_Control & 0x10)==0x10)              //判断标志位FLAG,if flag==1,then adcovert over
  90   1         {advalue=AD_Data;                        //读取转换数据
  91   2          SBUF=advalue;
  92   2          AD_Control=AD_Control & 0xe7;           //将标志位清零
  93   2         }
  94   1          //{test=0;}                       //when adconvert have not finished,just wait
  95   1      //AD_Control=AD_Control | 0x08;           //转换完成后,AD_start be set 0, need to restart     
  96   1      }
  97          
  98          
  99          
 100          //发送操作在TI=0时,执行SBUF=A指令后开始,然后发送电路自动在8位
 101          //发送字符前后分别添加一位起始位和停止位,发送完后,TI由硬件在
 102          //发送停止后置位
 103          //在一定条件下,向SBUF写入数据就启动了发送过程,
 104          //接收过程是自动进行的
 105          //***************************
 106          //串口中断函数
 107          void serial () interrupt 4 using 3     //串口接收发送中断程序
 108          {EA=0;       //close the main interrupt to sure this func successful 
 109   1      if(RI)       //接收完一帧数据后,硬件自动置1,如果再接收必须
 110   1        {RI=0;        //软件清零RI
 111   2         control=SBUF;
 112   2         if(control==0x01)
 113   2           test=0;
 114   2        }
 115   1      if(TI)      //发送完一帧数据后,硬件自动置1,如果再发送必须
 116   1        {TI=0;    //软件清零TI
 117   2        }          
C51 COMPILER V7.50   AD                                                                    05/22/2009 10:31:40 PAGE 3   

 118   1      EA=1;
 119   1      }
 120          
 121          
 122          void timer0() interrupt 1 using 1
 123          {EA=0;
 124   1       TH0=0x00;
 125   1       TH1=0xb8;
 126   1       ad_T=ad_T-1;
 127   1       if(ad_T==0)
 128   1         {AD_Control=AD_Control | 0x08;    //每秒钟启动AD一次
 129   2          delay1ms(1);
 130   2          adconvert();                    //wait for reading the addata
 131   2          //SBUF=advalue;                   //send the advalue through serial channel
 132   2          ad_T=0x32;
 133   2         }
 134   1       EA=1;
 135   1      }


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    176    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      3    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

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