【单片机】51单片机时钟——利用中断

#include 
#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;
    }
}

delay.c
void delay_us(unsigned char t)
{
    while (--t);
}

void delay_ms(unsigned char t)
{
    while (t--)
    {
	delay_us(245);
	delay_us(245);
    }
}

void delay_s(unsigned char t)
{
    while (t--)
    {
        delay_ms(200);
	delay_ms(200);
	delay_ms(200);
	delay_ms(200);
	delay_ms(200);
    }
}


delay.h
#ifndef _DELAY_H_
#define _DELAY_H_

extern void delay_us(unsigned char t);

extern void delay_ms(unsigned char t);

extern void delay_s(unsigned char t);

#endif


你可能感兴趣的:(单片机)