⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elecclockf1.c

📁 一款具有钟表
💻 C
字号:
//Clock.c
//
//Body: HT48R10A-1
//Mask option
//BZ/BZB : All Disable
//SysFreq: 4000KHz
//the others use the default value

#include <ht48R10A-1.h>

#define _tmrc4 _0e_4  //timer4 enable bit                             //
#define min_adj_button  _pb4
#define hour_adj_button _pb5

#pragma vector isr_4 @ 0x4
#pragma vector isr_8 @ 0x8

//ISR for safequard
void isr_4(){} // external ISR


//initialize registers for safeguard
void safeguard_init(){
        _intc = 0;
        _tmrc = 0;
        _tmr = 0;
        _pac = 0xff;
        _pbc = 0xff;
        _pcc = 0xff;
}


//void initial();
void check_time();
void show_clock();
unsigned char min_adj_pressed();
unsigned char hour_adj_pressed();
void min_adjust();
void hour_adjust();
//void arrange_hour();
void set_timer();

unsigned int count;
unsigned char half_second;
unsigned char min;
unsigned char hour;

void isr_8() // timer/event
{ 
    if(count >= 250) //0.5sec //250
	{
        half_second ++;
	    count = 0;
	}
    else
        count ++;

//	check_time();

	if (half_second >= 120)
	{
		half_second =0;
		min ++;

		if (min  >= 60)
		{
			count = 0;
			half_second =0;
			min  = 0;
			hour++;
			if(hour >= 12)
			{
				count = 0;
				half_second =0;
				min = 0;
				hour = 0;

			}
		}

	}

} 

void initial()
{
        _pac = 0;       //set port A to output port
        _pbc = 0xf0;    //set pb4~7 to input port pb0~3 output
        _pcc = 0;
		_pc = 0xff;
        _pb = 0xff;
        _pa = 0xff;

        min = 0;
        hour = 0;
        half_second = 0;
		count = 0;

        _intc = 0x5;    //enable timer
        _tmrc = 0x87;   //timer mode (internal clock)
        set_timer();
}

void main()
{
	safeguard_init();

	initial();

	while(1)
	{
//_pc = 0;
//_pb = 0xff;
//_pa = 0;
//_pb = 0xff;
//_pa0=1;
//_pc0 = 1;

		show_clock();
		if (min_adj_pressed())  min_adjust();
		if (hour_adj_pressed()) hour_adjust();
	}
}



/*
void check_time()
{

	if (half_second >= 120)
	{
		half_second =0;
		min ++;

		if (min  >= 60)
		{
			count = 0;
			half_second =0;
			min  = 0;
			hour++;
			if(hour >= 12)
			{
				count = 0;
				half_second =0;
				min = 0;
				hour = 0;

			}
		}

	}

}

*/





//check if the min_adj_button is pressed or not
//return 1: if the min_adj_button is pressed
//       0: otherwise
unsigned char min_adj_pressed(){
        if (min_adj_button == 0){//pressed
                _delay(2000); //debounce
                if (min_adj_button == 0)
                        return 1; //still pressed, recognize it
        }
        return 0;
}

//check if the hour_adj_button is pressed or not
//return 1: if the hour_adj_button is pressed
//       0: otherwise
unsigned char hour_adj_pressed(){
        if (hour_adj_button == 0){//pressed
                _delay(2000); //debounce
                if (hour_adj_button == 0)
                        return 1; //still pressed, recognize it
        }
        return 0;
}

//This function is to adjust the hour.
//The hour will increase 1 when the hour_adj_button is pressed .
//If the button is held longer than 1.5 seconds, the hour will
//increase 1 every 0.5 second
void hour_adjust(){
        bit held_long_time = 0;
        
repeat_inc:
		if(hour < 11)
			hour ++;
		else
	        hour = 0;
        half_second = 0;
        while(hour_adj_button == 0){
                show_clock();
                if (!held_long_time){
                        if (half_second>2){         //longer than 1.5 seconds
                                held_long_time = 1; //set flag
                                goto repeat_inc;    //increase hour
                        }
                        //less than 1.5 seconds, do nothing
                }
                else{
                        if (half_second)
                                goto repeat_inc; //add 1 every 0.5 second
                        //less than 0.5 second, do nothing
                }
        }
        half_second = 0;
        set_timer();
}

