ad

1. ad

#include   <string.h>
#include <intrins.h>                    // 加入此头文件后,可使用_nop_库函数
#define MAIN_Fosc        11059200L    //定义主时钟 
#define Main_Fosc_KHZ    (MAIN_Fosc / 1000) 
#include    "15W4KxxS4.h" 
#define Buf_Max 5    
#define  uint8    unsigned char 
#define  uint16   unsigned int 
uint8 data Rec_Buf[Buf_Max];
uint8 i = 0;  

 void  delay_ms(unsigned char ms) 
{
     unsigned int i;
         do
         {
          i = MAIN_Fosc / 13000;
                while(--i)    ;   //14T per loop
     }
         while(--ms);
}
 
void UartInit(void)        
{
     SCON = 0x50;        //8位数据,可变波特率
    AUXR |= 0x40;        //定时器1时钟为Fosc,即1T
    AUXR &= 0xFE;        //串口1选择定时器1为波特率发生器
    TMOD &= 0x0F;        //设定定时器1为16位自动重装方式
    TL1 = 0xE8;        //设定定时初值
    TH1 = 0xFF;        //设定定时初值
    ET1 = 0;        //禁止定时器1中断
    TR1 = 1;        //启动定时器1

}

 
void U1SendData(uint8 ch)
{
    SBUF = ch;                  //写数据到UART数据寄存器
        while(TI == 0);             //在停止位没有发送时,TI为0即一直等待
        TI = 0;                     //清除TI位(该位必须软件清零)
}

 
void U1SendString(uint8 *s)
{
    while (*s)                    //检测字符串结束标志
    {
        U1SendData(*s++);         //发送当前字符
    }
}
 
 
void Uart1() interrupt UART1_VECTOR using 1
{
    ES = 0;                       // 串口1中断关闭
    if (RI)                       //串行接收到停止位的中间时刻时,该位置1
  {
      RI = 0;                   //清除RI位 (该位必须软件清零)
            Rec_Buf[i] = SBUF;       //把串口1缓存SBUF寄存器数据依次存放到数组Rec_Buf中
            i++;                      
        if(i>Buf_Max)             //接收数大于定义接收数组最大个数时,覆盖接收数组之前值
                {
                    i = 0;                 
                }           
   }
   if (TI)                    //在停止位开始发送时,该位置1
   {
      TI = 0;                 //清除TI位(该位必须软件清零)
   }
     ES =  1;                   // 串口1中断打开
}



void    ADC_config(void)
{    
    ADC_CONTR|=0x80;          //开AD转换电源
    delay_ms(10);           //适当延时等待AD转换供电稳定
    P1ASF|=0x80;                  //选择P1.7作为模拟功能AD使用
    ADC_CONTR|=0x07;        //选择P1.7作为AD转换通道输入使用
    ADC_CONTR|=0x60;        //AD转换速度为90个时钟周期转换一次
    ADC_CONTR&=0xEF;        //清AD转换完成标志
    EADC=0;                 //禁止ADC转换中断
    CLK_DIV|=0x20;          //ADC转换结果ADC_RES存高2位,ADC_RESL存低8位
    ADC_CONTR|=0x08;        //启动AD转换,ADC_START=1
}

 
uint16    Get_ADC10bitResult(void)    
{
    uint16    AD_Dat=0;
  ADC_CONTR&=0xE7;              // 将ADC_FLAG清0
    //10位AD结果的高2位放ADC_RES的低2位,低8位在ADC_RESL
    AD_Dat = ADC_RES;               //将ADC_RES低2位移到应在的第9位和第10位
    AD_Dat <<= 8;
    AD_Dat|= ADC_RESL;           //将ADC_RESL的8位移到应在的低8位
    ADC_CONTR|=0x08;           //重新启动AD转换,ADC_START=1。
    return    AD_Dat;    
}


int  main(void)
{
      uint16    TempPhoto,Temp;
      uint8 strPhoto[6];
    ADC_config();                                 //ADC初始化 
    UartInit();                                   //串口1初始化     
      EA = 1;                                       //打开总中断    
    delay_ms(10);                                 //初始化后延时
    
    while (1)
    {      
        
      memset(strPhoto, 0, sizeof(strPhoto));    //strTemp数组清零    
        TempPhoto = Get_ADC10bitResult();            //实时读取P1.7通道的AD转换结果
    delay_ms(5);
        if(TempPhoto==Temp)                       //如果ADC检测结果没有变化,则不更新屏显示
        {
            ;
        }
        else                                      //如果ADC检测结果发生变化,则更新屏显示内容
        {
          Temp=TempPhoto;
            strPhoto[0] = TempPhoto/100+48;             //光强度百位      
      strPhoto[1] = (TempPhoto%100)/10+48;        //光强度十位
          strPhoto[2] = (TempPhoto%100)%10+48;        //光强度个位
          U1SendString(strPhoto);             
          U1SendString("
");                       //输出回车换行符,方便观察数据    
        }
    delay_ms(5);                              
    }
}
View Code
原文地址:https://www.cnblogs.com/https/p/9743361.html