/******************************************** 包含头文件 ********************************************/ #include #include /* 重定义关键词 */ #define uint unsigned int #define uchar unsigned char /* 全局变量声明 */ uchar hours; //时钟时间变量 uchar mins; uchar seconds; uchar target_hours; //预定时时间变量 uchar target_mins; uchar target_seconds; uchar countdown_hours; //定时时间变量 uchar countdown_mins; uchar countdown_seconds; uchar temp_hours; //临时时间变量,用于和定时时间比较,判断是否到达定时时间 uchar temp_mins; uchar temp_seconds; uchar flag = 0; //定时时间到标志 uchar num = 0; //定时器0计数次数 long sum = 0; //定时器1计数次数 /* PIN口定义 */ sbit button1 = P1^0; //确认键 sbit button2 = P1^1; //时分秒选择键 sbit button3 = P1^2; //加一操作 sbit ledy = P1^6; //黄灯 sbit ledr = P1^7; //红灯及蜂鸣器 /* 共阳极数码管字符显示编码表 */ uchar code table1[] = {0xc0,0xf9,0xa4,0xb0,0x99, //0,1,2,3,4 0x92,0x82,0xf8,0x80,0x90}; //5,6,7,8,9 /******************************************** 函数声明 ********************************************/ void display_time(); /****************************************************************************** * 函 数 名 : init_time * 函数功能 : 初始化时钟时间、定时时间及预定时时间 * 输 入 : 无 * 输 出 : 无 ******************************************************************************/ void init_time() { hours = mins = seconds = target_hours\ = target_mins = target_seconds = 0; hours = 23; //设置时钟初始时间值 mins = 59; seconds = 55; target_hours = 00; //设置预定时时间初始值 target_mins = 00; target_seconds = 30; countdown_hours = 00; //设置定时时时间初始值 countdown_mins = 01; countdown_seconds = 00; temp_hours = 0; //设置临时时间初始值 temp_mins = 0; temp_seconds = 0; } /****************************************************************************** * 函 数 名 : delay * 函数功能 : 软件延时函数 * 输 入 : xms * 输 出 : 无 ******************************************************************************/ void delay(uint xms) { uint i,j; for(i = 0; i=0 && cnt <100) { res = res | 0xff; } else { res = res | 0x00; } } P0 = res; //显示时间 P2=wei; //数码管位选控制 wei=_cror_(wei,1); //位选右移一位 delay(2); //延时2ms } ledy = 0; //黄灯亮 if(button1 == 0) //查询是否按下按键1 { delay(20); //按键消抖 if(button1 == 0) //确定按下按键1,即确定键 { initT1(); //初始化定时器1 break; } } if(button2 == 0) //查询是否按下按键2 { delay(20); if(button2 == 0) //确定按下按键2,则依次选择时、分、秒,闪烁 { flash = flash+1; if(flash == 3) //时分秒显示完一轮后重新开始闪烁显示 { flash = 0; } while(button2 == 0)//防止重复按键操作 { } } } if(button3 ==0) { delay(20); if(button3 == 0) //确定按下按键3 { if(flash ==0) //如果选择的秒 { countdown_seconds = countdown_seconds + 1;//秒数加一 if(countdown_seconds == 60) { countdown_seconds = 0; countdown_mins = countdown_mins + 1; if(countdown_mins == 60) { countdown_mins = 0; countdown_hours = countdown_hours + 1; if(countdown_hours == 24) { countdown_hours = 0; } } } } else if(flash == 1) //按下按键3,如果选择的分 { countdown_mins = countdown_mins + 1;//分钟数加一 if(countdown_mins == 60) { countdown_mins = 0; countdown_hours = countdown_hours +1; if(countdown_hours == 24) { countdown_hours = 0; } } } else //否则小时数加一 { countdown_hours = countdown_hours + 1; if(countdown_hours == 24) { countdown_hours = 0; } } while(button3 == 0); } } } } /****************************************************************************** * 函 数 名 : ex1_intr * 函数功能 : 外部中断1中断服务函数 -- 设置预定时时间 * 输 入 : 无 * 输 出 : 无 ******************************************************************************/ void ex1_intr() interrupt 2 { uchar i,wei=0x20; uchar n = 0x00; uchar dp = 0; uchar flash = 0; uchar cnt = 0; uchar res; while(1) { wei=0x20; for(i=0;i<6;i++) //动态显示六个数码管 { dp = 0; //小数点默认不显示 if(i == 0) //秒数个位 { n = target_seconds%10; dp = 1; //显示小数点 } else if(i == 1) //秒数十位 { n = target_seconds/10; } else if(i == 2) //分钟数个位 { n = target_mins%10; dp = 1; } else if(i == 3) //分钟数十位 { n = target_mins/10; } else if(i == 4) //时钟数个位 { n = target_hours%10; dp = 1; } else if(i == 5) //时钟数十位 { n = target_hours/10; } else { n = 0xff; //否则数码管全灭 } P0=0xff; if( dp == 1) { res = table1[n] ^ 0x80; } else { res = table1[n]; } if(i /2 == flash) { cnt = cnt + 1; if(cnt == 200) { cnt = 0; } if(cnt>=0 && cnt <100) { res = res | 0xff; }else{ res = res | 0x00; } } P0 = res; P2=wei; wei=_cror_(wei,1); delay(2); } ledy = 0; //黄灯亮 if(button1 == 0) { delay(20); if(button1 == 0){ break; } } if(button2 == 0) { delay(20); if(button2 == 0) { flash = flash+1; if(flash == 3) { flash = 0; } while(button2 == 0) { } } } if(button3 ==0) { delay(20); if(button3 == 0) { if(flash ==0) { target_seconds = target_seconds + 1; if(target_seconds == 60) { target_seconds = 0; target_mins = target_mins + 1; if(target_mins == 60) { target_mins = 0; target_hours = target_hours + 1; if(target_hours == 24) { target_hours = 0; } } } } else if(flash == 1) { target_mins = target_mins + 1; if(target_mins == 60) { target_mins = 0; target_hours = target_hours +1; if(target_hours == 24) { target_hours = 0; } } } else { target_hours = target_hours + 1; if(target_hours == 24) { target_hours = 0; } } while(button3 == 0); } } } } /****************************************************************************** * 函 数 名 : display_time * 函数功能 : 显示函数 * 输 入 : 无 * 输 出 : 无 ******************************************************************************/ void display_time() { uchar i,wei=0x20; uchar n = 0x00; uchar dp = 0; uchar state = 0; //状态变量。state=0表示正常,state=1红灯闪烁、蜂鸣器响 uchar cnt = 0; while(1) { ledy = 1; //黄灯灭 wei=0x20; for(i=0;i<6;i++) //动态显示六个数码管 { dp = 0; if(i == 0) { n = seconds%10; dp = 1; } else if(i == 1) { n = seconds/10; } else if(i == 2) { n = mins%10; dp = 1; } else if(i == 3) { n = mins/10; } else if(i == 4) { n = hours%10; dp = 1; } else if(i == 5) { n = hours/10; } else { n = 0xff; } P0=0xff; if( dp == 1) { P0 = table1[n] ^ 0x80; } else { P0 = table1[n]; } P2=wei; wei=_cror_(wei,1); delay(2); } if(state == 0) { ledr = 1; } else { cnt = cnt + 1; if(cnt == 20) { cnt = 0; } if(cnt>=0 && cnt <5) { ledr = 0; } else { ledr = 1; } } if(state == 0) { //如果预定时时间到或者计时时间到 if((hours == target_hours && mins == target_mins && seconds == target_seconds) || flag==1 ) { state = 1; } } else { if(button1 == 1 && button2 == 1 && button3 ==1) { } else //任意一个键按下则state=0 { state = 0; delay(20); } } } } /****************************************************************************** * 函 数 名 : main * 函数功能 : 主函数 -- 调用各种模块化函数实现功能 * 输 入 : 无 * 输 出 : 无 ******************************************************************************/ void main() { init_time(); //调用时间值初始化函数 initT0(); //调用定时器0初始化函数 initEx0(); //调用外部中断0初始化函数 delay(20); display_time(); //调用显示函数 }