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

📄 clock.c

📁 IO
💻 C
字号:
//Clock.c
//
//Body: HT48C50-1
//Mask option
//BZ/BZB : All Disable
//SysFreq: 400KHz
//the others use the default value

#include <ht48c50-1.h>

#define _tmr1c4 _11_4  //timer4 enable bit
#define min_adj_button  _pb0
#define hour_adj_button _pb1

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

//ISR for safequard
void isr_4(){} // external ISR
void isr_8(){} // timer/event 0

//initialize registers for safeguard
void safeguard_init(){
        _intc = 0;
        _tmr0c = 0;
        _tmr0 = 0;
        _tmr1c = 0;
        _tmr1h = 0;
        _tmr1l = 0;
        _pac = 0xff;
        _pbc = 0xff;
        _pcc = 0xff;
        _pdc = 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 char half_second;
unsigned char min_l, min_h;
unsigned char hour_l, hour_h;

void main(){

        safeguard_init();
        initial();

        while(1){
                check_time();
                show_clock();
                if (min_adj_pressed())  min_adjust();
                if (hour_adj_pressed()) hour_adjust();
        }
}

void isr_c(){           //timer1
        half_second++;
        _pb = ~_pb;     //flash 'dot' every 0.5 second
}

void initial(){
        _pac = 0;       //set port A to output port
        _pbc = 0x7f;    //set port B to input port exclude pb7
        _pb = 0;
        _pa = 0;

        min_l = 0;
        min_h = 0;
        hour_l = 0;
        hour_h = 0;
        half_second = 0;

        _intc = 0x9;    //enable timer1
        _tmr1c = 0x80;  //timer1 mode (internal clock)
        set_timer();
}

//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;
}

void check_time(){
        if (half_second >= 120){
                half_second -= 120;
                min_l++;
                if (min_l >= 10){
                        min_l = 0;
                        min_h++;
                        if (min_h >= 6){
                                min_h = 0;
                                hour_l++;
                                arrange_hour();
                        }
                }
        }
}

//This function is to arrange the hour value
void arrange_hour(){
        if (hour_h == 2 && hour_l == 4){
                hour_h = 0;
                hour_l = 0;
        }
        else if (hour_l == 10){
                hour_l = 0;
                hour_h++;
        }
}

void show_clock(){
        _pa = min_l | 0x10;
        _pa = min_h | 0x20;
        _pa = hour_l | 0x40;
        _pa = hour_h | 0x80;
}

//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:
        min_l++;
        if (min_l >= 10){
                min_l = 0;
                min_h++;
                if (min_h >= 6) //don't care hour
                        min_h = 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();
}

//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:
        hour_l++;
        arrange_hour();
        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();
}

void set_timer(){
        _tmr1c4 = 0;
        _tmr1l = 0xb0;
        _tmr1h = 0x3c;
        _tmr1c4 = 1;    //start timer1  
}

⌨️ 快捷键说明

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