单片机数码管显示

数码管的共阴极和共阳极:

c 图中:阴极连在一起,阳极独立,阳极置高便亮。

b 图中:阳极连在一起,用上拉电阻,使得led变化。




//下面的数组都需要在myhead.h内申明,然后在这儿定义
//注意只要在第一次使用的.c文件中定义
xdata unsigned char segdata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//段码
xdata unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位码
xdata unsigned char tempdata[8] = {0};

xdata unsigned char hour = 20;
xdata unsigned char min = 39;
xdata unsigned char sec = 45;

void display()
{
        static unsigned char i = 0;
        //1为打开,0为关闭
        PORT = 0xff;//消重影
        bit_select = 1;//bit_select在myhead中定义
        bit_select = 0;

        PORT = 0x0;
        seg_select = 1;//seg_select在myhead.h中定义
        seg_select = 0;

        PORT = tempdata[i];
        seg_select = 1;
        seg_select = 0;

        PORT = bitdata[i];
        bit_select = 1;
        bit_select = 0;

        i++;
        if(8 == i)
        {
            i = 0;
        }

}


void time0_init(void)//定时器0初始化
{
        EA = 1;//打开总开关
        TMOD |= 0x01;//设置工作方式,|= 不影响定时器1
        TH0 = (65536 - 20000) / 256;
        TL0 = (65536 - 20000) % 256;//设置定时器初值,20ms产生中断
        ET0 = 1;//打开定时器中断0
        TR0 = 1;//打开定时器0

}

void time0_isr() interrupt 1//定时器0的中断服务函数
{
        static unsigned char j = 0;

        TH0 = (65536 - 20000) / 256;
        TL0 = (65536 - 20000) % 256;

        j++;

        if(50 == j)//50是根据设置的初值得来,初值是20ms刷新一次,当j = 50 就过了一秒
        {
            j = 0;
            sec++;

            if(60 == sec)
            {
                sec = 0;
                min++;

                if(60 == min)
                {
                    min = 0;
                    hour++;

                    if(24 == hour)
                    {
                        hour = 0;
                    }
                }
            }
        }   
           /*将时间值分离成十位和各位*/
            tempdata[0] = segdata[hour / 10];
            tempdata[1] = segdata[hour % 10];
            tempdata[2] = 0x40;
            tempdata[3] = segdata[min / 10];
            tempdata[4] = segdata[min % 10];
            tempdata[5] = 0x40;
            tempdata[6] = segdata[sec / 10];
            tempdata[7] = segdata[sec % 10];        

}<pre name="code" class="html">#ifndef _DIGITAL_H
#define _DIGITAL_H

extern void display();
extern void time0_init(void);
//这里不能加interrupt 1(中断序列号)
extern void time0_isr();
extern void digital();

#endif

 
void digital()
{       
      unsigned char flag = 1;  
      unsigned char key;
        time0_init();


        while(flag)
        {  
                display();  

              key = key_scan();

              if(0xe7 == key)
                {
                      lcd_over();
                      flag = 0;
                      PORT = 0xff;                
                }

        }
}1




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值