Вы находитесь на странице: 1из 14

#include <htc.

h>
#include <stdio.h>
#include "LCD.c"
//khai bao cho pic
__CONFIG(INTIO & WDTDIS & PWRTEN &
MCLREN & UNPROTECT & DUNPROTECT &
BORDIS & IESODIS & FCMDIS & LVPDIS);
/////////////////////////
#define DS1307_SCL RC3
#define DS1307_SDA RC4
char Buf[16], RxWR =0, RxRD =0, check, a, b, c;
char dem=0,tanggiam,chon=0;
unsigned char sec, minute, hour, day, date, month, year, setminute, sethour,cdva
n;
unsigned char sec1, minute1, hour1, day1, date1, month1, year1, setminute1, seth
our1, cdvan1;
unsigned char sec2, minute2, hour2, day2, date2, month2, year2, setminute2, seth
our2, cdvan2;
unsigned char dday,dhour,setdhour,h,m,sh,sm;
unsigned char uart_h[8];
int n=0, ng, tr, ch, dv, e, f,kq;
char set;
unsigned char I2CData[11];
unsigned char I2CInit_value[11]= {0x50, 0x59, 0x49, 0x05, 0x27, 0x02,0x14, 0x00,
00, 10, 02};
unsigned char i;
char ddate[10];
char time[11];
char settime[8];
void I2CInit(void);
void I2CStart();
void I2CRestart();
void I2CStop();
void I2CAck();
void I2CNak();
void I2CWait();
void I2CWrite(unsigned char dat);
unsigned char I2CRead(void);
//////DS
void init_DS1307();
void write_DS1307(unsigned char address, unsigned char data);
void read_DS1307();
int Binary2BCD(int a);
int BCD2Binary(int a);
void DS1307() ;
unsigned char BCD2UpperCh(unsigned char bcd);
unsigned char BCD2LowerCh(unsigned char bcd);
unsigned char nutmode(void);
unsigned char nutup(void);
unsigned char nutdown(void);
unsigned char nutset(void);
void hienthi_time();
void hienthi_settime();
void hienthi_ngay();
void nutnhan();
void kichvan(void);
void capnhat(void);

void uart_tx( char dulieu);


