找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1991|回复: 2
收起左侧

用定时器定时1秒,在P0口接数码管(BCD数码管),每秒显示的数字加一

[复制链接]
ID:162360 发表于 2017-1-24 23:03 | 显示全部楼层 |阅读模式
用定时器定时1秒,在P0口接数码管(BCD数码管),每秒显示的数字加一,1,依次显示12345678,       求程序编写
回复

使用道具 举报

ID:158729 发表于 2017-1-25 10:35 | 显示全部楼层
#include <reg52.h>
#define uchar unsigned char
#define uint  unsigned int
sbit we = P2^7;
sbit du = P2^6;
uchar counter,time_counter;
uchar code leddata[]={

                0x3F,  //"0"
                0x06,  //"1"
                0x5B,  //"2"
                0x4F,  //"3"
                0x66,  //"4"
                0x6D,  //"5"
                0x7D,  //"6"
                0x07,  //"7"
                0x7F,  //"8"
                0x6F,  //"9"
                0x77,  //"A"
                0x7C,  //"B"
                0x39,  //"C"
                0x5E,  //"D"
                0x79,  //"E"
                0x71,  //"F"
                0x76,  //"H"
                0x38,  //"L"
                0x37,  //"n"
                0x3E,  //"u"
                0x73,  //"P"
                0x5C,  //"o"
                0x40,  //"-"
                0x00,  //熄灭
                0x00  //自定义

                         };

void delay(uint z)
{
        uint x,y;
        for(x = z; x > 0; x--)
                for(y = 114; y > 0 ; y--);
}

void display(uchar i)
{
        P0 = 0xff;
        we = 1;
        P0 = 0xfe;//点亮第一位数码管
        we = 0;

        du = 1;
        P0 = leddata[i];
        du = 0;
        delay(1);       
}

void main()
{
        TMOD = 0x01;//定时器0 工作模式1 16模式定时器
        TH0 = (65536 - 46082)/256;
        TL0 = (65536 - 46082)%256; //定时50ms
        TR0 = 1;//启动定时器0
        while(1)
        {
                if(TF0 == 1)
                {
                        TF0 = 0;
                        TH0 = 0x4b;
                        TL0 = 0xfe; //定时50ms
                        counter++;
                }
                if(counter == 20)
                {
                        counter = 0;
                        time_counter++;
                }
                if(time_counter == 9)
                        time_counter = 0;
                display(time_counter);       
        }
}
回复

使用道具 举报

ID:162360 发表于 2017-1-25 15:26 | 显示全部楼层
nessus 发表于 2017-1-25 10:35
#include
#define uchar unsigned char
#define uint  unsigned int

谢谢   但是我想让它在一位数码管上实现,用不来定时器,您能帮我修改下吗,这是我的程序
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code seg7ca[]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
      };   //0  1  2   3   4    5    6    7    8     9
void delay(uint t)
{
  uint i,j;
  for(i=0;i<t;i++)
  {
   for(j=0;j<124;j++);
  }
}
void main()
{
  while(1)
  {
    uchar i=0;
        for(i=0;i<10;i++)
        {
          P0=seg7ca;
          delay(1000);
        }

  }
}
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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