Code mã nguồn C:
#include<reg52.h>
sbit UP=P3^7;
sbit DOWN=P3^5;
sbit SET=P3^6;
sbit SDA=P1^1;
sbit SCL=P1^0;
sbit ALARM1=P1^7;
sbit ALARM2=P1^6;
sbit ALARM3=P1^5;
sbit ALARM4=P1^4;
sbit SPEAKER=P3^1;
const unsigned char Code7Seg[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
const unsigned char Select7Seg[7]={0x00,0x01,0x02,0x04,0x08,0x10,0x20};
#define DataPort P2
#define Controll P0
unsigned char seconds,minute,hour,dvphut,dvgio,dvgiay,chucphut,chucgio,chucgiay;
unsigned char chucgio_bt1,chucgio_bt2,chucgio_bt3,chucgio_bt4;
unsigned char chucphut_bt1,chucphut_bt2,chucphut_bt3,chucphut_bt4;
unsigned char chucgiay_bt1,chucgiay_bt2,chucgiay_bt3,chucgiay_bt4;
unsigned char dvgio_bt1,dvgio_bt2,dvgio_bt3,dvgio_bt4;
unsigned char dvphut_bt1,dvphut_bt2,dvphut_bt3,dvphut_bt4;
unsigned char dvgiay_bt1,dvgiay_bt2,dvgiay_bt3,dvgiay_bt4;
unsigned char hour_bt1=0,hour_bt2=0,hour_bt3=0,hour_bt4=0;
unsigned char minute_bt1=0,minute_bt2=0,minute_bt3=0,minute_bt4=0;
unsigned char seconds_bt1=0,seconds_bt2=0,seconds_bt3=0,seconds_bt4=0;
unsigned char bt1=0,bt2=0,bt3=0,bt4=0,bt_on=0;
unsigned char a=0,kt=0,set0=1,set1=1,up0=1,up1=1,down0=1,down1=1,al=0;
//----------------------------------------------
//##################DELAY#######################
//----------------------------------------------
void delay()
{
unsigned char i;
for (i = 0; i < 4; i++){};
}
//--------------------------------------------
void delay1(unsigned char time8us)
{
while(time8us--);
}
//-----------------------------------------
//===========I2C=============================
//-----------------------------------------
void SCL_high()
{
SCL = 1;
delay();
}
//------------------------------------------
void SCL_low()
{
SCL = 0;
delay();
}
//-------------------------------------------
void I2C_Start()
{
SDA = 1;
SCL = 1;
SDA = 0;
delay();
SCL = 0;
SDA = 1;
}
//--------------------------------------------
void I2C_Stop()
{
SDA = 0;
SCL_high();
SDA = 1;
}
//----------------------------------------------
bit I2C_Write(unsigned char dat)
{
unsigned char i;
bit outbit;
for (i = 1; i <= 8; i++)
{
outbit=dat&0x80;
SDA = outbit;
dat = dat << 1;
SCL_high();
SCL_low();
}
SDA = 1;
SCL_high();
outbit = SDA;
SCL_low();
return(outbit);
}
//----------------------------------------------------
unsigned char I2C_Read(bit ack)
{
unsigned char i, dat;
bit inbit;
dat = 0;
for(i=1;i<=8;i++)
{
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack) SDA = 0;
else SDA = 1;
SCL_high();
SCL = 0;
SDA = 1;
delay();
return(dat);
}
//-------------------------------------------------------------
//======================DS1307==============================
//--------------------------------------------------------------
void rtc_write(unsigned char add, unsigned char dat)
{
I2C_Start();
I2C_Write(0xd0);
I2C_Write(add);
I2C_Write(((dat/10)<<4)|(dat%10));
I2C_Stop();
}
unsigned char rtc_read(unsigned char add)
{
unsigned char dat;
I2C_Start();
I2C_Write(0xd0);
I2C_Write(add);
I2C_Start();
I2C_Write(0xd1);
dat = I2C_Read(0);
I2C_Stop();
dat = (dat & 0x0f) + (dat>>4)*10;
return (dat);
}
void write_rtc()
{
rtc_write(0x00, seconds); //Thanh ghi giây (SECONDS): thanh ghi này là thanh ghi dau tiên trong bo nho cua DS1307, dia chi cua nó là 0x00. Bon bit thap cua thanh ghi này chia ma BCD 4-bit cua chu so hàng don vi cua giá tri giây. Do giá tri cao nhat cua chu so hàng chuc là 5 (không có giây 60) nên chi can 3 bit (các bit SECONDS 6:4) là có the ma hóa duuc (so 5 =101, 3 bit).
/* 0x00 la gia tri cua thanh ghi seconds (giay), nap gia tri dang co trong bien seconds vao thanh ghi giay trong DS1307 tuong duong voi viec nap gia tri 0 cho bit ch (Bit cao nhat, bit 7, trong thanh ghi này là 1 bit dieu khien (Clock halt – treo dong ho), neu bit này duuc set bang 1 bo dao dong trong chip bi vô hieu hóa, dong ho không hoat dong. Vi vay, nhat thiet phai reset bit này xuong 0 ngay tu dau (^_^ tuc la chinh giay xong thi dong ho se chay lun, neu khong thi no khong chay). )*/
rtc_write(0x01, minute); //nap gia tri dang co trong bien minute vao thanh ghi phut trong DS1307, dia chi cua thanh ghi phut trong DS1307 la 0x01
rtc_write(0x02, hour); //nap gia tri dang co trong bien hour vao thanh ghi gio trong DS1307, dia chi cua thanh ghi gio trong DS1307 la 0x02
rtc_write(0x08, seconds_bt1);
rtc_write(0x09, seconds_bt2);
rtc_write(0x0a, seconds_bt3);
rtc_write(0x0b, seconds_bt4);
rtc_write(0x0c, minute_bt1);
rtc_write(0x0d, minute_bt2);
rtc_write(0x0e, minute_bt3);
rtc_write(0x0f, minute_bt4);
rtc_write(0x10, hour_bt1);
rtc_write(0x11, hour_bt2);
rtc_write(0x12, hour_bt3);
rtc_write(0x13, hour_bt4);
}
void read_rtc()
{
if(kt==0) //kiem tra gia tri dang co trong bien kt co = 0 hay khong, neu khong = 0 thi thoat khoi ham read_rtc()
{
if(a==1) //neu gia tri dang co trong bien a = 1 thi ghi vao DS1307 nhung gia tri cua cac bien da thay doi va dat lai gia tri 0 vao bien a
{
write_rtc(); //goi ham con write_rtc() de ghi nhung gia tri cua cac bien da thay doi
a=0; //nap lai gia tri 0 vao bien a
}
seconds = rtc_read(0x00); //doc gia tri dang co o thanh ghi giay trong DS1307 roi nap gia tri do vao bien seconds
minute = rtc_read(0x01); //doc gia tri dang co o thanh ghi phut trong DS1307 roi nap gia tri do vao bien minute
hour = rtc_read(0x02); //doc gia tri dang co o thanh ghi gio trong DS1307 roi nap gia tri do vao bien hour
}
}
//-----------------------------------------------------------------------------
//++++++++++++++++++++++++quet led 7 doan++++++++++++++++++++++++++++++++++++++
//-----------------------------------------------------------------------------
void hienthi()
{
unsigned char i;
dvphut = (Code7Seg[minute%10]);
chucphut = (Code7Seg[minute/10]);
dvgio = (Code7Seg[hour%10]);
chucgio = (Code7Seg[hour/10]);
dvgiay = (Code7Seg[seconds%10]);
chucgiay = (Code7Seg[seconds/10]);
dvphut_bt1 = (Code7Seg[minute_bt1%10]);
chucphut_bt1 = (Code7Seg[minute_bt1/10]);
dvgio_bt1 = (Code7Seg[hour_bt1%10]);
chucgio_bt1 = (Code7Seg[hour_bt1/10]);
dvgiay_bt1 = (Code7Seg[seconds_bt1%10]);
chucgiay_bt1 = (Code7Seg[seconds_bt1/10]);
dvphut_bt2 = (Code7Seg[minute_bt2%10]);
chucphut_bt2 = (Code7Seg[minute_bt2/10]);
dvgio_bt2 = (Code7Seg[hour_bt2%10]);
chucgio_bt2 = (Code7Seg[hour_bt2/10]);
dvgiay_bt2 = (Code7Seg[seconds_bt2%10]);
chucgiay_bt2 = (Code7Seg[seconds_bt2/10]);
dvphut_bt3 = (Code7Seg[minute_bt3%10]);
chucphut_bt3 = (Code7Seg[minute_bt3/10]);
dvgio_bt3 = (Code7Seg[hour_bt3%10]);
chucgio_bt3 = (Code7Seg[hour_bt3/10]);
dvgiay_bt3 = (Code7Seg[seconds_bt3%10]);
chucgiay_bt3 = (Code7Seg[seconds_bt3/10]);
dvphut_bt4 = (Code7Seg[minute_bt4%10]);
chucphut_bt4 = (Code7Seg[minute_bt4/10]);
dvgio_bt4 = (Code7Seg[hour_bt4%10]);
chucgio_bt4 = (Code7Seg[hour_bt4/10]);
dvgiay_bt4 = (Code7Seg[seconds_bt4%10]);
chucgiay_bt4 = (Code7Seg[seconds_bt4/10]);
//===============================hienthi======================================
if ( kt == 0 && al == 0) //hien thi che do binh thuong
{
i=0;
while(i<8) //t_on cua chu ky hien thi
{
if(hour<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];} //xoa so khong
else {Controll=Select7Seg[1];DataPort=chucgio;} //hien thi chuc gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[2];DataPort=dvgio; //hien thi don vi gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[3];DataPort=chucphut; //hien thi chuc phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[4];DataPort=dvphut; //hien thi don vi phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[5];DataPort=chucgiay; //hien thi chuc giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[6];DataPort=dvgiay; //hien thi don vi giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
if(bt1==1)ALARM1=0; else ALARM1=1;
if(bt2==1)ALARM2=0; else ALARM2=1;
if(bt3==1)ALARM3=0; else ALARM3=1;
if(bt4==1)ALARM4=0; else ALARM4=1;
i++;
}
}
if ( kt == 0 && al == 1) //hien thi che do bao thuc 1
{
i=0;
while(i<8) //t_on cua chu ky hien thi
{
if(hour_bt1<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];} //xoa so khong
else {Controll=Select7Seg[1];DataPort=chucgio_bt1;} //hien thi chuc gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[2];DataPort=dvgio_bt1; //hien thi don vi gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[3];DataPort=chucphut_bt1; //hien thi chuc phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[4];DataPort=dvphut_bt1; //hien thi don vi phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[5];DataPort=chucgiay_bt1; //hien thi chuc giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[6];DataPort=dvgiay_bt1; //hien thi don vi giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
ALARM1=0;ALARM2=1;ALARM3=1;ALARM4=1;
i++;
}
}
if ( kt == 0 && al == 2) //hien thi che do bao thuc 2
{
i=0;
while(i<8) //t_on cua chu ky hien thi
{
if(hour_bt2<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];} //xoa so khong
else {Controll=Select7Seg[1];DataPort=chucgio_bt2;} //hien thi chuc gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[2];DataPort=dvgio_bt2; //hien thi don vi gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[3];DataPort=chucphut_bt2; //hien thi chuc phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[4];DataPort=dvphut_bt2; //hien thi don vi phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[5];DataPort=chucgiay_bt2; //hien thi chuc giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[6];DataPort=dvgiay_bt2; //hien thi don vi giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
ALARM1=1;ALARM2=0;ALARM3=1;ALARM4=1;
i++;
}
}
if ( kt == 0 && al == 3) //hien thi che do bao thuc 3
{
i=0;
while(i<8) //t_on cua chu ky hien thi
{
if(hour_bt3<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];} //xoa so khong
else {Controll=Select7Seg[1];DataPort=chucgio_bt3;} //hien thi chuc gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[2];DataPort=dvgio_bt3; //hien thi don vi gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[3];DataPort=chucphut_bt3; //hien thi chuc phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[4];DataPort=dvphut_bt3; //hien thi don vi phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[5];DataPort=chucgiay_bt3; //hien thi chuc giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[6];DataPort=dvgiay_bt3; //hien thi don vi giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
ALARM1=1;ALARM2=1;ALARM3=0;ALARM4=1;
i++;
}
}
if ( kt == 0 && al == 4) //hien thi che do bao thuc 4
{
i=0;
while(i<8) //t_on cua chu ky hien thi
{
if(hour_bt4<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];} //xoa so khong
else {Controll=Select7Seg[1];DataPort=chucgio_bt4;} //hien thi chuc gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[2];DataPort=dvgio_bt4; //hien thi don vi gio
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[3];DataPort=chucphut_bt4; //hien thi chuc phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[4];DataPort=dvphut_bt4; //hien thi don vi phut
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[5];DataPort=chucgiay_bt4; //hien thi chuc giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
Controll=Select7Seg[6];DataPort=dvgiay_bt4; //hien thi don vi giay
delay1(25); //t_on cua chu ky chon led
Controll=Select7Seg[0];DataPort=Code7Seg[11]; //tat chong lem
delay1(2); //t_off cua chu ky chon led
ALARM1=1;ALARM2=1;ALARM3=1;ALARM4=0;
i++;
}
}
//================================hien thi trong luc chinh=================================
//--------------------------chinh gio-----------------------------
if (kt == 1 && al == 0) //chinh gio
{
i = 0;
while (i<8)
{
if(hour<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];}
else {Controll=Select7Seg[1];DataPort=chucgio;}
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[2];DataPort=dvgio;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 1 && al == 1) //chinh gio
{
i = 0;
while (i<8)
{
if(hour_bt1<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];}
else {Controll=Select7Seg[1];DataPort=chucgio_bt1;}
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[2];DataPort=dvgio_bt1;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 1 && al == 2) //chinh gio
{
i = 0;
while (i<8)
{
if(hour_bt2<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];}
else {Controll=Select7Seg[1];DataPort=chucgio_bt2;}
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[2];DataPort=dvgio_bt2;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 1 && al == 3) //chinh gio
{
i = 0;
while (i<8)
{
if(hour_bt3<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];}
else {Controll=Select7Seg[1];DataPort=chucgio_bt3;}
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[2];DataPort=dvgio_bt3;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 1 && al == 4) //chinh gio
{
i = 0;
while (i<8)
{
if(hour_bt4<10) {Controll=Select7Seg[0];DataPort=Code7Seg[11];}
else {Controll=Select7Seg[1];DataPort=chucgio_bt4;}
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[2];DataPort=dvgio_bt4;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
//--------------------------chinh phut-----------------------------
if (kt == 2 && al == 0) //chinh phut
{
i = 0;
while (i<8)
{
Controll=Select7Seg[3];DataPort=chucphut;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[4];DataPort=dvphut;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 2 && al == 1) //chinh phut
{
i = 0;
while (i<8)
{
Controll=Select7Seg[3];DataPort=chucphut_bt1;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[4];DataPort=dvphut_bt1;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 2 && al == 2) //chinh phut
{
i = 0;
while (i<8)
{
Controll=Select7Seg[3];DataPort=chucphut_bt2;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[4];DataPort=dvphut_bt2;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 2 && al == 3) //chinh phut
{
i = 0;
while (i<8)
{
Controll=Select7Seg[3];DataPort=chucphut_bt3;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[4];DataPort=dvphut_bt3;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 2 && al == 4) //chinh phut
{
i = 0;
while (i<8)
{
Controll=Select7Seg[3];DataPort=chucphut_bt4;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[4];DataPort=dvphut_bt4;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
//--------------------------chinh phut-----------------------------
if (kt == 3 && al == 0) //chinh giay
{
i = 0;
while (i<8)
{
Controll=Select7Seg[5];DataPort=chucgiay;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[6];DataPort=dvgiay;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 3 && al == 1) //chinh giay
{
i = 0;
while (i<8)
{
Controll=Select7Seg[5];DataPort=chucgiay_bt1;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[6];DataPort=dvgiay_bt1;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 3 && al == 2) //chinh giay
{
i = 0;
while (i<8)
{
Controll=Select7Seg[5];DataPort=chucgiay_bt2;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[6];DataPort=dvgiay_bt2;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 3 && al == 3) //chinh giay
{
i = 0;
while (i<8)
{
Controll=Select7Seg[5];DataPort=chucgiay_bt3;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[6];DataPort=dvgiay_bt3;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
if (kt == 3 && al == 4) //chinh giay
{
i = 0;
while (i<8)
{
Controll=Select7Seg[5];DataPort=chucgiay_bt4;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
Controll=Select7Seg[6];DataPort=dvgiay_bt4;
delay1(25);
Controll=Select7Seg[0];DataPort=Code7Seg[11];
delay1(2);
i++;
}
}
}
//-----------------------------------------------------------------------------
//++++++++++++++++++++++++quet phim nhan++++++++++++++++++++++++++++++++++++++
//-----------------------------------------------------------------------------
void ktphim()
{
set0=set1;set1=SET; //khi phim SET chua dc tac dong : set0=set1=SET=1; khi phim SET dc tac dong : set0=set1=1, set1=SET=0
if((set0==1)&&(set1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
kt++; //tang bien kt len 1 don vi
if(kt>3)kt=0; //neu gia tri dang co trong bien kt lon hon 4 thi nap lai gia tri 0 cho bien kt
}
switch(kt)
{
case 0:break; //bien kt = 0 thi nhay ra khoi ham ktphim()
case 1: //kt = 1 chinh gio
{
up0=up1;up1=UP; a=1; //khi phim UP chua dc tac dong : up0=up1=UP=1; khi phim UP dc tac dong : set0=set1=1, set1=SET=0
if((up0==1)&&(up1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
if (al == 0)hour++;if(hour>23)hour=0; //tang gia tri dang co trong bien hour len 1 don vi, neu gia tri dang co trong bien hour lon hon 24 thi nap lai gia tri 0 vao bien hour
if (al == 1)hour_bt1++;if(hour_bt1>23)hour_bt1=0; //tang gia tri dang co trong bien hour_bt1 len 1 don vi, neu gia tri dang co trong bien hour_bt1 lon hon 24 thi nap lai gia tri 0 vao bien hour_bt1
if (al == 2)hour_bt2++;if(hour_bt2>23)hour_bt2=0; //tang gia tri dang co trong bien hour_bt2 len 1 don vi, neu gia tri dang co trong bien hour_bt2 lon hon 24 thi nap lai gia tri 0 vao bien hour_bt2
if (al == 3)hour_bt3++;if(hour_bt3>23)hour_bt3=0; //tang gia tri dang co trong bien hour_bt3 len 1 don vi, neu gia tri dang co trong bien hour_bt3 lon hon 24 thi nap lai gia tri 0 vao bien hour_bt3
if (al == 4)hour_bt4++;if(hour_bt4>23)hour_bt4=0; //tang gia tri dang co trong bien hour_bt4 len 1 don vi, neu gia tri dang co trong bien hour_bt4 lon hon 24 thi nap lai gia tri 0 vao bien hour_bt4
}
down0=down1;down1=DOWN; //khi phim SET chua dc tac dong : set0=set1=SET=1; khi phim SET dc tac dong : set0=set1=1, set1=SET=0
if((down0==1)&&(down1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
if (al == 0)hour--;if(hour==-1)hour=23; //giam gia tri dang co trong bien hour xuong 1 don vi, neu gia tri dang co trong bien hour = -1 thi nap lai gia tri 0 vao bien hour
if (al == 1)hour_bt1--;if(hour_bt1==-1)hour_bt1=23; //giam gia tri dang co trong bien hour_bt1 xuong 1 don vi, neu gia tri dang co trong bien hour_bt1 = -1 thi nap lai gia tri 0 vao bien hour_bt1
if (al == 2)hour_bt2--;if(hour_bt2==-1)hour_bt2=23; //giam gia tri dang co trong bien hour_bt2 xuong 1 don vi, neu gia tri dang co trong bien hour_bt2 = -1 thi nap lai gia tri 0 vao bien hour_bt2
if (al == 3)hour_bt3--;if(hour_bt3==-1)hour_bt3=23; //giam gia tri dang co trong bien hour_bt3 xuong 1 don vi, neu gia tri dang co trong bien hour_bt3 = -1 thi nap lai gia tri 0 vao bien hour_bt3
if (al == 4)hour_bt4--;if(hour_bt4==-1)hour_bt4=23; //giam gia tri dang co trong bien hour_bt4 xuong 1 don vi, neu gia tri dang co trong bien hour_bt4 = -1 thi nap lai gia tri 0 vao bien hour_bt4
}
break; //nhay ra khoi ham ktphim()
}
case 2:
{ //kt = 2 chinh phut
up0=up1;up1=UP; a=1; //khi phim SET chua dc tac dong : set0=set1=SET=1; khi phim SET dc tac dong : set0=set1=1, set1=SET=0
if((up0==1)&&(up1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
if (al == 0)minute++;if(minute>59)minute=0; //tang gia tri dang co trong bien minute len 1 don vi, neu gia tri dang co trong bien minute lon hon 59 thi nap lai gia tri 0 vao bien minute
if (al == 1)minute_bt1++;if(minute_bt1>59)minute_bt1=0; //tang gia tri dang co trong bien minute_bt1 len 1 don vi, neu gia tri dang co trong bien minute_bt1 lon hon 59 thi nap lai gia tri 0 vao bien minute_bt1
if (al == 2)minute_bt2++;if(minute_bt2>59)minute_bt2=0; //tang gia tri dang co trong bien minute_bt2 len 1 don vi, neu gia tri dang co trong bien minute_bt2 lon hon 59 thi nap lai gia tri 0 vao bien minute_bt2
if (al == 3)minute_bt3++;if(minute_bt3>59)minute_bt3=0; //tang gia tri dang co trong bien minute_bt3 len 1 don vi, neu gia tri dang co trong bien minute_bt3 lon hon 59 thi nap lai gia tri 0 vao bien minute_bt3
if (al == 4)minute_bt4++;if(minute_bt4>59)minute_bt4=0; //tang gia tri dang co trong bien minute_bt4 len 1 don vi, neu gia tri dang co trong bien minute_bt4 lon hon 59 thi nap lai gia tri 0 vao bien minute_bt4
}
down0=down1;down1=DOWN; //khi phim SET chua dc tac dong : set0=set1=SET=1; khi phim SET dc tac dong : set0=set1=1, set1=SET=0
if((down0==1)&&(down1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
if (al == 0)minute--;if(minute==-1)minute=59; //giam gia tri dang co trong bien minute xuong 1 don vi, neu gia tri dang co trong bien minute = -1 thi nap lai gia tri 0 vao bien minute
if (al == 1)minute_bt1--;if(minute_bt1==-1)minute_bt1=59; //giam gia tri dang co trong bien minute_bt1 xuong 1 don vi, neu gia tri dang co trong bien minute_bt1 = -1 thi nap lai gia tri 0 vao bien minute_bt1
if (al == 2)minute_bt2--;if(minute_bt2==-1)minute_bt2=59; //giam gia tri dang co trong bien minute_bt2 xuong 1 don vi, neu gia tri dang co trong bien minute_bt2 = -1 thi nap lai gia tri 0 vao bien minute_bt2
if (al == 3)minute_bt3--;if(minute_bt3==-1)minute_bt3=59; //giam gia tri dang co trong bien minute_bt3 xuong 1 don vi, neu gia tri dang co trong bien minute_bt3 = -1 thi nap lai gia tri 0 vao bien minute_bt3
if (al == 4)minute_bt4--;if(minute_bt4==-1)minute_bt4=59; //giam gia tri dang co trong bien minute_bt4 xuong 1 don vi, neu gia tri dang co trong bien minute_bt4 = -1 thi nap lai gia tri 0 vao bien minute_bt4
}
break; //nhay ra khoi ham ktphim()
}
case 3:
{ //kt = 3 chinh giay
up0=up1;up1=UP; a=1; //khi phim SET chua dc tac dong : set0=set1=SET=1; khi phim SET dc tac dong : set0=set1=1, set1=SET=0
if((up0==1)&&(up1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
if (al == 0)seconds++;if(seconds>59)seconds=0; //tang gia tri dang co trong bien seconds len 1 don vi, neu gia tri dang co trong bien seconds lon hon 59 thi nap lai gia tri 0 vao bien seconds
if (al == 1)seconds_bt1++;if(seconds_bt1>59)seconds_bt1=0; //tang gia tri dang co trong bien seconds_bt1 len 1 don vi, neu gia tri dang co trong bien seconds_bt1 lon hon 59 thi nap lai gia tri 0 vao bien seconds_bt1
if (al == 2)seconds_bt2++;if(seconds_bt2>59)seconds_bt2=0; //tang gia tri dang co trong bien seconds_bt2 len 1 don vi, neu gia tri dang co trong bien seconds_bt2 lon hon 59 thi nap lai gia tri 0 vao bien seconds_bt2
if (al == 3)seconds_bt3++;if(seconds_bt3>59)seconds_bt3=0; //tang gia tri dang co trong bien seconds_bt3 len 1 don vi, neu gia tri dang co trong bien seconds_bt3 lon hon 59 thi nap lai gia tri 0 vao bien seconds_bt3
if (al == 4)seconds_bt4++;if(seconds_bt4>59)seconds_bt4=0; //tang gia tri dang co trong bien seconds_bt4 len 1 don vi, neu gia tri dang co trong bien seconds_bt4 lon hon 59 thi nap lai gia tri 0 vao bien seconds_bt4
}
down0=down1;down1=DOWN; //khi phim SET chua dc tac dong : set0=set1=SET=1; khi phim SET dc tac dong : set0=set1=1, set1=SET=0
if((down0==1)&&(down1==0)) //neu set0=1 va set1=0 thi tang bien kt len 1 don vi roi nhay den switch(kt)
{
if (al == 0)seconds--;if(seconds==-1)seconds=59; //giam gia tri dang co trong bien seconds xuong 1 don vi, neu gia tri dang co trong bien seconds = -1 thi nap lai gia tri 0 vao bien seconds
if (al == 1)seconds_bt1--;if(seconds_bt1==-1)seconds_bt1=59;//giam gia tri dang co trong bien seconds_bt1 xuong 1 don vi, neu gia tri dang co trong bien seconds_bt1 = -1 thi nap lai gia tri 0 vao bien seconds_bt1
if (al == 2)seconds_bt2--;if(seconds_bt2==-1)seconds_bt2=59;//giam gia tri dang co trong bien seconds_bt2 xuong 1 don vi, neu gia tri dang co trong bien seconds_bt2 = -1 thi nap lai gia tri 0 vao bien seconds_bt2
if (al == 3)seconds_bt3--;if(seconds_bt3==-1)seconds_bt3=59;//giam gia tri dang co trong bien seconds_bt3 xuong 1 don vi, neu gia tri dang co trong bien seconds_bt3 = -1 thi nap lai gia tri 0 vao bien seconds_bt3
if (al == 4)seconds_bt4--;if(seconds_bt4==-1)seconds_bt4=59;//giam gia tri dang co trong bien seconds_bt4 xuong 1 don vi, neu gia tri dang co trong bien seconds_bt4 = -1 thi nap lai gia tri 0 vao bien seconds_bt4
}
break; //nhay ra khoi ham ktphim()
}
}
}
void baothuc()
{
if((seconds==seconds_bt1)&&(minute==minute_bt1)&&(hour==hour_bt1)){bt_on=1;bt1=1;}
if((seconds==seconds_bt2)&&(minute==minute_bt2)&&(hour==hour_bt2)){bt_on=1;bt2=1;}
if((seconds==seconds_bt3)&&(minute==minute_bt3)&&(hour==hour_bt3)){bt_on=1;bt3=1;}
if((seconds==seconds_bt4)&&(minute==minute_bt4)&&(hour==hour_bt4)){bt_on=1;bt4=1;}
}
void read_bt()
{
seconds_bt1 = rtc_read(0x08);
seconds_bt2 = rtc_read(0x09);
seconds_bt3 = rtc_read(0x0a);
seconds_bt4 = rtc_read(0x0b);
minute_bt1 = rtc_read(0x0c);
minute_bt2 = rtc_read(0x0d);
minute_bt3 = rtc_read(0x0e);
minute_bt4 = rtc_read(0x0f);
hour_bt1 = rtc_read(0x10);
hour_bt2 = rtc_read(0x11);
hour_bt3 = rtc_read(0x12);
hour_bt4 = rtc_read(0x13);
}
//-----------------------------------------------------------------------------
//++++++++++++++++++++++++chuong trinh chinh+++++++++++++++++++++++++++++++++++
//-----------------------------------------------------------------------------
void main()
{
SPEAKER=0;
IE=0x81; //EA = 1 : bit thu 7 trong thanh ghi IE = 1 <=> cho phep thuc hien cac ngat trong thanh ghi quan ly ngat IE; EX0 = 1 : bit thu 0 trong thanh ghi IE = 1 <=> cho phep ngat ngoai 0
IT0=1; //cho phep INTO ngat suon am
I2C_Start(); //khoi dong qua trinh giao tiep I2C
I2C_Write(0xD0); //
I2C_Write(0x07); //
I2C_Write(0x10); //
I2C_Stop(); //ket thuc qua trinh giao tiep I2C
delay1(100);
read_bt();
while(1)
{
unsigned char f;
for (f=0;f<3;f++)
{
ktphim(); //goi chuong trinh con ktphim() de quet phim nhan
read_rtc(); //goi chuong trinh con read_rtc() de doc gia tri can thiet co trong DS1307 hoac ghi gia tri vua chinh sua vao DS1307
hienthi(); //goi chuong trinh con hienthi() de xuat du lieu can xem ra led 7 doan
baothuc();
}
if (bt_on==1)SPEAKER = ~SPEAKER; else SPEAKER=0;
if ((seconds==(seconds_bt1+0))&&(minute==(minute_bt1+1))&&(hour==hour_bt1)){bt_on=0;bt1=0;}
if ((seconds==(seconds_bt2+0))&&(minute==(minute_bt2+1))&&(hour==hour_bt2)){bt_on=0;bt2=0;}
if ((seconds==(seconds_bt3+0))&&(minute==(minute_bt3+1))&&(hour==hour_bt3)){bt_on=0;bt3=0;}
if ((seconds==(seconds_bt4+0))&&(minute==(minute_bt4+1))&&(hour==hour_bt4)){bt_on=0;bt4=0;}
}
}
void alarm() interrupt 0 //Interrupt Address = (X*8)+3 -/- INT0 : 0003h
{
if (kt == 0)al++;if (al==5) al=0;
}
( Empty)
Điện Tử | Tin Học - echipkool.com - Chia sẻ kiến thức - Kết nối đam mê điện tử
0 Response to "Đồng hồ Giờ - Phút - Giây 4 chế độ hẹn giờ"
Đăng nhận xét