打印
[学习资料]

c语言钟表程序,pic16f877a c语言时钟程序

[复制链接]
599|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主

用c语言写的一个时闹钟程序

单片机用16F877,主时钟用20MHz,用32768作定时时间。可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。连线在程序开头有说明。

程序的功能:

(1)上电后LCD背光打开,并显示倒计时5秒,然后时钟开始工作。

(2)用模式键(*)切换模式,如显示时间、日期、闹钟1、闹钟2等,并且可以用上、下键控制加1、减1或是闹钟的On、Off。

(3)原程序有16个键,包括0~9数字键,可以直接输入要设置的时间值,但后来将数字键取消了,你仍然可以通过修改程序的部分注释恢复此功能。

(4)闹钟有2路,时间到后闹2分钟,可按任意键取消本次闹钟。闹钟响时有2种音调,是用PIC的PWM实现的。

(5)按任意键可打开背光,1分钟后自动关闭背光。

(6)RA0~RA3为按键扫描输入,应接下拉电阻。


使用特权

评论回复
沙发
花间一壶酒sd|  楼主 | 2024-2-27 12:11 | 只看该作者
主程序

//FileName:Main.c

//MCU:MicroChipPIC16F877

//Tool:CCS-Ccompiler

//Author:KingEDA,MSN:kingeda@163.com,skype:kingeda,E-mail:kingeda@163.com

//Website:http://www.EDAKING.com

//Description:

//Atimerprogram

//Ver0.1:2003-03-31,allcLOCkfunctionwithdatedisplay,2wayalarm.

//Ver0.2:2003-05-05,(1)Alarmdefaultison,modifyalarm1timeto7:00:00,

//andalarm2to13:30:00.

//(2)BacklightwillbeenabLEDwhenalarming.

//(3)Automaticadjustday(28,30,31).

//(4)Automatic MOV ecursortonextlocationwhensetitem.

//PINConnection:

//RC0~1:32768Hzcrystal

//RC2:Buzzer

//RC3:LCDBackLight,driveaPNPBJT

//RD0~RD7:toLCDDB0~DB7

//RA0~RA3:keypadcolin

//RC4~RC7:keypadlineout

//789#

//456↑

//123↓

//0←→*

//RE0:LCDRS

//RE1:LCDRW

//RE2:LCDE

#include"my16f877.h"

#deviceICD=true

//#fusesHS,NOWDT,NOPROTECT,PUT,BROWNOUT

#usedelay(clock=24000000)

//#usefast_io(C)

#usefast_io(E)

#definelcd_busy(lcd_read_addr()&0x80)==0x80

#definetime_start_addr0x80+0x04

#definetime_hourh_addrtime_start_addr

#definetime_hourl_addrtime_start_addr+1

#definetime_minuteh_addrtime_start_addr+3

#definetime_minutel_addrtime_start_addr+4

#definetime_secondh_addrtime_start_addr+6

#definetime_secondl_addrtime_start_addr+7

#definekey_00x11

#definekey_10x21

#definekey_20x22

#definekey_30x24

#definekey_40x41

#definekey_50x42

#definekey_60x44

#definekey_70x81

#definekey_80x82

#definekey_90x84

#definekey_left0x12

#definekey_right0x14

#definekey_up0x48

#definekey_down0x28

#definekey_mode0x18

#definekey_CANcel0x88

charStrPower1[]="*Poweron*";

charStrSetTime[]="*Adjusttime*";

charStrSetDate[]="*Adjustdate*";

charStrAlarm1[]="*Setalarm1*";

charStrAlarm2[]="*Setalarm2*";

unsignedcharPORTC_MAP;

#bitBackLightEn=PORTC_MAP.3

unsignedcharBackLightTimer;

int1led;

#bitlcd_rs=PORTE.0

#bitlcd_rw=PORTE.1

#bitlcd_e=PORTE.2

#bytelcd_bus=PORTD

#bytelcd_dir=TRISD

#definePWM_on0x0c

#definePWM_off0x00

#definePWM_period200

#definePWM_DC100

unsignedcharlcd_addr;

unsignedcharKeyLine;

unsignedcharKeyOld;

unsignedcharKeyNew;

structmTime{

unsignedcharhourh;//hour,0~23

unsignedcharhourl;

unsignedcharminuteh;//minute,0~59

unsignedcharminutel;

unsignedcharsecondh;//second,0~59

unsignedcharsecondl;

};

