找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机数码管闪动,发暗是为什么?而且打开数码管后,led也会跟着闪动

[复制链接]
跳转到指定楼层
楼主
单片机数码管闪动,发暗是为什么?而且打开数码管后,led也会跟着闪动,两个数码管全开,有些led灯会直接黑了,测了发现变黑的led灯电压变低。
这是代码,刚接触一段时间,不是很懂,网上查了资料,改了时延,都不行,大神求教
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint cyc0=1200;
uchar codevalue[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};  //êy1üê,ê0~9oíèáá
uchar codevalue_dot[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78, 0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};
uchar codecho[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};                  //
uint count_s=60;
uint tvalue;
uchar button[8]={0,0,0,0,0,0,0,0}; //启动 设置 + - 低档 高档 照明 杀菌灯
uchar Jmark[5]={0,0,0,0,0};//低档 高档 风扇 灯 杀菌灯
uchar Ledmark[5]={0,0,0,0,0};//启动 低档 高档 照明 杀菌灯
uchar num;

dis[5]=0;
bit timeOpen=0;
sbit DQ=P3^7;//温度传输引脚定义
sbit duan=P3^2;
sbit wei=P3^3;
sbit led1=P2^0;
sbit led2=P2^1;
sbit led3=P2^2;
sbit led4=P2^3;
sbit led5=P2^4;
sbit led6=P3^6;

sbit j1=P2^5;
sbit j2=P2^6;
sbit j3=P2^7;
sbit j4=P3^0;
sbit j5=P3^1;

sbit Button1=P1^0;
sbit Button2=P1^1;
sbit Button3=P1^2;
sbit Button4=P1^3;
sbit Button5=P1^4;
sbit Button6=P1^5;
sbit Button7=P1^6;
sbit Button8=P1^7;




void t_clr()
{
        TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
//TH0=0x00; //给定初值
//TL0=0x00;
        EA=1; //总中断打开
        ET0=1; //定时器中断打开
        TR0=1; //定时器开关打开
        TH0=0X3c;
        TL0=0Xaf;
}

void dataCompute(){
        dis[0]=codevalue[tvalue%1000/100];
        dis[1]=codevalue_dot[tvalue%100/10];
        dis[2]=codevalue[tvalue%10];
        if(timeOpen==1){
                dis[3]=codevalue[count_s/10];
                dis[4]=codevalue[count_s%10];
        }else{
                dis[3]=0xff;
                dis[4]=0xff;
        }
       
}
void delay500us(void)   //误差 0us
{
    unsigned char a,b;
    for(b=71;b>0;b--)
        for(a=2;a>0;a--);
}
void delay(uint z)
{
        uint x,y;
    for(x=z;x>0;x--)
      for(y=110;y>0;y--);   
}

