#include <REGX52.H>#define data P1#define rs P3_5#define rw P3_4#define en P3_3unsigned char msg[88];void delay(unsigned int);void LCD_busy();void LCD_command(unsigned char var);void LCD_data(unsigned char var);void LCD_string(unsigned char *var);void LCD_init(void);void LCD_READY(void);void LCD_init(void) { LCD_command (0x38); LCD_command (0x0C); LCD_command (0x01); LCD_command (0x06); }
void LCD_command(unsigned char var) { LCD_READY(); data = var; //Function set: 2 Line, 8-bit, 5x8 dots rs = 0; //Selected command register rw = 0; //We are writing in instruction register en = 1; //Enable H->L en = 0; }void LCD_string(unsigned char *var) { while(*var) //till string ends LCD_data(*var++); //send characters one by one }void LCD_data(unsigned char var) { LCD_READY(); data = var; //Function set: 2 Line, 8-bit, 5x7 dots rs = 1; //Selected data register rw = 0; //We are writing en = 1; //Enable H->L en = 0; }void LCD_READY(void) { P1_7=1; rs=0; rw=1; do { en=0; en=1; delay(1); }while(P1_7); }void delay(unsigned int m) { unsigned int i,j; for(i=0;i<m;i++) for(j=0;j<10;j++); } #include <regx52.H>#include <Lcd.h>char key_scan();unsigned char key[4][3]={'1','2','3', '4','5','6', '7','8','9', '*','0','#'};
char key_scan(){unsigned char col, row;P2=0xF0;while(1) { do { col=P2&0x70; }while(col!=0x70); do { do { delay(20); col=P2&0x70; }while(col==0x70); delay(20); col=P2&0x70; }while(col==0x70);while(1){ P2=0x7E; col=P2&0x70; if(col!=0x70) { row=0; break; }
P2=0x7D; col=P2&0x70; if(col!=0x70) { row=1; break; } P2=0x7B; col=P2&0x70; if(col!=0x70) { row=2; break; } P2=0x77; col=P2&0x70; row=3; break;}if(col==0x60) return(key[row][0]);else if(col==0x50) return(key[row][1]);else if(col==0x30) return(key[row][2]); } }#include <regx52.H>#include <absacc.h>#include "lcd.h"#include "rtc1.h"#include "keypad.h"
void rtc_init(){XBYTE[10]=0x20;delay(100);}
void time_set(){unsigned char temp[3];XBYTE[11]=0x83;LCD_command(0x01);LCD_string("Enter the Time : ");LCD_command(0xC0);LCD_string("HH:mm:ss");LCD_command(0xC0);//Enter hourtemp[0]=key_scan();LCD_data(temp[0]);temp[1]=key_scan();LCD_data(temp[1]);LCD_string(":");XBYTE[4]=int_value(temp);//Enter mintemp[0]=key_scan();LCD_data(temp[0]);temp[1]=key_scan();LCD_data(temp[1]);LCD_string(":");XBYTE[2]=int_value(temp);//Enter sectemp[0]=key_scan();LCD_data(temp[0]);temp[1]=key_scan();LCD_data(temp[1]);XBYTE[0]=int_value(temp);delay(500);XBYTE[11]=0x03;}
void alarm_set(){unsigned char temp[3];XBYTE[11]=0x83;LCD_command(0x01);LCD_string("Enter the Time : ");LCD_command(0xC0);LCD_string("HH:mm:ss");LCD_command(0xC0);//Enter hourtemp[0]=key_scan();LCD_data(temp[0]);temp[1]=key_scan();LCD_data(temp[1]);LCD_string(":");XBYTE[5]=int_value(temp);//Enter mintemp[0]=key_scan();LCD_data(temp[0]);temp[1]=key_scan();LCD_data(temp[1]);LCD_string(":");XBYTE[3]=int_value(temp);//Enter sectemp[0]=key_scan();LCD_data(temp[0]);temp[1]=key_scan();LCD_data(temp[1]);XBYTE[1]=int_value(temp);delay(800); LCD_command(0x01);LCD_string("Enter OFF Time:");LCD_command(0xC0);temp[0]=key_scan();LCD_data(temp[0]);if(temp[0]=='#'){XBYTE[11]=0x03;return;}temp[1]=key_scan();LCD_data(temp[1]);XBYTE[14]=int_value(temp);XBYTE[11]=0x03;}
void bcdconv(unsigned var){unsigned char x,y;x=var&0x0F;y=y>>4;y=y|0x30;LCD_data(y);LCD_data(x);}
int int_value(unsigned char var[]){int val;val=(var[0]<<4)|(var[1]&0x0F);return(val);}#include <regx52.H>#include <absacc.h>#include "lcd.h"#include "rtc1.h"#include "keypad.h"int time;void update();void relay();void time_display();void alarm_display();
int main(){int irq;irq=XBYTE[12];P3_2=1;rtc_init();LCD_init();while(1){LCD_command(0x01);time_display();if(XBYTE[12]&0x20)relay();if(P3_2==0)update();alarm_display(); delay(7500);} }
void update(){char n;LCD_command(0x01);LCD_string("1. Time Update");LCD_command(0xC0);LCD_string("2. ALARM UPDATE");n=key_scan();switch(n) { case '1' : time_set(); break; case '2' : alarm_set(); break; default : LCD_command(0x01); LCD_string("Try Again"); delay(100); LCD_command(0x01); } }
void time_display(){unsigned char hr,min,sec;LCD_command(0x80);LCD_string("TIME : ");hr=XBYTE[4];min=XBYTE[2];sec=XBYTE[0];bcdconv(hr);LCD_string(":");bcdconv(min);LCD_string(":");bcdconv(sec);}
void alarm_display(){LCD_command(0xC0);LCD_string("PW_OFF: ");bcdconv(XBYTE[5]);LCD_string(":");bcdconv(XBYTE[3]);LCD_string(":");bcdconv(XBYTE[1]);}
void relay(){int al_hr,al_min,hh,mm,temp;temp=XBYTE[12];time=XBYTE[14];time=(((time>>4)*10)+(time&0x0F));P3_1=0;hh=XBYTE[5];mm=XBYTE[3];hh=(((hh>>4)*10)+(hh&0x0F));mm=(((mm>>4)*10)+(mm&0x0F));al_min=time+mm;al_hr=al_min/60;al_hr=al_hr+hh;al_min=al_min%60;al_hr=al_hr%24;al_min=(((al_min/10)<<4)|(al_min%10));al_hr=(((al_hr/10)<<4)|(al_hr%10));XBYTE[3]=al_min;XBYTE[5]=al_hr;LCD_command(0xC0);LCD_string("PWR_ON: ");bcdconv(al_hr);LCD_string(":");bcdconv(al_min);LCD_string(":");bcdconv(XBYTE[1]);mm=(((mm/10)<<4)|(mm%10));hh=(((hh/10)<<4)|(hh%10));while(1){time_display();if(XBYTE[12]&0x20){P3_1=1;XBYTE[3]=mm;XBYTE[5]=hh;break;}}}
http://www.catb.org/esr/faqs/smart-questions.html#code