Chia sẻ với các bạn mạch Đồng hồ All in One, Giờ - Phút - Giây - Ngày Tháng Năm Nhiệt độ dùng Lm35 + 89c51 + Ds1307.
Code mã nguồn C - Keil C:
/***********************************************************
FileName: Hai-Clock-7segment.c
MCU: 8051
Xtal: 12Mhz
App: Digital Clock
Author: manhcuong.k08@gmail.com
Creat Date: 4/5/2011
Finish Date:
************************************************************/
#include <at89x52.h>
/**********************define var,port*********************/
#define data_port P2
#define sel_port P0
sbit _RW = P3^6; //
sbit _RD = P3^7; //
sbit _INT = P1^7;
sbit MODE = P3^3; //external interrupt 1
sbit DEC = P3^4; //DEC value
sbit INC = P3^5; //increment value
sbit SDA = P3^0; //I2c data line
sbit SCL = P3^1; //I2c clock line
//===================time variable============================//
struct FREE_USER{
unsigned char SEC;
unsigned char MIN;
unsigned char HOUR;
unsigned char DAY;
unsigned char DATE;
unsigned char MONTH;
unsigned char YEAR;
} _TIME ;
//============================================================//
// Byte control Real Time Cloclk DS1307 //
// //
//============================================================//
#define CONTROL_WRITE 0xd0
#define CONTROL_READ 0xd1
/*********************sub program**************************/
//==========================================================//
// //
// RTC class //
//==========================================================//
unsigned char RTC_READ(unsigned char ADRESS);
void RTC_WRITE(unsigned char ADRESS,unsigned char DAT);
void I2C_START();
void I2C_STOP();
void I2C_WRITE(unsigned char byte);
unsigned char I2C_READ();
void RTC_INI();
//==========================================================//
// //
// other //
//==========================================================//
void DISPLAY_1__(unsigned char ARRAY[],unsigned char STATUS,bit istempsensor);
void DISPLAY_2__(unsigned char ARRAY[]);
void delay(int time);
void GET_TIME();
void TIMER0_ini();
void TIMER1_ini();
void CHECK_DAY(bit get);
void CONVERT();
void GET_T();
void DISPLAY_3__(unsigned char ARRAY[]);
unsigned char code seg_code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code HELLO[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x76,0x79,0x38,0x38,0x3f,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff};
unsigned char code MORNING[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x39,0x74,0x77,0x5C,0x00,0x7C,0x1C,0x5C,0x10,
0x00,0x6D,0x77,0x54,0x6F,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff};
unsigned char code AFTERNOON[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x39,0x74,0x77,0x5C,0x00,0x7C,0x1C,0x5C,0x10,
0x00,0x58,0x74,0x10,0x79,0x1c,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff};
//day on week==============================================
unsigned char code SUN[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x39,0x74,0x1c,0x00,0x54,0x74,0x77,0x31,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code MON[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x5b,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code TUE[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x4f,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code WED[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x66,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code THU[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x6d,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code FRI[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x7d,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code SAT[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x07,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char TIME_DISPLAY[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char DATE_DISPLAY[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char DAY_DISPLAY[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code T_SENSOR[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x63,0x39,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char T_value[2];
#define SHIFT 0x00
#define STOP 0x01
#define CLEAR 0x02
#define TIME_STATUS 1080
int GLOBAL_DISPLAY=0;
unsigned char IN_VISIBLE;
unsigned char CONTROL_INVISIBLE=0;
unsigned char SHIFT_CURCOR;
unsigned char counter_status;
/********************************************************/
/* Main Program */
/********************************************************/
void main()
{
/********Time Default**************
_TIME.SEC = 0x00 ;
_TIME.MIN = 0x00 ;
_TIME.HOUR = 0x12 ;
_TIME.DAY = 0x04 ;
_TIME.DATE = 0x10;
_TIME.MONTH = 0x04;
_TIME.YEAR = 0x11;
RTC_INI();
////////////////////////////////////*/
_RD = 1;
_RW = 1;
TIMER0_ini();
TIMER1_ini();
//enable global interrupt
EX1 = 1; //enable external interrupt 1
IT1 = 1; //
EX0 = 1; //enable external interrupt 1
IT0 = 1; //
EA = 1;
TR0 = 1; //timer0 run
PT1 =1;
PX0 = 1;
while(1)
{
// GET_TIME();DISPLAY_1__(TIME_DISPLAY,STOP);
switch(GLOBAL_DISPLAY)
{
case 0: DISPLAY_1__(HELLO,SHIFT,0);while(GLOBAL_DISPLAY<100)DISPLAY_1__(HELLO,STOP,0);DISPLAY_1__(HELLO,CLEAR,0);break;
case 140: DISPLAY_1__(TIME_DISPLAY,SHIFT,0);while(GLOBAL_DISPLAY<600)DISPLAY_1__(TIME_DISPLAY,STOP,0);DISPLAY_1__(TIME_DISPLAY,CLEAR,0);break;
case 620: if((_TIME.HOUR>=0)&&(_TIME.HOUR<12))DISPLAY_3__(MORNING);else if(_TIME.HOUR>=12)DISPLAY_3__(AFTERNOON);break;
case 700: CHECK_DAY(1);DISPLAY_1__(DAY_DISPLAY,SHIFT,0);while(GLOBAL_DISPLAY<800){CHECK_DAY(1);DISPLAY_1__(DAY_DISPLAY,STOP,0);}DISPLAY_1__(DAY_DISPLAY,CLEAR,0);break;
case 820: DISPLAY_1__(DATE_DISPLAY,SHIFT,0);while(GLOBAL_DISPLAY<920)DISPLAY_1__(DATE_DISPLAY,STOP,0);DISPLAY_1__(DATE_DISPLAY,CLEAR,0);break;
case 950: GET_T();DISPLAY_1__(T_SENSOR,SHIFT,1);while(GLOBAL_DISPLAY<1050)DISPLAY_1__(T_SENSOR,STOP,1);DISPLAY_1__(T_SENSOR,CLEAR,1);break;
default: {;}break;
}
}
}
//==========================================================//
// //
// DISPLAY_3__ //
//==========================================================//
void DISPLAY_3__(unsigned char ARRAY[])
{
unsigned char counter_char=0,index,time_shift,counter;
unsigned char __SELECT__ ;
while(ARRAY[counter_char]!=0xff)
counter_char++;
for(index=0;index<counter_char-8;index++)
{
for(time_shift=0;time_shift<20;time_shift++)
{
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
data_port = ARRAY[counter+index];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
}
}
//==========================================================//
// //
// DISPLAY_2__ //
//==========================================================//
void DISPLAY_2__(unsigned char ARRAY[])
{
unsigned char counter;
unsigned char __SELECT__=0x7f ;
for(counter=0;counter<8;counter++)
{
sel_port = __SELECT__;
sel_port |= IN_VISIBLE;
data_port = ARRAY[counter+8];
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
//==========================================================//
// //
// DISPLAY_1__ //
//==========================================================//
void DISPLAY_1__(unsigned char ARRAY[],unsigned char _STATUS_,bit istempsensor)
{
unsigned char counter_char=0,index,time_shift,counter;
unsigned char __SELECT__ ;
while(ARRAY[counter_char]!=0xff)
counter_char++;
switch(_STATUS_)
{
case SHIFT: {
for(index=0;index<counter_char-16;index++)
{
for(time_shift=0;time_shift<20;time_shift++)
{
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
if(istempsensor)
{
if(counter+index==0x0a)
data_port = T_value[0];
else if(counter+index==0x0b)
data_port = T_value[1];
else
data_port = ARRAY[counter+index];
}
else
data_port = ARRAY[counter+index];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
}
break;
}
case STOP: {
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
if(istempsensor)
{
if(counter+8==0x0a)
data_port = T_value[0];
else if(counter+8==0x0b)
data_port = T_value[1];
else
data_port = ARRAY[counter+8];
}
else
data_port = ARRAY[counter+8];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
break;
}
case CLEAR: {
for(index=8;index<counter_char-8;index++)
{
for(time_shift=0;time_shift<20;time_shift++)
{
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
if(istempsensor)
{
if(counter+index==0x0a)
data_port = T_value[0];
else if(counter+index==0x0b)
data_port = T_value[1];
else
data_port = ARRAY[counter+index];
}
else
data_port = ARRAY[counter+index];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
}
break;
}
}
}
//==========================================================//
// //
// delay //
//==========================================================//
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<time;j++)
{;}
}
//==========================================================//
// //
// I2c_START //
//==========================================================//
void I2C_START()
{
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
SDA = 1;
}
//==========================================================//
// //
// I2c_STOP //
//==========================================================//
void I2C_STOP()
{
SDA = 0;
SCL = 1;
SDA = 1;
}
//==========================================================//
// //
// I2c_WRITE //
//==========================================================//
void I2C_WRITE(unsigned char byte)
{
unsigned char counter;
for(counter = 0;counter<8;counter++)
{
if(byte&0x80)
SDA=1;
else
SDA=0;
SCL = 1;
SCL = 0;
byte<<=1;
}
SDA = 1;
SCL = 1;
SCL = 0;
}
//==========================================================//
// //
// I2c_READ //
//==========================================================//
unsigned char I2C_READ()
{
unsigned char return_value=0x00,counter=8;
while(counter>0)
{
SCL=1;
return_value<<=1;
return_value|=SDA;
SCL=0;
counter--;
}
SDA = 1;
SCL = 1;
SCL = 0;
SDA = 1;
return return_value;
}
//==========================================================//
// //
// RTC_WRITE //
//==========================================================//
void RTC_WRITE(unsigned char ADDRESS,unsigned char DAT)
{
I2C_START(); //i2c start
I2C_WRITE(CONTROL_WRITE); //send control byte
I2C_WRITE(ADDRESS); //send address byte
I2C_WRITE(DAT); //send data byte
I2C_STOP(); //i2c stop
}
//==========================================================//
// //
// RTC_READ //
//==========================================================//
unsigned char RTC_READ(unsigned char ADDRESS)
{
unsigned char return_value=0x00;
I2C_START();
I2C_WRITE(CONTROL_WRITE); //send byte control write
I2C_WRITE(ADDRESS); // send byte address
I2C_START(); // restart
I2C_WRITE(CONTROL_READ); // send byte control read
return_value = I2C_READ(); //read from RTC
I2C_STOP();
return return_value;
}
//==========================================================//
// //
// RTC_INI //
//==========================================================//
void RTC_INI()
{ //ADDRESS,DATA//
RTC_WRITE(0x00,_TIME.SEC);
RTC_WRITE(0x01,_TIME.MIN);
RTC_WRITE(0x02,_TIME.HOUR);
RTC_WRITE(0x03,_TIME.DAY);
RTC_WRITE(0x04,_TIME.DATE);
RTC_WRITE(0x05,_TIME.MONTH);
RTC_WRITE(0x06,_TIME.YEAR);
RTC_WRITE(0x07,0x10);//OUT = 1,SOUT = 1;
}
//==========================================================//
// //
// GET_TIME //
//==========================================================//
void GET_TIME()
{
_TIME.SEC = RTC_READ(0x00); //GET SECOND
_TIME.MIN = RTC_READ(0x01); //GET MIN
_TIME.HOUR = RTC_READ(0x02)&0x3f; //GET HOUR
_TIME.DAY = RTC_READ(0x03); //GET DAY
_TIME.DATE = RTC_READ(0x04); //GET DATE
_TIME.MONTH = RTC_READ(0x05); //GET MONTH
_TIME.YEAR = RTC_READ(0x06); //GET YEAR
CONVERT();
}
//==========================================================//
// //
// Timer0_ini //
//==========================================================//
void TIMER0_ini()
{
TMOD = 0x01 ;//timer 0 16bit reload
TH0 = 0x00;
TL0 = 0x00;
TF0 = 0;
TR0 = 0;
ET0 = 1; //enable timer0 interrupt
}
//==========================================================//
// //
// CONVERT //
//==========================================================//
void CONVERT()
{
unsigned char temp,index=15;
//GET SECCOND
temp = _TIME.SEC;
temp&=0x0f; //get the unit
TIME_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.SEC;
temp&=0xf0; //get the tens
temp>>=4;
TIME_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
TIME_DISPLAY[index]=0x40; //
index--;
//GET MIN
temp = _TIME.MIN;
temp&=0x0f; //get the unit
TIME_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.MIN;
temp&=0xf0; //get the tens
temp>>=4;
TIME_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
TIME_DISPLAY[index]=0x40; //
index--;
//GET HOUR
temp = _TIME.HOUR;
temp&=0x0f; //get the unit
TIME_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.HOUR;
temp&=0xf0; //get the tens
temp>>=4;
TIME_DISPLAY[index]=seg_code[temp];
index=15;
//GET YEAR
temp = _TIME.YEAR;
temp&=0x0f; //get the unit
DATE_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.YEAR;
temp&=0xf0; //get the tens
temp>>=4;
DATE_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
DATE_DISPLAY[index]=0x40; //
index--;
//GET MONTH
temp = _TIME.MONTH;
temp&=0x0f; //get the unit
DATE_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.MONTH;
temp&=0xf0; //get the tens
temp>>=4;
DATE_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
DATE_DISPLAY[index]=0x40; //
index--;
//GET DATE
temp = _TIME.DATE;
temp&=0x0f; //get the unit
DATE_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.DATE;
temp&=0xf0; //get the tens
temp>>=4;
DATE_DISPLAY[index]=seg_code[temp];
}
//==========================================================//
// //
// Timer1_ini //
//==========================================================//
void TIMER1_ini()
{
TMOD |= 0x10; //timer 1 16bit reload
TH1 = 0x00;
TL1 = 0x00;
TF1 = 0;
TR1 = 0;
ET1 = 1;
}
//==========================================================//
// //
// Timer0_interrupt //
//==========================================================//
void TIMER0_interrupt() interrupt 1
{
TR0 = 0;
TF0 = 0;
TH0 = 0;
TL0 = 0;
GLOBAL_DISPLAY++;
if(GLOBAL_DISPLAY==TIME_STATUS)
GLOBAL_DISPLAY=0;
TR0 = 1;
}
//==========================================================//
// //
// Timer1_interrupt //
//==========================================================//
void TIMER1_interrupt() interrupt 3
{
TR1 = 0; //stop timer1
TF1 = 0; //clear TF1
TH1 = 0;
TL1 = 1;
CONTROL_INVISIBLE++;
if(CONTROL_INVISIBLE==5)
{
IN_VISIBLE = 0x00;
}
else if(CONTROL_INVISIBLE==10)
{
IN_VISIBLE = SHIFT_CURCOR;
CONTROL_INVISIBLE=0;
}
else {;}
TR1 = 1;
}
//==========================================================//
// //
// External Interrupt 1 //
//==========================================================//
void EX1_interrupt()interrupt 2
{
unsigned char unit,tens;
EX1 = 0; // disable ex interrupt 1
EX0 = 0; // disable ex interrupt 0
counter_status=0x00;
TR1 = 1; //enable TIMER1
IN_VISIBLE = 0xc0;
SHIFT_CURCOR = IN_VISIBLE;
CONTROL_INVISIBLE=0;
// GET_TIME();
while(MODE==0)DISPLAY_2__(TIME_DISPLAY);
while(counter_status!=0x07)
{
switch(counter_status)
{
case 0x00: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.HOUR;
unit &=0x0f;
tens = _TIME.HOUR;
tens >>= 4;
if(tens!=2)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==4)
{
tens=0;
unit = 0;
}
}
_TIME.HOUR = tens;
_TIME.HOUR <<=4;
_TIME.HOUR |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.HOUR;
unit &=0x0f;
tens = _TIME.HOUR;
tens >>= 4;
if(tens!=0)
{
unit--;
if(unit==0xff)
{
tens--;
unit = 0x09;
}
}
else
{
unit--;
if(unit==0xff)
{
tens = 0x02;
unit = 0x03;
}
}
_TIME.HOUR = tens;
_TIME.HOUR <<=4;
_TIME.HOUR |= unit;
CONVERT();
}
DISPLAY_2__(TIME_DISPLAY);
}
while(MODE==0){DISPLAY_2__(TIME_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(TIME_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x01: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.MIN;
unit &=0x0f;
tens = _TIME.MIN;
tens >>= 4;
unit++;
if(unit==10)
{
tens++;
unit=0;
}
if(tens==6)
tens=0;
_TIME.MIN = tens;
_TIME.MIN <<=4;
_TIME.MIN |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.MIN;
unit &=0x0f;
tens = _TIME.MIN;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
tens=5;
_TIME.MIN = tens;
_TIME.MIN <<=4;
_TIME.MIN |= unit;
CONVERT();
}
DISPLAY_2__(TIME_DISPLAY);
}
while(MODE==0){DISPLAY_2__(TIME_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(TIME_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x02: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.SEC;
unit &=0x0f;
tens = _TIME.SEC;
tens >>= 4;
unit++;
if(unit==10)
{
tens++;
unit=0;
}
if(tens==6)
tens=0;
_TIME.SEC = tens;
_TIME.SEC <<=4;
_TIME.SEC|= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.SEC;
unit &=0x0f;
tens = _TIME.SEC;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
tens=5;
_TIME.SEC = tens;
_TIME.SEC <<=4;
_TIME.SEC|= unit;
CONVERT();
}
DISPLAY_2__(TIME_DISPLAY);
}
while(MODE==0){DISPLAY_2__(TIME_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(TIME_DISPLAY);}
IN_VISIBLE=0xc0;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x03: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.DATE;
unit &=0x0f;
tens = _TIME.DATE;
tens >>= 4;
if(_TIME.MONTH%2==1)
{
if(tens<3)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==2)
{
tens=0;
unit=0;
}
}
}
else if(_TIME.MONTH%2==0)
{
if(tens<3)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==1)
{
tens=0;
unit=0;
}
}
}
_TIME.DATE = tens;
_TIME.DATE <<=4;
_TIME.DATE |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.DATE;
unit &=0x0f;
tens = _TIME.DATE;
tens >>= 4;
if(_TIME.MONTH%2==1)
{
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
{
tens=3;
unit=1;
}
}
else if(_TIME.MONTH%2==0)
{
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
{
tens=3;
unit=0;
}
}
_TIME.DATE = tens;
_TIME.DATE <<=4;
_TIME.DATE |= unit;
CONVERT();
}
DISPLAY_2__(DATE_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DATE_DISPLAY); }
while(IN_VISIBLE==0x00){DISPLAY_2__(DATE_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x04: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.MONTH;
unit &=0x0f;
tens = _TIME.MONTH;
tens >>= 4;
if(tens!=1)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==3)
{
tens=0;
unit=0;
}
}
_TIME.MONTH = tens;
_TIME.MONTH <<=4;
_TIME.MONTH |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.MONTH;
unit &=0x0f;
tens = _TIME.MONTH;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
{
tens=1;
unit=2;
}
_TIME.MONTH = tens;
_TIME.MONTH <<=4;
_TIME.MONTH |= unit;
CONVERT();
}
DISPLAY_2__(DATE_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DATE_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(DATE_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x05: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.YEAR;
unit &=0x0f;
tens = _TIME.YEAR;
tens >>= 4;
unit++;
if(unit==10)
{
tens++;
unit=0;
}
if(tens==9)
tens=0;
_TIME.YEAR = tens;
_TIME.YEAR <<=4;
_TIME.YEAR |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.YEAR;
unit &=0x0f;
tens = _TIME.YEAR;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
tens=9;
_TIME.YEAR = tens;
_TIME.YEAR <<=4;
_TIME.YEAR |= unit;
CONVERT();
}
DISPLAY_2__(DATE_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DATE_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(DATE_DISPLAY);}
IN_VISIBLE = 0xff;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x06: {
while(MODE)
{
CHECK_DAY(0);
if(INC==0)
{
while(INC==0)DISPLAY_2__(DAY_DISPLAY);
_TIME.DAY++;
if(_TIME.DAY==8)
_TIME.DAY=1;
CHECK_DAY(0);
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DAY_DISPLAY);
_TIME.DAY--;
if(_TIME.DAY==0xff)
_TIME.DAY=7;
CHECK_DAY(0);
}
DISPLAY_2__(DAY_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DAY_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(DAY_DISPLAY);}
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
}
RTC_INI();
}
IE1 = 0;//clear external interrupt 1 flag
TR1 = 0;
EX0 = 1;
EX1 = 1;
}
//==========================================================//
// //
// CHECK_DAY //
//==========================================================//
void CHECK_DAY(bit get)
{
unsigned char index,DAY;
if(get)
DAY = RTC_READ(0x03);
else
DAY = _TIME.DAY;
switch(DAY)
{
case 0x01: for(index=0;index<24;index++)DAY_DISPLAY[index]=SUN[index];break;
case 0x02: for(index=0;index<24;index++)DAY_DISPLAY[index]=MON[index];break;
case 0x03: for(index=0;index<24;index++)DAY_DISPLAY[index]=TUE[index];break;
case 0x04: for(index=0;index<24;index++)DAY_DISPLAY[index]=WED[index];break;
case 0x05: for(index=0;index<24;index++)DAY_DISPLAY[index]=THU[index];break;
case 0x06: for(index=0;index<24;index++)DAY_DISPLAY[index]=FRI[index];break;
case 0x07: for(index=0;index<24;index++)DAY_DISPLAY[index]=SAT[index];break;
}
}
//===========================================================///
void update_time() interrupt 0
{
EX0 = 0;
GET_TIME();
IE0 = 0;
EX0 = 1;
}
//===========================================================//
void GET_T()
{
unsigned char temp;
_RW = 0;
_RW = 1;
while(_INT);
RD = 0;
temp = P1;
temp&=0x7f;
RD = 1;
T_value[0]=seg_code[temp/10];
T_value[1]=seg_code[temp%10];
}
( Code compiler + Schematic + Simulation)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn dientuvietnam.net
/***********************************************************
FileName: Hai-Clock-7segment.c
MCU: 8051
Xtal: 12Mhz
App: Digital Clock
Author: manhcuong.k08@gmail.com
Creat Date: 4/5/2011
Finish Date:
************************************************************/
#include <at89x52.h>
/**********************define var,port*********************/
#define data_port P2
#define sel_port P0
sbit _RW = P3^6; //
sbit _RD = P3^7; //
sbit _INT = P1^7;
sbit MODE = P3^3; //external interrupt 1
sbit DEC = P3^4; //DEC value
sbit INC = P3^5; //increment value
sbit SDA = P3^0; //I2c data line
sbit SCL = P3^1; //I2c clock line
//===================time variable============================//
struct FREE_USER{
unsigned char SEC;
unsigned char MIN;
unsigned char HOUR;
unsigned char DAY;
unsigned char DATE;
unsigned char MONTH;
unsigned char YEAR;
} _TIME ;
//============================================================//
// Byte control Real Time Cloclk DS1307 //
// //
//============================================================//
#define CONTROL_WRITE 0xd0
#define CONTROL_READ 0xd1
/*********************sub program**************************/
//==========================================================//
// //
// RTC class //
//==========================================================//
unsigned char RTC_READ(unsigned char ADRESS);
void RTC_WRITE(unsigned char ADRESS,unsigned char DAT);
void I2C_START();
void I2C_STOP();
void I2C_WRITE(unsigned char byte);
unsigned char I2C_READ();
void RTC_INI();
//==========================================================//
// //
// other //
//==========================================================//
void DISPLAY_1__(unsigned char ARRAY[],unsigned char STATUS,bit istempsensor);
void DISPLAY_2__(unsigned char ARRAY[]);
void delay(int time);
void GET_TIME();
void TIMER0_ini();
void TIMER1_ini();
void CHECK_DAY(bit get);
void CONVERT();
void GET_T();
void DISPLAY_3__(unsigned char ARRAY[]);
unsigned char code seg_code[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code HELLO[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x76,0x79,0x38,0x38,0x3f,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff};
unsigned char code MORNING[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x39,0x74,0x77,0x5C,0x00,0x7C,0x1C,0x5C,0x10,
0x00,0x6D,0x77,0x54,0x6F,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff};
unsigned char code AFTERNOON[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x39,0x74,0x77,0x5C,0x00,0x7C,0x1C,0x5C,0x10,
0x00,0x58,0x74,0x10,0x79,0x1c,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff};
//day on week==============================================
unsigned char code SUN[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x39,0x74,0x1c,0x00,0x54,0x74,0x77,0x31,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code MON[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x5b,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code TUE[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x4f,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code WED[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x66,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code THU[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x6d,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code FRI[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x7d,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code SAT[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x07,0x76,0x3e,0x00,0x07,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char TIME_DISPLAY[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char DATE_DISPLAY[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char DAY_DISPLAY[] = {
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char code T_SENSOR[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x63,0x39,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,/*null*/0xff
};
unsigned char T_value[2];
#define SHIFT 0x00
#define STOP 0x01
#define CLEAR 0x02
#define TIME_STATUS 1080
int GLOBAL_DISPLAY=0;
unsigned char IN_VISIBLE;
unsigned char CONTROL_INVISIBLE=0;
unsigned char SHIFT_CURCOR;
unsigned char counter_status;
/********************************************************/
/* Main Program */
/********************************************************/
void main()
{
/********Time Default**************
_TIME.SEC = 0x00 ;
_TIME.MIN = 0x00 ;
_TIME.HOUR = 0x12 ;
_TIME.DAY = 0x04 ;
_TIME.DATE = 0x10;
_TIME.MONTH = 0x04;
_TIME.YEAR = 0x11;
RTC_INI();
////////////////////////////////////*/
_RD = 1;
_RW = 1;
TIMER0_ini();
TIMER1_ini();
//enable global interrupt
EX1 = 1; //enable external interrupt 1
IT1 = 1; //
EX0 = 1; //enable external interrupt 1
IT0 = 1; //
EA = 1;
TR0 = 1; //timer0 run
PT1 =1;
PX0 = 1;
while(1)
{
// GET_TIME();DISPLAY_1__(TIME_DISPLAY,STOP);
switch(GLOBAL_DISPLAY)
{
case 0: DISPLAY_1__(HELLO,SHIFT,0);while(GLOBAL_DISPLAY<100)DISPLAY_1__(HELLO,STOP,0);DISPLAY_1__(HELLO,CLEAR,0);break;
case 140: DISPLAY_1__(TIME_DISPLAY,SHIFT,0);while(GLOBAL_DISPLAY<600)DISPLAY_1__(TIME_DISPLAY,STOP,0);DISPLAY_1__(TIME_DISPLAY,CLEAR,0);break;
case 620: if((_TIME.HOUR>=0)&&(_TIME.HOUR<12))DISPLAY_3__(MORNING);else if(_TIME.HOUR>=12)DISPLAY_3__(AFTERNOON);break;
case 700: CHECK_DAY(1);DISPLAY_1__(DAY_DISPLAY,SHIFT,0);while(GLOBAL_DISPLAY<800){CHECK_DAY(1);DISPLAY_1__(DAY_DISPLAY,STOP,0);}DISPLAY_1__(DAY_DISPLAY,CLEAR,0);break;
case 820: DISPLAY_1__(DATE_DISPLAY,SHIFT,0);while(GLOBAL_DISPLAY<920)DISPLAY_1__(DATE_DISPLAY,STOP,0);DISPLAY_1__(DATE_DISPLAY,CLEAR,0);break;
case 950: GET_T();DISPLAY_1__(T_SENSOR,SHIFT,1);while(GLOBAL_DISPLAY<1050)DISPLAY_1__(T_SENSOR,STOP,1);DISPLAY_1__(T_SENSOR,CLEAR,1);break;
default: {;}break;
}
}
}
//==========================================================//
// //
// DISPLAY_3__ //
//==========================================================//
void DISPLAY_3__(unsigned char ARRAY[])
{
unsigned char counter_char=0,index,time_shift,counter;
unsigned char __SELECT__ ;
while(ARRAY[counter_char]!=0xff)
counter_char++;
for(index=0;index<counter_char-8;index++)
{
for(time_shift=0;time_shift<20;time_shift++)
{
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
data_port = ARRAY[counter+index];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
}
}
//==========================================================//
// //
// DISPLAY_2__ //
//==========================================================//
void DISPLAY_2__(unsigned char ARRAY[])
{
unsigned char counter;
unsigned char __SELECT__=0x7f ;
for(counter=0;counter<8;counter++)
{
sel_port = __SELECT__;
sel_port |= IN_VISIBLE;
data_port = ARRAY[counter+8];
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
//==========================================================//
// //
// DISPLAY_1__ //
//==========================================================//
void DISPLAY_1__(unsigned char ARRAY[],unsigned char _STATUS_,bit istempsensor)
{
unsigned char counter_char=0,index,time_shift,counter;
unsigned char __SELECT__ ;
while(ARRAY[counter_char]!=0xff)
counter_char++;
switch(_STATUS_)
{
case SHIFT: {
for(index=0;index<counter_char-16;index++)
{
for(time_shift=0;time_shift<20;time_shift++)
{
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
if(istempsensor)
{
if(counter+index==0x0a)
data_port = T_value[0];
else if(counter+index==0x0b)
data_port = T_value[1];
else
data_port = ARRAY[counter+index];
}
else
data_port = ARRAY[counter+index];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
}
break;
}
case STOP: {
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
if(istempsensor)
{
if(counter+8==0x0a)
data_port = T_value[0];
else if(counter+8==0x0b)
data_port = T_value[1];
else
data_port = ARRAY[counter+8];
}
else
data_port = ARRAY[counter+8];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
break;
}
case CLEAR: {
for(index=8;index<counter_char-8;index++)
{
for(time_shift=0;time_shift<20;time_shift++)
{
__SELECT__ = 0x7f;
for(counter=0;counter<8;counter++)
{
if(istempsensor)
{
if(counter+index==0x0a)
data_port = T_value[0];
else if(counter+index==0x0b)
data_port = T_value[1];
else
data_port = ARRAY[counter+index];
}
else
data_port = ARRAY[counter+index];
sel_port = __SELECT__;
delay(7);
sel_port = 0xff; //disable all led
data_port = 0x00; //disable all led
__SELECT__ >>= 1;
__SELECT__ |= 0x80;
}
}
}
break;
}
}
}
//==========================================================//
// //
// delay //
//==========================================================//
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<time;j++)
{;}
}
//==========================================================//
// //
// I2c_START //
//==========================================================//
void I2C_START()
{
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
SDA = 1;
}
//==========================================================//
// //
// I2c_STOP //
//==========================================================//
void I2C_STOP()
{
SDA = 0;
SCL = 1;
SDA = 1;
}
//==========================================================//
// //
// I2c_WRITE //
//==========================================================//
void I2C_WRITE(unsigned char byte)
{
unsigned char counter;
for(counter = 0;counter<8;counter++)
{
if(byte&0x80)
SDA=1;
else
SDA=0;
SCL = 1;
SCL = 0;
byte<<=1;
}
SDA = 1;
SCL = 1;
SCL = 0;
}
//==========================================================//
// //
// I2c_READ //
//==========================================================//
unsigned char I2C_READ()
{
unsigned char return_value=0x00,counter=8;
while(counter>0)
{
SCL=1;
return_value<<=1;
return_value|=SDA;
SCL=0;
counter--;
}
SDA = 1;
SCL = 1;
SCL = 0;
SDA = 1;
return return_value;
}
//==========================================================//
// //
// RTC_WRITE //
//==========================================================//
void RTC_WRITE(unsigned char ADDRESS,unsigned char DAT)
{
I2C_START(); //i2c start
I2C_WRITE(CONTROL_WRITE); //send control byte
I2C_WRITE(ADDRESS); //send address byte
I2C_WRITE(DAT); //send data byte
I2C_STOP(); //i2c stop
}
//==========================================================//
// //
// RTC_READ //
//==========================================================//
unsigned char RTC_READ(unsigned char ADDRESS)
{
unsigned char return_value=0x00;
I2C_START();
I2C_WRITE(CONTROL_WRITE); //send byte control write
I2C_WRITE(ADDRESS); // send byte address
I2C_START(); // restart
I2C_WRITE(CONTROL_READ); // send byte control read
return_value = I2C_READ(); //read from RTC
I2C_STOP();
return return_value;
}
//==========================================================//
// //
// RTC_INI //
//==========================================================//
void RTC_INI()
{ //ADDRESS,DATA//
RTC_WRITE(0x00,_TIME.SEC);
RTC_WRITE(0x01,_TIME.MIN);
RTC_WRITE(0x02,_TIME.HOUR);
RTC_WRITE(0x03,_TIME.DAY);
RTC_WRITE(0x04,_TIME.DATE);
RTC_WRITE(0x05,_TIME.MONTH);
RTC_WRITE(0x06,_TIME.YEAR);
RTC_WRITE(0x07,0x10);//OUT = 1,SOUT = 1;
}
//==========================================================//
// //
// GET_TIME //
//==========================================================//
void GET_TIME()
{
_TIME.SEC = RTC_READ(0x00); //GET SECOND
_TIME.MIN = RTC_READ(0x01); //GET MIN
_TIME.HOUR = RTC_READ(0x02)&0x3f; //GET HOUR
_TIME.DAY = RTC_READ(0x03); //GET DAY
_TIME.DATE = RTC_READ(0x04); //GET DATE
_TIME.MONTH = RTC_READ(0x05); //GET MONTH
_TIME.YEAR = RTC_READ(0x06); //GET YEAR
CONVERT();
}
//==========================================================//
// //
// Timer0_ini //
//==========================================================//
void TIMER0_ini()
{
TMOD = 0x01 ;//timer 0 16bit reload
TH0 = 0x00;
TL0 = 0x00;
TF0 = 0;
TR0 = 0;
ET0 = 1; //enable timer0 interrupt
}
//==========================================================//
// //
// CONVERT //
//==========================================================//
void CONVERT()
{
unsigned char temp,index=15;
//GET SECCOND
temp = _TIME.SEC;
temp&=0x0f; //get the unit
TIME_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.SEC;
temp&=0xf0; //get the tens
temp>>=4;
TIME_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
TIME_DISPLAY[index]=0x40; //
index--;
//GET MIN
temp = _TIME.MIN;
temp&=0x0f; //get the unit
TIME_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.MIN;
temp&=0xf0; //get the tens
temp>>=4;
TIME_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
TIME_DISPLAY[index]=0x40; //
index--;
//GET HOUR
temp = _TIME.HOUR;
temp&=0x0f; //get the unit
TIME_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.HOUR;
temp&=0xf0; //get the tens
temp>>=4;
TIME_DISPLAY[index]=seg_code[temp];
index=15;
//GET YEAR
temp = _TIME.YEAR;
temp&=0x0f; //get the unit
DATE_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.YEAR;
temp&=0xf0; //get the tens
temp>>=4;
DATE_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
DATE_DISPLAY[index]=0x40; //
index--;
//GET MONTH
temp = _TIME.MONTH;
temp&=0x0f; //get the unit
DATE_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.MONTH;
temp&=0xf0; //get the tens
temp>>=4;
DATE_DISPLAY[index]=seg_code[temp];
index--;
//SPACER
DATE_DISPLAY[index]=0x40; //
index--;
//GET DATE
temp = _TIME.DATE;
temp&=0x0f; //get the unit
DATE_DISPLAY[index]=seg_code[temp];
index--;
temp = _TIME.DATE;
temp&=0xf0; //get the tens
temp>>=4;
DATE_DISPLAY[index]=seg_code[temp];
}
//==========================================================//
// //
// Timer1_ini //
//==========================================================//
void TIMER1_ini()
{
TMOD |= 0x10; //timer 1 16bit reload
TH1 = 0x00;
TL1 = 0x00;
TF1 = 0;
TR1 = 0;
ET1 = 1;
}
//==========================================================//
// //
// Timer0_interrupt //
//==========================================================//
void TIMER0_interrupt() interrupt 1
{
TR0 = 0;
TF0 = 0;
TH0 = 0;
TL0 = 0;
GLOBAL_DISPLAY++;
if(GLOBAL_DISPLAY==TIME_STATUS)
GLOBAL_DISPLAY=0;
TR0 = 1;
}
//==========================================================//
// //
// Timer1_interrupt //
//==========================================================//
void TIMER1_interrupt() interrupt 3
{
TR1 = 0; //stop timer1
TF1 = 0; //clear TF1
TH1 = 0;
TL1 = 1;
CONTROL_INVISIBLE++;
if(CONTROL_INVISIBLE==5)
{
IN_VISIBLE = 0x00;
}
else if(CONTROL_INVISIBLE==10)
{
IN_VISIBLE = SHIFT_CURCOR;
CONTROL_INVISIBLE=0;
}
else {;}
TR1 = 1;
}
//==========================================================//
// //
// External Interrupt 1 //
//==========================================================//
void EX1_interrupt()interrupt 2
{
unsigned char unit,tens;
EX1 = 0; // disable ex interrupt 1
EX0 = 0; // disable ex interrupt 0
counter_status=0x00;
TR1 = 1; //enable TIMER1
IN_VISIBLE = 0xc0;
SHIFT_CURCOR = IN_VISIBLE;
CONTROL_INVISIBLE=0;
// GET_TIME();
while(MODE==0)DISPLAY_2__(TIME_DISPLAY);
while(counter_status!=0x07)
{
switch(counter_status)
{
case 0x00: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.HOUR;
unit &=0x0f;
tens = _TIME.HOUR;
tens >>= 4;
if(tens!=2)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==4)
{
tens=0;
unit = 0;
}
}
_TIME.HOUR = tens;
_TIME.HOUR <<=4;
_TIME.HOUR |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.HOUR;
unit &=0x0f;
tens = _TIME.HOUR;
tens >>= 4;
if(tens!=0)
{
unit--;
if(unit==0xff)
{
tens--;
unit = 0x09;
}
}
else
{
unit--;
if(unit==0xff)
{
tens = 0x02;
unit = 0x03;
}
}
_TIME.HOUR = tens;
_TIME.HOUR <<=4;
_TIME.HOUR |= unit;
CONVERT();
}
DISPLAY_2__(TIME_DISPLAY);
}
while(MODE==0){DISPLAY_2__(TIME_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(TIME_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x01: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.MIN;
unit &=0x0f;
tens = _TIME.MIN;
tens >>= 4;
unit++;
if(unit==10)
{
tens++;
unit=0;
}
if(tens==6)
tens=0;
_TIME.MIN = tens;
_TIME.MIN <<=4;
_TIME.MIN |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.MIN;
unit &=0x0f;
tens = _TIME.MIN;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
tens=5;
_TIME.MIN = tens;
_TIME.MIN <<=4;
_TIME.MIN |= unit;
CONVERT();
}
DISPLAY_2__(TIME_DISPLAY);
}
while(MODE==0){DISPLAY_2__(TIME_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(TIME_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x02: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.SEC;
unit &=0x0f;
tens = _TIME.SEC;
tens >>= 4;
unit++;
if(unit==10)
{
tens++;
unit=0;
}
if(tens==6)
tens=0;
_TIME.SEC = tens;
_TIME.SEC <<=4;
_TIME.SEC|= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(TIME_DISPLAY);
unit = _TIME.SEC;
unit &=0x0f;
tens = _TIME.SEC;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
tens=5;
_TIME.SEC = tens;
_TIME.SEC <<=4;
_TIME.SEC|= unit;
CONVERT();
}
DISPLAY_2__(TIME_DISPLAY);
}
while(MODE==0){DISPLAY_2__(TIME_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(TIME_DISPLAY);}
IN_VISIBLE=0xc0;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x03: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.DATE;
unit &=0x0f;
tens = _TIME.DATE;
tens >>= 4;
if(_TIME.MONTH%2==1)
{
if(tens<3)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==2)
{
tens=0;
unit=0;
}
}
}
else if(_TIME.MONTH%2==0)
{
if(tens<3)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==1)
{
tens=0;
unit=0;
}
}
}
_TIME.DATE = tens;
_TIME.DATE <<=4;
_TIME.DATE |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.DATE;
unit &=0x0f;
tens = _TIME.DATE;
tens >>= 4;
if(_TIME.MONTH%2==1)
{
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
{
tens=3;
unit=1;
}
}
else if(_TIME.MONTH%2==0)
{
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
{
tens=3;
unit=0;
}
}
_TIME.DATE = tens;
_TIME.DATE <<=4;
_TIME.DATE |= unit;
CONVERT();
}
DISPLAY_2__(DATE_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DATE_DISPLAY); }
while(IN_VISIBLE==0x00){DISPLAY_2__(DATE_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x04: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.MONTH;
unit &=0x0f;
tens = _TIME.MONTH;
tens >>= 4;
if(tens!=1)
{
unit++;
if(unit==10)
{
tens++;
unit=0;
}
}
else
{
unit++;
if(unit==3)
{
tens=0;
unit=0;
}
}
_TIME.MONTH = tens;
_TIME.MONTH <<=4;
_TIME.MONTH |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.MONTH;
unit &=0x0f;
tens = _TIME.MONTH;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
{
tens=1;
unit=2;
}
_TIME.MONTH = tens;
_TIME.MONTH <<=4;
_TIME.MONTH |= unit;
CONVERT();
}
DISPLAY_2__(DATE_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DATE_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(DATE_DISPLAY);}
IN_VISIBLE>>=3;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x05: {
while(MODE)
{
if(INC==0)
{
while(INC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.YEAR;
unit &=0x0f;
tens = _TIME.YEAR;
tens >>= 4;
unit++;
if(unit==10)
{
tens++;
unit=0;
}
if(tens==9)
tens=0;
_TIME.YEAR = tens;
_TIME.YEAR <<=4;
_TIME.YEAR |= unit;
CONVERT();
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DATE_DISPLAY);
unit = _TIME.YEAR;
unit &=0x0f;
tens = _TIME.YEAR;
tens >>= 4;
unit--;
if(unit==0xff)
{
tens--;
unit=9;
}
if(tens==0xff)
tens=9;
_TIME.YEAR = tens;
_TIME.YEAR <<=4;
_TIME.YEAR |= unit;
CONVERT();
}
DISPLAY_2__(DATE_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DATE_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(DATE_DISPLAY);}
IN_VISIBLE = 0xff;
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
case 0x06: {
while(MODE)
{
CHECK_DAY(0);
if(INC==0)
{
while(INC==0)DISPLAY_2__(DAY_DISPLAY);
_TIME.DAY++;
if(_TIME.DAY==8)
_TIME.DAY=1;
CHECK_DAY(0);
}
if(DEC==0)
{
while(DEC==0)DISPLAY_2__(DAY_DISPLAY);
_TIME.DAY--;
if(_TIME.DAY==0xff)
_TIME.DAY=7;
CHECK_DAY(0);
}
DISPLAY_2__(DAY_DISPLAY);
}
while(MODE==0){DISPLAY_2__(DAY_DISPLAY);}
while(IN_VISIBLE==0x00){DISPLAY_2__(DAY_DISPLAY);}
SHIFT_CURCOR = IN_VISIBLE;
counter_status++;
break;
}
}
RTC_INI();
}
IE1 = 0;//clear external interrupt 1 flag
TR1 = 0;
EX0 = 1;
EX1 = 1;
}
//==========================================================//
// //
// CHECK_DAY //
//==========================================================//
void CHECK_DAY(bit get)
{
unsigned char index,DAY;
if(get)
DAY = RTC_READ(0x03);
else
DAY = _TIME.DAY;
switch(DAY)
{
case 0x01: for(index=0;index<24;index++)DAY_DISPLAY[index]=SUN[index];break;
case 0x02: for(index=0;index<24;index++)DAY_DISPLAY[index]=MON[index];break;
case 0x03: for(index=0;index<24;index++)DAY_DISPLAY[index]=TUE[index];break;
case 0x04: for(index=0;index<24;index++)DAY_DISPLAY[index]=WED[index];break;
case 0x05: for(index=0;index<24;index++)DAY_DISPLAY[index]=THU[index];break;
case 0x06: for(index=0;index<24;index++)DAY_DISPLAY[index]=FRI[index];break;
case 0x07: for(index=0;index<24;index++)DAY_DISPLAY[index]=SAT[index];break;
}
}
//===========================================================///
void update_time() interrupt 0
{
EX0 = 0;
GET_TIME();
IE0 = 0;
EX0 = 1;
}
//===========================================================//
void GET_T()
{
unsigned char temp;
_RW = 0;
_RW = 1;
while(_INT);
RD = 0;
temp = P1;
temp&=0x7f;
RD = 1;
T_value[0]=seg_code[temp/10];
T_value[1]=seg_code[temp%10];
}
( Code compiler + Schematic + Simulation)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn dientuvietnam.net
0 Response to "Mạch đồng hồ Giờ Phút - Nhiệt độ"
Đăng nhận xét