51单片机实现定时器00H-FFH定时器000-255

Posted Velma

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51单片机实现定时器00H-FFH定时器000-255相关的知识,希望对你有一定的参考价值。

#include< reg51.h>
#define uint unsigned int
#define uchar unsigned char
sfr P0M0 = 0x94;
sfr P0M1 = 0x93;
sfr P2M0 = 0x96;
sfr P2M1 = 0x95;
uchar count;
uchar i,j,num;
uchar code leddata[]={
    0xC0,  //"0"
    0xF9,  //"1"
    0xA4,  //"2"
    0xB0,  //"3"
    0x99,  //"4"
    0x92,  //"5"
    0x82,  //"6"
    0xF8,  //"7"
    0x80,  //"8"
    0x90,  //"9"
    0x88,  //"A"
    0x83,  //"B"
    0xC6,  //"C"
    0xA1,  //"D"
    0x86,  //"E"
    0x8E,  //"F"
    0x89,  //"H"
    0xC7,  //"L"
    0xC8,  //"n"
    0xC1,  //"u"
    0x8C,  //"P"
    0xA3,  //"o"
    0xBF,  //"-"
    0xFF,  //??
};
uchar code ledwei[]={
    0x01,0x02,0x04,0x08,
    0x10,0x20,0x40,0x80   
};
void delay(uint ms){
    uint i,j;
    for(i = 0;i < ms; i++)
    for(j = 0;j < 121; j++);
}
void timer_init(){
    TMOD = 0x01;
    TH0 = 0x3c;
    TL0 = 0x0b0;
    ET0 = 1;
    EA = 1;
    TR0 = 1;            
}
void display(){       
                P2 = ~ledwei[0];
                P0 = leddata[j];
            delay(50);
            P2 = ~ledwei[1];
            P0 = leddata[i];
            delay(50);
            P2 = ~ledwei[2];
            P0 = leddata[16];
            delay(50);        
}
void main(){
    P0M0 = 0x00;
    P0M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    timer_init();       
    while(1)
    {
        display();       
    }
}
void int0() interrupt 1{
    TH0 = 0x3c;
    TL0 = 0x0b0;
    count++;
    if(count == 20){
        count = 0;
        i++;
        num++;
        if(num == 255){
            num = 0;
            i = 0;
            j = 0;
        }
        if(i == 16){
            j++;
            i = 0;
            if(j == 16){
                 j = 0;
            }
        }            
    }
}

定时器000-255

#include< reg51.h>
#define uint unsigned int
#define uchar unsigned char
sfr P0M0 = 0x94;
sfr P0M1 = 0x93;
sfr P2M0 = 0x96;
sfr P2M1 = 0x95;
uchar count;
uchar i,j,k,num;
uchar code leddata[]={
    0xC0,  //"0"
    0xF9,  //"1"
    0xA4,  //"2"
    0xB0,  //"3"
    0x99,  //"4"
    0x92,  //"5"
    0x82,  //"6"
    0xF8,  //"7"
    0x80,  //"8"
    0x90,  //"9"
    0x88,  //"A"
    0x83,  //"B"
    0xC6,  //"C"
    0xA1,  //"D"
    0x86,  //"E"
    0x8E,  //"F"
    0x89,  //"H"
    0xC7,  //"L"
    0xC8,  //"n"
    0xC1,  //"u"
    0x8C,  //"P"
    0xA3,  //"o"
    0xBF,  //"-"
    0xFF,  //??
    0xFF  //???
};
uchar code ledwei[]={
    0x01,0x02,0x04,0x08,
    0x10,0x20,0x40,0x80   
};
void delay(uint ms){
    uint i,j;
    for(i = 0;i < ms; i++)
    for(j = 0;j < 121; j++);
}
void timer_init(){
    TMOD = 0x01;
    TH0 = 0x3c;
    TL0 = 0x0b0;
    ET0 = 1;
    EA = 1;
    TR0 = 1;            
}
void display(){       
                P2 = ~ledwei[2];
                P0 = leddata[i];
            delay(50);
            P2 = ~ledwei[1];
            P0 = leddata[j];
            delay(50);
            P2 = ~ledwei[0];
            P0 = leddata[k];
            delay(50);        
}
void main(){
    P0M0 = 0x00;
    P0M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    timer_init();       
    while(1)
    {
        display();       
    }
}
void int0() interrupt 1{
    TH0 = 0x3c;
    TL0 = 0x0b0;
    count++;
    if(count == 20){
        count = 0;
        i++;
        num++;
        if(num == 255){
            num = 0;
            i = 0;
            j = 0;
            k = 0;
        }
        if(i == 10){
            j++;
            i = 0;
            if(j == 10){
                 j = 0;
                 k++;
            }
        }            
    }
}

 

以上是关于51单片机实现定时器00H-FFH定时器000-255的主要内容,如果未能解决你的问题,请参考以下文章

51单片机定时器累加计数+2位数码管(循环显示00-99)+Proteus仿真

51单片机 通过定时器实现PWM输出

51单片机实现pwm对电机调速

如何使用51单片机定时器方式1如何实现外接并入串出的移位寄存

单片机定时器问题

Proteus仿真51单片机利用定时器倒计数(99-0)