1、将本文档拆分成 3 个文件:mian.c, all.h, key.cMian.c 文件包含:/*功能说明充电定时器,可以设置充电持续时间,充电时间等于预约时间时自动停止充电,再次按下充电键时继续充电。电路原理图缺省, 有需要请发邮件到 ,欢迎来信。按下充电键开始充电,充电灯闪烁,再次按下停止充电,在充电开始后不能进入设置状态。只有停止充电后方可进入。按下设置按键 1.5 秒后进入设置状态,充电灯熄灭,再次按设置键可以设置时间,相对应的时间灯亮,时间设置好后按下充电键退出设置,充电灯常亮。可设置的时间为 110 小时,在设置状态下 10 秒内无任何按键按下退出设置状态。晶振频率 22.1184M
2、HZ要更改端口定义在 all.h 文件里修改*/#include /*函数定义*/bit charge_over;bit f_time_25ms;char time_25ms;char hour_stop = 4; /缺省时间char hour = 4;char min = 0;char sec = 0;bit charge =0;extern void scan();void init()/初始化函数TMOD = 0x01;TH0 = (65536-2700)/256;TL0 = (65536-2700)%256;ET0 = 1;TR0 = 1;EA = 1;P0 = 0xFF;charge
3、_light = on;hour_light_1 = off;hour_light_2 = off;hour_light_3 = off;hour_light_4 = on;hour_light_5 = off;hour_light_6 = off;hour_light_7 = off;hour_light_8 = off;hour_light_9 = off;hour_light_10 = off;main()init();while(1)if (f_time_25ms)scan();f_time_25ms = 0;void light_on()charge_light = on;switc
4、h (hour+1)case 1:hour_light_1 = on;break;case 2:hour_light_2 = on;break;case 3:hour_light_3 = on;break;case 4:hour_light_4 = on;break;case 5:hour_light_5 = on;break;case 6:hour_light_6 = on;break;case 7:hour_light_7 = on;break;case 8:hour_light_8 = on;break;case 9:hour_light_9 = on;break;case 10:hou
5、r_light_10 = on;break;void light_off()charge_light = off;hour_light_1 = off;hour_light_2 = off;hour_light_3 = off;hour_light_4 = off;hour_light_5 = off;hour_light_6 = off;hour_light_7 = off;hour_light_8 = off;hour_light_9 = off;hour_light_10 = off;switch (hour_stop)case 1:hour_light_1 = on;break;cas
6、e 2:hour_light_2 = on;break;case 3:hour_light_3 = on;break;case 4:hour_light_4 = on;break;case 5:hour_light_5 = on;break;case 6:hour_light_6 = on;break;case 7:hour_light_7 = on;break;case 8:hour_light_8 = on;break;case 9:hour_light_9 = on;break;case 10:hour_light_10 = on;break;void t0(void) interrup
7、t 1TH0 = (65536-46200)/256;TL0 = (65536-46200)%256;time_25ms+;f_time_25ms = 1;if ( charge = 1)if ( time_25ms = 20 if ( time_25ms =40 )time_25ms = 0;if ( !charge_over )light_off();sec -;if ( sec /#include /*端口定义*/sbit charge_light = P01; /sbit hour_light_1 = P02; /1 小时sbit hour_light_2 = P03; /2 小时sb
8、it hour_light_3 = P04; /3 小时sbit hour_light_4 = P05; /4 小时sbit hour_light_5 = P06; /5 小时sbit hour_light_6 = P07; /6 小时sbit hour_light_7 = P20; /7 小时sbit hour_light_8 = P21; /8 小时sbit hour_light_9 = P22; /9 小时sbit hour_light_10 = P23; /10 小时sbit charge_out = P00; /充电输出sbit charge_key = P10; /充电键sbit
9、set_key = P11; /设置键/*函数定义*/#define on 0;#define off 1;extern bit charge_over;extern char time_25ms;extern char hour_stop;extern char hour;extern char min ;extern char sec ;extern bit charge;key.c 文件包含:#include“all.h“bit f_key = 0;char set_key_time;char time_set;char charge_key_dat;extern void scan()
10、;void scan()if ( charge_key = 0 | set_key = 0 )set_key_time = 0;if ( !set_key if (charge_key_dat = 60) /100charge_light = off;f_key = 1;time_set = 1;charge_light = off;charge_key_dat=0;hour_stop = hour;break;case 1:if (!set_key if (charge_key_dat = 4) /100f_key = 1;charge_key_dat=0;f_key = 1;hour_st
11、op+;if ( hour_stop 10 ) /可设置的最大时间 (单位小时)hour_stop = 1; /可设置的最小时间 不可以为 0switch (hour_stop)case 1:hour_light_10 = off;hour_light_1 = on;break;case 2:hour_light_1 = off;hour_light_2 = on;break;case 3:hour_light_2 = off;hour_light_3 = on;break;case 4:hour_light_3 = off;hour_light_4 = on;break;case 5:hou
12、r_light_4 = off;hour_light_5 = on;break;case 6:hour_light_5 = off;hour_light_6 = on;break;case 7:hour_light_6 = off;hour_light_7 = on;break;case 8:hour_light_7 = off;hour_light_8 = on;break;case 9:hour_light_8 = off;hour_light_9 = on;break;case 10:hour_light_9 = off;hour_light_10 = on;break;break;else if (!charge_keyswitch ( time_set )case 0:/TR0 = !TR0;charge = !charge;charge_out = on;break;case 1:hour = hour_stop;time_set = 0;charge_light = on;break;else f_key=0;set_key_time+;if( set_key_time=400) /如果 10 秒内无按键退出不保存数据;time_set=0;time_set = 0;charge_light = on;