找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1737|回复: 0
收起左侧

基于51单片机电压表的电路图与程序

[复制链接]
ID:967483 发表于 2021-9-22 13:13 | 显示全部楼层 |阅读模式
1.png

#include <AT89X52.H>  
unsigned char code dispbitcode[]={0xfe,0xfd,0xfb,0xf7, 0xef,0xdf,0xbf,0x7f};  //放数码管位选的值
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66, 0x6d,0x7d,0x07,0x7f,0x6f,0x00};//放数码管显示的数字
unsigned char dispbuf[8]={10,10,10,10,10,0,0,0};  //存放ad采集过来的值(只有最后3位有用)
unsigned char dispcount;
unsigned char getdata;
unsigned int temp;
long int  i;
unsigned int R1;

sbit ST=P3^0; //是否选中该芯片
sbit OE=P3^1; //所存转换的值
sbit EOC=P3^2; //当EAC数据有效时,将转换数据送出
sbit CLK=P3^3; //给adc0808提供时钟

void main(void)
{
  ST=0;
  OE=0;
  ET0=1; //启用定时器0
  ET1=1;   // 启用定时器1
  EA=1;    //开总中断
  TMOD=0x12;//设置定时器0和1的工作方式
  TH0=216;
  TL0=216; //设置定时器0的初值为216(为adc0808提供时钟)
  TH1=(65536-5000)/256;
  TL1=(65536-5000)%256;//设置定时器1的初值为5ms
  TR1=1;  //开定时器1
  TR0=1;  //开定时器0
  ST=1;
  ST=0;
  while(1)
    {
      if(EOC==1) //判断装换是否结束
        {
          OE=1;  
          getdata=P1;  //如果结束,给OE一个跳变,读出数据存入getdata
          OE=0;
                    i=getdata*196;         //256/getdata=5V/xV  >>>x=(getdata*5/256)V
                        dispbuf[5]=i/10000; //将数据存入数组中以待显示。 (个位)
                        i=i%10000;
                        dispbuf[6]=i/1000;        //小数点第一位
                        i=i%1000;
                        dispbuf[7]=i/100;   //小数点第二位      
          ST=1;
          ST=0;
        }
    }
}

void t0(void) interrupt 1 using 0           //定时器0  中断服务
{
  CLK=~CLK;  //给adc0808提供时钟
}

void t1(void) interrupt 3 using 0          //定时器1  中断服务
{
  TH1=(65536-5050)/256;
  TL1=(65536-5050)%256;
  P2=0xff;
  P0=dispcode[dispbuf[dispcount]];          //段选
  P2=dispbitcode[dispcount];                 //位选
  if(dispcount==5)         // 显示小数点
    {
      P0=P0 | 0x80;
    }
  dispcount++;          //让位选循环起来
  if(dispcount==8)
    {
      dispcount=0;
    }  
}


评分

参与人数 1黑币 +10 收起 理由
admin + 10 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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