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

#include

"msp430g2152.h"
#define
LCM_DIR
P1DIR
#define
LCM_OUT
P1OUT
#define
LCM_PIN_RS
BIT0
// P1.0
#define
LCM_PIN_EN
BIT1
// P1.1
#define
LCM_PIN_D7
BIT7
// P1.7
#define
LCM_PIN_D6
BIT6
// P1.6
#define
LCM_PIN_D5
BIT5
// P1.5
#define
LCM_PIN_D4
BIT4
// P1.4
#define
LCM_PIN_MASK ((LCM_PIN_RS | LCM_PIN_EN | LCM_PIN_D7 | LCM_PIN_D6 |L
CM_PIN_D5 | LCM_PIN_D4))
#define
FALSE
0
#define
TRUE
1
unsigned char hour=8,min=0,sec=0,i=0,k=0;
unsigned char day=1,month=1,year=12,daymax=0;
unsigned int ngay1,ngay2;
void kiemtra(void);
void PulseLcm()
{
LCM_OUT &= ~LCM_PIN_EN;
__delay_cycles(200);
LCM_OUT |= LCM_PIN_EN;
__delay_cycles(200);
LCM_OUT &= (~LCM_PIN_EN);
__delay_cycles(200);
}
//gui byte
void lcd_sendbyte(char ByteToSend, int IsData)
{
LCM_OUT &= (~LCM_PIN_MASK);
LCM_OUT |= (ByteToSend & 0xF0);
if (IsData == TRUE)
LCM_OUT |= LCM_PIN_RS;
else
LCM_OUT &= ~LCM_PIN_RS;
PulseLcm();
LCM_OUT &= (~LCM_PIN_MASK);
LCM_OUT |= ((ByteToSend & 0x0F) << 4);
if (IsData == TRUE)
LCM_OUT |= LCM_PIN_RS;
else
LCM_OUT &= ~LCM_PIN_RS;
PulseLcm();
}
//Di chuyen con tro toi vi tri (row ; col)
void lcd_gotoxy(char Row, char Col)
{
char address;
if (Row == 0)
address = 0;
else
address = 0x40;
address |= Col;
lcd_sendbyte(0x80 | address, FALSE);
}
//xoa lcd
void lcd_clear()
{
lcd_sendbyte(0x01, FALSE);
lcd_sendbyte(0x02, FALSE);

}
//Khoi tao LCD
void lcd_init(void)
{
LCM_DIR |= LCM_PIN_MASK;
LCM_OUT &= ~(LCM_PIN_MASK);
__delay_cycles(1000);
LCM_OUT &= ~LCM_PIN_RS;
LCM_OUT &= ~LCM_PIN_EN;
LCM_OUT = 0x20;
PulseLcm();
lcd_sendbyte(0x28, FALSE);
lcd_sendbyte(0x0E, FALSE);
lcd_sendbyte(0x06, FALSE);
}
//In 1 chuoi ky tu ra man hinh LCD
void lcd_putsf(char *Text)
{
char *c;
c = Text;
lcd_sendbyte(0x0C, FALSE );
while ((c != 0) && (*c != 0))
{
lcd_sendbyte(*c, TRUE);
c++;
}
}
//In 1 ky tu ra man hinh
void lcd_putchar(unsigned char Text)
{
unsigned char c;
c = Text;
lcd_sendbyte(0x0C,FALSE);
lcd_sendbyte(c,TRUE);
}
// cac ham tren viet cho LCD, phan nay tham khao cua nguoi khac
//thu trong tuan
void thu(void)
{
switch (k)
{
case 0: {lcd_putsf("
case 1: {lcd_putsf("
case 2: {lcd_putsf("
case 3: {lcd_putsf("
case 4: {lcd_putsf("
case 5: {lcd_putsf("
case 6: {lcd_putsf("
}
}
//----------------int thang2()
{
if(year%4==0)
return 29;
else
return 28;
}
void capnhatthu(void)

T2");
T3");
T4");
T5");
T6");
T7");
CN");

break;}
break;}
break;}
break;}
break;}
break;}
break;}

{
switch(month)
{
case 1: {ngay2=0;break;}
case 2: {ngay2=31;break;}
case 3: {ngay2=31+thang2();break;}
case 4: {ngay2=31*2+thang2();break;}
case 5: {ngay2=31*2+thang2()+30;break;}
case 6: {ngay2=31*3+thang2()+30;break;}
case 7: {ngay2=31*3+thang2()+30*2;break;}
case 8: {ngay2=31*4+thang2()+30*2;break;}
case 9: {ngay2=31*5+thang2()+30*2;break;}
case 10: {ngay2=31*5+thang2()+30*3;break;}
case 11: {ngay2=31*6+thang2()+30*3;break;}
case 12: {ngay2=31*6+thang2()+30*4;break;}
}
if(year%4==0)
ngay1=day+4+ngay2+(year-(year/4))*365+((year/4))*366;
else
ngay1=day+4+ngay2+(year-1-(year/4))*365+(year/4+1)*366;
k=ngay1%7;
}
//hien thi
void hienthi(void)
{
lcd_gotoxy(0,0);
lcd_putsf("TIME:");
lcd_putchar(hour/10+48);
lcd_putchar(hour%10+48);
lcd_putchar(58);
lcd_putchar(min/10+48);
lcd_putchar(min%10+48);
lcd_putchar(58);
lcd_putchar(sec/10+48);
lcd_putchar(sec%10+48);
thu();
lcd_gotoxy(1,0);
lcd_putsf("DATE:");
lcd_putchar(day/10+48);
lcd_putchar(day%10+48);
lcd_putchar(45);
lcd_putchar(month/10+48);
lcd_putchar(month%10+48);
lcd_putsf("-20");
lcd_putchar(year/10+48);
lcd_putchar(year%10+48);
}
//phim nhan
void nutnhan(void)
{
if(!(P2IN&BIT3))
{ __delay_cycles(20000);
i++; if(i>5) i=0;}
switch(i)
{
case 0:
break;
case 1:
{
lcd_sendbyte(0x85,FALSE);

lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
__delay_cycles(110000);
if(!(P2IN&BIT4)){ hour++;if(hour>23) hour=0; }
if(!(P2IN&BIT5)){if(hour>0) hour--; else hour=23; }
break;
}
case 2:
{
lcd_sendbyte(0x88,FALSE);
lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
__delay_cycles(110000);
if(!(P2IN&BIT4)){min++;if(min>59) min=0;}
if(!(P2IN&BIT5)){if(min>0) min--;else min=59;}
break;
}
case 3:
{
lcd_sendbyte(0xC5,FALSE);
lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
__delay_cycles(110000);
if(!(P2IN&BIT4))
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==1
0)||(month==12))
{day++;if(day>31) day=1;}
else if((month==4)||(month==6)||(month==9)||(month==11))
{day++;if(day>30) day=1;}
else if((month==2)&&((year%4)==0))
{day++;if(day>29) day=1;}
else
{day++;if(day>28) day=1;}
}
if(!(P2IN&BIT5))
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==
10)||(month==12))
{day--;if(day<1) day=31;}
else if((month==4)||(month==6)||(month==9)||(month==11))
{day--;if(day<1) day=30;}
else if((month==2)&&((year%4)==0))
{day--;if(day<1) day=29;}
else
{day--;if(day<1) day=28;}
}
break;
}
case 4:
{
lcd_sendbyte(0xC8,FALSE);
lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
__delay_cycles(110000);
if(!(P2IN&BIT4))
{
month++;if(month>12) month=1;}
if(!(P2IN&BIT5))
{

if(month>1) month--;else month=12;


}
kiemtra();
break;
}
case 5:
{
lcd_sendbyte(0xCB,FALSE);
lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
lcd_sendbyte(32,TRUE);
__delay_cycles(110000);
if(!(P2IN&BIT4)){year++;if(year>99) year=0;}
if(!(P2IN&BIT5)){if(year>0) year--;else year=99; }
break;
}
}
}
//kiem tra ngay cua thang 2 khi dieu chinh thoi gian
void kiemtra(void)
{
if(month==2)
{
if(year%4==0)
{ if(day>30) day=29;}
else if(day>30) day=28;
}
}
//cap nhat thoi gian
void update(void)
{
capnhatthu();
sec+=1;
if(sec==60)
{
sec=0;min+=1;
if(min==60)
{
min=0;hour+=1;
if(hour==24)
{
hour=0;day+=1;
if(month==2&&(year%4)==0)
daymax=29;
else if(month==2)
daymax=28;
else if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||
(month==10)||(month==12))
daymax=31;
else
daymax=30;
if(day>daymax)
{
day=1;
month+=1;
if(month==13)
{
month=1;year+=1;
if(year==99)
year=0;

}
}
}
}
}
}
//kiem tra bao gio
void baogio(void)
{
if(k!=6) //khong phai ngay chu nhat
if(((hour==7)&&(min==0))||
(((hour==8)||(hour==9))&&((min==40)||(min==50)))||
((hour==11)&&(min==30))||
((hour==13)&&(min==30))||
((hour==15)&&((min==10)||(min==20)))||
((hour==17)&&(min==0)))
{
if(sec<3)
P2OUT=0x01;
else P2OUT=0;
}
else P2OUT=0;
}
//chuong trinh chinh
void main(void)
{
P2DIR=0x07;
WDTCTL = WDTPW + WDTHOLD;
TACCR0=32768;
TACTL=TASSEL_1+MC_1;
lcd_init();
while(1)
{
while (!(TACTL&TAIFG))
{
hienthi();
nutnhan();
}
TACTL&=~TAIFG;
update();
baogio();
lcd_clear();
}
}

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