3. Code
#include <REGX52.H>
#include <MATH.H>
#define Menu P3_0 //phim menu de chon che do chinhthoigian
#define Next P3_1 //phim next de tang thoigian
#define Back P3_2 //phim back de giamthoigian
#define Timer_Date P3_3 //
#define LedCPU_on P0_0=0;
#define LedCPU_off P0_0=1;
#define SCL P3_5
#define SDA P3_6
//dinhnghiacho led 7 thanh
#define Data P2
#define Led1Sang P1=0x01;
#define Led2Sang P1=0x02;
#define Led3Sang P1=0x04;
#define Led4Sang P1=0x08;
#define Led5Sang P1=0x10;
#define Led6Sang P1=0x20;
#define Led7Sang P1=0x40;
#define Led8Sang P1=0x80;
#define OffLed P2=0xFF;
#define cham P2_7=0;
#define LedCPU P0_0
#define Relay_on P3_7=1;
#define Relay_off P3_7=0;
//dinhnghiadia chi cacthanhghitrong DS1307
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DAY 0x03
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
#define A_MIN 0x08
#define A_HOUR 0x09
#define yes 1
#define no 0
#define on 1
#define off 0
//cac ham dung cho DS1307
void I2C_Start(); //khoi dong cho DS1307
void I2C_stop();
unsigned char I2C_write(unsigned char dat); //ham ghi 1 byte du kieuvao DS1307
unsigned char I2C_read(); //ham doc 1 byte du kieuvao DS1307
unsigned char DS1307_Read(unsigned char addr); //ham doc du lieu tu 1 thanhghi bat ky
void DS1307_write(unsigned char addr,unsigned char dat); //ham ghi du lieu tu 1 thanhghi bat ky
void Write_DS1307(); //ham caidatthoigianvao DS1307
void Read_DS1307(); //ham doc thoigianvao DS1307
voidConvert_Number(); //ham chuyendoitach so de hienthitren led 7 thanh
voidDisplay_Gio_Phut_Giay(); //ham hienthigiophutgiaytren led 7 thanh
voidDisplay_Gio_Phut_Giay_Con_Lai();
voidDisplay_Gio_phut_giay_Hen_Gio();
voidScanKey(); //ham quetphimnhan
//khaibaobien //0 1 2 3 4 5 6 7 8 9
unsigned char Display_Number[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0};//khai baomangchuacacgia tri gom 10 phantu
unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhut HangDonVi,SoGiayHangChuc,SoGiayHangDonVi;
charSoGioConLai,SoPhutConlai,SoGiayConLai;
char SoGioConLaiHangChuc,SoGioConLaiHangDonVi,SoPhutCon LaiHangChuc,SoPhutConLaiHangDonVi,SoGiayConLaiHang Chuc,SoGiayConLaiHangDonVi;
char SoGioHenHangChuc,SoGioHenHangDonVi,SoPhutHenHangCh uc,SoPhutHenHangDonVi,SoGiayHenHangChuc,SoGiayHenH angDonVi;
charHenGio=24,HenPhut=0,HenGiay=0;
charMotPhanMuioiGiayHangChuc,MotPhanMuoiGiayHangDo nVi;
unsignedint Millisec,Millisec2,Millisec3; //biendemms
charMotPhanMuoiGiay;
charsec,hour,min,day,date,month,year; //cacbiendemgiophutgiay....
char sec2;
unsigned char kt_sec; //bienkiemtraxem RTC co hoat dong khong
unsigned char kt_sec2;
unsigned char kt_menu; //bienvaocacche do chinhthoigian
charbTimer; //kiemtratrangthaihienthi
//cac ham khaibao
voiddelay_ms(unsigned intms) //ham delay
{
unsignedinti,j;
for(i=0;i<ms;i++)
for(j=0;j<121;j++);
}
voiddelay_us(unsigned int us)
{
unsignedint i;
for(i=0;i<us;i++);
}
void ISR_1000ms(void) interrupt 1 // ham ngat timer 0 :1ms
{
TF0 = 0;//xoa co tran
TH0 =0xE2; // nap du lieu vaothanhghi timer
TL0 =0x0A;
TR0=1;
Millisec2++;
Millisec3++;
if(Millisec3>10000)
Millisec3=0;
Millisec++;
if(Millisec==0xFFFD)
Millisec=0;
}
//chuongtrinhchinh
void main(void)
{
//khoitaochobodem timer
TCON = 0x01; // che do 16 bit tu nap lai
TR0=1; //chophep timer hoat dong
TH0=0xfc;
TL0=0x18; //nap gia tri demchothanhghi timer 0
TR0=1; // chophép timer 0 hoat dong
TF0=0; //xoa co ngat
EA=1; //chophepngat
ET0=1; //chophepngat timer 0
LedCPU_off;
Relay_off;
Read_DS1307(); //doc thoigiantu DS1307
kt_sec=sec; //luugiayvao 1 bienkt_sec
delay_ms(2000);
Read_DS1307();//tre 1 khoangthoigiansau do doc tiep
kt_sec2=sec;
if(kt_sec==kt_sec2) //nguoclaineu DS1307 chuachaythiphai set thoigian
{
sec=8;min=7;hour=7;day=9;month=10;year=12;date=7;
Write_DS1307();// ghi du lieu vao DS1307
}
while (1)
{
ScanKey();
if(Millisec2>=100)
{
if(sec2!=sec)
MotPhanMuoiGiay=0;
Millisec2=0;
MotPhanMuoiGiay++;
if(MotPhanMuoiGiay>9)
MotPhanMuoiGiay=0;
}
if(Millisec>200)
{
Millisec=0;
if(kt_menu==0&&Millisec3>1000)
{
Read_DS1307();
Millisec3=0;
}
//hen gio o 24h,23phut,59giay
SoGioConLai=HenGio-hour-1;
SoPhutConlai=59-min;
SoGiayConLai=59-sec;
Convert_Number(); //cu 100 msthichuyendoi 1 lan
if(HenGio==hour)
{
LedCPU_on;
Relay_on;
}
}
if(bTimer==1&&kt_menu==0)
{
Display_Gio_Phut_Giay_Con_Lai();
}
else if(bTimer==0&&kt_menu==0) //trongche do giophutgiay
{
Display_Gio_Phut_Giay(); //hienthigioiphutgiaytren led 7thanh
}
else if(bTimer==2&&kt_menu==0) //trongche do hienthingaythangnam
{
Display_Gio_phut_giay_Hen_Gio(); //hienthithoigian can hen giolen led 7 thanh
}
sec2=sec;
}
}
voidScanKey() //ham quetphimnhan
{
if(Menu==0)//neuphim menu duocnhan
{
delay_ms(20);
kt_menu++; //bienluutrangthai menu hien tai
Write_DS1307();//cu moilannhanphimthiphaighi du lieu vao DS1307
if(kt_menu==4)
{
kt_menu=0; //trovetrangthaihienthithoigianbinhthuong
}
while(Menu==0);//chokhinaonhaphim menu
}
if(Timer_Date==0) //neuphim timer /date duocnhanthi se chuyenche do hienthi
{
delay_ms(20); //tre 20ms
bTimer++; //tang biendem
if(bTimer==3)
bTimer=0;
while(Timer_Date==0); //chocho den khiphim timer /date duocnhara
}
if(kt_menu!=0&&bTimer==0) //neu dang o trongche do hienthigiophutgiaybinhthuong
{
switch(kt_menu)
{
case 1: //chinhgio
{
if(Back==0)
{
delay_ms(20);
hour--;
if(hour<0)
hour=23;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
hour++;
if(hour==24)
hour=0;
while(Next==0);
}
Led1Sang;
Data=Display_Number[SoGioHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoGioHangDonVi];
delay_ms(1);
OffLed;
}
break; //chinhphut
case 2:
{
if(Back==0)
{
delay_ms(20);
min--;
if(min==0)
min=59;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
min++;
if(min==60)
min=0;
while(Next==0);
}
Led3Sang;
Data=Display_Number[SoPhutHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoPhutHangDonVi];
delay_ms(1);
OffLed;
}
break;
case 3: //chinhgiay
{
if(Back==0)
{
delay_ms(20);
sec--;
if(sec==0)
sec=59;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
sec++;
if(sec==60)
sec=0;
while(Next==0);
}
Led5Sang;
Data=Display_Number[SoGiayHangChuc]; //hienthi led giay hang chuc
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayHangDonVi]; //hienthi led giay hang don vi
delay_ms(1);
OffLed;
}
break;
}
}
else if(kt_menu!=0&&bTimer==2)
{
switch(kt_menu)
{
case 1: //chinhgio
{
if(Back==0)
{
delay_ms(20);
HenGio--;
if(HenGio<0)
HenGio=23;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
HenGio++;
if(HenGio==24)
HenGio=0;
while(Next==0);
}
Led1Sang;
Data=Display_Number[SoGioHenHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoGioHenHangDonVi];
delay_ms(1);
OffLed;
}
break; //chinhphut
case 2:
{
if(Back==0)
{
delay_ms(20);
HenPhut--;
if(HenPhut<=0)
HenPhut=59;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
HenPhut++;
if(HenPhut==60)
HenPhut=0;
while(Next==0);
}
Led3Sang;
Data=Display_Number[SoPhutHenHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoPhutHenHangDonVi];
delay_ms(1);
OffLed;
}
break;
case 3: //chinhgiay
{
if(Back==0)
{
delay_ms(20);
HenGiay--;
if(HenGiay<=0)
HenGiay=59;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
HenGiay++;
if(HenGiay==60)
HenGiay=0;
while(Next==0);
}
Led5Sang;
Data=Display_Number[SoGiayHenHangChuc];
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayHenHangDonVi];
delay_ms(1);
OffLed;
}
break;
}
}
}
//************************************************** ****************************
// CAC CHUONG TRINH CON CHO I2C
//************************************************** ****************************
void I2C_start() //ham khoi dong I2C
{
SCL = 1;
SCL = 0;
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
void I2C_stop()
{
SCL = 1; //keochan SCL len 1
SCL = 0;
SDA = 0;
SCL = 1;
SDA = 1;
}
unsigned char I2C_write(unsigned char dat)//ham ghi 1 byte du lieu vao DS1307
{
unsigned char i;
for (i=0;i<8;i++)
{
SDA = (dat& 0x80); ///? 1:0;
SCL=1;SCL=0;
dat<<=1;
}
SCL = 1;
SCL = 0;
returndat;
}
unsigned char I2C_read(void)
{
bitrd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
SCL = 1;
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
returndat;
}
// CAC CHUONG TRINH CON CHO DS1307
//************************************************** ****************************
unsigned char DS1307_read(unsigned char addr)
{
unsignedinttemp,ret;
I2C_start(); /* Start i2c bus */
I2C_write(0xD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(0XD1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop();
//************************************************** ********
temp = ret; /*BCD to HEX*/
ret = (((ret/16)*10)+ (temp & 0x0f)); /*for Led 7seg*/
//************************************************** ********
return ret;
}
void DS1307_Write(unsigned char addr,unsigned char dat)
{
unsignedint temp;
//**********************************************
temp = dat ; /*HEX to BCD*/
dat = (((dat/10)*16)|(temp %10)); /*for Led 7seg*/
//**********************************************
I2C_start(); /* Start i2c bus */
I2C_write(0XD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_write(dat); /* Connect to DS1307 for Read */
I2C_stop();
}
void Read_DS1307()
{
sec = DS1307_read(SEC); //doc giaytu DS1307
min = DS1307_read(MIN); //doc phuttu DS1307
hour = DS1307_read(HOUR); //doc giotu DS1307
// day = DS1307_read(DAY); //doc ngaytu DS1307
// date = DS1307_read(DATE); //doc thutu DS1307
// month = DS1307_read(MONTH); //doc thangtu DS1307
// year = DS1307_read(YEAR); //doc namtu DS1307
}
void Write_DS1307()
{
DS1307_Write(SEC,sec); //ghi du lieu vaothanhghigiay
DS1307_Write(MIN,min); //ghi du lieu vaothanhghiphut
DS1307_Write(HOUR,hour); //ghi du lieu vaothanhghigio
// DS1307_Write(DAY,day); //ghi du lieu vaothanhghingay
// DS1307_Write(DATE,date); //ghi du lieu vaothanhghithu
// DS1307_Write(MONTH,month); //ghi du lieu vaothanhghithang
// DS1307_Write(YEAR,year); //ghi du lieu vaothanhghinam
}
//ham quet led hienthigiophutgiay
voidDisplay_Gio_phut_giay()
{
Led1Sang; // led thunhat sang
Data=Display_Number[SoGioHangChuc]; //xuat du lieu ra led thunhat
delay_ms(1); //tre 1ms
Led2Sang;
Data=Display_Number[SoGioHangDonVi];
cham;
delay_ms(1);
Led3Sang;
Data=Display_Number[SoPhutHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoPhutHangDonVi];
cham;
delay_ms(1);
Led5Sang;
Data=Display_Number[SoGiayHangChuc];
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayHangDonVi];
cham;
delay_ms(1);
Led7Sang;
Data=Display_Number[0];
delay_ms(1);
Led8Sang;
Data=Display_Number[MotPhanMuoiGiay];
delay_ms(1);
OffLed;
}
voidDisplay_Gio_Phut_Giay_Con_Lai()
{
Led1Sang; // led thunhat sang
Data=Display_Number[SoGioConLaiHangChuc]; //xuat du lieu ra led thunhat
delay_ms(1); //tre 1ms
Led2Sang;
Data=Display_Number[SoGioConLaiHangDonVi];
cham;
delay_ms(1);
Led3Sang;
Data=Display_Number[SoPhutConLaiHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoPhutConLaiHangDonVi];
cham;
delay_ms(1);
Led5Sang;
Data=Display_Number[SoGiayConLaiHangChuc];
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayConLaiHangDonVi];
delay_ms(1);
OffLed;
}
voidDisplay_Gio_phut_giay_Hen_Gio()
{
Led1Sang; // led thunhat sang
Data=Display_Number[SoGioHenHangChuc]; //xuat du lieu ra led thunhat
delay_ms(1); //tre 1ms
Led2Sang;
Data=Display_Number[SoGioHenHangDonVi];
cham;
delay_ms(1);
Led3Sang;
Data=Display_Number[SoPhutHenHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoPhutHenHangDonVi];
cham;
delay_ms(1);
Led5Sang;
Data=Display_Number[SoGiayHenHangChuc];
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayHenHangDonVi];
delay_ms(1);
OffLed;
}
voidConvert_Number()
{
if(bTimer==2)
{
SoGioHenHangChuc=HenGio/10;
SoGioHenHangDonVi=HenGio%10;
SoPhutHenHangChuc=HenPhut/10;
SoPhutHenHangDonVi=HenPhut%10;
SoGiayHenHangChuc=HenGiay/10;
SoGiayHenHangDonVi=HenGiay%10;
}
else if(bTimer==1)
{
SoGioConLaiHangChuc=abs(SoGioConLai/10);
SoGioConLaiHangDonVi=abs(SoGioConLai%10);
SoPhutConLaiHangChuc=abs(SoPhutConlai/10);
SoPhutConLaiHangDonVi=abs(SoPhutConlai%10);
SoGiayConLaiHangChuc=abs(SoGiayConLai/10);
SoGiayConLaiHangDonVi=abs(SoGiayConLai%10);
}
else
{
SoGiayHangChuc=sec/10;
SoGiayHangDonVi=sec%10;
SoPhutHangChuc=min/10;
SoPhutHangDonVi=min%10;
SoGioHangChuc=hour/10;
SoGioHangDonVi=hour%10;
}
}
Comment