微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 各位来围观。。。大侠们 制作精确秒表

各位来围观。。。大侠们 制作精确秒表

时间:10-02 整理:3721RD 点击:
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
//sbit key1=P2^3;
//sbit key2=P2^2;
sbit key3=P2^1;
sbit key4=P2^0;
sbit EC=P1^2;
sbit EB=P1^1;
sbit ENA=P1^0;
uchar THR0=0x00;
uchar TLR0=0x00;
//uchar KeySta[4]={1,1,1,1};
uint watchrefresh=1;
uint watchrunning=1;
unsigned long integral=0;
unsigned long decimal=0;
uchar ledbuff[8]={0x00,0x00,0x00,0x00,0x00,0x00};
uchar ledchar[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
void Keydrive();
void Watchdisplay();
void ConfigTimer(uint ms);
void WatchReset();
void WatchFreash();
void main()
{
    ConfigTimer(2);
    P2=0xff;
        while(1)
        {
            if(watchrefresh)
                {
                        Watchdisplay();
                        watchrefresh=0;
                }
            
        }
}
void Keydrive()
{
    static uchar keybuff[2]={0xff,0xff};
        keybuff[0]=        (keybuff[0]<<1)|key3;
        keybuff[1]=        (keybuff[1]<<1)|key4;
           if(keybuff[0]==0xf0)
        {
           WatchReset();
        }
        if(keybuff[1]==0xf0)
        {
           WatchFreash();
        }
                 
}
void LedScan()
{
        static uint i=0;
           P0=0x00;
        switch(i)
          {
             case 0:EC=1;EB=1;ENA=1;i++;P0=ledbuff[0];break;
                 case 1:EC=1;EB=1;ENA=0;i++;P0=ledbuff[1];break;
                 case 2:EC=1;EB=0;ENA=1;i++;P0=ledbuff[2];break;
                 case 3:EC=1;EB=0;ENA=0;i++;P0=ledbuff[3];break;
                 case 4:EC=0;EB=1;ENA=1;i++;P0=ledbuff[4];break;
                 case 5:EC=0;EB=1;ENA=0;i++;P0=ledbuff[5];break;
                 case 6:EC=0;EB=0;ENA=1;i++;P0=ledbuff[6];break;
                 case 7:EC=0;EB=0;ENA=0;i=0;P0=ledbuff[7];break;
                 default:break;
                 }
}
void Watchdisplay()
{
    uint i=0;
        static uint buf[8]={0,0,0,0,0,0,0,0};
        ledbuff[0]=        ledchar[decimal%10];
        ledbuff[1]= ledchar[decimal/10];
        for(i=0;i<5;i++)
        {
            buf=integral%10;
                integral=integral/10;
        }
        for(i=4;i>=1;i--) //i++改为i--
        {
           if(buf==0)
           {
              ledbuff[i+3]=0x00;
           }
           else
             break;
        }
   for(;i>=1;i--)
   {
       ledbuff[i+3]=ledchar[buf];
   }
   ledbuff[2]=0x01;
   ledbuff[3]=ledchar[buf[0]];
}
void ConfigTimer(uint ms)
{
    unsigned long temp;
        temp=11059200/12;
        temp=(temp*ms)/1000;
        temp=65535-temp;
        temp+=12;
        THR0=(uchar)(temp>>8);
        TLR0=(uchar)temp;
        TMOD=TMOD&0xf0;
        TMOD=TMOD|0x01;
        TH0=THR0;
        TL0=TLR0;
        TR0=1;
        ET0=1;
        EA=1;
}
void WatchReset()
{
   watchrunning=0;
    decimal=0;
        integral=0;
        watchrefresh=1;  
}
void WatchFreash()
{
   if(watchrunning)        //暂定,连续两次刷新
     watchrunning=0;
        else
         watchrunning=1;
}
void WatchCount()
{
    if(watchrunning)
        {
            decimal++;
                if(decimal>=99)//为什么decimal变化到99了integral这个变量不增加啊。(数码管上不显示)
                {
                    decimal=0;
                        integral++;
                if(integral>=100000)
                {
                   integral=0;
                }
                }
                watchrefresh=1;         
        }        
}
void timer() interrupt 1
{
    static uint timer=0;
           TH0=THR0;
        TL0=TLR0;
        timer++;
        LedScan();
    Keydrive();
        if(timer>=5)
        {
           timer=0;
          WatchCount();
        }
}




为什么没人来拯救我啊

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top