标题: 用51做一个0.00-99.9S的秒表 [打印本页]

作者: 51hhcc    时间: 2017-3-28 19:03
标题: 用51做一个0.00-99.9S的秒表
#include "reg52.h"                         //此文件中定义了单片机的一些特殊功能寄存器
typedef unsigned int uint;          //对数据类型进行声明定义
typedef unsigned char uchar;

uchar datas[3]={0,0,0};
uchar code smgduan[18]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};//显示0~F的值


uint sec = 0;                       
uchar i = 0;
uint count = 0;
bit flag = 0;


void Timer0Init()
{
        TMOD=0x01;//选择为定时器0 模式1,工作方式1,仅用TR0打开启动。
        TH0=(-2500)>>8;        //给定时器赋初值,定时1ms
        TL0=(-2500)&0x00ff;       
        ET0=1;//打开定时器0中断允许
        EA=1;//打开总中断
        TR0=1;//打开定时器                       
}
void jisuan()
{
   datas[0] = sec%10;
   datas[1] = sec/10%10;
   datas[2] = sec/100%10;
}
void main()
{       
        Timer0Init(); //定时器0初始化
        while(1)
        {
                if(flag)
                {
                    sec ++;
            jisuan();
                    flag = 0;
                    if(sec == 999)
                   {
                         sec = 0;
                   }
                }
        }               
}
void Timer0() interrupt 1
{       
    TH0=(-2500)>>8;        //给定时器赋初值,定时2.5ms
        TL0=(-2500)&0x00ff;       
        P0 = 0x00;
        P2 = i <<2;
    if(i == 1)         
           P0 = smgduan[datas[i]]|0x80;
    else
       P0 = smgduan[datas[i]];
        i ++;
        if(i == 3)
          i = 0;
        count ++;
        if(count == 40)//0.1S
        {
            count = 0;
            flag = 1;
        }
}

                                       






欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1