structmTimeCurrentTime={1,2,0,0,0,0};

structmTimeAlarmTime1={0,7,0,0,0,0};//07:00:00

structmTimeAlarmTime2={1,3,3,0,0,0};//13:30:00

unsignedcharAlarmStatus;

#bitAlarm1Enable=AlarmStatus.0

#bitAlarm2Enable=AlarmStatus.1

#bitAlarm1Alarm=AlarmStatus.2

#bitAlarm2Alarm=AlarmStatus.3

unsignedcharAlarm1Cnt;//alarm1secondcount

unsignedcharAlarm2Cnt;

unsignedcharCurrentMode;

#definemode_time0

#definemode_set_time1

#definemode_set_date2

#definemode_set_alarm13

#definemode_set_alarm24

unsignedcharadjust_item;

structmDate{

unsignedcharyear1;//

unsignedcharyear2;

unsignedcharyear3;

unsignedcharyear4;

unsignedcharmonthh;

unsignedcharmonthl;

unsignedchardayh;

unsignedchardayl;

};

structmDateCurrentDate={2,0,0,3,0,1,0,1};

unsignedchar*pStr;

//-------------------------------------------------------

unsignedcharlcd_read_addr()

{

unsignedcharch;

lcd_dir=0xff;//readfromlcd

lcd_rs=0;

lcd_rw=1;//inst

lcd_e=1;

#asm

nop

nop

nop

#endasm

ch=lcd_bus;

lcd_e=0;

lcd_dir=0x00;//setwritetolcd

returnch;

}

//-------------------------------------------------------

unsignedcharlcd_write_data(unsignedcharch)

{

while(lcd_busy)

{restart_wdt();}

lcd_rs=1;//data

lcd_rw=0;//write

lcd_bus=ch;//writeout

lcd_e=1;

#asm

nop

nop

nop

#endasm

lcd_e=0;

return’Y’;

}

//-------------------------------------------------------

unsignedcharlcd_write_inst(unsignedcharch)

{

while(lcd_busy)

{restart_wdt();}

lcd_rs=0;//inst

lcd_rw=0;//write

lcd_bus=ch;

lcd_e=1;

#asm

nop

nop

nop

#endasm

lcd_e=0;

return’Y’;

}

//-------------------------------------------------------

unsignedcharlcd_read_data()

{

unsignedcharch;

while(lcd_busy)

{restart_wdt();}

lcd_dir=0xff;//readfromlcd

lcd_rs=1;//data

lcd_rw=1;//read

lcd_e=1;

#asm

nop

nop

nop

#endasm

ch=lcd_bus;//readin

lcd_e=0;

lcd_dir=0x00;//setwritetolcd

returnch;

}

//-------------------------------------------------------

voidlcd_init()

{

unsignedcharTempch;

lcd_addr=0;

delay_ms(100);

Tempch=0x38;//1-linemode,5x8dots

lcd_write_inst(Tempch);//Functionset

Tempch=0x0f;//lcdon,cursoron,blinkon

lcd_write_inst(Tempch);//Displayon/off

Tempch=0x06;//Incrementmode,Entireshiftoff

lcd_write_inst(Tempch);

Tempch=0x01;//cleardisplay

lcd_write_inst(Tempch);

delay_ms(3);

}

//-------------------------------------------------------

//#int_timer1

//voidtimer1_interrupt(void)

#int_ccp2

voidccp2_interrupt(void)