//设置
void setMark(){

                        //低档
                if(Jmark[0]==0){
                        j1=1;
                }else{
                        j1=0;
                }
                //高档
                if(Jmark[1]==0){
                        j2=1;
                }else{
                        j2=0;
                }

                //风扇
                if(Jmark[2]==0){
                        j3=1;
                }else{
                        j3=0;
                }
                //-----------led控制-----------
                //启动
                if(Ledmark[0]==0){
                        led1=1;
                        led6=1;
                }else{
                        led1=0;
                        led6=0;
                }
                //低档
                if(Ledmark[1]==0){
                        led2=1;
                }else{
                        led2=0;
                }
                //高档
                if(Ledmark[2]==0){
                        led3=1;
                }else{
                        led3=0;
                }
       
                        //照明
                if(Jmark[3]==0){
                        j4=1;
                }else{
                        j4=0;
                }
                //杀菌灯
                if(Jmark[4]==0){
                        j5=1;
                }else{
                        j5=0;
                }
                //照明
                if(Ledmark[3]==0){
                        led4=1;
                }else{
                        led4=0;
                }
                //杀菌灯
                if(Ledmark[4]==0){
                        led5=1;
                }else{
                        led5=0;
                }
}
void display()
{
        uchar k;
        for(k=0;k<5;k++)                                //LEDê
        {
       
         wei=1;
           P0=codecho[k];
         wei=0;
         duan=1;
                P0=dis[k];
         duan=0;
                delay(10);
                wei=0;
                duan=0;
         }
       
}
void key(){
        if(!Button1){
                               
                                delay(120);
                                if(!Button1){

                                        if(button[1]==1){
                                                        button[1]=0;
                                                        Ledmark[0]=0;
                                                        Ledmark[1]=0;
                                                        Ledmark[2]=0;
                                                        Ledmark[4]=0;
                                                        Jmark[0]=0;
                                                        Jmark[1]=0;
                                                        Jmark[2]=0;
                                                        timeOpen=0;
                                                        TR0=0; //关中断 暂停倒计时
                                        }else{
                                                        button[1]=1;
                                                        Ledmark[0]=1;
                                                        Jmark[2]=1;
                                                        Jmark[0]=1;
                                                if(timeOpen==1){
                                                        TR0=1; //开中断
                                                }                                               
                                        }
                        }
                }
                        if(!Button2){ //时间设置按钮
                                       
                                delay(120);
                                if(!Button2){
                                        if(button[2]==1){        
                                                        button[2]=0;
                                                        timeOpen=0;  //时间显示部分可能需要修改
                                        }else{                                                       
                                                        button[2]=1;
                                                       
                                                        dataCompute();
                                                        if(button[1]==1){
                                                                timeOpen=1;
                                                        }
                                        }
                                }
                       
                }
                        if(!Button3){ //+
                               
                                delay(120);
                                if(!Button3){
                                        if(button[2]==1){
                                                if(count_s<99){
                                                        count_s++;
                                                }
                                                       
                                        }
                                }
                       
                }
                        if(!Button4){ //-
                               
                                delay(120);
                                if(!Button4){
                                        if(button[2]==1){
                                                        if(count_s){
                                                                count_s--;
                                                        }
                                        }
                                }
                       
                }
                        if(!Button5){
                               
                                delay(120);
                                if(!Button5){
                                        if(button[1]==1){
                                                if(button[4]==1){
                                                button[4]=0;
                                        }else{
                                                button[4]=1;
                                                Ledmark[1]=1;
                                                Ledmark[2]=0;
                                                Jmark[0]=1;
                                                Jmark[1]=0;
                                        }
                                        }
                                       
                                }
                       
                }
                        if(!Button6){
                               
                                delay(120);
                                if(!Button6){
                                        if(button[1]==1){
                                                if(button[5]==1){
                                                button[5]=0;
                                                Ledmark[1]=1;
                                                Ledmark[2]=0;
                                                Jmark[0]=1;
                                                Jmark[1]=0;
                                        }else{
                                                button[5]=1;
                                                        Ledmark[1]=0;
                                                Ledmark[2]=1;
                                                Jmark[0]=1;
                                                Jmark[1]=1;
                                        }
                                        }
                                       
                                }
                       
                }
                        if(!Button7){
                                delay(120);
                                if(!Button7){
                                        if(button[7]==1){
                                                        button[7]=0;
                                                        Jmark[3]=0;
                                                        Ledmark[3]=0;
                                        }else{
                                                        button[7]=1;
                                                        Jmark[3]=1;
                                                        Ledmark[3]=1;
                                        }
                                }
                       
                }
                        if(!Button8){
                                delay(120);
                                if(!Button8){
                                        if(button[8]==1){
                                                        button[8]=0;
                                                        Jmark[4]=0;
                                                        Ledmark[4]=0;
                                        }else{
                                                        button[8]=1;
                                                        Jmark[4]=1;
                                                        Ledmark[4]=1;
                                        }
                                }
                       
                }
        }




void init(){  //初始化
led1=1;
led2=1;
led3=1;
led4=1;
led5=1;
led6=1;

j1=1;
j2=1;
j3=1;
j4=1;
j5=1;
timeOpen=0;
count_s=60;
}
/******************************ds18203***************************************/
void delay_18B20(unsigned int i)//óê±1¢
{
        while(i--);
}

void ds1820rst()/*ds1820′*/
{  unsigned char x=0;
         DQ = 1;          //DQ′
         delay_18B20(4);  //óê±
         DQ = 0;          //DQà-μí
         delay_18B20(100); //è·óê±′óóú480us
         DQ = 1;          //à-
         delay_18B20(40);         
   }  

   uchar ds1820rd()
  { unsigned char i=0;
         unsigned char dat = 0;
         for (i=8;i>0;i--)
         {   DQ = 0; //3Do
                  dat>>=1;
                  DQ = 1; //3Do
                  if(DQ)
                  dat|=0x80;
                  delay_18B20(10);
         }
        return(dat);
  }

  void ds1820wr(uchar wdata)
  {unsigned char i=0;
    for (i=8; i>0; i--)
   { DQ = 0;
     DQ = wdata&0x01;
     delay_18B20(10);
     DQ = 1;
     wdata>>=1;
   }
}



  read_temp()
{uchar a,b;
  ds1820rst();   
  ds1820wr(0xcc);
  ds1820wr(0x44);
  ds1820rst();   
  ds1820wr(0xcc);
  ds1820wr(0xbe);
  a=ds1820rd();
  b=ds1820rd();
  tvalue=b;
  tvalue<<=8;
  tvalue=tvalue|a;
  tvalue=tvalue*(0.625);
        return(tvalue);
  }

