zoukankan      html  css  js  c++  java
  • pc send instructor pc ad

     1. 
    #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;  
    
    
    #define S2_S  0x00    
    #define S2RI 0x01
    #define S2TI 0x02
    #define S3RI 0x01
    #define S3TI 0x02
    #define S4RI 0x01
    #define S4TI 0x02
            
                  
    uint8 j = 0; 
    uint8 m = 0;              
    uint8 n = 0;   
    uint8 td=0;
    volatile bit Flag=FALSE;
              
    uint8  uart1temp;
     void  delay_ms(unsigned char ms) 
    {
         unsigned int i;
             do
             {
              i = MAIN_Fosc / 13000;
                    while(--i)    ;   //14T per loop
         }
             while(--ms);
    }
     
    void UartInit(void)        
    {
         P_SW1|=0x80;    //串口1在P1.6,P1.7    
        P_SW1&=0xBF; 
            SCON = 0x50;  
        S2CON = 0x50;        //8位数据,可变波特率
        S3CON = 0x10;        //8位数据,可变波特率
        S3CON &= 0x30;        //串口3选择定时器2为波特率发生器
        S4CON = 0x10;        //8位数据,可变波特率
        S4CON &= 0x30;        //串口4选择定时器2为波特率发生器
       
        AUXR |= 0x01;        //串口1选择定时器2为波特率发生器
        AUXR |= 0x04;        //定时器2时钟为Fosc,即1T
        T2L = 0xE8;        //设定定时初值
        T2H = 0xFF;        //设定定时初值
        AUXR |= 0x10;        //启动定时器2
        
    
    }
    
     
    void U1SendData(uint8 ch)
    {
        SBUF = ch;                  //写数据到UART数据寄存器
            while(TI == 0);             //在停止位没有发送时,TI为0即一直等待
            TI = 0;                     //清除TI位(该位必须软件清零)
    }
    
     
    void U1SendString(uint8 *s)
    {
        while (*s)                    //检测字符串结束标志
        {
            U1SendData(*s++);         //发送当前字符
        }
    }
     
     void SendDataByUart1(uint8 dat)
    { 
     if(!(dat^'a')|!(dat^'b')|!(dat^'c')|!(dat^'q')   ){TI=1; td= dat;   } 
        else if(td=='a')
        {
                 
            S2BUF = dat;                  
            while(!(S2CON&S2TI));         
            S2CON&=~S2TI;    
        }
        
        else if(td=='b')
        {  
            S3BUF = dat;                  
            while(!(S3CON&S3TI));         
            S3CON&=~S3TI;    
        }  
            
            else if (td=='c')
        {    
                     S4BUF = dat;                  
            while(!(S4CON&S4TI));         
                    S4CON&=~S4TI;
        }  
    }
     
     
    void USART1_Tx_Puts(void)
    {
      if(Flag)                 
        {    
            ES = 0;                                                  
        SendDataByUart1(uart1temp);        
             
           ES = 1;                                           
            Flag=FALSE;                       
      }
    } 
       /*
    void Uart1() interrupt UART1_VECTOR  
    {
        ES = 0;                        
        Flag=TRUE;                   
        if (RI )                     
            {
             RI = 0;                  
             uart1temp = SBUF;       
            }
           if (TI)                    
            {
             TI = 0;                 
            }
         ES =  1;                    
    } */
    void Uart1() interrupt UART1_VECTOR using 1
    {
        ES = 0;                       // 串口1中断关闭
        Flag=TRUE;  
    if (RI)                       //串行接收到停止位的中间时刻时,该位置1
      {
          RI = 0;                   //清除RI位 (该位必须软件清零)
                uart1temp = SBUF; 
           Rec_Buf[i] = uart1temp;       //把串口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=0x10;                  //选择P1.4作为模拟功能AD使用
        ADC_CONTR|=0x04;        //选择P1.4作为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];
                                           
                                              //打开总中断    
               P1M1 &= 0x3F;    P1M0 &= 0x3F;      //设置P1.6~P1.7为准双向口                               
      UartInit(); 
        delay_ms(10);                                 //初始化后延时
      ES = 1;  
        EA = 1; 
        while(1)
        {
             
         USART1_Tx_Puts ( ); 
              if( td=='q')
             {
                 
             break;  
             } 
        }  
    
        ADC_config(); 
        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
  • 相关阅读:
    Windows10 iis10 arr webfarm
    两个command的疑惑
    关于controller和apicontroller的跨域实现过滤器的不同
    抽象工厂
    c# 字体库跨域解决
    c# 父类的引用指向子类的实例
    垂直居中
    扇形导航
    2D变换
    京东放大镜效果
  • 原文地址:https://www.cnblogs.com/https/p/9745182.html
Copyright © 2011-2022 走看看