當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀] 用c語(yǔ)言寫的一個(gè)時(shí)鬧鐘程序單片機(jī)用16F877,主時(shí)鐘用20MHz,用32768作定時(shí)時(shí)間。可以實(shí)現(xiàn)2路定鬧,每一路都可分別設(shè)置和開關(guān),采用4x4鍵盤,16x2的字符型LCD顯示。連線在程序開頭有說(shuō)明。程序的功能:(

c語(yǔ)言寫的一個(gè)時(shí)鬧鐘程序

單片機(jī)用16F877,主時(shí)鐘用20MHz,用32768作定時(shí)時(shí)間??梢詫?shí)現(xiàn)2路定鬧,每一路都可分別設(shè)置和開關(guān),采用4x4鍵盤,16x2的字符型LCD顯示。連線在程序開頭有說(shuō)明。
程序的功能:
(1)上電后LCD背光打開,并顯示倒計(jì)時(shí)5秒,然后時(shí)鐘開始工作。
(2)用模式鍵(*)切換模式,如顯示時(shí)間、日期、鬧鐘1、鬧鐘2等,并且可以用上、下鍵控制加1、減1或是鬧鐘的On、Off。

(3)原程序有16個(gè)鍵,包括0~9數(shù)字鍵,可以直接輸入要設(shè)置的時(shí)間值,但后來(lái)將數(shù)字鍵取消了,你仍然可以通過(guò)修改程序的部分注釋恢復(fù)此功能。
(4)鬧鐘有2路,時(shí)間到后鬧2分鐘,可按任意鍵取消本次鬧鐘。鬧鐘響時(shí)有2種音調(diào),是用PIC的PWM實(shí)現(xiàn)的。
(5)按任意鍵可打開背光,1分鐘后自動(dòng)關(guān)閉背光。
(6)RA0~RA3為按鍵掃描輸入,應(yīng)接下拉電阻。



主程序
// FileName: Main.c
// MCU: MicroChipPIC16F877
// Tool: CCS-C compiler
// Author: KingEDA, MSN:kingeda@163.com, skype:kingeda, E-mail:kingeda@163.com
// Website:http://www.EDAKING.com
// Description:
// A timer program
// Ver 0.1: 2003-03-31, all cLOCk function with date display, 2 way alarm.
// Ver 0.2: 2003-05-05, (1) Alarm default is on,modify alarm1 time to 7:00:00,
// and alarm2 to 13:30:00.
// (2) Backlight will be enabLEDwhen alarming.
// (3) Automatic adjust day(28,30,31).
// (4) Automatic move cursor to next location when set item.
// PINConnection:
// RC0~1 : 32768Hz crystal
// RC2 : Buzzer
// RC3 : LCD Back Light,drive aPNPBJT
// RD0~RD7 : to LCD DB0~DB7
// RA0~RA3 : keypad col in
// RC4~RC7 : keypad line out
// 7 8 9 #
// 4 5 6 ↑
// 1 2 3 ↓
// 0 ← → *
// RE0 : LCD RS
// RE1 : LCD RW
// RE2 : LCD E

#include "my16f877.h"
#device ICD=true
//#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT
#use delay(clock = 24000000)
//#use fast_io(C)
#use fast_io(E)
#define lcd_busy (lcd_read_addr()&0x80) == 0x80
#define time_start_addr 0x80+0x04
#define time_hourh_addr time_start_addr
#define time_hourl_addr time_start_addr+1
#define time_minuteh_addr time_start_addr+3
#define time_minutel_addr time_start_addr+4
#define time_secondh_addr time_start_addr+6
#define time_secondl_addr time_start_addr+7
#define key_0 0x11
#define key_1 0x21
#define key_2 0x22
#define key_3 0x24
#define key_4 0x41
#define key_5 0x42
#define key_6 0x44
#define key_7 0x81
#define key_8 0x82
#define key_9 0x84
#define key_left 0x12
#define key_right 0x14
#define key_up 0x48
#define key_down 0x28
#define key_mode 0x18
#define key_CANcel 0x88

