Em lập trình được hiển thị thời gian rồi nhưng khi mô phỏng nó lại hiển thị không liên tục lắm mặc dù em đã điều chỉnh thời gian quét led. Các bác xem hộ em với ạ. Cảm ơn các bác và xin hậu tạ 20k thẻ điện thoại cho bác nào giúp được em (các bác thông cảm em là sinh viên ạ).
Dưới đây là toàn bộ code cũng như project của em.
project: thoi gian
Dưới đây là toàn bộ code cũng như project của em.
PHP Code:
#include<AT89X52.H>
#define MODE P1_0
#define UP P1_1
#define DOWN P1_2
#define SDA P3_1
#define SCL P3_0
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DAY 0x03
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
#define Date1 P2_0
#define Date2 P2_1
#define Mth1 P2_2
#define Mth2 P2_3
#define Year1 P2_4
#define Year2 P2_5
#define Year3 P2_6
#define Year4 P2_7
#define Day1 P1_4
#define Hour1 P3_3
#define Hour2 P3_4
#define Min1 P3_5
#define Min2 P3_6
#define Sec1 P3_7
#define Sec2 P1_3
#define Display_Date_Chuc {\
Date1 = 1;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Date_DV {\
Date1 = 0;\
Date2 = 1;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Month_Chuc {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 1;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Month_DV {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 1;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Year_Nghin {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 1;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Year_Tram {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 1;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Year_Chuc {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 1;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Year_DV {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 1;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Day {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 1;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Hour_Chuc {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 1;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Hour_DV {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 1;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Min_Chuc {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 1;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Min_DV {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 1;\
Sec1 = 0;\
Sec2 = 0;\
}
#define Display_Sec_Chuc {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 1;\
Sec2 = 0;\
}
#define Display_Sec_DV {\
Date1 = 0;\
Date2 = 0;\
Mth1 = 0;\
Mth2 = 0;\
Year1 = 0;\
Year2 = 0;\
Year3 = 0;\
Year4 = 0;\
Day1 = 0;\
Hour1 = 0;\
Hour2 = 0;\
Min1 = 0;\
Min2 = 0;\
Sec1 = 0;\
Sec2 = 1;\
}
unsigned char led7_data[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 } ;
unsigned char t_view,time,index,F_set,B_nhay,get_t ;
unsigned char hour,sec,min,day,date,month,year;
/************KHAI BAO HAM, CHUONG TRINH CON*********************************************** ********/
void set_main(void); // Chuong trinh khoi tao main
void setup_timer(void); // Chuong trinh cai dat timer 1 va 0
void delay(unsigned int time); // Delay
void Fix_time(void); // Kiem tra va hieu chinh gia tri cua gio,phut,giay
void Keypad(void); // Kiem tra xu ly phim nhan.
void T1_ISR(void); //ngat timer 1 phuc vu nhay led
void T0_ISR(void); // Dung ngat timer 0 de quet led
/************CAC CHUONG TRINH CON*********************************************** ******************/
// FOR I2C
/************************************************** *********************/
void I2C_start(void)
{
SCL = 1; SCL = 0;
SDA = 1; SCL = 1;
delay(2);
SDA = 0; delay(2);
SCL = 0; delay(2);
}
void I2C_stop(void)
{
SCL = 1; SCL = 0;
SDA = 0; delay(2);
SCL = 1; delay(2);
SDA = 1;
}
bit I2C_write(unsigned char dat)
{
unsigned char i;
for (i=0;i<8;i++)
{
SDA = (dat & 0x80) ? 1:0;
SCL=1;SCL=0;
dat<<=1;
}
SCL = 1; delay(2);
SCL = 0;
}
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(2);
SCL = 1; /* Set SCL */
delay(2);
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;
}
/************************************************** *********************/
// FOR DS1307
/************************************************** *********************/
unsigned char DS1307_get(unsigned char addr)
{
unsigned int temp,ret;
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID+1); /* 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;
//********************************************** /*HEX to BCD*/
temp = dat ; /*for Led 7seg*/
dat = (((dat/10)*16)|(temp %10));
//**********************************************
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_write(dat); /* Connect to DS1307 for Read */
I2C_stop();
}
/************************************************** *********************/
void delay(unsigned int time)
{ while(time--); }
void set_main(void) // Chuong trinh khoi tao main
{
P1=0x00;
P0=0xff;
P2=0x00;
P3=0x00;
}
void setup_timer(void) // Setup timer 0 va timer 1
{
TMOD=0x11; // timer0 & timer1 set che do 1
TH0=0xfc; TL0=0x18;
TH1=0x3C; TL1=0xAF;
ET1=1; ET0=1; EA=1;
TF0=0; TF1=0; TR0=1; TR1=1;
}
void Fix_time(void) // Kiem tra va hieu chinh gia tri cua gio,phut,giay
{
//Tang
if(sec==60) {sec=0;min++; }
if(min==60) {min=0;hour++; }
if(hour==24) hour=0;
if(day==8) day=1;
if(month==2)
{
if((year%4!=0)&&(date==29))
{
date=1;
month++;
}
else if(date==30)
{
date=1;
month++;
}
}
if(date==32)
{
date=1;
month++;
}
if(month==13)
{
month=1;
year++;
}
if(year==100)
{
year=0;
}
if(sec== -1) {sec=59;min--;}
if(min== -1) {min=59;hour-- ;}
if(hour== -1)hour= 23;
}
void Keypad(void) // Kiem tra phim nhan.
{
if(!MODE){ //phim Set duoc nhan ?
F_set++; // Bien F_set co gia tri tu 0->2
if(F_set==4) {F_set=0;
DS1307_Write(0x02,hour);
DS1307_Write(0x01,min);
DS1307_Write(0x00,sec); }
}
if(F_set==1){ //Tang hoac giam phut neu F_set = 1
if(!UP) hour++;
if(!DOWN) hour--;
}
if(F_set==2){ //Tang hoac giam gio neu F_set = 2
if(!UP) min++;
if(!DOWN) min--;
}
if(F_set==3){ //Tang hoac giam gio neu F_set = 2
if(!UP) sec++;
if(!DOWN) sec--;
}
Fix_time(); //kiem tra tran so
delay(20000);
}
void T1_ISR(void) interrupt 3 //ngat timer 1 chay dong ho
{
TR1=0;
TF1=0;
TH1=0x3C; // nap lai gia tri cho thanh ghi
TL1=0xAF;
time++;
if(time==10)
{time=0; B_nhay++; get_t=1;
if(B_nhay==2)
B_nhay=0;
}
TR1=1;
}
/*************************************/
void T0_ISR(void) interrupt 1 // Dung ngat timer 0 de quet led
{
TR0=0;
TF0=0;
TH0=0xfc;//-1000/256; // Nap lai gia tri cho thanh ghi
TL0=0x18;//-1000%256;
index++;
//giay
switch(index)
{
case 1:
if(F_set==1 && B_nhay==1)
{index++; index++;}
else
{
t_view=sec;
P0=0xff;
Display_Sec_DV;
P0=led7_data[t_view%10];
}
break;
case 2:
P0=0xff;
Display_Sec_Chuc;
P0=led7_data[t_view/10];
break;
case 3:
if(F_set==2 && B_nhay==1)
{
index++;
index++;
}
else
{
t_view=min;
P0=0xff;
Display_Min_DV;
P0=led7_data[t_view%10];
}
break;
case 4:
P0=0xff;
Display_Min_Chuc;
P0=led7_data[t_view/10];
break;
case 5:
if(F_set==1 && B_nhay==1){index++;index++;}
else
{
t_view=hour;
P0=0xff;
Display_Hour_DV;
P0=led7_data[t_view%10];
}
break;
case 6:
P0=0xff;
Display_Hour_Chuc;//00000001
P0=led7_data[t_view/10];
break;
case 7:
if(F_set==4 && B_nhay==1){index=index+4;}
else
{
t_view=year;
P0=0xff;
Display_Year_DV;//00000001
P0=led7_data[t_view%10];
}
break;
case 8:
P0=0xff;
Display_Year_Chuc;//00000001
P0=led7_data[t_view/10];
break;
case 9:
P0=0xff;
Display_Year_Tram;//00000001
P0=led7_data[0];
break;
case 10:
P0=0xff;
Display_Year_Nghin;//00000001
P0=led7_data[2];
break;
case 11:
if(F_set==5 && B_nhay==1)
{index++; index++;}
else
{
t_view=month;
P0=0xff;
Display_Month_DV;//00000001
P0=led7_data[t_view%10];
}
break;
case 12:
P0=0xff;
Display_Month_Chuc;
P0=led7_data[t_view/10];
break;
case 13:
if (F_set==6 && B_nhay==1)
{index++;
index++;
}
else
{
t_view=date;
P0=0xff;
Display_Date_DV;//00000001
P0=led7_data[t_view%10];
}
break;
case 14:
P0=0xff;
Display_Date_Chuc;//00000001
P0=led7_data[t_view/10];
break;
case 15:
t_view=day;
P0=0xff;
Display_Day;//00000001
P0=led7_data[t_view%10];
index=0;
break;
}
TR0=1;
}
void main()
{
time=index=0;
hour=0;
min=0;
sec=0;
F_set=0;
get_t=0;
setup_timer();
while(1)
{
Keypad();
if(F_set==0)
{
if(get_t == 1)
{
get_t = 0;
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
day = DS1307_get(DAY);
date = DS1307_get(DATE);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
}
}
}
}
Comment