STC15W408AS0-10V电压测量

 /****LCD1602 *********电压表测量0-1024PAST*******/
/*************2018 12  28 CODE1844 L283**********/
              #include                  //按键加减输入数字
              #include                //输出0-10电压
              #define       uint unsigned int  
              #define       uchar unsigned char
              sfr           P1ASF=0x9D;          //P1口第2功能控制寄存器
              #define       ADC_SPEEDLL 0x00     //540个时钟  
              #define       Lcd_Bus P3
              sbit          RS=P5^4;             //LCD PIN4
              sbit          RW=P1^7;             //LCD PIN5
              sbit          E=P1^6;              //LCD PIN6
              sbit          OUT1=P1^5;
              float         ADvalue;             //存放AD转换返回的结果 
              uint          val;
              uint          aa=0;                                      /****RUN FLAG****/
              uchar         code disdt[10]={0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39};
/*------------------延时子程序------------------------*/
              void delay(uint ms)
              {
              uint i,j;
              for(i=0;i               for(j=0;j<2000;j++)
              ;
              }    
/*------------------延时子程序------------------------*/
               void delay_1ms(uint x)
               {
               int i, j;
               for (i=0; i                for (j=0; j<1500; j++);
               }
/*********************************/
               void chk_busy()
              {
               RS=0;
               RW=1;
               E=1;
               Lcd_Bus=0xff;
               while((Lcd_Bus&0x80)==0x80);
               E=0;
               }
/*------------------写命令到LCD-----------------------*/
               void write_com(uchar cmdcode)
               {
                chk_busy();
                RS=0;
                RW=0;
                E=1;
                Lcd_Bus=cmdcode;
                delay(5);                        /*在数据写入的时候加入适当的延时*/
                E=0;
                delay(5);
               }
/*-------------------写数据到LCD----------------------*/
               void write_data(uchar Dispdata)
               {
                chk_busy();
                RS=1;
                RW=0;
                E=1;
                Lcd_Bus=Dispdata;
                delay(5);                        /*在数据写入的时候加入适当的延时 */
                E=0;
                delay(5);
               }
/*------------------初始化LCD屏-----------------------*/
               void lcd_init()
               {
                write_com(0X38);                 /**/
                delay_1ms(100);
                write_com(0X0E);
                delay_1ms(100);
                write_com(0X06);
               }
/*************************************/
               void dis_play1(uint val)           /*显示A函数*/
               {
                uint  Data_a,Data_b,Data_c,Data_d;
                Data_a=val/1000;
                Data_b=(val-Data_a*1000)/100;   
                Data_c=(val-Data_a*1000-Data_b*100)/10;
                Data_d=val-Data_a*1000-Data_b*100-Data_c*10;   
                write_com(0X80);                 /*地址*/
                write_data(0X50);                /*P*/
                write_com(0X81);                 /*地址*/
                write_data(0X31);                /*1*/
                write_com(0X82);                 /*地址*/
                write_data(0X3A);                /*1*/                                  
                write_com(0X83);                 /*地址*/
                write_data(disdt[Data_b]);       /*十位*/
                write_com(0X84);                 /*地址*/
                write_data(disdt[Data_c]);       /*个位*/
                write_com(0X85);                 /*地址*/
                write_data(0X2E);                /*.*/
                write_com(0X86);                 /*地址*/
                write_data(disdt[Data_d]);       /*个位*/
                write_com(0X87);                 /*地址*/
                write_data(0X56);                /*V*/
               }  
/*************************************/
               void dis_play2(uint val)           /*显示A函数*/
               {
                uint  Data_a,Data_b,Data_c,Data_d;
                Data_a=val/1000;
                Data_b=(val-Data_a*1000)/100;   
                Data_c=(val-Data_a*1000-Data_b*100)/10;
                Data_d=val-Data_a*1000-Data_b*100-Data_c*10;   
                write_com(0X88);                 /*地址*/
                write_data(0X53);                /*S*/
                write_com(0X89);                 /*地址*/
                write_data(0X31);                /*1*/
                write_com(0X8A);                 /*地址*/
                write_data(0X3A);                /*1*/                                  
                write_com(0X8B);                 /*地址*/
                write_data(disdt[Data_b]);       /*十位*/
                write_com(0X8C);                 /*地址*/
                write_data(disdt[Data_c]);       /*个位*/
                write_com(0X8D);                 /*地址*/
                write_data(0X2E);                /*.*/
                write_com(0X8E);                 /*地址*/
                write_data(disdt[Data_d]);       /*个位*/
                write_com(0X8F);                 /*地址*/
                write_data(0X56);                /*V*/
               }        
/*************************************/
               void dis_play3(uint val)           /*显示A函数*/
               {
                uint  Data_a,Data_b,Data_c,Data_d;
                Data_a=val/1000;
                Data_b=(val-Data_a*1000)/100;   
                Data_c=(val-Data_a*1000-Data_b*100)/10;
                Data_d=val-Data_a*1000-Data_b*100-Data_c*10;   
                write_com(0XC0);                 /*地址*/
                write_data(0X50);                /*P*/
                write_com(0XC1);                 /*地址*/
                write_data(0X32);                /*2*/
                write_com(0XC2);                 /*地址*/
                write_data(0X3A);                /*1*/                                  
                write_com(0XC3);                 /*地址*/
                write_data(disdt[Data_b]);       /*十位*/
                write_com(0XC4);                 /*地址*/
                write_data(disdt[Data_c]);       /*个位*/
                write_com(0XC5);                 /*地址*/
                write_data(0X2E);                /*.*/
                write_com(0XC6);                 /*地址*/
                write_data(disdt[Data_d]);       /*个位*/
                write_com(0XC7);                 /*地址*/
                write_data(0X56);                /*V*/
               }  
/*************************************/
               void dis_play4(uint val)           /*显示A函数*/
               {
                uint  Data_a,Data_b,Data_c,Data_d;
                Data_a=val/1000;
                Data_b=(val-Data_a*1000)/100;   
                Data_c=(val-Data_a*1000-Data_b*100)/10;
                Data_d=val-Data_a*1000-Data_b*100-Data_c*10;   
                write_com(0XC8);                 /*地址*/
                write_data(0X53);                /*S*/
                write_com(0XC9);                 /*地址*/
                write_data(0X32);                /*2*/
                write_com(0XCA);                 /*地址*/
                write_data(0X3A);                /*1*/                                  
                write_com(0XCB);                 /*地址*/
                write_data(disdt[Data_b]);       /*十位*/
                write_com(0XCC);                 /*地址*/
                write_data(disdt[Data_c]);       /*个位*/
                write_com(0XCD);                 /*地址*/
                write_data(0X2E);                /*.*/
                write_com(0XCE);                 /*地址*/
                write_data(disdt[Data_d]);       /*个位*/
                write_com(0XCF);                 /*地址*/
                write_data(0X56);                /*V*/
               }      
/*****************************************/
              unsigned int ADC_P10()             //
              {
               uint i;
               uchar status=0;                   //AD转换结束标志
               uint  AD_Dat=0;                   //10位AD转换值
               ADC_CONTR|=0X80;                  //打开ADC转换电源,第一次使用时要打开内部电源
               for(i=0;i<10000;i++)              //等待电源稳定1ms
               P1ASF|=0X00;                      //0000 0000
               ADC_CONTR=0XE0;                      //1110 0000    通道P1.0
               for(i=0;i<1000;i++)               //如果多通道测量,更换通道后延时200us
               ADC_CONTR|=0X08;                  //启动AD转换  0000 1000ADC_START
                while(status==0)                 //等待AD转换结束
                {
                status=ADC_CONTR&0X10;           //判断ADC_FLAG是否等于1,
                }
                ADC_CONTR&=0XE7;                 //将ADC_FLAG清零
                AD_Dat=(ADC_RES<<2)|(ADC_RESL&0X03);    //
                return AD_Dat;                   //
               }      
/*****************************************/
              unsigned int ADC_P11()             //
              {
               uint i;
               uchar status=0;                   //AD转换结束标志
               uint  AD_Dat=0;                   //10位AD转换值
               ADC_CONTR|=0X80;                  //打开ADC转换电源,第一次使用时要打开内部电源
               for(i=0;i<10000;i++)              //等待电源稳定1ms
               P1ASF|=0X02;                      //0000 0010
               ADC_CONTR=0XE1;                      //1110 0001
               for(i=0;i<1000;i++)               //如果多通道测量,更换通道后延时200us
               ADC_CONTR|=0X08;                  //启动AD转换
                while(status==0)                 //等待AD转换结束
                {
                status=ADC_CONTR&0X10;           //判断ADC_FLAG是否等于1,
                }
                ADC_CONTR&=0XE7;                 //将ADC_FLAG清零
                AD_Dat=(ADC_RES<<2)|(ADC_RESL&0X03);    //
                return AD_Dat;                   //
               }
/*****************************************/
              unsigned int ADC_P13()             //
              {
               uint i;
               uchar status=0;                   //AD转换结束标志
               uint  AD_Dat=0;                   //10位AD转换值
               ADC_CONTR|=0X80;                  //打开ADC转换电源,第一次使用时要打开内部电源
               for(i=0;i<10000;i++)              //等待电源稳定1ms
               P1ASF|=0X08;                      //0000 1000
               ADC_CONTR=0XE3;                      //1110 0011   P1.3
               for(i=0;i<1000;i++)               //如果多通道测量,更换通道后延时200us
               ADC_CONTR|=0X08;                  //启动AD转换
                while(status==0)                 //等待AD转换结束
                {
                status=ADC_CONTR&0X10;           //判断ADC_FLAG是否等于1,
                }
                ADC_CONTR&=0XE7;                 //将ADC_FLAG清零
                AD_Dat=(ADC_RES<<2)|(ADC_RESL&0X03);    //
                return AD_Dat;                   //
               }
/*****************************************/
              unsigned int ADC_P14()             //
              {
               uint i;
               uchar status=0;                   //AD转换结束标志
               uint  AD_Dat=0;                   //10位AD转换值
               ADC_CONTR|=0X80;                  //打开ADC转换电源,第一次使用时要打开内部电源
               for(i=0;i<10000;i++)              //等待电源稳定1ms
               P1ASF|=0X10;                      //0001 0000
               ADC_CONTR=0XE4;                      //1110 0100
               for(i=0;i<1000;i++)               //如果多通道测量,更换通道后延时200us
               ADC_CONTR|=0X08;                  //启动AD转换
                while(status==0)                 //等待AD转换结束
                {
                status=ADC_CONTR&0X10;           //判断ADC_FLAG是否等于1,
                }
                ADC_CONTR&=0XE7;                 //将ADC_FLAG清零
                AD_Dat=(ADC_RES<<2)|(ADC_RESL&0X03);    //
                return AD_Dat;                   //
               }
/*------------------主循环程序----------------*/      
               void   main( )                    /*主程序开始*/
               {   
               P1M0=0X00;
               P1M1=0X1B;
               P5M0=0X00;
               P5M1=0X00;
               OUT1=0;
               delay(500);
               OUT1=1;
               lcd_init() ;                      /*液晶复位*/
               while(1)
                {
                OUT1=~OUT1;
                ADvalue=ADC_P11()/9;             //采样P1.1口模拟输入电压  9根据输入电压,芯片电压测试得出
                dis_play1(ADvalue);
                delay(10);                         //1023/12  V输入
                ADvalue=ADC_P10()/9;             //采样P1.1口模拟输入电压  9根据输入电压,芯片电压测试得出
                dis_play2(ADvalue);
                delay(10);
                ADvalue=ADC_P13()/9;             //采样P1.1口模拟输入电压  9根据输入电压,芯片电压测试得出
                dis_play3(ADvalue);
                delay(10);
                ADvalue=ADC_P14()/9;             //采样P1.1口模拟输入电压  9根据输入电压,芯片电压测试得出
                dis_play4(ADvalue);
                delay(10);
                }
               }                                 //主循环

你可能感兴趣的:(STC15W408AS0-10V电压测量)