char StrPower1[] = " *Power on* ";
char StrSetTime[] = " * Adjust time* ";
char StrSetDate[] = " * Adjust date* ";
char StrAlarm1[] = " * Set alarm 1* ";
char StrAlarm2[] = " * Set alarm 2* ";
unsigned char PORTC_MAP;
#bit BackLightEn = PORTC_MAP.3
unsigned char BackLightTimer;
int1 led;
#bit lcd_rs = PORTE.0
#bit lcd_rw = PORTE.1
#bit lcd_e= PORTE.2
#byte lcd_bus = PORTD
#byte lcd_dir = TRISD
#define PWM_on 0x0c
#define PWM_off 0x00
#define PWM_period 200
#define PWM_DC 100
unsigned char lcd_addr;
unsigned char KeyLine;
unsigned char KeyOld;
unsigned char KeyNew;

struct mTime {
unsigned char hourh; // hour,0~23
unsigned char hourl;
unsigned char minuteh; // minute,0~59
unsigned char minutel;
unsigned char secondh; // second,0~59
unsigned char secondl;
};
struct mTime CurrentTime = {1,2,0,0,0,0};
struct mTime AlarmTime1 = {0,7,0,0,0,0}; // 07:00:00
struct mTime AlarmTime2 = {1,3,3,0,0,0}; // 13:30:00
unsigned char AlarmStatus;
#bit Alarm1Enable = AlarmStatus.0
#bit Alarm2Enable = AlarmStatus.1
#bit Alarm1Alarm = AlarmStatus.2
#bit Alarm2Alarm = AlarmStatus.3
unsigned char Alarm1Cnt; // alarm1 second count
unsigned char Alarm2Cnt;
unsigned char CurrentMode;
#define mode_time 0
#define mode_set_time 1
#define mode_set_date 2
#define mode_set_alarm1 3
#define mode_set_alarm2 4
unsigned char adjust_item;
struct mDate {
unsigned char year1; //
unsigned char year2;
unsigned char year3;
unsigned char year4;
unsigned char monthh;
unsigned char monthl;
unsigned char dayh;
unsigned char dayl;
};
struct mDate CurrentDate = {2,0,0,3,0,1,0,1};
unsigned char *pStr;

// -------------------------------------------------------
unsigned char lcd_read_addr()
{
unsigned char ch;
lcd_dir = 0xff; // read from lcd
lcd_rs = 0;
lcd_rw = 1; // inst
lcd_e = 1;
#asm
nop
nop
nop
#endasm
ch = lcd_bus;
lcd_e = 0;
lcd_dir = 0x00; //set write to lcd
return ch;
}

// -------------------------------------------------------
unsigned char lcd_write_data(unsigned char ch)
{
while (lcd_busy)
{ restart_wdt(); }
lcd_rs = 1; // data
lcd_rw = 0; // write
lcd_bus = ch; // write out
lcd_e = 1;
#asm
nop
nop
nop
#endasm
lcd_e = 0;
return 'Y';
}

// -------------------------------------------------------
unsigned char lcd_write_inst(unsigned char ch)
{
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';
}


// -------------------------------------------------------
unsigned char lcd_read_data()
{
unsigned char ch;
while (lcd_busy)
{ restart_wdt(); }
lcd_dir = 0xff; // read from lcd
lcd_rs = 1; // data
lcd_rw = 1; // read
lcd_e = 1;
#asm
nop
nop
nop
#endasm
ch = lcd_bus; // read in
lcd_e = 0;
lcd_dir = 0x00; //set write to lcd
return ch;
}

// -------------------------------------------------------
void lcd_init()
{
unsigned char Tempch;
lcd_addr = 0;
delay_ms(100);
Tempch = 0x38; // 1-line mode,5x8 dots
lcd_write_inst(Tempch); // Function set
Tempch = 0x0f; // lcd on,cursor on,blink on
lcd_write_inst(Tempch); // Display on/off
Tempch = 0x06; // Increment mode,Entire shift off
lcd_write_inst(Tempch);
Tempch = 0x01; // clear display
lcd_write_inst(Tempch);
delay_ms(3);
}

