单片机计数器定时器实验

img


参考上图程序并修改,实现两位显示的秒表,秒表时间范围为100秒(00-99),要求采用数码管的最右边两位数码管显示,采用定时器T0编写程序。

数码管的最右边两位数码管显示,数码管是几位的?位引脚是怎么接的?给出电路图才好写代码,代码依据电路写的。假设是8位数码管位引脚接P1,则修改代码

# include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uchar n=0,shiw=0,gew=0;
uchar code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void main()
 {
uchar y=0;
TMOD|=0x01;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-15536)/256;//3cb0=15536
TL0=(65536-15536)%256;  
while(1)
{
P0=~smgduan[gew];
P1=0x01;
while(++y);
P0=~smgduan[shiw];
P1=0x02;
while(++y);
P0=P1=0x00;//P0=P1=0xff;
}
}
void T0_time() interrupt 1   
{
uchar i;
TH0=(65536-15536)/256;
TL0=(65536-15536)%256;
i++;
if(i==20)
{
i=0;
P0=~smgduan[n++];
if(n==100)n=0;
shiw=n/10;
gew=n%10; 
}
}

看看https://blog.csdn.net/weixin_39531378/article/details/111114910


 
#include<stc89c5xrc.h>
volatile int i,j,k,l;
volatile unsigned char a;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
 
void main()
{       
 
        EA=1;
        ET0=1;
 
        TMOD=0xA1;
        TL0=(65535-50000)%256;
        TH0=(65535-50000)/256;
        IE0=134;
        TF0=0;
        TR0=1;
 
       
        k=0;
        l=0;
        a=0;       
        while(1)
        {
                P0=1;
                P1=table[l];
                P0=2;
                P1=table[k];
        }
}
void Timer0Int(void) interrupt 1
{
    TR0=0;
          TL0=(65535-50000)%256;
          TH0=(65535-50000)/256;
                k=a/10;
                l=a%10;
                a++;
                TR0=1;
}