{

//TMR1H=0x80;

if(CurrentTime.secondl==9)

{

CurrentTime.secondl=0;

if(CurrentTime.secondh==5)

{

CurrentTime.secondh=0;

if(CurrentTime.minutel==9)

{

CurrentTime.minutel=0;

if(CurrentTime.minuteh==5)

{

CurrentTime.minuteh=0;

if(CurrentTime.hourl==9)

{

CurrentTime.hourl=0;

CurrentTime.hourh++;

}

elseif((CurrentTime.hourl==3)&&(CurrentTime.hourh==2))

{

CurrentTime.hourl=0;

CurrentTime.hourh=0;

if((((CurrentDate.dayl==8)||(CurrentDate.dayl==9))&&(CurrentDate.dayh==2)&&(CurrentDate.monthl==2)&&(CurrentDate.monthh==0))||

((CurrentDate.dayl==0)&&(CurrentDate.dayh==3)&&((((CurrentDate.monthl==4)||(CurrentDate.monthl==6)

||(CurrentDate.monthl==9))&&(CurrentDate.monthh==0))||((CurrentDate.monthl==1)&&(CurrentDate.monthh==1))))||

((CurrentDate.dayl==1)&&(CurrentDate.dayh==3)))

{

CurrentDate.dayl=1;

CurrentDate.dayh=0;

if((CurrentDate.monthl==2)&&(CurrentDate.monthh==1))

{

CurrentDate.monthl=1;

CurrentDate.monthh=0;

if(CurrentDate.year4==9)

{

CurrentDate.year4=0;

if(CurrentDate.year3==9)

{

CurrentDate.year3=0;

if(CurrentDate.year2==9)

{

CurrentDate.year2=0;

CurrentDate.year1++;

}

else

CurrentDate.year2++;

}

else

CurrentDate.year3++;

}

else

CurrentDate.year4++;

}

elseif(CurrentDate.monthl==9)

{

CurrentDate.monthl=0;

CurrentDate.monthh++;

}

else

CurrentDate.monthl++;

}

elseif(CurrentDate.dayl==9)

{

CurrentDate.dayl=0;

CurrentDate.dayh++;

}

else

CurrentDate.dayl++;

}

else

CurrentTime.hourl++;

}

else

CurrentTime.minuteh++;

}

else

CurrentTime.minutel++;

}

else

CurrentTime.secondh++;

}

else

CurrentTime.secondl++;

if((Alarm1Alarm==false)&(Alarm2Alarm==false))

{

led=0;

CCP1CON=PWM_off;

}

else

{

if(led==1)

{

led=0;

PR2=PWM_period;//setpwmperiod

CCPR1L=PWM_DC;//setpwmdutycycle

//CCP1CON=PWM_on;

}

else

{

led=1;

PR2=PWM_period/2;//setpwmperiod

CCPR1L=PWM_DC/2;//setpwmdutycycle

//CCP1CON=PWM_off;

}

}

Alarm1Cnt++;

Alarm2Cnt++;

if(BackLightEn==0)

if(((BackLightTimer++)>=60)&(Alarm1Alarm==false)&(Alarm1Alarm==false))

BackLightEn=1;//dISAblebacklight

PORTC=PORTC_MAP;

//TMR1IF=0;

//PIR1=PIR2=0x00;

CCP2IF=0;

}

//-------------------------------------------------------

unsignedcharget_key(void)

{

unsignedcharkey_in,tmp;

TRISC=0x03;

KeyLine=0xf0;

PORTC=KeyLine|PORTC_MAP;

#asm

nop

nop

nop

#endasm

if((PORTA&0x0f)!=0)

{

tmp=0x10;

for(KeyLine=tmp;KeyLine!=0;KeyLine=tmp)

{

PORTC=KeyLine|PORTC_MAP;

tmp=KeyLine<<1;

#asm

nop

nop

nop

#endasm

key_in=PORTA&0x0f;

if(key_in!=0)

{

return(key_in|KeyLine);

}

}

return0;

}

else

return0;

}

//-------------------------------------------------------

voidset_mode(void)

