标题: 关于单片机数码管显示程序 如果delay的值调大一点,2和4才能能显示出来? [打印本页]

作者: wangliqi23    时间: 2022-1-24 19:06
标题: 关于单片机数码管显示程序 如果delay的值调大一点,2和4才能能显示出来?
这个地方数码管显示,如果delay的值调大一点,2和4就能显示出来,如果delay调小,2和4就不会显示,只显示0135,不知道哪里有问题


单片机源程序如下:
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};//数码管段选
uchar code wela[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//数码管位选
sbit d1=P1^0;
sbit beep=P2^3;
sbit switch1=P2^6;//段选LE
sbit switch2=P2^7;//位选LE
uchar x,tt;
void delay(uint z)//unit是宏定义
{
        uint x;
        for(x=z;x>0;x--);
            
}
void display()
{
        uchar x,y;
        switch1=0;
        switch2=1;
        for(x=0;x<6;x++)
        {
                P0=wela[x];
                switch2=0;
                switch1=1;
                P0=table[x];
                switch1=0;
                delay(95);
                switch2=1;
        }
        
        
}
void main()
{ tt=0;
        TMOD=0x01;//设置定时器0工作方式
  TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        EA=1;//总中断
        ET0=1;//
        TR0=1;//启动定时器
        beep=0;
        while(1)
        {
                display();
        }
        
}



作者: wulin    时间: 2022-1-24 20:40

  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. //uchar code table[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90};//数码管段选
  6. uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴数码管段码"0~9"
  7. uchar code wela[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//数码管位选
  8. //sbit d1=P1^0;
  9. //sbit beep=P2^3;
  10. sbit switch1=P2^6;//段选LE
  11. sbit switch2=P2^7;//位选LE
  12. uchar x;//tt;
  13. /*
  14. void delay(uint z)//unit是宏定义
  15. {
  16.         uint x;
  17.         for(x=z;x>0;x--);      
  18. }*/

  19. void display()
  20. {
  21.         P0=0x00;
  22.         switch1=1;
  23.         switch1=0;

  24.         P0=wela[x];
  25.         switch2=1;
  26.         switch2=0;

  27.         P0=table[x];
  28.         switch1=1;
  29.         switch1=0;

  30.         if(++x>=6)
  31.                 x=0;
  32. }

  33. void main()
  34. {
  35. //        tt=0;
  36.         TMOD=0x01;//设置定时器0工作方式
  37.         TH0=(65536-2000)/256;//2ms
  38.         TL0=(65536-2000)%256;
  39. //        EA=1;//总中断
  40. //        ET0=1;//
  41.         TF0=0;
  42.         TR0=1;//启动定时器
  43. //        beep=0;
  44.         while(1)
  45.         {
  46.                 if(TF0)//查询方式
  47.                 {
  48.                         TF0=0;
  49.                         TH0=(65536-2000)/256;
  50.                         TL0=(65536-2000)%256;
  51.                         display();
  52.                 }
  53.         }
  54. }
复制代码




作者: wangliqi23    时间: 2022-1-24 21:46
wulin 发表于 2022-1-24 20:40

谢谢





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1