#include <REG51F.H>
sbit SDA = P1^0;
sbit SCL = P1^1;
sbit key_status = P3^2;
sbit key_seclect = P3^3;
sbit key_inc = P3^1;
sbit key_dec = P3^0;
// khai bao bien cuc bo
unsigned char sec,min,hour,sec1,sec2,min1,min2,hour1,hour2,day,d ay1,date,month,year,date1,date2,month1,month2,year 1,year2;
unsigned char maled[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F };
// khai bao chuong tring con
void delay_us(unsigned char t);
void giophutgiay(void);
void thungaythangnam(void);
// chuong trinh con RTC
void i2c_start(void);
void i2c_stop(void);
void i2c_write(unsigned char data2send);
unsigned char i2c_read(bit ACK_bit);
void update_time(void);
void update_rtc(void);
// code chuong trinh con
void init_main(void)
{
P0=0;
P1=0;
P2=0;
P3=0xff;
}
void delay_us(unsigned char t)
{
unsigned char i;
for(i=0;i<=t;i++);
}
// code chuong trinh con giao tiep I2C
void i2c_start(void)
{
SDA=1;
SCL=1;
delay_us(10);
SDA=0;
SCL=0;
delay_us(10);
}
void i2c_stop(void)
{
SDA=0;
SCL=0;
delay_us(10);
SDA=1;
SCL=1;
delay_us(10);
}
void i2c_write(unsigned char data2send)
{
unsigned char i;
for(i=0;i<=7;i++)
{
SDA=(data2send&0x80)?1:0;
SCL=1;
delay_us(5); // truyen 1 byte du lieu tu song song sang noi tiep
SCL=1;
delay_us(5);
data2send<<=1;
}
SDA=0; // gui bit ACK bao ket thuc viec truyen xong 1 byte du lieu
SCL=1;
delay_us(5); // tao mot xung clock de nhan bit ACK
SCL=0;
}
unsigned char i2c_read(bit ACK_bit) // Bien ACK_bit co gia tri 0 hoac 1
{
unsigned char i,data_recive=0;
for(i=0;i<=7;i++)
{ SDA=1;
SCL=1;
data_recive<<=1;
data_recive=(data_recive|SDA);
delay_us(3);
SCL=0;
delay_us(5);
}
SDA=ACK_bit; // neu SDA=ACK_bit=0 thi ket thuc nhan du lieu, nguo
SCL=1;
delay_us(5); // Tao 1 xung clock de nhan bit ACK hay NOT ACK
SCL=0;
delay_us(5);
return data_recive; // tra ve gia tri nhan duoc
}
// Code giao tiep RTC
void update_time(void)
{
i2c_start();
i2c_write(0xD0); // dia chi ds1307
i2c_write(0x00); // bat dau ghi vao thanh ghi Giay
i2c_stop();
i2c_start();
i2c_write(0xD1);// Cho phep doc du lieu tu rtc
sec=i2c_read(0);
min=i2c_read(0);
hour=i2c_read(0);
day=i2c_read(0);
date=i2c_read(0);
month=i2c_read(0);
year=i2c_read(1);
i2c_stop();
// chuyen ma hien thi led 7seg
sec1=sec&0x0f; // lay 4 bit thap
sec2=(sec&0x70)>>4;
min1=min&0x0f;
min2=(min&0x70)>>4;
hour1=hour&0x0f;
hour2=(hour&0x30)>>4;
day1=day&0x0f;
date1=date&0x0f;
date2=(date&0x30)>>4;
month1=month&0x0f;
month2=(month&0x10)>>4;
year1=year&0x0f;
year2=(year&0xf0)>>4;
}
void giophutgiay(void)
{
P0=maled[sec1];
P2=0x01;
delay_us(100);
P2=0x00;
P0=maled[sec2]; // hien thi Giay
P2=0x02;
delay_us(100);
P2=0x00;
P0=0x40;
P2=0x04;
delay_us(100); // hien thi dau gach noi
P2=0;
P0=maled[min1];
P2=0x08;
delay_us(100);
P2=0;
P0=maled[min2];
P2=0x10;
delay_us(100);
P2=0;
P0=0x40;
P2=0x20; // hien thi dau gach noi
delay_us(100);
P2=0;
P0=maled[hour1];
P2=0x40;
delay_us(100);
P2=0;
P0=maled[hour2];
P2=0x80;
delay_us(100);
P2=0;
}
void ngaythangnam(void)
{
int i=0;
while(i<600)
{
P0=maled[year1];
P2=0x01;
delay_us(100);
P2=0; // hien thi nam
P0=maled[year2];
P2=0x02;
delay_us(100);
P2=0;
P0=0x40;
P2=0x04;
delay_us(100); // hien thi dau gach noi
P2=0;
P0=maled[month1];
P2=0x08;
delay_us(100);
P2=0;
P0=maled[month2];
P2=0x10;
delay_us(100);
P2=0;
P0=0x40;
P2=0x20; // hien thi dau gach noi
delay_us(100);
P2=0;
P0=maled[date1];
P2=0x40;
delay_us(100);
P2=0;
P0=maled[date2];
P2=0x80;
delay_us(100);
P2=0;
i++;
}
}
void update_rtc(void)
{
unsigned char d;
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // bat dau ghi tu thanh ghi Giay
d=sec1+(sec2<<4);
d=d&0x7f;
i2c_write(d);
d=min1+(min2<<4);
i2c_write(d);
d=hour1+(hour2<<4);
i2c_write(d);
d=day; // Vi ngay tu 1 - 7 ung voi SUN - SAT
i2c_write(d);
d=date1+(date2<<4);
i2c_write(d);
d=month1+(month2<<4);
i2c_write(d);
d=year1+(year2<<4);
i2c_write(d);
i2c_stop(); // ket thuc truyen du lieu
}
void set_min(void)
{
if(key_inc==0)
{
while(key_inc==0){}
delay_us(10);
min1++;
if((min1==10)&(min2=5))
{
min1=0; min2=0;
}
else if(min1==10)
{
min2++; min1=0;
}
}
if(key_dec==0)
{
while(key_dec==0){}
delay_us(10);
min1--;
if((min1==255)&(min2==0))
{
min1=9; min2=5;
}
if(min1==255)
{
min1=9;
min2--;
}
}
P0=maled[min1]; P2=0x04; delay_us(5000); P2=0;
P0=maled[min2]; P2=0x10; delay_us(5000); P2=0;
}
void set_hour(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10); // chong rung phim
hour1++;
if((hour1==4)&(hour2==2))
{
hour1=0; hour2=0;
}
if(hour1==10)
{
hour1=0; hour2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10); // chong rung phim
hour1--;
if((hour1==255)&(hour2==0))
{
hour1=3; hour2=2;
}
if(hour1==255)
{
hour1=0; hour2--;
}
}
P0=maled[hour1]; P2=0x40; delay_us(5000); P2=0;
P0=maled[hour2]; P2=0x80; delay_us(5000); P2=0;
}
void set_date(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10);
date1++;
if((date1==2)&(date2==3))
{
date1=1; date2=0;
}
if(date1==10)
{
date1=9; date2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10);
date--;
if((date1==0)&(date2==0))
{
date1=1; date2=3;
}
if(date1==255)
{
date2--; date1=0;
}
}
P0=maled[date1]; P2=0x80; delay_us(5000); P2=0;
P0=maled[date2]; P2=0x40; delay_us(5000); P2=0;
}
void set_month(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10);
month1++;
if((month1==3)&(month2==1))
{
month1=1; month2=0;
}
if(month1==10)
{
month1=0; month2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10);
month--;
if((month=0)&(month2=0))
{
month1=2; month2=1;
}
if(month1==0)
{
month1=9; month2--;
}
}
P0=maled[month1]; P2=0x08; delay_us(5000); P2=0;
P0=maled[month2]; P2=0x10; delay_us(5000); P2=0;
}
void set_year(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10);
year1++;
if((year1==10)&(year2==9))
{
year1=0; year2=0;
}
if(year1==10)
{
year1=0; year2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10);
year1--;
if((year1==255)&(year2==0));
{
year1=9; year2=9;
}
if(year1==255)
{
year1=9; year2--;
}
}
P0=maled[year1]; P2=0x01; delay_us(5000); P2=0; // nhap nhay led
P0=maled[year2]; P2=0x02; delay_us(5000); P2=0;
}
void scan_key(void)
{
int j=0,i=0;
if(!key_status)
{
ngaythangnam();
if(!key_seclect)
{
i++;
switch(i)
{
case 1: set_year();break;
case 2: set_month();break;
case 3: set_date();break;
}
}
}
else
{
giophutgiay();
if(!key_seclect)
{
j++;
switch(j)
{
case 1: set_min();break;
case 2: set_hour();break;
}
}
}
update_rtc();
}
// Chuog trinh chinh
void main()
{
min=0x57;
sec=0x00;
hour=0x10;
day=0x01;
date=0x27;
month=0x07;
year=0x10;
init_main();
while(1)
{
update_time();
scan_key();
}
}
bac nao cao thu giup em voi nha ! em ngoi viet ca buoi toi, cuoi cung thi ko chay duoc. Chan wa !
Cau truc phan cung: P0= data to LED 7 seg
P2= chon LED xx-xx-xx
sbit SDA = P1^0;
sbit SCL = P1^1;
sbit key_status = P3^2;
sbit key_seclect = P3^3;
sbit key_inc = P3^1;
sbit key_dec = P3^0;
// khai bao bien cuc bo
unsigned char sec,min,hour,sec1,sec2,min1,min2,hour1,hour2,day,d ay1,date,month,year,date1,date2,month1,month2,year 1,year2;
unsigned char maled[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F };
// khai bao chuong tring con
void delay_us(unsigned char t);
void giophutgiay(void);
void thungaythangnam(void);
// chuong trinh con RTC
void i2c_start(void);
void i2c_stop(void);
void i2c_write(unsigned char data2send);
unsigned char i2c_read(bit ACK_bit);
void update_time(void);
void update_rtc(void);
// code chuong trinh con
void init_main(void)
{
P0=0;
P1=0;
P2=0;
P3=0xff;
}
void delay_us(unsigned char t)
{
unsigned char i;
for(i=0;i<=t;i++);
}
// code chuong trinh con giao tiep I2C
void i2c_start(void)
{
SDA=1;
SCL=1;
delay_us(10);
SDA=0;
SCL=0;
delay_us(10);
}
void i2c_stop(void)
{
SDA=0;
SCL=0;
delay_us(10);
SDA=1;
SCL=1;
delay_us(10);
}
void i2c_write(unsigned char data2send)
{
unsigned char i;
for(i=0;i<=7;i++)
{
SDA=(data2send&0x80)?1:0;
SCL=1;
delay_us(5); // truyen 1 byte du lieu tu song song sang noi tiep
SCL=1;
delay_us(5);
data2send<<=1;
}
SDA=0; // gui bit ACK bao ket thuc viec truyen xong 1 byte du lieu
SCL=1;
delay_us(5); // tao mot xung clock de nhan bit ACK
SCL=0;
}
unsigned char i2c_read(bit ACK_bit) // Bien ACK_bit co gia tri 0 hoac 1
{
unsigned char i,data_recive=0;
for(i=0;i<=7;i++)
{ SDA=1;
SCL=1;
data_recive<<=1;
data_recive=(data_recive|SDA);
delay_us(3);
SCL=0;
delay_us(5);
}
SDA=ACK_bit; // neu SDA=ACK_bit=0 thi ket thuc nhan du lieu, nguo
SCL=1;
delay_us(5); // Tao 1 xung clock de nhan bit ACK hay NOT ACK
SCL=0;
delay_us(5);
return data_recive; // tra ve gia tri nhan duoc
}
// Code giao tiep RTC
void update_time(void)
{
i2c_start();
i2c_write(0xD0); // dia chi ds1307
i2c_write(0x00); // bat dau ghi vao thanh ghi Giay
i2c_stop();
i2c_start();
i2c_write(0xD1);// Cho phep doc du lieu tu rtc
sec=i2c_read(0);
min=i2c_read(0);
hour=i2c_read(0);
day=i2c_read(0);
date=i2c_read(0);
month=i2c_read(0);
year=i2c_read(1);
i2c_stop();
// chuyen ma hien thi led 7seg
sec1=sec&0x0f; // lay 4 bit thap
sec2=(sec&0x70)>>4;
min1=min&0x0f;
min2=(min&0x70)>>4;
hour1=hour&0x0f;
hour2=(hour&0x30)>>4;
day1=day&0x0f;
date1=date&0x0f;
date2=(date&0x30)>>4;
month1=month&0x0f;
month2=(month&0x10)>>4;
year1=year&0x0f;
year2=(year&0xf0)>>4;
}
void giophutgiay(void)
{
P0=maled[sec1];
P2=0x01;
delay_us(100);
P2=0x00;
P0=maled[sec2]; // hien thi Giay
P2=0x02;
delay_us(100);
P2=0x00;
P0=0x40;
P2=0x04;
delay_us(100); // hien thi dau gach noi
P2=0;
P0=maled[min1];
P2=0x08;
delay_us(100);
P2=0;
P0=maled[min2];
P2=0x10;
delay_us(100);
P2=0;
P0=0x40;
P2=0x20; // hien thi dau gach noi
delay_us(100);
P2=0;
P0=maled[hour1];
P2=0x40;
delay_us(100);
P2=0;
P0=maled[hour2];
P2=0x80;
delay_us(100);
P2=0;
}
void ngaythangnam(void)
{
int i=0;
while(i<600)
{
P0=maled[year1];
P2=0x01;
delay_us(100);
P2=0; // hien thi nam
P0=maled[year2];
P2=0x02;
delay_us(100);
P2=0;
P0=0x40;
P2=0x04;
delay_us(100); // hien thi dau gach noi
P2=0;
P0=maled[month1];
P2=0x08;
delay_us(100);
P2=0;
P0=maled[month2];
P2=0x10;
delay_us(100);
P2=0;
P0=0x40;
P2=0x20; // hien thi dau gach noi
delay_us(100);
P2=0;
P0=maled[date1];
P2=0x40;
delay_us(100);
P2=0;
P0=maled[date2];
P2=0x80;
delay_us(100);
P2=0;
i++;
}
}
void update_rtc(void)
{
unsigned char d;
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // bat dau ghi tu thanh ghi Giay
d=sec1+(sec2<<4);
d=d&0x7f;
i2c_write(d);
d=min1+(min2<<4);
i2c_write(d);
d=hour1+(hour2<<4);
i2c_write(d);
d=day; // Vi ngay tu 1 - 7 ung voi SUN - SAT
i2c_write(d);
d=date1+(date2<<4);
i2c_write(d);
d=month1+(month2<<4);
i2c_write(d);
d=year1+(year2<<4);
i2c_write(d);
i2c_stop(); // ket thuc truyen du lieu
}
void set_min(void)
{
if(key_inc==0)
{
while(key_inc==0){}
delay_us(10);
min1++;
if((min1==10)&(min2=5))
{
min1=0; min2=0;
}
else if(min1==10)
{
min2++; min1=0;
}
}
if(key_dec==0)
{
while(key_dec==0){}
delay_us(10);
min1--;
if((min1==255)&(min2==0))
{
min1=9; min2=5;
}
if(min1==255)
{
min1=9;
min2--;
}
}
P0=maled[min1]; P2=0x04; delay_us(5000); P2=0;
P0=maled[min2]; P2=0x10; delay_us(5000); P2=0;
}
void set_hour(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10); // chong rung phim
hour1++;
if((hour1==4)&(hour2==2))
{
hour1=0; hour2=0;
}
if(hour1==10)
{
hour1=0; hour2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10); // chong rung phim
hour1--;
if((hour1==255)&(hour2==0))
{
hour1=3; hour2=2;
}
if(hour1==255)
{
hour1=0; hour2--;
}
}
P0=maled[hour1]; P2=0x40; delay_us(5000); P2=0;
P0=maled[hour2]; P2=0x80; delay_us(5000); P2=0;
}
void set_date(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10);
date1++;
if((date1==2)&(date2==3))
{
date1=1; date2=0;
}
if(date1==10)
{
date1=9; date2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10);
date--;
if((date1==0)&(date2==0))
{
date1=1; date2=3;
}
if(date1==255)
{
date2--; date1=0;
}
}
P0=maled[date1]; P2=0x80; delay_us(5000); P2=0;
P0=maled[date2]; P2=0x40; delay_us(5000); P2=0;
}
void set_month(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10);
month1++;
if((month1==3)&(month2==1))
{
month1=1; month2=0;
}
if(month1==10)
{
month1=0; month2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10);
month--;
if((month=0)&(month2=0))
{
month1=2; month2=1;
}
if(month1==0)
{
month1=9; month2--;
}
}
P0=maled[month1]; P2=0x08; delay_us(5000); P2=0;
P0=maled[month2]; P2=0x10; delay_us(5000); P2=0;
}
void set_year(void)
{
if(!key_inc)
{
while(!key_inc){}
delay_us(10);
year1++;
if((year1==10)&(year2==9))
{
year1=0; year2=0;
}
if(year1==10)
{
year1=0; year2++;
}
}
if(!key_dec)
{
while(!key_dec){}
delay_us(10);
year1--;
if((year1==255)&(year2==0));
{
year1=9; year2=9;
}
if(year1==255)
{
year1=9; year2--;
}
}
P0=maled[year1]; P2=0x01; delay_us(5000); P2=0; // nhap nhay led
P0=maled[year2]; P2=0x02; delay_us(5000); P2=0;
}
void scan_key(void)
{
int j=0,i=0;
if(!key_status)
{
ngaythangnam();
if(!key_seclect)
{
i++;
switch(i)
{
case 1: set_year();break;
case 2: set_month();break;
case 3: set_date();break;
}
}
}
else
{
giophutgiay();
if(!key_seclect)
{
j++;
switch(j)
{
case 1: set_min();break;
case 2: set_hour();break;
}
}
}
update_rtc();
}
// Chuog trinh chinh
void main()
{
min=0x57;
sec=0x00;
hour=0x10;
day=0x01;
date=0x27;
month=0x07;
year=0x10;
init_main();
while(1)
{
update_time();
scan_key();
}
}
bac nao cao thu giup em voi nha ! em ngoi viet ca buoi toi, cuoi cung thi ko chay duoc. Chan wa !
Cau truc phan cung: P0= data to LED 7 seg
P2= chon LED xx-xx-xx
Comment