{

if(CurrentMode==mode_set_alarm2)

CurrentMode=mode_time;

else

{

CurrentMode++;

adjust_item=0;

}

lcd_write_inst(0x01);//clearLCDdisplay

lcd_write_inst(time_start_addr);//setLCDline1

if(CurrentMode==mode_set_time)

{

lcd_write_data(CurrentTime.hourh+’0’);

lcd_write_data(CurrentTime.hourl+’0’);

lcd_write_data(’:’);

lcd_write_data(CurrentTime.minuteh+’0’);

lcd_write_data(CurrentTime.minutel+’0’);

lcd_write_data(’:’);

lcd_write_data(CurrentTime.secondh+’0’);

lcd_write_data(CurrentTime.secondl+’0’);

pStr=StrSetTime;

}

elseif(CurrentMode==mode_set_date)

{

lcd_write_data(CurrentDate.year1+’0’);

lcd_write_data(CurrentDate.year2+’0’);

lcd_write_data(CurrentDate.year3+’0’);

lcd_write_data(CurrentDate.year4+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.monthh+’0’);

lcd_write_data(CurrentDate.monthl+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.dayh+’0’);

lcd_write_data(CurrentDate.dayl+’0’);

pStr=StrSetDate;

}

elseif(CurrentMode==mode_set_alarm1)

{

lcd_write_data(AlarmTime1.hourh+’0’);

lcd_write_data(AlarmTime1.hourl+’0’);

lcd_write_data(’:’);

lcd_write_data(AlarmTime1.minuteh+’0’);

lcd_write_data(AlarmTime1.minutel+’0’);

lcd_write_data(’:’);

lcd_write_data(AlarmTime1.secondh+’0’);

lcd_write_data(AlarmTime1.secondl+’0’);

lcd_write_data(’’);

lcd_write_data(’O’);

if(Alarm1Enable)

{

lcd_write_data(’n’);

}

else

{

lcd_write_data(’f’);

lcd_write_data(’f’);

}

pStr=StrAlarm1;

Alarm1Cnt=0;

}

elseif(CurrentMode==mode_set_alarm2)

{

lcd_write_data(AlarmTime2.hourh+’0’);

lcd_write_data(AlarmTime2.hourl+’0’);

lcd_write_data(’:’);

lcd_write_data(AlarmTime2.minuteh+’0’);

lcd_write_data(AlarmTime2.minutel+’0’);

lcd_write_data(’:’);

lcd_write_data(AlarmTime2.secondh+’0’);

lcd_write_data(AlarmTime2.secondl+’0’);

lcd_write_data(’’);

lcd_write_data(’O’);

if(Alarm2Enable)

{

lcd_write_data(’n’);

}

else

{

lcd_write_data(’f’);

lcd_write_data(’f’);

}

pStr=StrAlarm2;

Alarm2Cnt=0;

}

lcd_write_inst(0xc0);//setLCDline2

if(CurrentMode!=mode_time)

{

for(;*pStr!=0;pStr++)

{//writehintmessage

lcd_write_data(*pStr);

}

lcd_write_inst(0x0f);//LCDcursoron

lcd_write_inst(time_start_addr);// MOV ecursortostart

}

else//timemode,writedatetosecondline

{

lcd_write_inst(0x0c);//LCDsursoroff

/*lcd_write_inst(0xc0+3);//setdatestartaddress

lcd_write_data(CurrentDate.year1+’0’);

lcd_write_data(CurrentDate.year2+’0’);

lcd_write_data(CurrentDate.year3+’0’);

lcd_write_data(CurrentDate.year4+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.monthh+’0’);

lcd_write_data(CurrentDate.monthl+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.dayh+’0’);

lcd_write_data(CurrentDate.dayl+’0’);

*/}

if(CurrentMode==mode_set_time)

{

lcd_write_inst(time_start_addr);// MOV ecursortostart

}

elseif(CurrentMode==mode_set_date)

{

lcd_write_inst(time_start_addr);// MOV ecursortostart

}

elseif(CurrentMode==mode_set_alarm1)

{

lcd_write_inst(time_secondl_addr+3);

adjust_item=6;

}

elseif(CurrentMode==mode_set_alarm2)

{

lcd_write_inst(time_secondl_addr+3);

adjust_item=6;

}

else

{

lcd_write_inst(0x0c);//LCDcursoroff

}

}

//-------------------------------------------------------

voidset_date(void)

{

if(adjust_item==0)//adjustyear

{

if((KeyNew>=0)&(KeyNew<=9))

{

CurrentDate.year1=KeyNew;

lcd_write_data(CurrentDate.year1+’0’);

//lcd_write_inst(time_start_addr);

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item=7;

lcd_write_inst(time_start_addr+9);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+1);

}

}

elseif(adjust_item==1)

{

if((KeyNew>=0)&(KeyNew<=9))

{

CurrentDate.year2=KeyNew;

lcd_write_data(CurrentDate.year2+’0’);

//lcd_write_inst(time_start_addr+1);

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+0);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+2);

}

}

elseif(adjust_item==2)

{

if((KeyNew>=0)&(KeyNew<=9))

{

CurrentDate.year3=KeyNew;

lcd_write_data(CurrentDate.year3+’0’);

//lcd_write_inst(time_start_addr+2);

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+1);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+3);

}

}

elseif(adjust_item==3)

{

if((KeyNew>=0)&(KeyNew<=9))

{

CurrentDate.year4=KeyNew;

lcd_write_data(CurrentDate.year4+’0’);

//lcd_write_inst(time_start_addr+3);

adjust_item++;

lcd_write_inst(time_start_addr+5);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+2);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+5);

}

}

elseif(adjust_item==4)