void main()
{
                t_clr();  
                init();
   while(1)
        {
                read_temp();
                dataCompute();
                key();
                setMark();
    display();
               
               

         }
  }
void t0() interrupt 1
{       
    TR0=0;
    TH0=0X3c;
                TL0=0Xaf;
    cyc0--;
        if (cyc0==0)
                {cyc0=1200;
                count_s--;  
        if (count_s!=0 && timeOpen==1)  
            {
             TR0=1;
            }
        else if(count_s==0){
            TR0=0;
                                                init();
                                }
                }
    else
        TR0=1;
          
}



BJ1S]N_P{~W}[UN8R[7%UW5.png (60.5 KB, 下载次数: 28)

BJ1S]N_P{~W}[UN8R[7%UW5.png

IMG_20200310_213528.jpg (6.32 MB, 下载次数: 22)

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

使用道具 举报

沙发
ID:692132 发表于 2020-3-11 05:50 来自手机 | 只看该作者
电源电压跌了就是电源带负载能力弱,测电压
回复

使用道具 举报

板凳
ID:401564 发表于 2020-3-11 08:21 | 只看该作者
看不懂,因为不会
你这个是自己画的板子?如果是我,我就不会用74573,所以,我到现在还不会用这个IC
8051单片机带8个数字以下的数码是没有压力的,完全可以的,特别是STC的8051,它吸入电流大
这样一来,你只需要12个IO就可以了,省去了画板的的难度和IO
要是我的话,直接开工画个V2.0版本的,几天就可以到手了
回复

使用道具 举报

地板
ID:332444 发表于 2020-3-11 08:36 | 只看该作者
好滥的程序,去淘换点好的,逻辑思路清晰的程序.
回复

使用道具 举报

5#
ID:413157 发表于 2020-3-11 10:27 | 只看该作者
有可能是电流过小,当LED的电流小于5mA时是不会亮的,你可以计数一下,数码管和LED都会有压降。
回复

使用道具 举报

6#
ID:679425 发表于 2020-3-11 10:35 | 只看该作者
数码管和LED都会有压降
回复

使用道具 举报

7#
ID:699194 发表于 2020-3-11 10:38 | 只看该作者
实现这么小点的功效,没必要用一大堆程序,可以去优化简洁程序,其实实现你那种效果,程序就只是你的十分之一而已,你要学会优化程序
回复

使用道具 举报

8#
ID:676158 发表于 2020-3-11 12:56 | 只看该作者
学习单片机lq 发表于 2020-3-11 05:50
电源电压跌了就是电源带负载能力弱,测电压

我换了红色灯泡,就能亮,应该就是电压问题。。
回复

使用道具 举报

9#
ID:676158 发表于 2020-3-11 13:00 | 只看该作者
Y_G_G 发表于 2020-3-11 08:21
看不懂,因为不会
你这个是自己画的板子?如果是我,我就不会用74573,所以,我到现在还不会用这个IC
8051单片 ...

是的,是我自己画的。因为我还有8个键盘和6的led,引脚不够,所以才用的那个74573
回复

使用道具 举报

10#
ID:676158 发表于 2020-3-11 13:03 | 只看该作者
xianfajushi 发表于 2020-3-11 08:36
好滥的程序,去淘换点好的,逻辑思路清晰的程序.

这是我自己写的。。开发的时候需求还不是很清楚,所以我自己写了一个。。。程序有改进的地方吗?
回复

使用道具 举报

11#
ID:676158 发表于 2020-3-11 13:04 | 只看该作者
付浩东 发表于 2020-3-11 10:27
有可能是电流过小,当LED的电流小于5mA时是不会亮的,你可以计数一下,数码管和LED都会有压降。

谢谢!是这个原因。
回复

使用道具 举报

12#
ID:676158 发表于 2020-3-11 13:04 | 只看该作者
haierzaifei11 发表于 2020-3-11 10:35
数码管和LED都会有压降

谢谢!是这个原因
回复

使用道具 举报

13#
ID:676158 发表于 2020-3-11 13:05 | 只看该作者
我叫小大打 发表于 2020-3-11 10:38
实现这么小点的功效,没必要用一大堆程序,可以去优化简洁程序,其实实现你那种效果,程序就只是你的十分之 ...

怎么优化。不是很懂!求告知!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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