// -------------------------------------------------------
//#int_timer1
//void timer1_interrupt(void)
#int_ccp2
void ccp2_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++;
}
else if((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++;
}
else if(CurrentDate.monthl == 9)
{
CurrentDate.monthl = 0;
CurrentDate.monthh++;
}
else
CurrentDate.monthl++;
}
else if(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; // set pwm period
CCPR1L = PWM_DC; // set pwm duty cycle
//CCP1CON = PWM_on;
}
else
{
led = 1;
PR2 = PWM_period/2; // set pwm period
CCPR1L = PWM_DC/2; // set pwm duty cycle
//CCP1CON = PWM_off;
}
}
Alarm1Cnt++;
Alarm2Cnt++;
if (BackLightEn == 0)
if (((BackLightTimer++)>=60) & (Alarm1Alarm == false) & (Alarm1Alarm == false))
BackLightEn = 1; // dISAble backlight
PORTC = PORTC_MAP;
//TMR1IF = 0;
//PIR1 = PIR2 = 0x00;
CCP2IF = 0;
}

// -------------------------------------------------------
unsigned char get_key(void)
{
unsigned char key_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);
}
}
return 0;
}
else
return 0;
}

// -------------------------------------------------------
void set_mode(void)
{
if (CurrentMode == mode_set_alarm2)
CurrentMode = mode_time;
else
{
CurrentMode++;
adjust_item = 0;
}
lcd_write_inst(0x01); // clear LCD display
lcd_write_inst(time_start_addr); // set LCD line1
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;
}
else if(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;
}
else if(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;
}
else if(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); // set LCD line2
if (CurrentMode != mode_time)
{
for (;*pStr!=0;pStr++)
{ // write hint message
lcd_write_data(*pStr);
}
lcd_write_inst(0x0f); // LCD cursor on
lcd_write_inst(time_start_addr); // move cursor to start
}
else // time mode,write date to second line
{
lcd_write_inst(0x0c); // LCD sursor off
/* lcd_write_inst(0xc0 + 3); // set date start address
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); // move cursor to start
}
else if (CurrentMode == mode_set_date)
{
lcd_write_inst(time_start_addr); // move cursor to start
}
else if (CurrentMode == mode_set_alarm1)
{
lcd_write_inst(time_secondl_addr+3);
adjust_item = 6;
}
else if (CurrentMode == mode_set_alarm2)
{
lcd_write_inst(time_secondl_addr+3);
adjust_item = 6;
}
else
{
lcd_write_inst(0x0c); // LCD cursor off
}
}


// -------------------------------------------------------
void set_date(void)
{
if (adjust_item == 0) // adjust year
{
if ((KeyNew >=0) & (KeyNew <= 9))
{
CurrentDate.year1 = KeyNew;
lcd_write_data(CurrentDate.year1 + '0');
//lcd_write_inst(time_start_addr);
adjust_item ++;
}
else if (KeyNew == key_left)
{
adjust_item = 7;
lcd_write_inst(time_start_addr + 9);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 1);
}
}
else if(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 ++;
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 0);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 2);
}
}
else if(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 ++;
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 1);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 3);
}
}
else if(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);
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 2);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 5);
}
}
else if(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 ++;
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 3);
}
else if (KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 6);
}
}
else if(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);
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 5);
}
else if (KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 8);
}
}
else if(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 ++;
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 6);
}
else if (KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_start_addr + 9);
}
}
else if(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);
}
else if (KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_start_addr + 8);
}
else if (KeyNew == key_right)
{
adjust_item = 0;
lcd_write_inst(time_start_addr + 0);
}
}
}

// -------------------------------------------------------
void set_time(void)
{
if (adjust_item == 0) // set hourh
{
if (((CurrentTime.hourl <4) & (KeyNew < 3)) | ((CurrentTime.hourl >3) & (KeyNew <2)))
{
CurrentTime.hourh = KeyNew;
lcd_write_data(CurrentTime.hourh + '0'); // refresh hourh
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item = 5;
lcd_write_inst(time_secondl_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_hourl_addr);
}
}
else if (adjust_item == 1) // set hourl
{
if (((CurrentTime.hourh == 2) & (KeyNew < 4)) | ((CurrentTime.hourh < 2) & (KeyNew <=9)))
{
CurrentTime.hourl = KeyNew;
lcd_write_data(CurrentTime.hourl + '0'); // refresh hourl
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_minuteh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_hourh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_minuteh_addr);
}
}
else if (adjust_item == 2) // set minuteh
{
if (KeyNew <6)
{
CurrentTime.minuteh = KeyNew;
lcd_write_data(CurrentTime.minuteh + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_hourl_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_minutel_addr);
}
}
else if (adjust_item == 3) // set minutel
{
if ((KeyNew >=0) & (KeyNew <=9))
{
CurrentTime.minutel = KeyNew;
lcd_write_data(CurrentTime.minutel + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_secondh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_minuteh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondh_addr);
}
}
else if (adjust_item == 4) // set secondh
{
if (KeyNew <6)
{
CurrentTime.secondh = KeyNew;
lcd_write_data(CurrentTime.secondh + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item --;
&nb, sp; lcd_write_inst(time_minutel_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondl_addr);
}
}
else if (adjust_item == 5) // set secondl
{
if ((KeyNew >=0) & (KeyNew <=9))
{
CurrentTime.secondl = KeyNew;
lcd_write_data(CurrentTime.secondl + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item = 0;
lcd_write_inst(time_hourh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_secondh_addr);
}
else if(KeyNew == key_right)
{
adjust_item = 0;
lcd_write_inst(time_hourh_addr);
}
}
}

// -------------------------------------------------------
void set_alarm1(void)
{
if (adjust_item == 0) // set hourh
{
if (((AlarmTime1.hourl <4) & (KeyNew < 3)) | ((AlarmTime1.hourl >3) & (KeyNew <2)))
{
AlarmTime1.hourh = KeyNew;
lcd_write_data(AlarmTime1.hourh + '0'); // refresh hourh
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item = 6;
lcd_write_inst(time_secondl_addr + 3);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_hourl_addr);
}
}
else if (adjust_item == 1) // set hourl
{
if (((AlarmTime1.hourh == 2) & (KeyNew < 4)) | ((AlarmTime1.hourh < 2) & (KeyNew <=9)))
{
AlarmTime1.hourl = KeyNew;
lcd_write_data(AlarmTime1.hourl + '0'); // refresh hourl
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_minuteh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_hourh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_minuteh_addr);
}
}
else if (adjust_item == 2) // set minuteh
{
if (KeyNew <6)
{
AlarmTime1.minuteh = KeyNew;
lcd_write_data(AlarmTime1.minuteh + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_hourl_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_minutel_addr);
}
}
else if (adjust_item == 3) // set minutel
{
if ((KeyNew >=0) & (KeyNew <=9))
{
AlarmTime1.minutel = KeyNew;
lcd_write_data(AlarmTime1.minutel + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_secondh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_minuteh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondh_addr);
}
}
else if (adjust_item == 4) // set secondh
{
if (KeyNew <6)
{
AlarmTime1.secondh = KeyNew;
lcd_write_data(AlarmTime1.secondh + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_minutel_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondl_addr);
}
}
else if (adjust_item == 5) // set secondl
{
if ((KeyNew >=0) & (KeyNew <=9))
{
AlarmTime1.secondl = KeyNew;
lcd_write_data(AlarmTime1.secondl + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_secondl_addr+3);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_secondh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondl_addr+3);
}
}
else if (adjust_item == 6) // set on/off
{
if ((KeyNew == key_up) | (KeyNew == key_down))
{
if (Alarm1Enable)
{
Alarm1Enable =false; // disable alarm1
lcd_write_data('f');
lcd_write_data('f');
}
else
{
Alarm1Enable =true; // enable alarm1
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;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_secondl_addr);
}
else if(KeyNew == key_right)
{
adjust_item = 0;
lcd_write_inst(time_hourh_addr);
}
}

}

// -------------------------------------------------------
void set_alarm2(void)
{
if (adjust_item == 0) // set hourh
{
if (((AlarmTime2.hourl <4) & (KeyNew < 3)) | ((AlarmTime2.hourl >3) & (KeyNew <2)))
{
AlarmTime2.hourh = KeyNew;
lcd_write_data(AlarmTime2.hourh + '0'); // refresh hourh
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item = 6;
lcd_write_inst(time_secondl_addr+3);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_hourl_addr);
}
}
else if (adjust_item == 1) // set hourl
{
if (((AlarmTime2.hourh == 2) & (KeyNew < 4)) | ((AlarmTime2.hourh < 2) & (KeyNew <=9)))
{
AlarmTime2.hourl = KeyNew;
lcd_write_data(AlarmTime2.hourl + '0'); // refresh hourl
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_minuteh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_hourh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_minuteh_addr);
}
}
else if (adjust_item == 2) // set minuteh
{
if (KeyNew <6)
{
AlarmTime2.minuteh = KeyNew;
lcd_write_data(AlarmTime2.minuteh + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_hourl_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_minutel_addr);
}
}
else if (adjust_item == 3) // set minutel
{
if ((KeyNew >=0) & (KeyNew <=9))
{
AlarmTime2.minutel = KeyNew;
lcd_write_data(AlarmTime2.minutel + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_secondh_addr);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_minuteh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondh_addr);
}
}
else if (adjust_item == 4) // set secondh
{
if (KeyNew <6)
{
AlarmTime2.secondh = KeyNew;
lcd_write_data(AlarmTime2.secondh + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_minutel_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondl_addr);
}
}
else if (adjust_item == 5) // set secondl
{
if ((KeyNew >=0) & (KeyNew <=9))
{
AlarmTime2.secondl = KeyNew;
lcd_write_data(AlarmTime2.secondl + '0');
//lcd_write_inst(0x10); // move cursor back
adjust_item ++;
lcd_write_inst(time_secondl_addr+3);
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_secondh_addr);
}
else if(KeyNew == key_right)
{
adjust_item ++;
lcd_write_inst(time_secondl_addr+3);
}
}
else if (adjust_item == 6) // set on/off
{
if ((KeyNew == key_up) | (KeyNew == key_down))
{
if (Alarm2Enable)
{
Alarm2Enable =false; // disable alarm2
lcd_write_data('f');
lcd_write_data('f');
}
else
{
Alarm2Enable =true; // enable alarm2
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;
}
else if(KeyNew == key_left)
{
adjust_item --;
lcd_write_inst(time_secondl_addr);
}
else if(KeyNew == key_right)
{
adjust_item = 0;
lcd_write_inst(time_hourh_addr);
}
}

}

// -------------------------------------------------------
void main(void)
{
unsigned char cnt;
TRISC = 0x03; // PORTC.3 drive led,low active
PORTC_MAP = 0x00;
led = 0;
BackLightEn = 0;
BackLightTimer = 0;
PORTC = PORTC_MAP;
TRISA = 0xff; // low half byte as keyscan in
TRISE = 0x00;
ADCON0 = 0x00;
ADCON1 = 0x06; // all digital I/Os
lcd_init();
INTCON = 0x00;
lcd_write_inst(0x80); // set lcd ddram address
for (pStr = StrPower1;*pStr!=0;pStr++)
{
lcd_write_data(*pStr);
}
lcd_write_inst(0x0c); // LCD cursor off
PIR1 = PIR2 = 0x00;
T1CON = 0x0f; // T1CON: -- T1CKPS1 T1CPS0 T1OSCEN /T1SYNC TMR1CS TMR1ON
TMR1H = 0x80;
TMR1L = 0x00;
TMR1IF = 0;
CCPR2H = 0x7f;
CCPR2L = 0xff;
CCP2CON = 0x0b; // compare mode,set ccp2if,reset tmr1
CCP2IF = 0;
for (cnt=5;cnt>0;cnt--) // test for 5sec.
{
lcd_write_inst(0xc0+8); // set LCD adress
lcd_write_data(cnt + '0');
while (CCP2IF == 0) // wait for timer1 overflow
{
restart_wdt();
}
//TMR1H = 0x80;
CCP2IF = 0;
}
// write current date to lcd line 2
lcd_write_inst(0x01); // clear LCD display
lcd_write_inst(0xc0+3); // set LCD line2
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; // set GIE & PEIE
//TMR1IE = 1; // enable timer1 interrupt
CCP2IE = 1; // enable ccp2 interrupt
CurrentMode = mode_time;
Alarm1Cnt = Alarm2Cnt = 0;
T2CON = 0x07; // T2CON: - TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
// timer2 enable,prescaler = 16
PR2 = PWM_period; // set pwm period
CCPR1L = PWM_DC; // set pwm duty cycle
CCP1CON = PWM_off;
while(1)
{
restart_wdt();
KeyOld = get_key();
delay_ms(20);
KeyNew = get_key();
if (((KeyNew & 0x0f) != 0x00) & (KeyNew == KeyOld))
{ // some key pressed
if (BackLightEn == 1) // back light not on
{
BackLightEn = 0; // enable back-light
BackLightTimer = 0; // start delay counter
}
else
{
if (KeyNew == key_0)
KeyNew = 0;
else if(KeyNew == key_1)
KeyNew = 1;
else if(KeyNew == key_2)
KeyNew = 2;
else if(KeyNew == key_3)
KeyNew = 3;
else if(KeyNew == key_4)
KeyNew = 4;
else if(KeyNew == key_5)
KeyNew = 5;
else if(KeyNew == key_6)
KeyNew = 6;
else if(KeyNew == key_7)
KeyNew = 7;
else if(KeyNew == key_8)
KeyNew = 8;
else if(KeyNew == key_9)
KeyNew = 9;
else if (KeyNew == key_mode) // MODE key pressed
set_mode();
else if (KeyNew == key_cancel) // cancel buzzy
{
led = 0;
CCP1CON = PWM_off;
BackLightEn = 1;
BackLightTimer = 0;
}
if (CurrentMode == mode_set_time)
{
set_time();
}
else if (CurrentMode == mode_set_date)
{
set_date();
}
else if (CurrentMode == mode_set_alarm1)
{
set_alarm1();
}
else if (CurrentMode == mode_set_alarm2)
{
set_alarm2();
}
}
do // wait for key released
{
delay_ms(30);
KeyNew = get_key();
restart_wdt();
BackLightTimer = 0; // key pressed yet
}while ((KeyNew& 0x0f) != 0x00);
KeyOld = KeyNew = 0x00;
}
if ((CurrentMode == mode_set_time) | (CurrentMode == mode_time))
{ // refresh time display,bacause int_timer1 dosn't do this
lcd_addr = lcd_read_addr() & 0x7f; // save cursor location
lcd_write_inst(time_start_addr); // set LCD line1
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); // resume cursor location
}
if (CurrentMode == mode_time)
{ // refresh date display
lcd_addr = lcd_read_addr() & 0x7f; // save cursor location
lcd_write_inst(0xc0 + 3); // set LCD line2
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); // resume cursor location
}
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) // two minutes
{
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) // two minutes
{
Alarm2Alarm = false;
Alarm2Cnt = 0;
BackLightEn = 1;
BackLightTimer = 0;
}
}
else
{
Alarm2Alarm = false;
Alarm2Cnt = 0;
}
}
}

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