【单片机】秒表

#include <reg52.h>
#include "./delay/delay.h"

#define TIMEPORT P0

sbit bit_select = P2^0;
sbit seg_select = P2^1;
sbit key1 = P2^2;
sbit key2 = P2^3;

code unsigned char timedata[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
code unsigned char bitdata[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned disdata[8];

unsigned char ms = 0;
unsigned char sec = 0;
unsigned char min = 0;

void timer0_init();
void timer1_init();
void display();
void change_time();

void main()
{
    change_time();    //修改时间显示值
    timer0_init();
	timer1_init();
	
	while(1)
	{
	    if(0 == key1)            //key1按下
		{
		    delay_ms(10);
			
			if(0 == key1)        //key1确实按下
			{
			    while(!key1);    //key1抬起
				delay_ms(10);
				while(!key1);    //key1确实抬起
				TR0 = ~TR0;      //计时器取反
			}
		}
		
		if(0 == key2)            //key2按下
		{
		    delay_ms(10);
			
			if(0 == key2)        //key2确实按下
			{
			    while(!key2);    //key2抬起
				delay_ms(10);
				while(!key2);    //key2确实抬起
				TR0 = 0;
				ms = 0;
				sec = 0;
				min = 0;
                change_time();   //修改时间显示值				
			}
		}
	}
}

/*
===================
功能:初始化中断0
===================
*/
void timer0_init()
{
    EA = 1;
	TMOD |= 0x01;
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;
	ET0 = 1;
	TR0 = 0;
}

/*
===================
功能:初始化中断1
===================
*/
void timer1_init()
{
    EA = 1;
	TMOD |= 0x10;
	TH1 = (65536 - 2000) / 256;
	TL1 = (65536 - 2000) % 256;
	ET1 = 1;
	TR1 = 1;
}

/*
==========================
功能:中断1服务函数,显示
==========================
*/
void timer1_isr() interrupt 3
{
	TH1 = (65536 - 2000) / 256;
	TL1 = (65536 - 2000) % 256;

    display();    
}

/*
==========================
功能:中断0服务函数,计数
==========================
*/
void timer0_isr() interrupt 1
{
    static unsigned char i = 0;
	
	TH0 = (65536 - 1000) / 256;
	TL0 = (65536 - 1000) % 256;

    i++;
	
    if(10 == i)
	{
	    i = 0;
        ms++;

        if(100 == ms)
        {
	        ms = 0;
		    sec++;
		
            if(60 == sec)
		    {
		        sec = 0;
			    min++;
			
			    if(60 == min)
			    {
			        min = 0;
			    }
		    }
		}
	}

    change_time();    //修改显示时间
}

/*
======================
功能:修改时间显示值
======================
*/
void change_time()
{
    disdata[0] = timedata[min / 10];
    disdata[1] = timedata[min % 10];
    disdata[2] = 0x40;
    disdata[3] = timedata[sec / 10];
    disdata[4] = timedata[sec % 10];
    disdata[5] = 0x40;
    disdata[6] = timedata[ms / 10];
    disdata[7] = timedata[ms % 10];  
}

/*
==========================
功能:动态扫描,显示数据
==========================
*/
void display()
{
    static unsigned char i = 0;
	
    TIMEPORT = 0x00;            //清空数据,防止重影
    seg_select = 1;
    seg_select = 0;

    TIMEPORT = 0xff;            //清空数据,防止重影
    bit_select = 1;
    bit_select = 0;

    TIMEPORT = disdata[i];      //取出显示值
    seg_select = 1;
    seg_select = 0;

    TIMEPORT = bitdata[i];      //取出位码
    bit_select = 1;
    bit_select = 0;	
	
	i++;
	if(8 == i)
	{
	    i = 0;
	}
}



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值