找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2492|回复: 0
打印 上一主题 下一主题
收起左侧

基于单片机的定时器设计

[复制链接]
跳转到指定楼层
楼主
ID:239925 发表于 2017-10-17 08:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include < reg51.h >
#include <intrins.h>
sbit a=P1^0;
sbit b=P1^1;
sbit c=P1^2;
sbit d=P1^3;
sbit T =  P2^0;
sbit   K1 = P1^4;
sbit   K2 = P1^5;
sbit   K3 = P1^6;
sbit   K4 = P1^7;
#define GPIO_DIG   P0   
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned intLedNumVal,g,m,DisplayData[4];
void Delay10ms(unsigned int c);
void DigDisplay1();
void beep();
void system_Ini()
{
    TMOD = 0x01;
    TH0 = 0xee;
    TL0 = 0x00;
    IE = 0x8A;   
     TR0  =1;   
}
void main()
{
  unsigned char i;
  system_Ini()   ;
  LedNumVal=300;
  g=0;
  while(1)
  {
        i=0;
        DisplayData[0] = ~ DIG_CODE[LedNumVal/60/10];   
        DisplayData[1] = ~ (DIG_CODE[LedNumVal/60%10]|0x80);   
        DisplayData[2] = ~ DIG_CODE[LedNumVal%60/10];
        DisplayData[3] = ~ DIG_CODE[LedNumVal%10];
        DigDisplay1();
        if (K1==0)      
           {
                  Delay10ms(1);
                  if(K1==0)
                  {
                         TR0  = 0;
                         LedNumVal=(LedNumVal/60+1)*60+30;
                         if(LedNumVal>3601)
                            LedNumVal=30;
                         while(!K1);
                         TR0  = 1;
                  }
           }
           if(K2==0)   
           {
                  Delay10ms(1);
                  if(K2==0)
                  {
                         LedNumVal=300;
                         while(!K2);
                  }
           }
           if(K3==0)   
           {
                  Delay10ms(1);
                  if(K3==0)
                  {
                         LedNumVal=1200;
                         while(!K3);
                  }
           }
           if(K4==0)   
           {
                  Delay10ms(1);
                  if(K4==0)
                  {
                         LedNumVal=3600;
                         while(!K4);
                  }
           }
  }
}
void T0zd(void) interrupt 1      
{
     TH0 = 0xee;
     TL0 = 0x00;  
     g++;
     if(g==200)  
     {
        g=0;
        LedNumVal--;
        if(LedNumVal==-1)
            while(1)
                  beep();
     }
}
void beep()
{
       a=b=c=d=0;
       P0=~ 0x40;
       T=1;
       Delay10ms(50);
       T=0;
       Delay10ms(50);
}
void Delay10ms(unsigned int c)
{
   unsigned char a, b;
   for (;c>0;c--)
    {
       for(b=38;b>0;b--)
       {
           for(a=130;a>0;a--);
       }      
    }      
}
void DigDisplay1()
{
       unsigned int j;
       a=0,b=1,c=1,d=1;
       GPIO_DIG= DisplayData[0];  
       j= 100;         
       while(j--);   
       GPIO_DIG= 0xff;  
       a=1,b=0,c=1,d=1;
       GPIO_DIG= DisplayData[1];
       j= 100;                    
       while(j--);   
       GPIO_DIG= 0xff;
       a=1,b=1,c=0,d=1;
       GPIO_DIG= DisplayData[2];   
       j= 100;                    
       while(j--);   
       GPIO_DIG= 0xff;
       a=1,b=1,c=1,d=0;
       GPIO_DIG= DisplayData[3];   
       j= 100;                    
       while(j--);   
       GPIO_DIG= 0xff;
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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