【单片机】定时炸弹倒计时

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

#define TIMEPORT P0

sbit bit_select = P2^0;
sbit seg_select = P2^1;
sbit beep = P2^2;

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 = 1;
unsigned char sec = 10;
unsigned char min = 0;

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

void main()
{
    unsigned char i;
	unsigned char cont;
    timer0_init();
	timer1_init();
	
	while(1)
	{
	    for(cont = 9; cont > 0; cont--)
		{
		    if(cont == sec && 99 == ms)
			{
			    for(i = 0; i < 200; i++)
				{
					beep = ~beep;
					delay_us(100);
				}
			}
		}
		
		if(0 == sec)
		{
		    for(i = 0; i < 200; i++)
			{
			    beep = ~beep;
				delay_us(200);
			}
		}
	}
}

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

/*
===================
功能:初始化中断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(0 == ms)
        {
	        ms = 99;
		    sec--;
		
            if(0 == sec)
		    {
		        sec = 0;
				ms = 0;
				TR0 = 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;
	}
}

 

 

  • 3
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值