{

if(((CurrentDate.monthl>2)&(KeyNew==0))|((CurrentDate.monthl==0)&(KeyNew==1))

|(((CurrentDate.monthl==1)|(CurrentDate.monthl==2))&(KeyNew<2)))

{

CurrentDate.monthh=KeyNew;

lcd_write_data(CurrentDate.monthh+’0’);

//lcd_write_inst(time_start_addr+5);

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+3);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+6);

}

}

elseif(adjust_item==5)

{

if(((CurrentDate.monthh==3)&(KeyNew<2))|((CurrentDate.monthh!=3)&(KeyNew>=0)&(KeyNew<=9)))

{

CurrentDate.monthl=KeyNew;

lcd_write_data(CurrentDate.monthl+’0’);

//lcd_write_inst(time_start_addr+6);

adjust_item++;

lcd_write_inst(time_start_addr+8);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+5);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+8);

}

}

elseif(adjust_item==6)

{

if(((CurrentDate.dayl>1)&((KeyNew==1)|(KeyNew==2)))|((CurrentDate.dayl==0)&(KeyNew>0)&(KeyNew<4))

|((CurrentDate.dayl==1)&(KeyNew<4)))

{

CurrentDate.dayh=KeyNew;

lcd_write_data(CurrentDate.dayh+’0’);

//lcd_write_inst(time_start_addr+8);

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+6);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_start_addr+9);

}

}

elseif(adjust_item==7)

{

if(((CurrentDate.dayh==3)&(KeyNew<2))|((CurrentDate.dayh!=3)&(KeyNew>=0)&(KeyNew<=9)))

{

CurrentDate.dayl=KeyNew;

lcd_write_data(CurrentDate.dayl+’0’);

//lcd_write_inst(time_start_addr+9);

adjust_item++;

lcd_write_inst(time_start_addr+0);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_start_addr+8);

}

elseif(KeyNew==key_right)

{

adjust_item=0;

lcd_write_inst(time_start_addr+0);

}

}

}

//-------------------------------------------------------

voidset_time(void)

{

if(adjust_item==0)//sethourh

{

if(((CurrentTime.hourl<4)&(KeyNew<3))|((CurrentTime.hourl>3)&(KeyNew<2)))

{

CurrentTime.hourh=KeyNew;

lcd_write_data(CurrentTime.hourh+’0’);//refreshhourh

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item=5;

lcd_write_inst(time_secondl_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_hourl_addr);

}

}

elseif(adjust_item==1)//sethourl

{

if(((CurrentTime.hourh==2)&(KeyNew<4))|((CurrentTime.hourh<2)&(KeyNew<=9)))

{

CurrentTime.hourl=KeyNew;

lcd_write_data(CurrentTime.hourl+’0’);//refreshhourl

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_minuteh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_hourh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_minuteh_addr);

}

}

elseif(adjust_item==2)//setminuteh

{

if(KeyNew<6)

{

CurrentTime.minuteh=KeyNew;

lcd_write_data(CurrentTime.minuteh+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_hourl_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_minutel_addr);

}

}

elseif(adjust_item==3)//setminutel

{

if((KeyNew>=0)&(KeyNew<=9))

{

CurrentTime.minutel=KeyNew;

lcd_write_data(CurrentTime.minutel+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_secondh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_minuteh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondh_addr);

}

}

elseif(adjust_item==4)//setsecondh