//This function is to adjust the minute.
//The minute will increase 1 when the min_adj_button is pressed .
//If the button is held longer than 1.5 seconds, the minute will
//increase 1 every 0.5 second
void min_adjust(){
        bit held_long_time = 0;
        
repeat_inc:
		if(min < 55)
			min += 5;
		else
	        min = 0;

        half_second = 0;
        while(min_adj_button == 0){//while min_adj_button is held
                show_clock();
                if (!held_long_time){
                        if (half_second>2){         //longer than 1.5 seconds
                                held_long_time = 1; //set flag
                                goto repeat_inc;    //increase minute
                        }
                        //less than 1.5 seconds, do nothing
                }
                else{
                        if (half_second)
                                goto repeat_inc; //add 1 every 0.5 second
                        //less than 0.5 second, do nothing
                }
        }
        half_second = 0;
        set_timer();
}


void set_timer(){
        _tmrc4 = 0;
        _tmr = 0xe1;
        _tmrc4 = 1;    //start timer1  
}

void show_clock()
{
        long int j;
    _pa = 0xff;
    _pb = 0xff;
	_pc0 = 1;
	_pc1 = 0;
    switch(hour)
	{
		case 0:
			_pa = 0xfe;
			_pb = 0xff;
			break;
		case 1:
			_pa = 0xfd;
			_pb = 0xff;
			break;
		case 2:
			_pa = 0xfb;
			_pb = 0xff;
			break;
		case 3:
			_pa = 0xf7;
			_pb = 0xff;
			break;
		case 4:
			_pa = 0xef;
			_pb = 0xff;
			break;
		case 5:
			_pa = 0xdf;
			_pb = 0xff;
			break;
		case 6:
			_pa = 0xbf;
			_pb = 0xff;
			break;
		case 7:
			_pa = 0x7f;
			_pb = 0xff;
			break;
		case 8:
			_pa = 0xff;
			_pb = 0xfe;
			break;
		case 9:
			_pa = 0xff;
			_pb = 0xfd;
			break;
		case 10:
			_pa = 0xff;
			_pb = 0xfb;
			break;
		case 11:
			_pa = 0xff;
			_pb = 0xf7;
			break;
		default:
			break;

	}
        for(j = 0x00; j < 0xd0; j++)
        {
            ;
        }


    _pa = 0xff;
    _pb = 0xff;
	_pc0 = 0;
	_pc1 = 1;
		if(min < 5)
		{
			_pa = 0xfe;
			_pb = 0xff;
		}
		else
		if(min < 10)
		{
			_pa = 0xfd;
			_pb = 0xff;
		}
		else
		if(min < 15)
		{
			_pa = 0xfb;
			_pb = 0xff;
		}
		else
		if(min < 20)
		{
			_pa = 0xf7;
			_pb = 0xff;
		}
		else
		if(min < 25)
		{
			_pa = 0xef;
			_pb = 0xff;
		}
		else
		if(min < 30)
		{
			_pa = 0xdf;
			_pb = 0xff;
		}
		else
		if(min < 35)
		{
			_pa = 0xbf;
			_pb = 0xff;
		}
		else
		if(min < 40)
		{
			_pa = 0x7f;
			_pb = 0xff;
		}
		else
		if(min < 45)
		{
			_pa = 0xff;
			_pb = 0xfe;
		}
		else
		if(min < 50)
		{
			_pa = 0xff;
			_pb = 0xfd;
		}
		else
		if(min < 55)
		{
			_pa = 0xff;
			_pb = 0xfb;
		}
		else
		{
			_pa = 0xff;
			_pb = 0xf7;
		}

        for(j = 0x00; j < 0x60; j++)
        {
            ;
        }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -