码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-01-10 21:38:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:pre   单片机   while   define   play   bsp   clu   isp   for   

#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

标签:pre   单片机   while   define   play   bsp   clu   isp   for   

原文地址:https://www.cnblogs.com/weimin1314/p/8260582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!