void interrupt uart_isr();
////////////I2c
void I2CInit(void)
{
TRISC3 = 1;
/*
TRISC4 = 1;
/*
SSPSTAT |= 0x80; /*
SSPCON = 0x28; /*
D + 1)) */
SSPADD = 0x28; /*
}

SDA and SCL as input pin */


these pins can be configured either i/p or o/p */
Slew rate disabled */
SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPAD
100Khz @ 4Mhz Fosc */

////////////////////////////////////
void I2CStart()
{
SEN = 1;
/* Start condition enabled */
while(SEN);
/* automatically cleared by hardware */
/* wait for start condition to finish */
}
//////////////////////////
void I2CRestart()
{
RSEN = 1;
/* Repeated start enabled */
while(RSEN);
/* wait for condition to finish */
}
///////////////////////////////
void I2CStop()
{
PEN = 1;
/* Stop condition enabled */
while(PEN);
/* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
///////////////////
void I2CAck()
{
ACKDT = 0;
ACKEN = 1;
while(ACKEN);
}

/* Acknowledge data bit, 0 = ACK */


/* Ack data enabled */
/* wait for ack data to send on bus */

///////////////////////
void I2CNak()
{
ACKDT = 1;
/* Acknowledge data bit, 1 = NAK */
ACKEN = 1;
/* Ack data enabled */
while(ACKEN);
/* wait for ack data to send on bus */
}
///////////////////
void I2CWait()
{
while ((SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );
/* wait for any pending transfer */
}
//////////////////////

void I2CWrite(unsigned char dat)


{
SSPBUF = dat;
/* Move data to SSPBUF */
while(BF);
/* wait till complete data is sent from buffer */
I2CWait();
/* wait for any pending transfer */
}
///////////////////
unsigned char I2CRead(void)
{
unsigned char temp;
/* Reception works if transfer is initiated in read mode */
RCEN = 1;
/* Enable data reception */
while(!BF);
/* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
I2CWait();
/* wait to check any pending transfer */
return temp;
/* Return the read data from bus */
}
/////////////////ds
/////////////////////////////
void read_DS1307()
{
//
unsigned short data;
I2CStart();
I2CWrite(0xd0);
I2CWrite(0x00);/* Send a repeated start, after a dummy write to start readin
g */
I2CRestart();
I2CWrite(0xd1);
for (i = 11; i > 0; i--)
{
/* read a byte */
I2CData[i] = I2CRead();
/* ACK if its not the last byte to read */
/* if its the last byte then send a NAK */
if (i - 1)
I2CAck();
else
I2CNak();
}
I2CStop();
}
/////////////////////
int Binary2BCD(int a)
{
int t1, t2;
t1 = a%10;
t2 = a/10;
t2 = (t2 <<4 );
t2 = t2 | t1;
return t2;
}
////////////////////
int BCD2Binary(int a)
{
int r,t;
t = a >> 4;
r = t*10 ;

r = r + (a & 0x0f);
return r;
}
///////////////////////
void init_DS1307()
{
DS1307_SCL = 0;
DS1307_SDA = 0;
DS1307();
I2CStart();
I2CWrite(0xd0);
I2CWrite(0x00);
/* Loop to write 8 bytes */
for (i = 0; i < 11; i++)
{
/* send I2C data one by one */
I2CWrite(I2CInit_value[i]);
}
I2CStop();
}
///////////////////////////////////
void write_DS1307(unsigned char address, unsigned char data )
{
DS1307_SCL = 0;
DS1307_SDA = 0;
//
DS1307();
I2CStart();
I2CWrite(0xd0);
I2CWrite(address);
/* send I2C data one by one */
I2CWrite(data);
I2CStop();
}
//////////////////////////////////////
void DS1307(void)
{
sec
= I2CData[11]; // read second
minute = I2CData[10]; // read minute
hour = I2CData[9]; // read hour
day
= I2CData[8]; // read day
date = I2CData[7]; // read date
month = I2CData[6]; // read month
year = I2CData[5]; // read year
setminute
= I2CData[3]; // read setminute
sethour = I2CData[2]; // read sethour
cdvan = I2CData[1];
}
///////////////////////
//chuyen doi 4 bit cao cua BCD sang ma ASCII
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}

//'0' l 0x30 m ASCII

/////////////////////////
unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
////////////////////////
unsigned char nutmode(void)
{
if (RD1==0)
{
delay_ms(500);
//while(RD1==0);
if(RD1)
return 1;
}
else
return 0;// if key is not pressed, return 0
}
unsigned char nutup(void)
{
if (RD2==0)
{
delay_ms(500);
//while(RD2==0);
if(RD2)
return 1;
}
else
return 0;// if key is not pressed, return 0
}
unsigned char nutdown(void)
{
if (RD3==0)
{
delay_ms(500);
//while(RD3==0);
if(RD3)
return 1;
}
else
return 0;// if key is not pressed, return 0
}
unsigned char nutset(void)
{
if (RD4==0)
{
delay_ms(500);
//while(RD4==0);
if(RD4)
//nha phim
return 1;
}
else
return 0;// if key is not pressed, return 0
}
//////////////////

void hienthi_time()
{
read_DS1307();
DS1307();
dday = BCD2LowerCh(day);
LCD_cmd(0x80);
if (dday == '1') {LCD_string("Sun.");}
else if (dday == '2') { LCD_string("Mon.");}
else if (dday == '3') { LCD_string("Tue.");}
else if (dday == '4') { LCD_string("Wed.");}
else if (dday == '5') { LCD_string("Thu.");}
else if (dday == '6') { LCD_string("Fri.");}
else if (dday == '7') { LCD_string("Sat.");}
dhour = BCD2UpperCh(hour);
if (dhour == '4')
{
time[0] = ' '; LCD_cmd(0x84); LCD_string( " ");
time[9] = 'A'; LCD_cmd(0x8D); LCD_string( "A");
}
else if (dhour == '5')
{
time[0] = '1'; LCD_cmd(0x84); LCD_string( "1");
time[9] = 'A'; LCD_cmd(0x8D); LCD_string( "A");
}
else if (dhour == '6')
{
time[0] = ' '; LCD_cmd(0x84); LCD_string( " ");
time[9] = 'P'; LCD_cmd(0x8D); LCD_string( "P");
}
else if (dhour == '7')
{
time[0] = '1'; LCD_cmd(0x84); LCD_string( "1");
time[9] = 'P'; LCD_cmd(0x8D); LCD_string( "P");
}
///////////////////
time[1] = BCD2LowerCh(hour);LCD_cmd(0x85); LCD_data( (hour & 0x0F) + 48);
time[2] = ':'; LCD_cmd(0x86); LCD_string(":");
time[3] = BCD2UpperCh(minute);LCD_cmd(0x87); LCD_data( (minute>>4) + 48);
time[4] = BCD2LowerCh(minute);LCD_cmd(0x88); LCD_data( (minute & 0x0f) + 48);
time[5] = ':';LCD_cmd(0x89); LCD_string( ":");
time[6] = BCD2UpperCh(sec);LCD_cmd(0x8A); LCD_data( (sec>>4) + 48);
time[7] = BCD2LowerCh(sec);LCD_cmd(0x8B); LCD_data( (sec &0x0f) + 48);
time[8] = '.';LCD_cmd(0x8C); LCD_string( ".");
time[10] = 'M';LCD_cmd(0x8E); LCD_string( "M");
}
////////////////////////////////////
void hienthi_settime()
{
read_DS1307();
DS1307();
LCD_cmd(0xC0); LCD_string("settime");
setdhour = BCD2UpperCh(sethour);
if (setdhour == '4')
{
settime[0] = ' '; LCD_cmd(0xC7); LCD_string( " ");
settime[6] = 'A';LCD_cmd(0xCD); LCD_string( "A");
}

else if (setdhour ==
{
settime[0] =
settime[6] =
}
else if (setdhour ==
{
settime[0] =
settime[6] =
}
else if (setdhour ==
{
settime[0] =
settime[6] =
}

'5')
'1'; LCD_cmd(0xC7); LCD_string( "1");
'A';LCD_cmd(0xCD); LCD_string( "A");
'6')
' '; LCD_cmd(0xC7); LCD_string( " ");
'P';LCD_cmd(0xCD); LCD_string( "P");
'7')
'1';LCD_cmd(0xC7);
'P';LCD_cmd(0xCD);

LCD_string( "1");
LCD_string( "P");

settime[1] = BCD2LowerCh(sethour);LCD_cmd(0xC8);LCD_data((sethour & 0x0f)+48);


settime[3] = BCD2UpperCh(setminute);;LCD_cmd(0xCA);LCD_data((setminute >> 4)+48)
;
settime[4] = BCD2LowerCh(setminute);;LCD_cmd(0xCB);LCD_data((setminute & 0x0f)+4
8);
settime[2] = ':';LCD_cmd(0xC9);
settime[5] = '.';LCD_cmd(0xCC);
settime[7] = '.';LCD_cmd(0xCE);
}

LCD_string( ":");
LCD_string( ".");
LCD_string( "M");

//////////////////////////////////////
void hienthi_ngay()
{
read_DS1307();
DS1307();
LCD_cmd(0xC0);
LCD_string("date:");
ddate[0] = BCD2UpperCh(date);LCD_cmd(0xC5);LCD_data((date>>4) +48);
ddate[1] = BCD2LowerCh(date);LCD_cmd(0xC6);LCD_data( (date & 0x0F) + 48
);
ddate[2] = '/';LCD_cmd(0xC7);LCD_string("/");
ddate[3] = BCD2UpperCh(month);LCD_cmd(0xC8);LCD_data((month>>4) +48);
ddate[4] = BCD2LowerCh(month);LCD_cmd(0xC9);LCD_data((month & 0x0f) +48)
;
ddate[5]
ddate[6]
ddate[7]
ddate[8]
ddate[9]

=
=
=
=
=

'/';LCD_cmd(0xCA);LCD_string("/");
'2';LCD_cmd(0xCB);LCD_string("2");
'0';LCD_cmd(0xCC);LCD_string("0");
BCD2UpperCh(year);LCD_cmd(0xCD);LCD_data((year>>4) +48);
BCD2LowerCh(year);LCD_cmd(0xCE);LCD_data((year & 0x0f) +48);

}
////////////////////////////////////
void nutnhan()
{
if (nutmode()==1)
{
dem++;
while(dem!=0)
{

read_DS1307();
DS1307();
switch(dem)
{
case 1:
hienthi_time();
LCD_cmd(0xC0);
LCD_string("dc gio :");
break;
case 2:
hienthi_time();
LCD_cmd(0xC0);
LCD_string("dc phut:");
break;
case 3:
hienthi_time();
LCD_cmd(0xC0);;
LCD_string("cd gio :");
sethour1=(sethour/10)+0x30;LCD_data(sethour1);
sethour2=(sethour%10)+0x30;LCD_data(seth
our2);
break;
case 4:
hienthi_time();
LCD_cmd(0xC0);
LCD_string("cd phut:");
setminute1=(setminute/10)+0x30;LCD_data(
setminute1);
setminute2=(setminute%10)+0x30;LCD_data(
setminute2);
break;
case 5:
hienthi_time();
LCD_cmd(0xC0);
LCD_string("cd van:");
cdvan1=(cdvan/10)+0x30;LCD_data(cdvan1);
cdvan2=(cdvan%10)+0x30;LCD_data(cdvan2);
LCD_string(" ");
break;
}
if (nutmode()==1)
{
dem++;
if(dem > 5)
{
dem = 0;
hienthi_time();
LCD_cmd(0xC0);
LCD_string("
}
}
if(nutup()==1)
{
switch(dem)
{
case 1:

");

hour = hour & 0x1f;


hour = BCD2Binary(hour);
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
hour = hour + 1;
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
if(hour > 12 )
hour = 1;
hour = Binary2BCD(hour);
hour = hour | 0x40;
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
write_DS1307(0x02, hour);
break;
case 2:
minute = BCD2Binary(minute);
minute1=(minute/10)+0x30;
minute2=(minute%10)+0x30;
minute=minute+1;
minute1=(minute/10)+0x30;
minute2=(minute%10)+0x30;
if(minute > 59)
minute = 0;
minute = Binary2BCD(minute);
minute1=(minute&0xf0)>>4;
minute2=(minute&0x0f);
write_DS1307(0x01, minute);
break;
case 3:
sethour1=(sethour/10)+0x30;
sethour2=(sethour%10)+0x30;
sethour = sethour + 1;
sethour1=(sethour/10)+0x30;
sethour2=(sethour%10)+0x30;
if(sethour > 12 )
sethour = 1;
write_DS1307(0x09, sethour);
//
a= sethour;
break;
case 4:
setminute1=(setminute/10)+0x30;
setminute2=(setminute%10)+0x30;
setminute=setminute+1;
setminute1=(setminute/10)+0x30;
setminute2=(setminute%10)+0x30;
if(setminute > 59)
setminute = 0;
setminute1=(setminute&0xf0)>>4;
setminute2=(setminute&0x0f);
write_DS1307(0x08, setminute);
break;
case 5:
cdvan1=(cdvan/10)+0x30;
cdvan2=(cdvan%10)+0x30;
cdvan=cdvan+1;
cdvan1=(cdvan/10)+0x30;
cdvan2=(cdvan%10)+0x30;

if(cdvan > 59)


cdvan = 0;
cdvan1=(cdvan & 0xf0)>>4;
cdvan2=(cdvan & 0x0f);
write_DS1307(0x0A, cdvan);
break;
}
}
////////////////////
if(nutdown()==1)
{
switch(dem)
{
hienthi_time();
case 1:
hour = hour & 0x1f;
hour = BCD2Binary(hour);
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
hour = hour - 1;
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
if(hour == 0x00)
hour = 12;
hour = Binary2BCD(hour);
hour = hour | 0x40;
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
write_DS1307(0x02, hour);
break;
case 2:
minute = BCD2Binary(minute);
minute1=(minute/10)+0x30;
minute2=(minute%10)+0x30;
minute=minute-1;
minute1=(minute/10)+0x30;
minute2=(minute%10)+0x30;
if(minute == 0xff)
minute = 59;
minute = Binary2BCD(minute);
minute1=(minute&0xf0)>>4;
minute2=(minute&0x0f);
write_DS1307(0x01, minute);
break;
case 3:
sethour1=(sethour/10)+0x30;
sethour2=(sethour%10)+0x30;
sethour = sethour - 1;
sethour1=(sethour/10)+0x30;
sethour2=(sethour%10)+0x30;
if(sethour ==0x00 )
sethour = 12;
write_DS1307(0x09, sethour);
break;
case 4:
setminute1=(setminute/10)+0x30;
setminute2=(setminute%10)+0x30;

setminute=setminute - 1;
setminute1=(setminute/10)+0x30;
setminute2=(setminute%10)+0x30;
if(setminute == 0xff)
setminute = 59;
write_DS1307(0x08, setminute);
break;
case 5:
cdvan1=(cdvan/10)+0x30;
cdvan2=(cdvan%10)+0x30;
cdvan=cdvan-1;
cdvan1=(cdvan/10)+0x30;
cdvan2=(cdvan%10)+0x30;
if(cdvan == 0xff)
cdvan = 59;
write_DS1307(0x0A, cdvan);
break;
}
}
}
if (nutset()==1)
{
chon++;
if(chon > 2)
chon = 1;
switch(chon)
{
case 1:
hienthi_settime();
break;
case 2:
hienthi_ngay();
break;
}
}
}
}
////////////////////////////////////
void kichvan(void)
{
//
init_DS1307();
read_DS1307();
//lay du lieu tu DS1307 luu vao I2Cdata[
i]
DS1307();
//gan cac gia tri gio, phut, gia
y tu I2Cdata[i] vao cac bien hour, minute,sec
hour=hour & 0x1F;
hour = BCD2Binary(hour);
hour1=(hour/10)+0x30;
hour2=(hour%10)+0x30;
sethour=sethour & 0x1F;
sethour = BCD2Binary(sethour);
sethour1=(sethour/10)+0x30;
sethour2=(sethour%10)+0x30;
minute = BCD2Binary(minute);
minute1=(minute/10)+0x30;

minute2=(minute%10)+0x30;
setminute = BCD2Binary(setminute);
setminute1=(setminute/10)+0x30;
setminute2=(setminute%10)+0x30;
if((hour == sethour) && (minute == setminute))
RE0=1;
if(cdvan < (minute - setminute))
RE0=0;
}
void capnhat(void)
{
unsigned char chucgio, dvgio, gio, chucphut, dvphut, phut;
unsigned char setchucgio, setdvgio, setgio;
unsigned char setchucphut, setdvphut, setphut;
unsigned char cdchucvan,cddvvan, cdvan;
if(uart_h[0] =='@')
{
char x;
uart_h[0] = x;
chucgio=(uart_h[1])-0x30;
chucgio <<= 4;
dvgio=(uart_h[2])-0x30;
if(uart_h[7] =='$')
gio = ((chucgio| dvgio )& 0x1F)|0x40;
if(uart_h[7] =='%')
gio = ((chucgio| dvgio )& 0x1F)|0x60;
write_DS1307(0x02, gio);
chucphut=(uart_h[3])-0x30;
chucphut <<=4;
dvphut=(uart_h[4])-0x30;
phut=chucphut | dvphut;
write_DS1307(0x01, phut);
}
if(uart_h[0] =='#')
{
//thanh ghi cai dat hen mo van la thanh ghi chua gia tri nhi phan => khong dich
4 lan,
//ma se la nhan 10 va cong don vi
LCD_cmd(0xC0);LCD_string("SETUP ");
setchucgio=(uart_h[1])-0x30;
setdvgio=(uart_h[2])-0x30;
setgio = (setchucgio *10)+ setdvgio;
setchucgio= (setgio/10)+0x30;LCD_data(setchucgio);
setdvgio= (setgio%10)+0x30;LCD_data(setdvgio);
write_DS1307(0x09, setgio);
LCD_cmd(0xC8);LCD_string(":");
setchucphut=(uart_h[3])-0x30;
setdvphut=(uart_h[4])-0x30;
setphut = (setchucphut *10)+ setdvphut;
setchucphut= (setphut/10)+0x30;LCD_data(setchucphut);
setdvphut= (setphut%10)+0x30;LCD_data(setdvphut);

write_DS1307(0x08, setphut);
//

LCD_data(uart_h[5]);

//ma asc

LCD_cmd(0xCB);LCD_string(" -");
cdchucvan=(uart_h[5])-0x30;

//gia tr

ii
i nhi phan
cddvvan=(uart_h[6])-0x30;
cdvan =( cdchucvan *10) + cddvvan;
cdchucvan= (cdvan/10)+0x30;LCD_data(cdchucvan);
cddvvan= (cdvan%10)+0x30;LCD_data(cddvvan);
write_DS1307(0x0A, cdvan);
}
}
////////////////////////////////
void main()
{
/////////KHAI BAO CHO UART
char c;
TXSTA = 0x20;
// kich hoat chan truyen
RCSTA = 0x90; // kich cong com - nhan du lieu
BRG16 = 1;
SPBRGH = 0;
SPBRG = 25;
c= RCREG;
RCIE =1;
PEIE = 1;
GIE = 1;
ANSEL = ANSELH =0;
////////khai bao ADC
ANS1 = 1;
VCFG0 = 0;
VCFG1 = 0;
ADCS0 = 1;
ADCS1 = 1;
ADFM = 1;
ADON =1;
CHS1 = CHS2 =CHS3 = 0;
CHS0= 1;//AN1
TRISE = 0X00;
//////////////lcd
TRISA=0xFF;
TRISB=0;
// SU DUNG CHO LCD
TRISD = 0XFF;
TRISC = 0X00;
LCD_init();
LCD_cmd(0x80);
LCD_string("DO AN B");
delay_ms(1000);
LCD_cmd(0x01);
///////i2c
I2CInit();
init_DS1307();
while (1)
{
nutnhan();
hienthi_time();

capnhat();
kichvan();
GODONE = 1;
while (GODONE);
e = ADRESH;
f = ADRESL;
e <<= 8;
kq = e + f;
ng = kq / 1000 + 0x30;
tr =((kq % 1000) / 100) + 0x30;
ch = (((kq % 1000) %100) /10) + 0x30;
dv = (((kq % 1000) % 100) % 10) + 0x30;
uart_tx(ng);
uart_tx(tr);
uart_tx(ch);
uart_tx(dv);
if ( kq >=1000)
RE1 = 1;
else
RE1 = 0 ;
delay_ms(100);
}
}
///////////////////////
void uart_tx( char dulieu)
{
TXREG = dulieu;
while (TXIF == 0);
}
/////////////////////////
void interrupt uart_isr()
{
if (RCIE && RCIF)
uart_h[n] = RCREG;
n++;
if (n>7)
n =0;
}
///////////////////////

Вам также может понравиться