Mình làm đồ án về đồng hồ vạn năng hiển thị trên 8 led 7 thanh
phần thời gian : giờ-phút-giây
phần ngày : ngày-tháng-năm(xxxx)
mạch chạy Nhưng mình gặp 1 số lỗi sau :
-Phần hiển thị các số còn nhấp nháy .
-Mình thấy phần thời gian chậm so với trên máy tính là 10s , Phần ngày chậm 5 ngày
(1 có tải 1 số mô phỏng trên mạng về thì cũng thấy bị chậm tương tự như vậy - không biết do máy hay do mạch và code )
Code và mô phỏng :
Link download
Đây là code ( Như thế này có quá dài không mọi người )
Cần phải sửa đâu cho hợp lý mọi người chỉ bảo nhé
phần thời gian : giờ-phút-giây
phần ngày : ngày-tháng-năm(xxxx)
mạch chạy Nhưng mình gặp 1 số lỗi sau :
-Phần hiển thị các số còn nhấp nháy .
-Mình thấy phần thời gian chậm so với trên máy tính là 10s , Phần ngày chậm 5 ngày
(1 có tải 1 số mô phỏng trên mạng về thì cũng thấy bị chậm tương tự như vậy - không biết do máy hay do mạch và code )
Code và mô phỏng :
Link download
Đây là code ( Như thế này có quá dài không mọi người )
Cần phải sửa đâu cho hợp lý mọi người chỉ bảo nhé
/*
*/
#include <REGX52.H>
#define Menu P3_0 //phim menu de chon che do chinh thoi gian
#define Next P3_1 //phim next de tang thoi gian
#define Back P3_2 //phim back de giam thoi gian
#define Timer_Date P3_3 //phim de chuyen che do hien thi
#define LedCPU P3_4
#define LedCPU_on P1_3=0;
#define LedCPU_off P1_3=1;
#define SCL P3_5
#define SDA P3_6
//dinh nghia cho 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;
//dinh nghia dia chi cac thanh ghi trong 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 kieu vao DS1307
unsigned char I2C_read(); //ham doc 1 byte du kieu vao DS1307
unsigned char DS1307_Read(unsigned char addr); //ham doc du lieu tu 1 thanh ghi bat ky
void DS1307_write(unsigned char addr,unsigned char dat); //ham ghi du lieu tu 1 thanh ghi bat ky
void Write_DS1307(); //ham cai dat thoi gian vao DS1307
void Read_DS1307(); //ham doc thoi gian vao DS1307
void Convert_Number(); //ham chuyen doi tach so de hien thi tren led 7 thanh
void Display_Gio_Phut_Giay(); //ham hien thi gio phut giay tren led 7 thanh
void Display_Ngay_Thang_Nam(); //ham hien thi ngay thang nam
void ScanKey(); //ham quet phim nhan
//khai bao bien //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 bao mang chua cac gia tri gom 10 phan tu
unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhut HangDonVi,SoGiayHangChuc,SoGiayHangDonVi;
unsigned char SoNgayHangChuc,SoNgayHangDonVi,SoThangHangChuc,SoT hangHangDonVi,SoNamHangChuc,SoNamHangDonVi;
unsigned int Millisec; //bien dem ms
char sec,hour,min,day,date,month,year; //cac bien dem gio phut giay....
unsigned char kt_sec; //bien kiem tra xem RTC co hoat dong khong
unsigned char kt_sec2;
unsigned char kt_menu; //bien vao cac che do chinh thoi gian
bit bTimer; //kiem tra trang thai hien thi
//cac ham khai bao
void delay_ms(unsigned int ms) //ham delay
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<121;j++);
}
void delay_us(unsigned int us)
{
unsigned int i;
for(i=0;i<us;i++);
}
void ISR_1000ms(void) interrupt 1 // ham ngat timer 0 :1ms
{
TF0 = 0;//xoa co tran
TH0 =0xE5; // nap du lieu vao thanh ghi timer
TL0 =0xF8;
TR0=1;
Millisec++;
if(Millisec==0xFFFD)
Millisec=0;
if(Millisec%1000==0)
LedCPU=~LedCPU;
}
//chuong trinh chinh
void main(void)
{
LedCPU_on;
Read_DS1307(); //doc thoi gian tu DS1307
kt_sec=sec; //luu giay vao 1 bien kt_sec
delay_ms(2000);
Read_DS1307();//tre 1 khoang thoi gian sau do doc tiep
kt_sec2=sec;
if(kt_sec==kt_sec2) //nguoc lai neu DS1307 chua chay thi phai set thoi gian
{
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(Millisec%500==0)
{
LedCPU=~LedCPU; //nhap nhay den led
Convert_Number(); //cu 100 ms thi chuyen doi 1 lan
}
if(bTimer==1&&kt_menu==0) //trong che do hien thi ngay thang nam
{
if(Millisec%500==0) //500ms doc thoi gian tu DS1307 1 lan
Read_DS1307(); //doc thoi gian tu DS1307
Display_Ngay_Thang_Nam();//hien thi ngay thang nam len led 7 thanh
}
else if(bTimer==0&&kt_menu==0) //trong che do gio phut giay
{
if(Millisec%500==0)
Read_DS1307();
Display_Gio_Phut_Giay(); //hien thi gioi phut giay tren led 7thanh
}
}
}
void ScanKey()
{
if(Menu==0)//neu phim menu duoc nhan
{
delay_ms(20);
kt_menu++; //bien luu trang thai menu hien tai
Write_DS1307();//cu moi lan nhan phim thi phai ghi du lieu vao DS1307
if(kt_menu==7)
{
kt_menu=0; //tro ve trang thai hien thi thoi gian binh thuong
}
while(Menu==0);//cho khi nao nha phim menu
}
if(Timer_Date==0)
{
delay_ms(20);
bTimer=~bTimer;
while(Timer_Date==0);
}
if(kt_menu!=0)
{
switch(kt_menu)
{
case 1: //chinh thoi gian ngay
{
if(Back==0)
{
delay_ms(20);
day--;
if(day==0)
day=31;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
day++;
if(day==32)
day=1;
while(Next==0);
}
Led1Sang;
Data=Display_Number[SoNgayHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoNgayHangDonVi];
delay_ms(1);
OffLed;
}
break;
case 2: //chinh thang
{
if(Back==0)
{
delay_ms(20);
month--;
if(month==0)
month=12;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
month++;
if(month==13)
month=1;
while(Next==0);
}
Led3Sang;
Data=Display_Number[SoThangHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoThangHangDonVi];
delay_ms(1);
OffLed;
}
break;
case 3: //chinh nam
{
if(Back==0)
{
delay_ms(20);
year--;
if(year==0)
year=99;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
year++;
if(month==100)
year=0;
while(Next==0);
}
Led7Sang;
Data=Display_Number[SoNamHangChuc];
delay_ms(1);
Led8Sang;
Data=Display_Number[SoNamHangDonVi];
delay_ms(1);
OffLed; }
break;
case 4: //chinh gio
{
if(Back==0)
{
delay_ms(20);
hour--;
if(hour==0)
hour=59;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
hour++;
if(hour==60)
hour=0;
while(Next==0);
}
Led1Sang;
Data=Display_Number[SoGioHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoGioHangDonVi];
delay_ms(1);
OffLed;
}
break; //chinh phut
case 5:
{
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 6: //chinh giay
{
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];
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayHangDonVi];
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;
delay_us(2);
SDA = 0;
delay_us(2);
SCL = 0;
delay_us(2);
}
void I2C_stop()
{
SCL = 1; //keo chan SCL len 1
SCL = 0;
SDA = 0;
delay_us(2);
SCL = 1;
delay_us(2);
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; delay_us(2);
SCL = 0;
return dat;
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
delay_us(2);
SCL = 1;
delay_us(2); /* Set SCL */
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
// CAC CHUONG TRINH CON CHO DS1307
//************************************************** ****************************
unsigned char DS1307_read(unsigned char addr)
{
unsigned int temp,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)
{
unsigned int 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 giay tu DS1307
min = DS1307_read(MIN); //doc phut tu DS1307
hour = DS1307_read(HOUR); //doc gio tu DS1307
day = DS1307_read(DAY); //doc ngay tu DS1307
date = DS1307_read(DATE); //doc thu tu DS1307
month = DS1307_read(MONTH); //doc thang tu DS1307
year = DS1307_read(YEAR); //doc nam tu DS1307
}
void Write_DS1307()
{
DS1307_Write(SEC,sec); //ghi du lieu vao thanh ghi giay
DS1307_Write(MIN,min); //ghi du lieu vao thanh ghi phut
DS1307_Write(HOUR,hour); //ghi du lieu vao thanh ghi gio
DS1307_Write(DAY,day); //ghi du lieu vao thanh ghi ngay
DS1307_Write(DATE,date); //ghi du lieu vao thanh ghi thu
DS1307_Write(MONTH,month); //ghi du lieu vao thanh ghi thang
DS1307_Write(YEAR,year); //ghi du lieu vao thanh ghi nam
}
//ham quet led hien thi gio phut giay
void Display_Gio_phut_giay()
{
Led1Sang; // led thu nhat sang
Data=Display_Number[SoGioHangChuc]; //xuat du lieu ra led thu nhat
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];
delay_ms(1);
OffLed;
}
//void Display_Gio_phut_giay()
//{
// if(uiTimeDelay<2)
// {
// Led1Sang; // led thu nhat sang
// Data=Display_Number[SoGioHangChuc]; //xuat du lieu ra led thu nhat
// }
// else if(uiTimeDelay<4)
// {
// Led2Sang;
// Data=Display_Number[SoGioHangDonVi];
// }
// else if(uiTimeDelay<6)
// {
// Led3Sang;
// Data=Display_Number[SoPhutHangChuc];
// }
// else if(uiTimeDelay<8)
// {
// Led4Sang;
// Data=Display_Number[SoPhutHangDonVi];
// }
// else if(uiTimeDelay<10)
// {
// Led5Sang;
// Data=Display_Number[SoGiayHangChuc];
// }
// else if(uiTimeDelay<12)
// {
// Led6Sang;
// Data=Display_Number[SoGiayHangDonVi];
// }
// else
// uiMocThoiGian=Millisec;
//}
//ham quet led hien thi ngay thang nam
void Display_Ngay_Thang_Nam()
{
Led1Sang;
Data=Display_Number[SoNgayHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoNgayHangDonVi];
cham;
delay_ms(1);
Led3Sang;
Data=Display_Number[SoThangHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoThangHangDonVi];
cham;
delay_ms(1);
Led5Sang;
Data=Display_Number[2];
delay_ms(1);
Led6Sang;
Data=Display_Number[0];
delay_ms(1);
Led7Sang;
Data=Display_Number[SoNamHangChuc];
delay_ms(1);
Led8Sang;
Data=Display_Number[SoNamHangDonVi];
delay_ms(1);
OffLed;
}
void Convert_Number()
{
SoNgayHangChuc=day/10;
SoNgayHangDonVi=day%10;
SoThangHangChuc=month/10;
SoThangHangDonVi=month%10;
SoNamHangChuc=year/10;
SoNamHangDonVi=year%10;
SoGiayHangChuc=sec/10;
SoGiayHangDonVi=sec%10;
SoPhutHangChuc=min/10;
SoPhutHangDonVi=min%10;
SoGioHangChuc=hour/10;
SoGioHangDonVi=hour%10;
}
*/
#include <REGX52.H>
#define Menu P3_0 //phim menu de chon che do chinh thoi gian
#define Next P3_1 //phim next de tang thoi gian
#define Back P3_2 //phim back de giam thoi gian
#define Timer_Date P3_3 //phim de chuyen che do hien thi
#define LedCPU P3_4
#define LedCPU_on P1_3=0;
#define LedCPU_off P1_3=1;
#define SCL P3_5
#define SDA P3_6
//dinh nghia cho 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;
//dinh nghia dia chi cac thanh ghi trong 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 kieu vao DS1307
unsigned char I2C_read(); //ham doc 1 byte du kieu vao DS1307
unsigned char DS1307_Read(unsigned char addr); //ham doc du lieu tu 1 thanh ghi bat ky
void DS1307_write(unsigned char addr,unsigned char dat); //ham ghi du lieu tu 1 thanh ghi bat ky
void Write_DS1307(); //ham cai dat thoi gian vao DS1307
void Read_DS1307(); //ham doc thoi gian vao DS1307
void Convert_Number(); //ham chuyen doi tach so de hien thi tren led 7 thanh
void Display_Gio_Phut_Giay(); //ham hien thi gio phut giay tren led 7 thanh
void Display_Ngay_Thang_Nam(); //ham hien thi ngay thang nam
void ScanKey(); //ham quet phim nhan
//khai bao bien //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 bao mang chua cac gia tri gom 10 phan tu
unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhut HangDonVi,SoGiayHangChuc,SoGiayHangDonVi;
unsigned char SoNgayHangChuc,SoNgayHangDonVi,SoThangHangChuc,SoT hangHangDonVi,SoNamHangChuc,SoNamHangDonVi;
unsigned int Millisec; //bien dem ms
char sec,hour,min,day,date,month,year; //cac bien dem gio phut giay....
unsigned char kt_sec; //bien kiem tra xem RTC co hoat dong khong
unsigned char kt_sec2;
unsigned char kt_menu; //bien vao cac che do chinh thoi gian
bit bTimer; //kiem tra trang thai hien thi
//cac ham khai bao
void delay_ms(unsigned int ms) //ham delay
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<121;j++);
}
void delay_us(unsigned int us)
{
unsigned int i;
for(i=0;i<us;i++);
}
void ISR_1000ms(void) interrupt 1 // ham ngat timer 0 :1ms
{
TF0 = 0;//xoa co tran
TH0 =0xE5; // nap du lieu vao thanh ghi timer
TL0 =0xF8;
TR0=1;
Millisec++;
if(Millisec==0xFFFD)
Millisec=0;
if(Millisec%1000==0)
LedCPU=~LedCPU;
}
//chuong trinh chinh
void main(void)
{
LedCPU_on;
Read_DS1307(); //doc thoi gian tu DS1307
kt_sec=sec; //luu giay vao 1 bien kt_sec
delay_ms(2000);
Read_DS1307();//tre 1 khoang thoi gian sau do doc tiep
kt_sec2=sec;
if(kt_sec==kt_sec2) //nguoc lai neu DS1307 chua chay thi phai set thoi gian
{
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(Millisec%500==0)
{
LedCPU=~LedCPU; //nhap nhay den led
Convert_Number(); //cu 100 ms thi chuyen doi 1 lan
}
if(bTimer==1&&kt_menu==0) //trong che do hien thi ngay thang nam
{
if(Millisec%500==0) //500ms doc thoi gian tu DS1307 1 lan
Read_DS1307(); //doc thoi gian tu DS1307
Display_Ngay_Thang_Nam();//hien thi ngay thang nam len led 7 thanh
}
else if(bTimer==0&&kt_menu==0) //trong che do gio phut giay
{
if(Millisec%500==0)
Read_DS1307();
Display_Gio_Phut_Giay(); //hien thi gioi phut giay tren led 7thanh
}
}
}
void ScanKey()
{
if(Menu==0)//neu phim menu duoc nhan
{
delay_ms(20);
kt_menu++; //bien luu trang thai menu hien tai
Write_DS1307();//cu moi lan nhan phim thi phai ghi du lieu vao DS1307
if(kt_menu==7)
{
kt_menu=0; //tro ve trang thai hien thi thoi gian binh thuong
}
while(Menu==0);//cho khi nao nha phim menu
}
if(Timer_Date==0)
{
delay_ms(20);
bTimer=~bTimer;
while(Timer_Date==0);
}
if(kt_menu!=0)
{
switch(kt_menu)
{
case 1: //chinh thoi gian ngay
{
if(Back==0)
{
delay_ms(20);
day--;
if(day==0)
day=31;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
day++;
if(day==32)
day=1;
while(Next==0);
}
Led1Sang;
Data=Display_Number[SoNgayHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoNgayHangDonVi];
delay_ms(1);
OffLed;
}
break;
case 2: //chinh thang
{
if(Back==0)
{
delay_ms(20);
month--;
if(month==0)
month=12;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
month++;
if(month==13)
month=1;
while(Next==0);
}
Led3Sang;
Data=Display_Number[SoThangHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoThangHangDonVi];
delay_ms(1);
OffLed;
}
break;
case 3: //chinh nam
{
if(Back==0)
{
delay_ms(20);
year--;
if(year==0)
year=99;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
year++;
if(month==100)
year=0;
while(Next==0);
}
Led7Sang;
Data=Display_Number[SoNamHangChuc];
delay_ms(1);
Led8Sang;
Data=Display_Number[SoNamHangDonVi];
delay_ms(1);
OffLed; }
break;
case 4: //chinh gio
{
if(Back==0)
{
delay_ms(20);
hour--;
if(hour==0)
hour=59;
while(Back==0);
}
else if(Next==0)
{
delay_ms(20);
hour++;
if(hour==60)
hour=0;
while(Next==0);
}
Led1Sang;
Data=Display_Number[SoGioHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoGioHangDonVi];
delay_ms(1);
OffLed;
}
break; //chinh phut
case 5:
{
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 6: //chinh giay
{
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];
delay_ms(1);
Led6Sang;
Data=Display_Number[SoGiayHangDonVi];
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;
delay_us(2);
SDA = 0;
delay_us(2);
SCL = 0;
delay_us(2);
}
void I2C_stop()
{
SCL = 1; //keo chan SCL len 1
SCL = 0;
SDA = 0;
delay_us(2);
SCL = 1;
delay_us(2);
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; delay_us(2);
SCL = 0;
return dat;
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
delay_us(2);
SCL = 1;
delay_us(2); /* Set SCL */
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
// CAC CHUONG TRINH CON CHO DS1307
//************************************************** ****************************
unsigned char DS1307_read(unsigned char addr)
{
unsigned int temp,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)
{
unsigned int 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 giay tu DS1307
min = DS1307_read(MIN); //doc phut tu DS1307
hour = DS1307_read(HOUR); //doc gio tu DS1307
day = DS1307_read(DAY); //doc ngay tu DS1307
date = DS1307_read(DATE); //doc thu tu DS1307
month = DS1307_read(MONTH); //doc thang tu DS1307
year = DS1307_read(YEAR); //doc nam tu DS1307
}
void Write_DS1307()
{
DS1307_Write(SEC,sec); //ghi du lieu vao thanh ghi giay
DS1307_Write(MIN,min); //ghi du lieu vao thanh ghi phut
DS1307_Write(HOUR,hour); //ghi du lieu vao thanh ghi gio
DS1307_Write(DAY,day); //ghi du lieu vao thanh ghi ngay
DS1307_Write(DATE,date); //ghi du lieu vao thanh ghi thu
DS1307_Write(MONTH,month); //ghi du lieu vao thanh ghi thang
DS1307_Write(YEAR,year); //ghi du lieu vao thanh ghi nam
}
//ham quet led hien thi gio phut giay
void Display_Gio_phut_giay()
{
Led1Sang; // led thu nhat sang
Data=Display_Number[SoGioHangChuc]; //xuat du lieu ra led thu nhat
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];
delay_ms(1);
OffLed;
}
//void Display_Gio_phut_giay()
//{
// if(uiTimeDelay<2)
// {
// Led1Sang; // led thu nhat sang
// Data=Display_Number[SoGioHangChuc]; //xuat du lieu ra led thu nhat
// }
// else if(uiTimeDelay<4)
// {
// Led2Sang;
// Data=Display_Number[SoGioHangDonVi];
// }
// else if(uiTimeDelay<6)
// {
// Led3Sang;
// Data=Display_Number[SoPhutHangChuc];
// }
// else if(uiTimeDelay<8)
// {
// Led4Sang;
// Data=Display_Number[SoPhutHangDonVi];
// }
// else if(uiTimeDelay<10)
// {
// Led5Sang;
// Data=Display_Number[SoGiayHangChuc];
// }
// else if(uiTimeDelay<12)
// {
// Led6Sang;
// Data=Display_Number[SoGiayHangDonVi];
// }
// else
// uiMocThoiGian=Millisec;
//}
//ham quet led hien thi ngay thang nam
void Display_Ngay_Thang_Nam()
{
Led1Sang;
Data=Display_Number[SoNgayHangChuc];
delay_ms(1);
Led2Sang;
Data=Display_Number[SoNgayHangDonVi];
cham;
delay_ms(1);
Led3Sang;
Data=Display_Number[SoThangHangChuc];
delay_ms(1);
Led4Sang;
Data=Display_Number[SoThangHangDonVi];
cham;
delay_ms(1);
Led5Sang;
Data=Display_Number[2];
delay_ms(1);
Led6Sang;
Data=Display_Number[0];
delay_ms(1);
Led7Sang;
Data=Display_Number[SoNamHangChuc];
delay_ms(1);
Led8Sang;
Data=Display_Number[SoNamHangDonVi];
delay_ms(1);
OffLed;
}
void Convert_Number()
{
SoNgayHangChuc=day/10;
SoNgayHangDonVi=day%10;
SoThangHangChuc=month/10;
SoThangHangDonVi=month%10;
SoNamHangChuc=year/10;
SoNamHangDonVi=year%10;
SoGiayHangChuc=sec/10;
SoGiayHangDonVi=sec%10;
SoPhutHangChuc=min/10;
SoPhutHangDonVi=min%10;
SoGioHangChuc=hour/10;
SoGioHangDonVi=hour%10;
}
Comment