找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 14176|回复: 7
打印 上一主题 下一主题
收起左侧

STC15w408AS_ADC10模数转换程序例子

[复制链接]
跳转到指定楼层
楼主
ID:262 发表于 2016-8-12 17:01 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
//////////////stc15MCU//////////////////////
//                ------------                     //
//  ADC--|P1.2    P1.1|--rs          //
//           --|P1.3    P1.0|--en         //
//      D4--|P1.4    P3.7|--D7         //
//      D5--|P1.5    P3.6|--D6         //
//           --|P5.4    P3.3|--D3         //
//    Vcc--|V+       P3.2|--D2         //  
//           --|P5.5    P3.1|--D1         //
//  GND--|V-        P3.0|--D0         //
//                  ------------                    //
/////////////////////////////////////////////////////////
#include"reg51.h"
#define U16 unsigned int
#define U8 unsigned char
sbit rs=P1^1;
sbit en=P1^0;
U8 dd0[]="shanxi dianzi";
U16 temp;
void delay(U16 us)
{
  while(us--);
}
///////////////////////////////////LCD1602///////////////////////////////
//   --------------------------------------------------------------------------------     //
//  |     --------------------------------------------------------------------------     |     //
//  |     |                                                                                                      |    |     //
//  |     |                                                                                                      |    |     //
//  |      -----------------------------------------------------------------------  |    |     //
//   ---------------------------------------------------------------------------------      //
//    |        |      |       |      |        |      |       |      |      |      |      |       |      |     |    |       //
//  Vss Vdd   Vo    RS   RW   EN  D0  D1   D2   D3   D4   D5   D6   D7  A   K     //
////////////////////////////////////////////////////////////////////////////////////
void w1602(bit w,U8 dat)
{
ACC=dat;
rs=w;
en=0;
P3 &=~0xcf;       //According to the LCD1602 pin  clear
P3 |=dat & 0xcf;  //For 1602 pin assignment
P1 &=~0x30;       //LCD1602 pin  clear
P1 |=dat & 0x30;  //For 1602 pin assignment
delay(10);
en=1;
delay(10);
en=0;
}
void init1602()
{
w1602(0,0x38);
w1602(0,0x06);
w1602(0,0x0c);
w1602(0,0x01);
}
void initADC()
{
  P1ASF=0x04;     //ADC_in in P1.2
ADC_RES=0;
ADC_RESL=0;
ADC_CONTR=0xE2;  // 1)ON Power; 2)speed 90 3)ADC in P1.2
delay(15);
}
void main()
{
U8 n;
init1602();
P1M1=0x04;
P1M0=0x00;
CLK_DIV &=~0x20;
while(1)
{
initADC();
 ADC_CONTR =0xea;
 delay(10);
 while(!(ADC_CONTR & 0x10));
 ADC_CONTR &=~0x10;   //clear ADC_flag
 temp =ADC_RES;
 temp =temp<<2;
 temp |=ADC_RESL;
    w1602(0,0x80);
 for(n=0;n<13;n++)
  w1602(1,dd0[n]);
 
 w1602(0,0xc0);
 w1602(1,(temp/1000+0x30));
w1602(1,((temp%1000)/100+0x30));
w1602(1,(((temp%1000)%100)/10+0x30));
w1602(1,(temp%10+0x30));  
  }
}

