You are on page 1of 6

//***************************************************

//CHUONH TRINH CHO HE THONG DONG HO THOI GIAN THUC


//
Version 01
//
Mua Dong 2012
//
//Gmail : trungsonkmt@gmail.com
//Website: traitimlanhgia.jimdo.Com
//
//
Copyright TrungSonKMT
//***************************************************
#include <16f877a.h> //KHAI BAO THU VIEN PIC16F877A
#device *=16 adc=10 //KHAI BAO CON TRO VA ADC
#FUSES NOWDT, HS, NOPUT, NOPROTECT //THIET LAP CAU HINH CHO PIC
#use delay(clock=20000000) //KHAI BAO TAN SO DAO DONG LA 20MHZ
#include <lcd_lib_4bit.c>
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3) //BAT CHE DO GIAO TIEP I2C
#BIT
#BIT
#BIT
#BIT

mode
increase
decrease
ok

=
=
=
=

0x06.0
0x06.2
0x06.3
0x06.4

#define menu_max 7
int8
int8
int8
void
void
void
void
void
void
void
void
void
void

menu=0;
sec,min,hour,dow,date,month,year;
sec1,sec2,min1,min2,hour1,hour2,day,date1,date2,month1,month2,year1,year2;
set_time(void) ;
set_min(void);
set_hour(void);
set_date(void);
set_day(void);
set_month(void);
set_year(void);
update_time();
display();
hello();

