Sie sind auf Seite 1von 8

#include <16F877a.

h> // KHAI BAO SU DUNG PIC 16F877A


#include // FILE DINH NGHIA CAC THANH GHI VA CAC BIT
#FUSES WDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,
NOLVP,NOCPD,
NOWRT
#use delay(clock=20000000)
#include
/////////////////////////////////////////////////////////////////////////////
byte sec,min,hour;
byte const LED[10] =
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 };
byte TG1,TG2,TG3,TG4,TG5,TG6; //bien hien led trung gian giay, phut, gio
#bit SW1 = 0x08.0 // D0 - chon che do chinh
#bit SW2 = 0x08.1 // D1 - tang thoi gian
#bit SW3 = 0x08.2 // D2 - giam thoi gian
#bit SW4 = 0x08.3 // D3 - ok
#bit pc = 0x05.3 // phut- hang chuc
#bit pd = 0x05.2 // phut- hang don vi
#bit gc = 0x05.1 // giay- hang chuc
#bit gd = 0x07.0 // giay- hang don vi
byte chinh_phut, chinh_gio;
void update_1307();
//mac dinh int8
//CAP NHAT DS1307
void set_sec();
void set_min();
void set_hour();
void read_time();
void update_time();
void display();
//DOC THOI GIAN DS1307
//CAP NHAT THOI GIAN
//HIEN THI
// BAT DAU CHUONG TRINH CHINH///////////////////////////
void main()
{
byte u;
//doc time
set_Tris_d(0x0F);
//00001111
set_Tris_b(0x00);
set_Tris_a(0x00);
set_Tris_c(0x18);
//00011000
set_Tris_e(0x00);
u=read_ds1307(0);
sec=u & 0x0F;
setup_wdt (WDT_1152MS);
//////////////////////////////////////////////
write_ds1307(0,sec);
// ket noi dong ho(gan bit7=0)
chinh_gio=0; chinh_phut=0;
gic=1; gid=1; pc=1; pd=1; gc=1; gd=1;//tat cac led
while(true)
{
read_time();
display();
if(sec%2==0)
// chan tat, le sang
{ lednhay=1; }
else
{ lednhay=0; }
if(sw1==0)
// chon 'che do'
{
if(chinh_phut==0&&sw4==1)
{
else if(chinh_gio==0&&sw4==1)
{
while(sw1==0) {}
set_hour(); chinh_gio=1;
}
else if(chinh_phut==1&&chinh_gio==1)
{
chinh_phut=0; chinh_gio=0;
}
}
if(sw4==0)
{
chinh_gio=0;chinh_phut=0;
}
restart_wdt();
}
}
//////////////////////////////////////////////////
void read_time()
{
sec = read_DS1307(0);
min = read_DS1307(1);
hour = read_DS1307(2);
update_time();
//goi chuyen doi
}
/////////////////////////////////////////////////////
void update_1307()
{
write_DS1307(0,sec);
write_DS1307(1,min);
write_DS1307(2,hour);
}
////////////////////////////////////////////////////
void update_time()
{
TG1= sec & 0x0F;
TG2=(sec & 0xF0)>>4; //convert to BCD SEC
TG3= min & 0x0F;
TG4=(min & 0xF0)>>4; //convert to BCD MIN
TG5= hour & 0x0F;
TG6=(hour & 0x30)>>4; //convert to BCD HOUR
}
//////////////////////////////////////////////////////
void set_min()
{
gic=1; gid=1; pc=1; pd=1; gc=1; gd=1;
min= read_DS1307(1);
TG3= min & 0x0F;
TG4=(min & 0xF0)>>4;
while(sw1==1&&sw4==1)
{
portb= LED[TG3]; pd=0; delay_us(500); pd=1;
while(sw2==0) {}
if(TG4==5&&TG3==9) { TG4=0; TG3=0;}
else if(TG3==9) { TG4++; TG3=0;}
else TG3++;
}
if(sw3==0)//giam
{
while(sw3==0) {}
if(TG4==0&&TG3==0) {TG4=5; TG3=9;}
else if(TG3==0) {TG4--; TG3=9;}
else TG3--;
}
min= (TG4<<4) + TG3;
write_DS1307(1,min);
delay_us(10);
restart_wdt();
}
}
////////////////////////////////////////////////////
void set_hour()
{
gic=1; gid=1; pc=1; pd=1; gc=1; gd=1;
hour= read_DS1307(2);
TG5= hour & 0x0F;
TG6=(hour & 0x30)>>4;
while(sw1==1&&sw4==1)
{
portb= LED[TG5]; gid=0; delay_us(500); gid=1;
portb= LED[TG6]; gic=0; delay_us(500); gic=1;
if(sw2==0)//tang
{
while(sw2==0) {}
if(TG6==2&&TG5==3) {TG6=0;TG5=0;}
else if(TG5==9) {TG6++; TG5=0;}
else TG5++;
}
if(sw3==0)//giam
{
while(sw3==0) {}
if(TG6==0&&TG5==0) {TG6=2; TG5=3;}
else if(TG5==0) {TG6--; TG5=9;}
else TG5--;
}
hour= (TG6<<4) + TG5;
write_DS1307(2,hour);
delay_us(10);
restart_wdt();
SVTH: Dương Phát
// sec - min - hour
PortB=LED[TG6];//gio chuc
gic=0;
delay_us(500);
gic=1;
PortB=LED[TG5];//gio don vi
gid=0;
delay_us(500);
gid=1;
PortB=LED[TG4];//phut chuc
pc=0;
delay_us(500);
pc=1;
PortB=LED[TG3];//phut don vi
pd=0;
delay_us(500);
pd=1;
PortB=LED[TG2];//giay chuc
gc=0;
delay_us(500);
gc=1;
PortB=LED[TG1];//giay don vi
gd=0;
delay_us(500);
gd=1;
restart_wdt();
}
Chương trình con DS1307.c
#define DS1307_SDA PIN_C4
#define DS1307_SCL PIN_C3
#use i2c(master, sda=DS1307_SDA, scl=DS1307_SCL)

// write data one byte to


// DS1307
//==========================
void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}
//==========================
// read data one byte from DS1307
//==========================
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}

Das könnte Ihnen auch gefallen