{

if(KeyNew<6)

{

CurrentTime.secondh=KeyNew;

lcd_write_data(CurrentTime.secondh+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

&nb,sp;lcd_write_inst(time_minutel_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondl_addr);

}

}

elseif(adjust_item==5)//setsecondl

{

if((KeyNew>=0)&(KeyNew<=9))

{

CurrentTime.secondl=KeyNew;

lcd_write_data(CurrentTime.secondl+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item=0;

lcd_write_inst(time_hourh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_secondh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item=0;

lcd_write_inst(time_hourh_addr);

}

}

}

//-------------------------------------------------------

voidset_alarm1(void)

{

if(adjust_item==0)//sethourh

{

if(((AlarmTime1.hourl<4)&(KeyNew<3))|((AlarmTime1.hourl>3)&(KeyNew<2)))

{

AlarmTime1.hourh=KeyNew;

lcd_write_data(AlarmTime1.hourh+’0’);//refreshhourh

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item=6;

lcd_write_inst(time_secondl_addr+3);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_hourl_addr);

}

}

elseif(adjust_item==1)//sethourl

{

if(((AlarmTime1.hourh==2)&(KeyNew<4))|((AlarmTime1.hourh<2)&(KeyNew<=9)))

{

AlarmTime1.hourl=KeyNew;

lcd_write_data(AlarmTime1.hourl+’0’);//refreshhourl

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_minuteh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_hourh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_minuteh_addr);

}

}

elseif(adjust_item==2)//setminuteh

{

if(KeyNew<6)

{

AlarmTime1.minuteh=KeyNew;

lcd_write_data(AlarmTime1.minuteh+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_hourl_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_minutel_addr);

}

}

elseif(adjust_item==3)//setminutel

{

if((KeyNew>=0)&(KeyNew<=9))

{

AlarmTime1.minutel=KeyNew;

lcd_write_data(AlarmTime1.minutel+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_secondh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_minuteh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondh_addr);

}

}

elseif(adjust_item==4)//setsecondh

{

if(KeyNew<6)

{

AlarmTime1.secondh=KeyNew;

lcd_write_data(AlarmTime1.secondh+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_minutel_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondl_addr);

}

}

elseif(adjust_item==5)//setsecondl

{

if((KeyNew>=0)&(KeyNew<=9))

{

AlarmTime1.secondl=KeyNew;

lcd_write_data(AlarmTime1.secondl+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_secondl_addr+3);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_secondh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondl_addr+3);

}

}

elseif(adjust_item==6)//seton/off

{

if((KeyNew==key_up)|(KeyNew==key_down))

{

if(Alarm1Enable)

{

Alarm1Enable=false;//disablealarm1

lcd_write_data(’f’);

lcd_write_data(’f’);

}

else

{

Alarm1Enable=true;//enablealarm1

lcd_write_data(’n’);

lcd_write_data(’’);

}

//lcd_write_inst(time_secondl_addr+3);

adjust_item=0;

lcd_write_inst(time_hourh_addr);

Alarm1Cnt=0;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_secondl_addr);

}

elseif(KeyNew==key_right)

{

adjust_item=0;

lcd_write_inst(time_hourh_addr);

}

}

}

//-------------------------------------------------------

voidset_alarm2(void)

{

if(adjust_item==0)//sethourh

{

if(((AlarmTime2.hourl<4)&(KeyNew<3))|((AlarmTime2.hourl>3)&(KeyNew<2)))

{

AlarmTime2.hourh=KeyNew;

lcd_write_data(AlarmTime2.hourh+’0’);//refreshhourh

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item=6;

lcd_write_inst(time_secondl_addr+3);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_hourl_addr);

}

}

elseif(adjust_item==1)//sethourl

{

if(((AlarmTime2.hourh==2)&(KeyNew<4))|((AlarmTime2.hourh<2)&(KeyNew<=9)))

{

AlarmTime2.hourl=KeyNew;

lcd_write_data(AlarmTime2.hourl+’0’);//refreshhourl

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_minuteh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_hourh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_minuteh_addr);

}

}

elseif(adjust_item==2)//setminuteh

{

if(KeyNew<6)

{

AlarmTime2.minuteh=KeyNew;

lcd_write_data(AlarmTime2.minuteh+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_hourl_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_minutel_addr);

}

}

elseif(adjust_item==3)//setminutel

{

if((KeyNew>=0)&(KeyNew<=9))

{

AlarmTime2.minutel=KeyNew;

lcd_write_data(AlarmTime2.minutel+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_secondh_addr);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_minuteh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondh_addr);

}

}

elseif(adjust_item==4)//setsecondh

{

if(KeyNew<6)

{

AlarmTime2.secondh=KeyNew;

lcd_write_data(AlarmTime2.secondh+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_minutel_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondl_addr);

}

}

elseif(adjust_item==5)//setsecondl

{

if((KeyNew>=0)&(KeyNew<=9))

{

AlarmTime2.secondl=KeyNew;

lcd_write_data(AlarmTime2.secondl+’0’);

//lcd_write_inst(0x10);// MOV ecursorback

adjust_item++;

lcd_write_inst(time_secondl_addr+3);

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_secondh_addr);

}

elseif(KeyNew==key_right)

{

adjust_item++;

lcd_write_inst(time_secondl_addr+3);

}

}

elseif(adjust_item==6)//seton/off