#INT_RB
void ok_()
{
if(ok)menu=menu_max;
}
#INT_EXT
void set()
{
while(mode==0);
if(++menu>menu_max)menu=0;
}
///////////////////////////
void main ()
{
////////////DINH HUONG VAO/RA//////////////
set_tris_b(0xff);
set_tris_d(0x00);
/////////////THIET LAP NGAT////////////////
enable_interrupts(global); //NGAT TOAN CUC
enable_interrupts(INT_RB); //NGAT RB
enable_interrupts(int_ext);//NGAT NGOAI;

Lcd_init();
hello();
while(1)
{
if (menu!=0) {set_time();}
update_time();
display();
}
}
//////////////////////CHUONG TRINH CAP NHAT THOI GIAN/////////////////////////
void update_time()
{
i2c_start(); //phat bit start I2C
i2c_write(0xD0); //gui dia chi cua slave
i2c_write(0x00); //thiet lap lai con tro - set register pointer
i2c_stop(); //phat bit stop I2C
I2C_start(); //phat bit start I2C
I2C_write(0xD1); //gui lenh doc du lieu
sec = i2c_read(1); //doc du lieu tai dia chi 0x00
min = i2c_read(1); //doc du lieu tai dia chi 0x01
hour = i2c_read(1); //doc du lieu tai dia chi 0x02
dow = i2c_read(1);
date = i2c_read(1);
month = i2c_read(1);
year = i2c_read(0);
i2c_stop(); //phat bit stop I2C
delay_us(100);
sec1=sec & 0x0F;
sec2=(sec & 0x70)>>4; //convert to BCD SEC
min1=min & 0x0F;
min2=(min & 0x70)>>4; //convert to BCD MIN
hour1=hour & 0x0F;
hour2=(hour & 0x30)>>4; //convert to BCD HOUR
day=(dow & 0x07);
date1=date & 0x0F;
date2=(date & 0x30)>>4; //convert to BCD DATE
month1=month & 0x0F;
month2=(month & 0x10)>>4;//convert to BCD MONTH
year1=year & 0x0F;
year2=(year & 0xF0)>>4; //convert to BCD YEAR
delay_us(10);
menu=0;
}
//////////////////////////////////////////////////////////////////////////////
void update_ds1307(void) {
int8 data=0; // luu tru du lieu tam thoi de gui vao ds1307
i2c_start();
i2c_write(0xd0);
i2c_write(0x00); // ghi du lieu bat dau tu vi tri 00
data=sec1+(sec2<<4);
data=data & 0b01111111;
i2c_write(data);
data=min1+(min2<<4);
i2c_write(data);
data=hour1+(hour2<<4);
i2c_write(data);
data=day;
i2c_write(data);
data=date1+(date2<<4);

i2c_write(data);
data=month1+(month2<<4);
i2c_write(data);
data=year1+(year2<<4);
i2c_write(data);
data=0x00;
i2c_write(data);
i2c_stop(); // ket thuc truyen du lieu
LCD_init();
menu=0;
}
//////////////////////CHUONG TRINH CAI DAT THOI GIAN//////////////////////////
void set_time(void) {
while (menu!=0)
{
if (menu==1) set_min();
if (menu==2) set_hour();
if (menu==3) set_day();
if (menu==4) set_date();
if (menu==5) set_month();
if (menu==6) set_year();
if (menu==7) update_ds1307();
}
}
/////////////////////////////////////////
void set_year(void) { // Cai dat nam
LCD_init();
while(menu==6) {
if (increase ==0) { // neu phim 2 duoc nhan
while (increase ==0);// khong lam gi cho phim nha
year1++;
if ((year2==9)&&(year1==10)) {year1=year2=0;}
if (year1==10) {year1=0;year2++;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease ==0);// khong lam gi cho phim nha
year1--;
if ((year2==0)&&(year1==255)) {year1=year2=9;}
if (year1==255) {year1=9;year2--;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," DATE SETING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Year : 20%u%u",year2,year1);
}
}
/////////////////////////////////////////
void set_month(void) { // Cai dat thang
LCD_init();
while(menu==5){
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0); // khong lam gi cho phim nha
month1++;
if ((month2==1) && (month1==3)) { month2=0;month1=1;}
if (month1==10) { month2++;month1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0); // cho phim nha
month1--;

if ((month2==0) && (month1==0)) {month2=1;month1=2; }


if (month1==0) {month2--;month1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," DATE SETING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Month : %u%u
",month2,month1);
}
}
/////////////////////////////////////////
void set_date(void) { // Cai dat ngay
LCD_init();
while(menu==4) {
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0); // khong lam gi cho phim nha
date1++;
if ((date2==3) && (date1==2)) { date2=0;date1=1;}
if (date1==10) { date2++;date1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0);// cho phim nha
date1--;
if ((date2==0) && (date1==0)) {date2=3;date1=1; }
if (date1==255) {date2--;date1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," DATE SETING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Date : %u%u
",date2,date1);
}
}
/////////////////////////////////////////
void set_day(void) { // cai dat thu'
LCD_init();
while(menu==3) {
if (increase==0) {
while (increase==0);
day++;
if (day==8) { day=1;}
}
if (decrease==0) {
while (decrease==0);
day--;
if (day==255) {day=7;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," DATE SETING ");
if(day==1)
{
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Day : Chu Nhat");
}
else
{
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Day : Thu %u
",day);
}
}
}
/////////////////////////////////////////

void set_hour(void) { // Cai dat gio


LCD_init();
while (menu==2)
{
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0);// khong lam gi cho phim nha
hour1++;
if ((hour2==2) && (hour1==4)) { hour2=0;hour1=0;}
if (hour1==10) { hour2++;hour1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0); // cho phim nha
hour1--;
if ((hour2==0) && (hour1==255)) {hour2=2;hour1=3; }
if (hour1==255) {hour2--;hour1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," TIME SETTING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Hour : %u%u
",hour2,hour1);
}
}
/////////////////////////////////////////
void set_min(void) { // Cai dat phut
LCD_init();
while (menu==1){// cho phim nha
if (increase==0) { // neu phim 2 duoc nhan
while (increase==0); // khong lam gi cho phim nha
min1++;
if ((min2==5) && (min1==10)) { min2=0;min1=0;}
if (min1==10) { min2++;min1=0;}
}
if (decrease==0) { // neu phim 3 duoc nhan
while (decrease==0); // cho phim nha
min1--;
if ((min2==0) & (min1==255)) {min2=5;min1=9; }
if (min1==255) {min2--;min1=9;}
}
Lcd_Setposition(Line_1);
printf(Lcd_putchar," TIME SETTING ");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"Minute : %u%u
",min2,min1);
}
}
//////////////////////////CHUONG TRINH HIEN THI///////////////////////
void display()
{
if(day==1)
{
Lcd_Setposition(Line_1);
printf(Lcd_putchar,"Cnhat,%u%u/%u%u/20%u%u",date2,date1,month2,month1,year2,y
ear1);
}
else
{
Lcd_Setposition(Line_1);
printf(Lcd_putchar,"THU %u,%u%u/%u%u/20%u%u",day,date2,date1,month2,month1,ye
ar2,year1);

}
;
Lcd_Setposition(Line_2);
printf(Lcd_putchar," [%u%u:%u%u:%u%u]

",hour2,hour1,min2,min1,sec2,sec1);

}
/////////////////////////////////////////////////////////////////////
void hello()
{
lcd_init();
Lcd_Setposition(Line_1);
printf(Lcd_putchar,"
DH CNTN
");
Lcd_Setposition(Line_2);
printf(Lcd_putchar,"SV: NGUYEN VAN A");
delay_ms(500);
Lcd_Setposition(Line_1);
printf(Lcd_putchar,"
DONG HO
");
Lcd_Setposition(Line_2);
printf(Lcd_putchar," THOI GIAN THUC ");
delay_ms(500);
lcd_init();
}

You might also like