///不移动不闪烁的成功程序///+++++++++++++++++++++++OK
#include <REGX51.H>
#include"intrins.h"
sbit rs=P1^1;
sbit en=P1^0;
sbit D0=ACC^0;
sbit D1=ACC^1;
sbit D2=ACC^2;
sbit D3=ACC^3;
sbit D4=ACC^4;
sbit D5=ACC^5;
sbit D6=ACC^6;
sbit D7=ACC^7;
unsigned int temp,temp1;
unsigned char dd0[]=" H:0530;L:0630";
unsigned char dd1[]=" Pow L:";
void delay(unsigned int ms)
{ char i;
  while(ms--)
for(i=0;i<5;i++);
}
void w1602c(unsigned char dat)
{
  unsigned char i;
  
ACC=dat;
P30=D0;
P31=D1;
P32=D2;
P33=D3;
P14=D4;
P15=D5;
P36=D6;
P37=D7;
rs=0;
en=0;
i=20;
while(i--);
en=1;
i=20;
while(i--);
en=0;
i=20;
while(i--);
}
void w1602d(unsigned char dat)
{
  unsigned char i;
ACC=dat;
P30=D0;
P31=D1;
P32=D2;
P33=D3;
P14=D4;
P15=D5;
P36=D6;
P37=D7;
rs=1;
en=0;
i=20;
while(i--);
en=1;
i=20;
while(i--);
en=0;
i=20;
while(i--);
}
void init1602()
{
w1602c(0x38);
w1602c(0x06);
 w1602c(0x0c);
 w1602c(0x01);
}
void InitADC()
{
char n;
  P1ASF =0x04;  //P1.2 ADC_IN
  ADC_RES=0;
  ADC_RESL=0;
  ADC_CONTR =0xE2;//ON_POWER
  for(n=0;n<10;n++);                 
void disp(unsigned int dd)
{
char i;
  for(i=0;i<7;i++)  
 w1602d(dd0[i]); 
 w1602d(0x20);
 w1602d(dd/1000+0x30);
 w1602d((dd%1000)/100+0x30);
 w1602d(((dd%1000)%100)/10+0x30);
 w1602d(dd%10+0x30);
}
void main()
{
unsigned int nn;
init1602();
P1M1=0x04;
P1M0=0x00; 
CLK_DIV &=~0x20;
while(1)
{
InitADC();
    ADC_CONTR=0xea;
    nn=10;
while(nn--);
while(!(ADC_CONTR & 0x10));
ADC_CONTR &=~0x10;
temp=ADC_RES;
temp=temp<<2;
temp |=ADC_RESL;
w1602c(0x80);
for(nn=0;nn<15;nn++)
w1602d(dd0[nn]);
delay(100);
w1602c(0xc0);
disp(temp-529);
  }
}
/////////////////////////////////////////////+++++++++++++++++++++
#include <REGX51.H>
#include"intrins.h"
sbit rs=P1^1;
sbit en=P1^0;
unsigned int temp;
unsigned char dd0[]=" Pow H:";
unsigned char dd1[]=" Pow L:";
void delay(unsigned int ms)
{
   char i;
  while(ms--)
for(i=0;i<5;i++);
}
void w1602c(unsigned char dat)
{
       char i;
P3 &=~0xcf;
P3 |=(dat & 0xcf);
P1 &=~0x30;
P1 |=((dat) & 0x30);
rs=0;
en=0;
i=6;
while(i--);
en=1;
i=8;
while(i--);
en=0;
}
void w1602d(unsigned char dat)
{
        char i;
P3 &=~0xcf;
P3 |=(dat & 0xcf);
P1 &=~0x30;
P1 |=((dat) & 0x30);
rs=1;
en=0;
i=6;
while(i--);
en=1;
i=8;
while(i--);
en=0;
}
void InitADC()
{
char n;
        P1ASF =0x02;  //P10
ADC_RES=0;
ADC_RESL=0;
        ADC_CONTR =0xE2;//ON_POWER
        for(n=0;n<10;n++);                 
}     
void init1602()
{
        w1602c(0x38);
w1602c(0x06);
w1602c(0x0c);
w1602c(0x01);
}

void main()
{
  unsigned char i,nn;
  init1602(); 
  CLK_DIV &=~0x20;  
  while(1)
  { 
    InitADC();   //on exchange power
           ADC_CONTR =0xea; //start exchange
           nn=10;
           while(nn--);
           while(!(ADC_CONTR & 0x10));//wait for AD exchenge end
           ADC_CONTR &=~0x10;
           temp =ADC_RES;  //read high 8bit to temp
           temp =temp<<2;
           temp |=ADC_RESL;//read low 2bit to temp
           temp=temp-400;
           w1602c(0x80);
           for(i=0;i<7;i++) 
  w1602d(dd0[i]);     
           w1602d(temp/1000+0x30);
           w1602d((temp%1000)/100+0x30);
           w1602d(((temp%1000)%100)/10+0x30);
           w1602d(temp%10+0x30);
           delay(60000); 
           delay(60000);
           delay(60000);
           delay(60000);
           w1602c(0x01);
}
}

///////////////////////////////////////////////////////////////////////////////////
#include <REGX51.H>
#include"intrins.h"
sbit rs=P3^7;
sbit en=P5^4;

unsigned int temp;
unsigned char dd0[]=" Pow H:";
unsigned char dd1[]=" Pow L:";
void delay(unsigned int ms)
{ char i;
  while(ms--)
for(i=0;i<5;i++);
}
void w1602c(unsigned char dat) //d7 d6 d5 d4 d3 d2 d1 d0
{
  char i;
P3 &=~0x4c;                //      s7  s6   s5   s4   s3   s2   s1    s0
P3 |=(dat>>3 & 0x0c) |(dat>>1 & 0x40);
                            //     P36  P33  P32  P15  P14  P13  P12   P11
P1 &=~0x3e;                //P1:   x    x   P15  P14  P13  P12   P11   x
P1 |=((dat<<1) & 0x3e);    //P3:   x   P36   x    x    P33  P32   x    x 
rs=0;
en=0;
i=6;
while(i--);
en=1;
i=8;
while(i--);
en=0;
}
void w1602d(unsigned char dat)
{
  char i;
P3 &=~0x4c;                //      s7  s6   s5   s4   s3   s2   s1    s0
P3 |=(dat>>3 & 0x0c) |(dat>>1 & 0x40);
                            //     P36  P33  P32  P15  P14  P13  P12   P11
P1 &=~0x3e;                //P1:   x    x   P15  P14  P13  P12   P11   x
P1 |=((dat<<1) & 0x3e);    //P3:   x   P36   x    x    P33  P32   x    x 
rs=1;
en=0;
i=6;
while(i--);
en=1;
i=8;
while(i--);
en=0;
}
void InitADC()
{
char n;
  P1ASF =0x01;  //P1.0 ADC_IN
ADC_RES=0;
ADC_RESL=0;
  ADC_CONTR =0xE0;//ON_POWER
  for(n=0;n<10;n++);                 
}     
void init1602()
{
  w1602c(0x38);
w1602c(0x06);
w1602c(0x0c);
w1602c(0x01);
}

void main()
{
  unsigned char i,nn;
init1602(); 
CLK_DIV &=~0x20;  
  while(1)
  { 
InitADC();   //on exchange power
  ADC_CONTR =0xe8; //start exchange
  nn=10;
  while(nn--);
  while(!(ADC_CONTR & 0x10));//wait for AD exchenge end
ADC_CONTR &=~0x10;
  temp =ADC_RES;  //read high 8bit to temp
  temp =temp<<2;
  temp |=ADC_RESL;//read low 2bit to temp
//temp=temp-400;
  w1602c(0x80);
  for(i=0;i<7;i++) 
  w1602d(dd0[i]);     
  w1602d(temp/1000+0x30);
w1602d((temp%1000)/100+0x30);
w1602d(((temp%1000)%100)/10+0x30);
w1602d(temp%10+0x30);
for(i=0;i<5;i++)
   w1602d(0x20);
w1602c(0xc0);
  for(i=0;i<16;i++)
   w1602d(dd1[i]);
delay(60000); 
  delay(60000);
  delay(60000);
  delay(60000);
  //w1602c(0x01);
}
}
 



















分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

7#
ID:839438 发表于 2021-7-12 18:31 | 只看该作者
P1ASF =0x02;  //P10这个是不是错?0x01因该是正确?
回复

使用道具 举报

6#
ID:84765 发表于 2018-11-12 10:10 | 只看该作者

初始化都不完整当然编译不过啦!
回复

使用道具 举报

5#
ID:400811 发表于 2018-11-11 15:43 | 只看该作者
编译不通过...
回复

使用道具 举报

地板
ID:81249 发表于 2016-12-12 19:07 来自手机 | 只看该作者
好资料,学习了。
回复

使用道具 举报

板凳
ID:69481 发表于 2016-12-12 16:45 | 只看该作者
都不做注释,别看起来不好懂!
回复

使用道具 举报

沙发
ID:98767 发表于 2016-8-16 16:15 | 只看该作者
这是测量电压的吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表