{

if((KeyNew==key_up)|(KeyNew==key_down))

{

if(Alarm2Enable)

{

Alarm2Enable=false;//disablealarm2

lcd_write_data(’f’);

lcd_write_data(’f’);

}

else

{

Alarm2Enable=true;//enablealarm2

lcd_write_data(’n’);

lcd_write_data(’’);

}

//lcd_write_inst(time_secondl_addr+3);

adjust_item=0;

lcd_write_inst(time_hourh_addr);

Alarm2Cnt=0;

}

elseif(KeyNew==key_left)

{

adjust_item--;

lcd_write_inst(time_secondl_addr);

}

elseif(KeyNew==key_right)

{

adjust_item=0;

lcd_write_inst(time_hourh_addr);

}

}

}

//-------------------------------------------------------

voidmain(void)

{

unsignedcharcnt;

TRISC=0x03;//PORTC.3driveled,lowactive

PORTC_MAP=0x00;

led=0;

BackLightEn=0;

BackLightTimer=0;

PORTC=PORTC_MAP;

TRISA=0xff;//lowhalfbyteaskeyscanin

TRISE=0x00;

ADCON0=0x00;

ADCON1=0x06;//alldigitalI/Os

lcd_init();

INTCON=0x00;

lcd_write_inst(0x80);//setlcdddramaddress

for(pStr=StrPower1;*pStr!=0;pStr++)

{

lcd_write_data(*pStr);

}

lcd_write_inst(0x0c);//LCDcursoroff

PIR1=PIR2=0x00;

T1CON=0x0f;//T1CON:--T1CKPS1T1CPS0T1OSCEN/T1SYNCTMR1CSTMR1ON

TMR1H=0x80;

TMR1L=0x00;

TMR1IF=0;

CCPR2H=0x7f;

CCPR2L=0xff;

CCP2CON=0x0b;//comparemode,setccp2if,resettmr1

CCP2IF=0;

for(cnt=5;cnt>0;cnt--)//testfor5sec.

{

lcd_write_inst(0xc0+8);//setLCDadress

lcd_write_data(cnt+’0’);

while(CCP2IF==0)//waitfortimer1overflow

{

restart_wdt();

}

//TMR1H=0x80;

CCP2IF=0;

}

//writecurrentdatetolcdline2

lcd_write_inst(0x01);//clearLCDdisplay

lcd_write_inst(0xc0+3);//setLCDline2

lcd_write_data(CurrentDate.year1+’0’);

lcd_write_data(CurrentDate.year2+’0’);

lcd_write_data(CurrentDate.year3+’0’);

lcd_write_data(CurrentDate.year4+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.monthh+’0’);

lcd_write_data(CurrentDate.monthl+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.dayh+’0’);

lcd_write_data(CurrentDate.dayl+’0’);

Alarm1Enable=true;//false;

Alarm2Enable=true;//false;

INTCON=0xc0;//setGIE&PEIE

//TMR1IE=1;//enabletimer1interrupt

CCP2IE=1;//enableccp2interrupt

CurrentMode=mode_time;

Alarm1Cnt=Alarm2Cnt=0;

T2CON=0x07;//T2CON:-TOUTPS3TOUTPS2TOUTPS1TOUTPS0TMR2ONT2CKPS1T2CKPS0

//timer2enable,prescaler=16

PR2=PWM_period;//setpwmperiod

CCPR1L=PWM_DC;//setpwmdutycycle

CCP1CON=PWM_off;

while(1)

{

restart_wdt();

KeyOld=get_key();

delay_ms(20);

KeyNew=get_key();

if(((KeyNew&0x0f)!=0x00)&(KeyNew==KeyOld))

{//somekeypressed

if(BackLightEn==1)//backlightnoton

{

BackLightEn=0;//enableback-light

BackLightTimer=0;//startdelaycounter

}

else

{

if(KeyNew==key_0)

KeyNew=0;

elseif(KeyNew==key_1)

KeyNew=1;

elseif(KeyNew==key_2)

KeyNew=2;

elseif(KeyNew==key_3)

KeyNew=3;

elseif(KeyNew==key_4)

KeyNew=4;

elseif(KeyNew==key_5)

KeyNew=5;

elseif(KeyNew==key_6)

KeyNew=6;

elseif(KeyNew==key_7)

KeyNew=7;

elseif(KeyNew==key_8)

KeyNew=8;

elseif(KeyNew==key_9)

KeyNew=9;

elseif(KeyNew==key_mode)//MODEkeypressed

set_mode();

elseif(KeyNew==key_cancel)//cancelbuzzy

{

led=0;

CCP1CON=PWM_off;

BackLightEn=1;

BackLightTimer=0;

}

if(CurrentMode==mode_set_time)

{

set_time();

}

elseif(CurrentMode==mode_set_date)

{

set_date();

}

elseif(CurrentMode==mode_set_alarm1)

{

set_alarm1();

}

elseif(CurrentMode==mode_set_alarm2)

{

set_alarm2();

}

}

do//waitforkeyreleased

{

delay_ms(30);

KeyNew=get_key();

restart_wdt();

BackLightTimer=0;//keypressedyet

}while((KeyNew&0x0f)!=0x00);

KeyOld=KeyNew=0x00;

}

if((CurrentMode==mode_set_time)|(CurrentMode==mode_time))

{//refreshtimedisplay,bacauseint_timer1dosn’tdothis

lcd_addr=lcd_read_addr()&0x7f;//savecursorlocation

lcd_write_inst(time_start_addr);//setLCDline1

lcd_write_data(CurrentTime.hourh+’0’);

lcd_write_data(CurrentTime.hourl+’0’);

lcd_write_data(’:’);

lcd_write_data(CurrentTime.minuteh+’0’);

lcd_write_data(CurrentTime.minutel+’0’);

lcd_write_data(’:’);

lcd_write_data(CurrentTime.secondh+’0’);

lcd_write_data(CurrentTime.secondl+’0’);

lcd_write_inst(lcd_addr|0x80);//resumecursorlocation

}

if(CurrentMode==mode_time)

{//refreshdatedisplay

lcd_addr=lcd_read_addr()&0x7f;//savecursorlocation

lcd_write_inst(0xc0+3);//setLCDline2

lcd_write_data(CurrentDate.year1+’0’);

lcd_write_data(CurrentDate.year2+’0’);

lcd_write_data(CurrentDate.year3+’0’);

lcd_write_data(CurrentDate.year4+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.monthh+’0’);

lcd_write_data(CurrentDate.monthl+’0’);

lcd_write_data(’/’);

lcd_write_data(CurrentDate.dayh+’0’);

lcd_write_data(CurrentDate.dayl+’0’);

lcd_write_inst(lcd_addr|0x80);//resumecursorlocation

}

if(Alarm1Enable)

{

if((AlarmTime1.hourh==CurrentTime.hourh)&(AlarmTime1.hourl==CurrentTime.hourl)

&(AlarmTime1.minuteh==CurrentTime.minuteh)&(AlarmTime1.minutel==CurrentTime.minutel)

&(AlarmTime1.secondh==CurrentTime.secondh)&(AlarmTime1.secondl==CurrentTime.secondl))

{

Alarm1Alarm=true;

CCP1CON=PWM_on;

BackLightEn=0;

}

if(Alarm1Cnt>120)//twominutes

{

Alarm1Alarm=false;

Alarm1Cnt=0;

BackLightEn=1;

BackLightTimer=0;

}

}

else

{

Alarm1Alarm=false;

Alarm1Cnt=0;

}

if(Alarm2Enable)

{

if((AlarmTime2.hourh==CurrentTime.hourh)&(AlarmTime2.hourl==CurrentTime.hourl)

&(AlarmTime2.minuteh==CurrentTime.minuteh)&(AlarmTime2.minutel==CurrentTime.minutel)

&(AlarmTime2.secondh==CurrentTime.secondh)&(AlarmTime2.secondl==CurrentTime.secondl))

{

Alarm2Alarm=true;

CCP1CON=PWM_on;

BackLightEn=0;

}

if(Alarm2Cnt>120)//twominutes

{

Alarm2Alarm=false;

Alarm2Cnt=0;

BackLightEn=1;

BackLightTimer=0;

}

}

else

{

Alarm2Alarm=false;

Alarm2Cnt=0;

}

}

}

使用特权

评论回复
板凳
在线DIY23| | 2024-2-27 12:15 | 只看该作者
这个也可以在别的单片机上使用吗/

使用特权

评论回复
地板
szt1993| | 2024-2-28 22:08 | 只看该作者
单片机用16F877,主时钟用20MHz,用32768作定时时间。

使用特权

评论回复
5
LOVEEVER| | 2024-2-28 22:27 | 只看该作者
单片机能够一直其他品牌呢?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

84

主题

1129

帖子

2

粉丝