cho e hỏi bị lỗi này thì làm sao ạ, demo limit code thì đúng nhưng khi dịch dùng mikroc 8.0 lại báo lỗi đó e fai làm sao.
đây là code của e. mọi người giúp e với
/* KHAI BAO BIEN ************************************************** ************/
unsigned short second, minute, hour, day, date, month, year;
unsigned short minute_al, hour_al, type_al, count_al;
unsigned short flag_time, delay10s_1 = 0, delay10s_2 = 0;
unsigned short mode;
char *day_txt;
/* KHOI TAO ************************************************** *****************/
void INIT_MAIN() {
/* Khoi tao cong A -----------------------------------------------------------*/
TRISA = 0xFF; // Dinh nghia cong A la Digital Input
PORTA = 0x00;
/* Khoi tao cong B -----------------------------------------------------------*/
TRISB = 0xFF; // Dinh nghia cong B la Digital Input
PORTB = 0x00;
/* Khoi tao cong C -----------------------------------------------------------*/
TRISC = 0x00;
PORTC = 0x00;
/* Khoi tao cong D -----------------------------------------------------------*/
TRISD = 0x00; // Dinh nghia cong D la Digital Output
PORTD = 0x00;
/* Khoi tao cong E -----------------------------------------------------------*/
ADCON1 = 0x0E; // Dinh nghia cong E la Digital + su dung A/D channel AN0
TRISE = 0x00; // Dinh nghia cong E la Output
PORTE = 0x00; // Xoa du lieu cu tren cong E
/* Khoi tao LCD --------------------------------------------------------------*/
Lcd_Config(&PORTD,4,7,6,3,2,1,0);
Lcd_Init(&PORTD);
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);
/* Khoi tao truyen thong I2C -------------------------------------------------*/
I2C_Init(100000);
/* Khoi tao DS1307 -----------------------------------------------------------*/
/* I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00); // Khoi tao cac gia tri ban dau cho cac thanh ghi thoi gian
I2C_Wr(0x13); // chi lam 1 lan.
I2C_Wr(0x03);
I2C_Wr(0x02);
I2C_Wr(0x03);
I2C_Wr(0x05);
I2C_Wr(0x03);
I2C_Wr(0x03);
I2C_Wr(0x90);
I2C_Stop(); */
/* Khoi tao ngat ngoai va Timer0 ---------------------------------------------*/
INTCON = 0x90; // Mo ngat toan cuc, mo ngat ngoai, xoa co ngat ngoai, xoa co ngat Timer0
OPTION_REG = 0x87; // Chon suon len cho ngat ngoai va Timer0, bo chia ti le 1:256 cho Timer0
TMR0 = 0x00;
/* Khoi tao cac bien luu trong EEPROM*/
hour_al = Eeprom_Read(0x00);
minute_al = Eeprom_Read(0x01);
type_al = Eeprom_Read(0x02);
}
/* CHUONG TRINH CON ************************************************** *********/
/* Doc du lieu RTC tu DS1307 -------------------------------------------------*/
void READ_RTC() {
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00);
I2C_Repeated_Start();
I2C_Wr(0xD1);
second = I2C_Rd(1u);
minute = I2C_Rd(1u);
hour = I2C_Rd(1u);
day = I2C_Rd(1u);
date = I2C_Rd(1u);
month = I2C_Rd(1u);
year = I2C_Rd(1u);
I2C_Stop();
}
/* Xu ly du lieu Date RTC ----------------------------------------------------*/
void TRANSFORM_RTC() {
switch (day) {
case 1: day_txt = "ERO"; break;
case 2: day_txt = "T.2"; break;
case 3: day_txt = "T.3"; break;
case 4: day_txt = "T.4"; break;
case 5: day_txt = "T.5"; break;
case 6: day_txt = "T.6"; break;
case 7: day_txt = "T.7"; break;
case 8: day_txt = "C.N"; break;
}
}
/* Xu ly du lieu RTC ---------------------------------------------------------*/
void BCD(unsigned short x, unsigned short y,unsigned short var) {
unsigned short var1, var2;
var1 = (var >> 4) + 0x30;
LCD_Chr(x,y,var1);
var2 = (var & 0x0F) + 0x30;
LCD_Chr_Cp(var2);
}
/* Hien thi ra LCD -----------------------------------------------------------*/
void LCD_DISPLAY() {
LCD_Out(1,1,day_txt);
BCD(1,7,date);
LCD_Chr_Cp('/');
BCD(1,10,month);
LCD_Out_Cp("/20");
BCD(1,15,year);
if (type_al == 1) Lcd_Out(2, 1,"Chuong");
else Lcd_Out(2, 1," ");
BCD(2,9,hour);
LCD_Chr_Cp(':');
BCD(2,12,minute);
LCD_Chr_Cp(':');
BCD(2,15,second);
}
/* Cap nhat thoi gian --------------------------------------------------------*/
void UPDATE_TIME() {
READ_RTC();
TRANSFORM_RTC();
LCD_DISPLAY();
PORTE.F0 = ~PORTE.F0;
}
/* Xu ly Du lieu Nhiet do tu LM335 -------------------------------------------*/
void TEMPERATURE() {
unsigned short temp1, temp2;
char degree[5];
temp1 = Adc_Read(0);
temp2 = (temp1 - 558)/2;
ByteToStr(temp2, degree);
LCD_Out_CP(degree);
LCD_Chr_Cp(223);
LCD_Chr_Cp('C');
}
/* XOA TIMER0 ----------------------------------------------------------------*/
void CLEAR_TIMER0() {
delay10s_1 = 0;
delay10s_2 = 0;
TMR0 = 0;
}
/* Tinh so ngay Julius -------------------------------------------------------*/
unsigned long jdFromDate(char dd, char mm, char yy) {
char a, m;
unsigned y;
unsigned long jd;
a = (14 - mm)/12;
y = (2000 + yy) + 4800 - a;
m = mm + 12*a - 3;
jd = dd + (153*m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045;
if (jd < 2299161) jd = dd + (153*m + 2)/5 + 365*y + y/4 - 32083;
return jd;
}
/* Tinh ngay soc -------------------------------------------------------------*/
char getNewMoonDay(unsigned long k) {
double T, T2, T3, dr, Jd1, M, Mpr, F, C1, deltat, JdNew;
T = k/1236.85;
T2 = T * T;
T3 = T2 * T;
dr = 3.1415926/180;
Jd1 = 2415020.75933 + 29.53058868*k + 0.0001178*T2 - 0.000000155*T3;
Jd1 = Jd1 + 0.00033*sin((166.56 + 132.87*T - 0.009173*T2)*dr);
M = 359.2242 + 29.10535608*k - 0.0000333*T2 - 0.00000347*T3;
Mpr = 306.0253 + 385.81691806*k + 0.0107306*T2 + 0.00001236*T3;
F = 21.2964 + 390.67050646*k - 0.0016528*T2 - 0.00000239*T3;
C1=(0.1734 - 0.000393*T)*sin(M*dr) + 0.0021*sin(2*dr*M);
C1 = C1 - 0.4068*sin(Mpr*dr) + 0.0161*sin(dr*2*Mpr);
C1 = C1 - 0.0004*sin(dr*3*Mpr);
C1 = C1 + 0.0104*sin(dr*2*F) - 0.0051*sin(dr*(M+Mpr));
C1 = C1 - 0.0074*sin(dr*(M-Mpr)) + 0.0004*sin(dr*(2*F+M));
C1 = C1 - 0.0004*sin(dr*(2*F-M)) - 0.0006*sin(dr*(2*F+Mpr));
C1 = C1 + 0.0010*sin(dr*(2*F-Mpr)) + 0.0005*sin(dr*(2*Mpr+M));
if (T < -11) deltat = 0.001 + 0.000839*T + 0.0002261*T2 - 0.00000845*T3 - 0.000000081*T*T3;
else deltat= -0.000278 + 0.000265*T + 0.000262*T2;
JdNew = Jd1 + C1 - deltat;
return (JdNew + 0.5 + 7/24);
}
/* Tinh toa do mat troi ------------------------------------------------------*/
char getSunLongitude(unsigned long jdn) {
double T, T2, dr, M, L0, DL, L;
T = (jdn - 2451545.5 - 7/24) / 36525;
T2 = T*T;
dr = 3.1415926/180;
M = 357.52910 + 35999.05030*T - 0.0001559*T2 - 0.00000048*T*T2;
L0 = 280.46645 + 36000.76983*T + 0.0003032*T2;
DL = (1.914600 - 0.004817*T - 0.000014*T2)*sin(dr*M);
DL = DL + (0.019993 - 0.000101*T)*sin(dr*2*M) + 0.000290*sin(dr*3*M);
L = L0 + DL;
L = L*dr;
L = L - 3.1415926*2*(L/(3.1415926*2));
return (L / 3.1415926 * 6);
}
/* Tim ngay bat dau thang 11 am lich -----------------------------------------*/
char getLunarMonth11(char yy) {
unsigned long k, off, nm, sunLong;
off = jdFromDate(31, 12, (2000 + yy)) - 2415021;
k = (off / 29.530588853);
nm = getNewMoonDay(k);
sunLong = getSunLongitude(nm);
if (sunLong >= 9) nm = getNewMoonDay(k-1);
return nm;
}
/* Xac dinh thang nhuan ------------------------------------------------------*/
char getLeapMonthOffset(unsigned long a11) {
unsigned long k, last, arc, i;
char tmp;
k = (a11 - 2415021.076998695) / 29.530588853 + 0.5;
last = 0;
i = 1;
tmp = getNewMoonDay(k+i);
arc = getSunLongitude(tmp);
do {
last = arc;
i++;
arc = getSunLongitude(tmp);
}
while (arc != last && i < 14);
return i-1;
}
/* Tinh am lich --------------------------------------------------------------*/
void AM_LICH() {
unsigned long k, dayNumber, monthStart, a11, b11, lunarDay, lunarMonth, lunarYear, lunarLeap, diff, leapMonthDiff;
char dd, mm, yy;
unsigned short ld, lm, ly;
dd = Bcd2Dec(date);
mm = Bcd2Dec(month);
yy = Bcd2Dec(year);
dayNumber = jdFromDate(dd, mm, (2000+yy));
k = (dayNumber - 2415021.076998695) / 29.530588853;
monthStart = getNewMoonDay(k+1);
if (monthStart > dayNumber) monthStart = getNewMoonDay(k);
a11 = getLunarMonth11(2000+yy);
b11 = a11;
if (a11 >= monthStart) {
lunarYear = 2000+yy;
a11 = getLunarMonth11(2000+yy-1);
}
else {
lunarYear = 2000+yy+1;
b11 = getLunarMonth11(2000+yy+1);
}
lunarDay = dayNumber-monthStart+1;
diff = (monthStart - a11)/29;
lunarLeap = 0;
lunarMonth = diff+11;
if (b11 - a11 > 365) {
leapMonthDiff = getLeapMonthOffset(a11);
if (diff >= leapMonthDiff) {
lunarMonth = diff + 10;
if (diff == leapMonthDiff) lunarLeap = 1;
}
}
if (lunarMonth > 12) lunarMonth = lunarMonth - 12;
if (lunarMonth >= 11 && diff < 4) lunarYear -= 1;
ld = Dec2Bcd (lunarDay);
lm = Dec2Bcd (lunarMonth);
BCD(1,10,ld);
Lcd_Chr_Cp(':');
BCD(1,13,lm);
}
/* MODE BUTTON ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~*/
/* Hien thi lich am ----------------------------------------------------------*/
void MODE_1() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 1, "AM LICH: SORRY");
Lcd_Out(2, 1, "Next Update");
// AM_LICH();
mode = 1;
Mode_1l:
if (PORTB.F1 == 0) goto Mode_1l;
}
/* Hien thi nhiet do moi truong ----------------------------------------------*/
void MODE_2() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 1, "NHIET DO:");
TEMPERATURE();
mode = 2;
Mode_2l:
if (PORTB.F1 == 0) goto Mode_2l;
}
/* Hien thi cai dat chuong bao thuc ------------------------------------------*/
void MODE_3() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 1, "CHUONG BAO THUC");
if (type_al == 0) Lcd_Out(2, 1,"Khong su dung");
else {
BCD(2,6,hour_al);
LCD_Chr_Cp(':');
BCD(2,9,minute_al);
}
mode = 3;
Mode_3l:
if (PORTB.F1 == 0) goto Mode_3l;
}
/* Hien thi thong tin ve Version thiet ke ------------------------------------*/
void MODE_4() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 3, "PIC VIET NAM");
Lcd_Out(2, 1, "Version 1 - 2007");
mode = 4;
Mode_4l:
if (PORTB.F1 == 0) goto Mode_4l;
}
/* Xu ly phim bam Mode -------------------------------------------------------*/
void MODE_BUTTON() {
INTCON.INTE = 0; // OFF ngat ngoai
PORTE = 0x02; // Bat LED 2
MODE_1(); // Vao man hinh MODE 1
INTCON.T0IE = 1; // Kich hoat Timer0 dem thoi gian tre man hinh 10s
while (delay10s_2 < 5) { // Quet phim MODE
Delay_ms(200); // Khu rung phim
if (PORTB.F1 == 0) {
mode++;
CLEAR_TIMER0();
switch (mode) {
case 1 : MODE_1(); break;
case 2 : MODE_2(); break;
case 3 : MODE_3(); break;
case 4 : MODE_4(); break;
case 5 : goto OUT_MODE;
}
}
}
OUT_MODE: // Ra khoi vong quet
PORTE = 0x00; // OFF LED 2
Lcd_Cmd(Lcd_CLEAR); // Xoa man hinh
CLEAR_TIMER0(); // Xoa bo dem thoi gian tre man hinh
}
/* SET BUTTON ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~*/
/* Cap nhat du lieu cai dat --------------------------------------------------*/
void SET_DATA() {
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00);
I2C_Wr(second);
I2C_Wr(minute);
I2C_Wr(hour);
I2C_Wr(day);
I2C_Wr(date);
I2C_Wr(month);
I2C_Wr(year);
I2C_Stop();
EEprom_Write(0x00, hour_al);
EEprom_Write(0x01, minute_al);
EEprom_Write(0x02, type_al);
}
/* Up data -------------------------------------------------------------------*/
unsigned short UP_BUTTON(unsigned short var) {
Delay_ms(200);
CLEAR_TIMER0();
var = Bcd2Dec(var);
var++;
var = Dec2Bcd(var);
return var;
}
/* Down data -----------------------------------------------------------------*/
unsigned short DOWN_BUTTON(unsigned short var) {
Delay_ms(200);
CLEAR_TIMER0();
var = Bcd2Dec(var);
if (var == 0) var = 101;
else var--;
var = Dec2Bcd(var);
return var;
}
/* Nhap nhay underline cursor ------------------------------------------------*/
void BLINK_CURSOR() {
Lcd_Cmd(LCD_MOVE_CURSOR_LEFT);
Lcd_Cmd(LCD_UNDERLINE_ON);
}
/* Hieu chinh gio ------------------------------------------------------------*/
void SET_HOUR() {
Delay_ms(200);
hour_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto hour_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) hour = UP_BUTTON(hour);
if (Bcd2Dec(hour) == 24) hour = Dec2Bcd(0);
if (PORTB.F4 == 0) hour = DOWN_BUTTON(hour);
if (Bcd2Dec(hour) == 101 ) hour = Dec2Bcd(23);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,5,hour);
BLINK_CURSOR();
}
}
/* Hieu chinh phut -----------------------------------------------------------*/
void SET_MINUTE() {
Delay_ms(100);
minute_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto minute_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(200);
if (PORTB.F3 == 0) minute = UP_BUTTON(minute);
if (Bcd2Dec(minute) == 60) minute = Dec2Bcd(0);
if (PORTB.F4 == 0) minute = DOWN_BUTTON(minute);
if (Bcd2Dec(minute) == 101 ) minute = Dec2Bcd(59);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,8,minute);
BLINK_CURSOR();
}
}
/* Hieu chinh giay -----------------------------------------------------------*/
void SET_SECOND() {
Delay_ms(200);
second_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto second_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) second = UP_BUTTON(second);
if (Bcd2Dec(second) == 60) second = Dec2Bcd(0);
if (PORTB.F4 == 0) second = DOWN_BUTTON(second);
if (Bcd2Dec(second) == 101 ) second = Dec2Bcd(59);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,11,second);
BLINK_CURSOR();
}
}
/* Cai dat thoi gian CLOCK ---------------------------------------------------*/
void SET_1() {
Lcd_Cmd(Lcd_CLEAR);
LCD_Out(1,1,"Cai dat dong ho:");
BCD(2,5,hour);
LCD_Chr_Cp(':');
BCD(2,8,minute);
LCD_Chr_Cp(':');
BCD(2,11,second);
Delay_ms(200);
Set_1l:
if (PORTB.F2 == 0) goto Set_1l;
SET_HOUR();
SET_MINUTE();
SET_SECOND();
}
/* Cai dat Thu (DAY) ---------------------------------------------------------*/
void SET_DAY() {
Delay_ms(200);
day_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto day_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) day = UP_BUTTON(day);
if (Bcd2Dec(day) == 9) day = Dec2Bcd(2);
if (PORTB.F4 == 0) day = DOWN_BUTTON(day);
if (Bcd2Dec(day) == 1) day = Dec2Bcd(8);
TRANSFORM_RTC();
Lcd_Cmd(LCD_CURSOR_OFF);
LCD_Out(2,1,day_txt);
BLINK_CURSOR();
}
}
/* Cai dat ngay (DATE) -------------------------------------------------------*/
void SET_DATE() {
Delay_ms(200);
date_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto date_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) date = UP_BUTTON(date);
if (Bcd2Dec(date) == 32) date = Dec2Bcd(1);
if (PORTB.F4 == 0) date = DOWN_BUTTON(date);
if (Bcd2Dec(date) == 0 ) date = Dec2Bcd(31);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,7,date);
BLINK_CURSOR();
}
}
/* Cai dat thang (MONTH) -----------------------------------------------------*/
void SET_MONTH() {
Delay_ms(100);
month_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto month_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(200);
if (PORTB.F3 == 0) month = UP_BUTTON(month);
if (Bcd2Dec(month) == 13) month = Dec2Bcd(1);
if (PORTB.F4 == 0) month = DOWN_BUTTON(month);
if (Bcd2Dec(month) == 0 ) month = Dec2Bcd(12);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,10,month);
BLINK_CURSOR();
}
}
/* Cai dat nam (YEAR) --------------------------------------------------------*/
void SET_YEAR() {
Delay_ms(200);
year_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto year_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) year = UP_BUTTON(year);
if (Bcd2Dec(year) == 100) year = Dec2Bcd(0);
if (PORTB.F4 == 0) year = DOWN_BUTTON(year);
if (Bcd2Dec(year) == 101) year = Dec2Bcd(99);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,15,year);
BLINK_CURSOR();
}
}
/* Cai dat lich duong CALENDAR ---------------------------------------------- */
void SET_2() {
Lcd_Cmd(Lcd_CLEAR);
LCD_Out(1,1,"Cai dat Lich:");
LCD_Out(2,1,day_txt);
BCD(2,7,date);
LCD_Chr_Cp('/');
BCD(2,10,month);
LCD_Out_Cp("/20");
BCD(2,15,year);
Delay_ms(200);
Set_2l:
if (PORTB.F2 == 0) goto Set_2l;
SET_DAY();
SET_DATE();
SET_MONTH();
SET_YEAR();
}
/* Cai dat gio bao thuc ------------------------------------------------------*/
void TIME_ALARM() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1,1,"Gio rung Chuong:");
BCD(2,6,hour_al);
Lcd_Out(2,8,":");
BCD(2,9,minute_al);
Delay_ms(200);
Set_4l:
if (PORTB.F2 == 0) goto Set_4l;
Delay_ms(200);
hour_al_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto hour_al_lb;
}
Delay_ms(200);
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) hour_al = UP_BUTTON(hour_al);
if (Bcd2Dec(hour_al) == 24) hour_al = Dec2Bcd(0);
if (PORTB.F4 == 0) hour_al = DOWN_BUTTON(hour_al);
if (Bcd2Dec(hour_al) == 101 ) hour_al = Dec2Bcd(23);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,6,hour_al);
BLINK_CURSOR();
}
Delay_ms(200);
minute_al_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto minute_al_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) minute_al = UP_BUTTON(minute_al);
if (Bcd2Dec(minute_al) == 60) minute_al = Dec2Bcd(0);
if (PORTB.F4 == 0) minute_al = DOWN_BUTTON(minute_al);
if (Bcd2Dec(minute_al) == 101 ) minute_al = Dec2Bcd(59);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,9,minute_al);
BLINK_CURSOR();
}
}
/* Cai dat bao thuc ----------------------------------------------------------*/
void SET_3() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Out(1,1,"Cai dat Chuong:");
Delay_ms(200);
Set_3l:
if (PORTB.F2 == 0) goto Set_3l;
Delay_ms(200);
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(200);
if ((PORTB.F3 == 0) || (PORTB.F4 == 0)) {
if (type_al == 0) type_al = 1;
else type_al =0;
CLEAR_TIMER0();
}
if (type_al == 0) Lcd_Out(2, 1,"Khong");
if (type_al == 1) Lcd_Out(2, 1,"Co ");
}
if (type_al == 1) TIME_ALARM();
}
/* Xu ly nut bam Set ---------------------------------------------------------*/
void SET_BUTTON() {
INTCON.INTE = 0; // OFF ngat ngoai
PORTE = 0x04; // Bat LED 3
INTCON.T0IE = 1; // Kich hoat Timer0 dem thoi gian tre man hinh 30s
SET_1();
SET_2();
SET_3();
PORTE = 0x00; // OFF LED 3
Lcd_Cmd(Lcd_CLEAR); // Xoa man hinh
Lcd_Cmd(Lcd_CURSOR_OFF); // OFF con tro man hinh
CLEAR_TIMER0(); // Xoa bo dem thoi gian tre man hinh
SET_DATA(); // Cap nhat du lieu
}
/* Nhay bao chuong -----------------------------------------------------------*/
void BLINK_ALARM() {
INTCON.INTE = 0; // OFF ngat ngoai
PORTE = 0x00;
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);
Lcd_Out(1,1,"Bay gio la ");
BCD(1,12,hour_al);
Lcd_Out(1,14,":");
BCD(1,15,minute_al);
LCD_Out(2,1,day_txt);
BCD(2,7,date);
LCD_Chr_Cp('/');
BCD(2,10,month);
LCD_Out_Cp("/20");
BCD(2,15,year);
while ((count_al < 100) && (PORTB.F1 == 1) && (PORTB.F2 == 1) && (PORTB.F3 == 1) && (PORTB.F4 == 1)) {
Delay_ms(200);
PORTE = ~PORTE;
count_al++;
}
Lcd_Cmd(Lcd_CLEAR);
PORTE = 0x00;
}
/* Quet phim -----------------------------------------------------------------*/
void CYCLE_BUTTON() {
while(1) {
INTCON.T0IE = 0;
INTCON.INTE = 1;
if (flag_time == 1) {
UPDATE_TIME();
flag_time = 0;
}
Delay_ms(200);
if (PORTB.F1 == 0) MODE_BUTTON();
if (PORTB.F2 == 0) SET_BUTTON();
if ((hour == hour_al) && (minute == minute_al) && (second == 0x00)) BLINK_ALARM();
count_al = 0;
}
}
/* XU LY NGAT ************************************************** ***************/
void interrupt() {
if (INTCON.T0IF) {
delay10s_1++;
if (delay10s_1 == 200) {
delay10s_2++;
delay10s_1 = 0;
}
INTCON.T0IF = 0;
}
if (INTCON.INTF) {
flag_time = 1;
INTCON.INTF = 0; // Xoa co ngat ngoai
}
}
/* CHUONG TRINH CHINH ************************************************** *******/
void MAIN() {
INIT_MAIN();
CYCLE_BUTTON();
}
/* KET THUC ************************************************** *****************/
đây là code của e. mọi người giúp e với
/* KHAI BAO BIEN ************************************************** ************/
unsigned short second, minute, hour, day, date, month, year;
unsigned short minute_al, hour_al, type_al, count_al;
unsigned short flag_time, delay10s_1 = 0, delay10s_2 = 0;
unsigned short mode;
char *day_txt;
/* KHOI TAO ************************************************** *****************/
void INIT_MAIN() {
/* Khoi tao cong A -----------------------------------------------------------*/
TRISA = 0xFF; // Dinh nghia cong A la Digital Input
PORTA = 0x00;
/* Khoi tao cong B -----------------------------------------------------------*/
TRISB = 0xFF; // Dinh nghia cong B la Digital Input
PORTB = 0x00;
/* Khoi tao cong C -----------------------------------------------------------*/
TRISC = 0x00;
PORTC = 0x00;
/* Khoi tao cong D -----------------------------------------------------------*/
TRISD = 0x00; // Dinh nghia cong D la Digital Output
PORTD = 0x00;
/* Khoi tao cong E -----------------------------------------------------------*/
ADCON1 = 0x0E; // Dinh nghia cong E la Digital + su dung A/D channel AN0
TRISE = 0x00; // Dinh nghia cong E la Output
PORTE = 0x00; // Xoa du lieu cu tren cong E
/* Khoi tao LCD --------------------------------------------------------------*/
Lcd_Config(&PORTD,4,7,6,3,2,1,0);
Lcd_Init(&PORTD);
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);
/* Khoi tao truyen thong I2C -------------------------------------------------*/
I2C_Init(100000);
/* Khoi tao DS1307 -----------------------------------------------------------*/
/* I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00); // Khoi tao cac gia tri ban dau cho cac thanh ghi thoi gian
I2C_Wr(0x13); // chi lam 1 lan.
I2C_Wr(0x03);
I2C_Wr(0x02);
I2C_Wr(0x03);
I2C_Wr(0x05);
I2C_Wr(0x03);
I2C_Wr(0x03);
I2C_Wr(0x90);
I2C_Stop(); */
/* Khoi tao ngat ngoai va Timer0 ---------------------------------------------*/
INTCON = 0x90; // Mo ngat toan cuc, mo ngat ngoai, xoa co ngat ngoai, xoa co ngat Timer0
OPTION_REG = 0x87; // Chon suon len cho ngat ngoai va Timer0, bo chia ti le 1:256 cho Timer0
TMR0 = 0x00;
/* Khoi tao cac bien luu trong EEPROM*/
hour_al = Eeprom_Read(0x00);
minute_al = Eeprom_Read(0x01);
type_al = Eeprom_Read(0x02);
}
/* CHUONG TRINH CON ************************************************** *********/
/* Doc du lieu RTC tu DS1307 -------------------------------------------------*/
void READ_RTC() {
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00);
I2C_Repeated_Start();
I2C_Wr(0xD1);
second = I2C_Rd(1u);
minute = I2C_Rd(1u);
hour = I2C_Rd(1u);
day = I2C_Rd(1u);
date = I2C_Rd(1u);
month = I2C_Rd(1u);
year = I2C_Rd(1u);
I2C_Stop();
}
/* Xu ly du lieu Date RTC ----------------------------------------------------*/
void TRANSFORM_RTC() {
switch (day) {
case 1: day_txt = "ERO"; break;
case 2: day_txt = "T.2"; break;
case 3: day_txt = "T.3"; break;
case 4: day_txt = "T.4"; break;
case 5: day_txt = "T.5"; break;
case 6: day_txt = "T.6"; break;
case 7: day_txt = "T.7"; break;
case 8: day_txt = "C.N"; break;
}
}
/* Xu ly du lieu RTC ---------------------------------------------------------*/
void BCD(unsigned short x, unsigned short y,unsigned short var) {
unsigned short var1, var2;
var1 = (var >> 4) + 0x30;
LCD_Chr(x,y,var1);
var2 = (var & 0x0F) + 0x30;
LCD_Chr_Cp(var2);
}
/* Hien thi ra LCD -----------------------------------------------------------*/
void LCD_DISPLAY() {
LCD_Out(1,1,day_txt);
BCD(1,7,date);
LCD_Chr_Cp('/');
BCD(1,10,month);
LCD_Out_Cp("/20");
BCD(1,15,year);
if (type_al == 1) Lcd_Out(2, 1,"Chuong");
else Lcd_Out(2, 1," ");
BCD(2,9,hour);
LCD_Chr_Cp(':');
BCD(2,12,minute);
LCD_Chr_Cp(':');
BCD(2,15,second);
}
/* Cap nhat thoi gian --------------------------------------------------------*/
void UPDATE_TIME() {
READ_RTC();
TRANSFORM_RTC();
LCD_DISPLAY();
PORTE.F0 = ~PORTE.F0;
}
/* Xu ly Du lieu Nhiet do tu LM335 -------------------------------------------*/
void TEMPERATURE() {
unsigned short temp1, temp2;
char degree[5];
temp1 = Adc_Read(0);
temp2 = (temp1 - 558)/2;
ByteToStr(temp2, degree);
LCD_Out_CP(degree);
LCD_Chr_Cp(223);
LCD_Chr_Cp('C');
}
/* XOA TIMER0 ----------------------------------------------------------------*/
void CLEAR_TIMER0() {
delay10s_1 = 0;
delay10s_2 = 0;
TMR0 = 0;
}
/* Tinh so ngay Julius -------------------------------------------------------*/
unsigned long jdFromDate(char dd, char mm, char yy) {
char a, m;
unsigned y;
unsigned long jd;
a = (14 - mm)/12;
y = (2000 + yy) + 4800 - a;
m = mm + 12*a - 3;
jd = dd + (153*m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045;
if (jd < 2299161) jd = dd + (153*m + 2)/5 + 365*y + y/4 - 32083;
return jd;
}
/* Tinh ngay soc -------------------------------------------------------------*/
char getNewMoonDay(unsigned long k) {
double T, T2, T3, dr, Jd1, M, Mpr, F, C1, deltat, JdNew;
T = k/1236.85;
T2 = T * T;
T3 = T2 * T;
dr = 3.1415926/180;
Jd1 = 2415020.75933 + 29.53058868*k + 0.0001178*T2 - 0.000000155*T3;
Jd1 = Jd1 + 0.00033*sin((166.56 + 132.87*T - 0.009173*T2)*dr);
M = 359.2242 + 29.10535608*k - 0.0000333*T2 - 0.00000347*T3;
Mpr = 306.0253 + 385.81691806*k + 0.0107306*T2 + 0.00001236*T3;
F = 21.2964 + 390.67050646*k - 0.0016528*T2 - 0.00000239*T3;
C1=(0.1734 - 0.000393*T)*sin(M*dr) + 0.0021*sin(2*dr*M);
C1 = C1 - 0.4068*sin(Mpr*dr) + 0.0161*sin(dr*2*Mpr);
C1 = C1 - 0.0004*sin(dr*3*Mpr);
C1 = C1 + 0.0104*sin(dr*2*F) - 0.0051*sin(dr*(M+Mpr));
C1 = C1 - 0.0074*sin(dr*(M-Mpr)) + 0.0004*sin(dr*(2*F+M));
C1 = C1 - 0.0004*sin(dr*(2*F-M)) - 0.0006*sin(dr*(2*F+Mpr));
C1 = C1 + 0.0010*sin(dr*(2*F-Mpr)) + 0.0005*sin(dr*(2*Mpr+M));
if (T < -11) deltat = 0.001 + 0.000839*T + 0.0002261*T2 - 0.00000845*T3 - 0.000000081*T*T3;
else deltat= -0.000278 + 0.000265*T + 0.000262*T2;
JdNew = Jd1 + C1 - deltat;
return (JdNew + 0.5 + 7/24);
}
/* Tinh toa do mat troi ------------------------------------------------------*/
char getSunLongitude(unsigned long jdn) {
double T, T2, dr, M, L0, DL, L;
T = (jdn - 2451545.5 - 7/24) / 36525;
T2 = T*T;
dr = 3.1415926/180;
M = 357.52910 + 35999.05030*T - 0.0001559*T2 - 0.00000048*T*T2;
L0 = 280.46645 + 36000.76983*T + 0.0003032*T2;
DL = (1.914600 - 0.004817*T - 0.000014*T2)*sin(dr*M);
DL = DL + (0.019993 - 0.000101*T)*sin(dr*2*M) + 0.000290*sin(dr*3*M);
L = L0 + DL;
L = L*dr;
L = L - 3.1415926*2*(L/(3.1415926*2));
return (L / 3.1415926 * 6);
}
/* Tim ngay bat dau thang 11 am lich -----------------------------------------*/
char getLunarMonth11(char yy) {
unsigned long k, off, nm, sunLong;
off = jdFromDate(31, 12, (2000 + yy)) - 2415021;
k = (off / 29.530588853);
nm = getNewMoonDay(k);
sunLong = getSunLongitude(nm);
if (sunLong >= 9) nm = getNewMoonDay(k-1);
return nm;
}
/* Xac dinh thang nhuan ------------------------------------------------------*/
char getLeapMonthOffset(unsigned long a11) {
unsigned long k, last, arc, i;
char tmp;
k = (a11 - 2415021.076998695) / 29.530588853 + 0.5;
last = 0;
i = 1;
tmp = getNewMoonDay(k+i);
arc = getSunLongitude(tmp);
do {
last = arc;
i++;
arc = getSunLongitude(tmp);
}
while (arc != last && i < 14);
return i-1;
}
/* Tinh am lich --------------------------------------------------------------*/
void AM_LICH() {
unsigned long k, dayNumber, monthStart, a11, b11, lunarDay, lunarMonth, lunarYear, lunarLeap, diff, leapMonthDiff;
char dd, mm, yy;
unsigned short ld, lm, ly;
dd = Bcd2Dec(date);
mm = Bcd2Dec(month);
yy = Bcd2Dec(year);
dayNumber = jdFromDate(dd, mm, (2000+yy));
k = (dayNumber - 2415021.076998695) / 29.530588853;
monthStart = getNewMoonDay(k+1);
if (monthStart > dayNumber) monthStart = getNewMoonDay(k);
a11 = getLunarMonth11(2000+yy);
b11 = a11;
if (a11 >= monthStart) {
lunarYear = 2000+yy;
a11 = getLunarMonth11(2000+yy-1);
}
else {
lunarYear = 2000+yy+1;
b11 = getLunarMonth11(2000+yy+1);
}
lunarDay = dayNumber-monthStart+1;
diff = (monthStart - a11)/29;
lunarLeap = 0;
lunarMonth = diff+11;
if (b11 - a11 > 365) {
leapMonthDiff = getLeapMonthOffset(a11);
if (diff >= leapMonthDiff) {
lunarMonth = diff + 10;
if (diff == leapMonthDiff) lunarLeap = 1;
}
}
if (lunarMonth > 12) lunarMonth = lunarMonth - 12;
if (lunarMonth >= 11 && diff < 4) lunarYear -= 1;
ld = Dec2Bcd (lunarDay);
lm = Dec2Bcd (lunarMonth);
BCD(1,10,ld);
Lcd_Chr_Cp(':');
BCD(1,13,lm);
}
/* MODE BUTTON ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~*/
/* Hien thi lich am ----------------------------------------------------------*/
void MODE_1() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 1, "AM LICH: SORRY");
Lcd_Out(2, 1, "Next Update");
// AM_LICH();
mode = 1;
Mode_1l:
if (PORTB.F1 == 0) goto Mode_1l;
}
/* Hien thi nhiet do moi truong ----------------------------------------------*/
void MODE_2() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 1, "NHIET DO:");
TEMPERATURE();
mode = 2;
Mode_2l:
if (PORTB.F1 == 0) goto Mode_2l;
}
/* Hien thi cai dat chuong bao thuc ------------------------------------------*/
void MODE_3() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 1, "CHUONG BAO THUC");
if (type_al == 0) Lcd_Out(2, 1,"Khong su dung");
else {
BCD(2,6,hour_al);
LCD_Chr_Cp(':');
BCD(2,9,minute_al);
}
mode = 3;
Mode_3l:
if (PORTB.F1 == 0) goto Mode_3l;
}
/* Hien thi thong tin ve Version thiet ke ------------------------------------*/
void MODE_4() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1, 3, "PIC VIET NAM");
Lcd_Out(2, 1, "Version 1 - 2007");
mode = 4;
Mode_4l:
if (PORTB.F1 == 0) goto Mode_4l;
}
/* Xu ly phim bam Mode -------------------------------------------------------*/
void MODE_BUTTON() {
INTCON.INTE = 0; // OFF ngat ngoai
PORTE = 0x02; // Bat LED 2
MODE_1(); // Vao man hinh MODE 1
INTCON.T0IE = 1; // Kich hoat Timer0 dem thoi gian tre man hinh 10s
while (delay10s_2 < 5) { // Quet phim MODE
Delay_ms(200); // Khu rung phim
if (PORTB.F1 == 0) {
mode++;
CLEAR_TIMER0();
switch (mode) {
case 1 : MODE_1(); break;
case 2 : MODE_2(); break;
case 3 : MODE_3(); break;
case 4 : MODE_4(); break;
case 5 : goto OUT_MODE;
}
}
}
OUT_MODE: // Ra khoi vong quet
PORTE = 0x00; // OFF LED 2
Lcd_Cmd(Lcd_CLEAR); // Xoa man hinh
CLEAR_TIMER0(); // Xoa bo dem thoi gian tre man hinh
}
/* SET BUTTON ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~*/
/* Cap nhat du lieu cai dat --------------------------------------------------*/
void SET_DATA() {
I2C_Start();
I2C_Wr(0xD0);
I2C_Wr(0x00);
I2C_Wr(second);
I2C_Wr(minute);
I2C_Wr(hour);
I2C_Wr(day);
I2C_Wr(date);
I2C_Wr(month);
I2C_Wr(year);
I2C_Stop();
EEprom_Write(0x00, hour_al);
EEprom_Write(0x01, minute_al);
EEprom_Write(0x02, type_al);
}
/* Up data -------------------------------------------------------------------*/
unsigned short UP_BUTTON(unsigned short var) {
Delay_ms(200);
CLEAR_TIMER0();
var = Bcd2Dec(var);
var++;
var = Dec2Bcd(var);
return var;
}
/* Down data -----------------------------------------------------------------*/
unsigned short DOWN_BUTTON(unsigned short var) {
Delay_ms(200);
CLEAR_TIMER0();
var = Bcd2Dec(var);
if (var == 0) var = 101;
else var--;
var = Dec2Bcd(var);
return var;
}
/* Nhap nhay underline cursor ------------------------------------------------*/
void BLINK_CURSOR() {
Lcd_Cmd(LCD_MOVE_CURSOR_LEFT);
Lcd_Cmd(LCD_UNDERLINE_ON);
}
/* Hieu chinh gio ------------------------------------------------------------*/
void SET_HOUR() {
Delay_ms(200);
hour_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto hour_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) hour = UP_BUTTON(hour);
if (Bcd2Dec(hour) == 24) hour = Dec2Bcd(0);
if (PORTB.F4 == 0) hour = DOWN_BUTTON(hour);
if (Bcd2Dec(hour) == 101 ) hour = Dec2Bcd(23);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,5,hour);
BLINK_CURSOR();
}
}
/* Hieu chinh phut -----------------------------------------------------------*/
void SET_MINUTE() {
Delay_ms(100);
minute_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto minute_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(200);
if (PORTB.F3 == 0) minute = UP_BUTTON(minute);
if (Bcd2Dec(minute) == 60) minute = Dec2Bcd(0);
if (PORTB.F4 == 0) minute = DOWN_BUTTON(minute);
if (Bcd2Dec(minute) == 101 ) minute = Dec2Bcd(59);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,8,minute);
BLINK_CURSOR();
}
}
/* Hieu chinh giay -----------------------------------------------------------*/
void SET_SECOND() {
Delay_ms(200);
second_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto second_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) second = UP_BUTTON(second);
if (Bcd2Dec(second) == 60) second = Dec2Bcd(0);
if (PORTB.F4 == 0) second = DOWN_BUTTON(second);
if (Bcd2Dec(second) == 101 ) second = Dec2Bcd(59);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,11,second);
BLINK_CURSOR();
}
}
/* Cai dat thoi gian CLOCK ---------------------------------------------------*/
void SET_1() {
Lcd_Cmd(Lcd_CLEAR);
LCD_Out(1,1,"Cai dat dong ho:");
BCD(2,5,hour);
LCD_Chr_Cp(':');
BCD(2,8,minute);
LCD_Chr_Cp(':');
BCD(2,11,second);
Delay_ms(200);
Set_1l:
if (PORTB.F2 == 0) goto Set_1l;
SET_HOUR();
SET_MINUTE();
SET_SECOND();
}
/* Cai dat Thu (DAY) ---------------------------------------------------------*/
void SET_DAY() {
Delay_ms(200);
day_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto day_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) day = UP_BUTTON(day);
if (Bcd2Dec(day) == 9) day = Dec2Bcd(2);
if (PORTB.F4 == 0) day = DOWN_BUTTON(day);
if (Bcd2Dec(day) == 1) day = Dec2Bcd(8);
TRANSFORM_RTC();
Lcd_Cmd(LCD_CURSOR_OFF);
LCD_Out(2,1,day_txt);
BLINK_CURSOR();
}
}
/* Cai dat ngay (DATE) -------------------------------------------------------*/
void SET_DATE() {
Delay_ms(200);
date_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto date_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) date = UP_BUTTON(date);
if (Bcd2Dec(date) == 32) date = Dec2Bcd(1);
if (PORTB.F4 == 0) date = DOWN_BUTTON(date);
if (Bcd2Dec(date) == 0 ) date = Dec2Bcd(31);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,7,date);
BLINK_CURSOR();
}
}
/* Cai dat thang (MONTH) -----------------------------------------------------*/
void SET_MONTH() {
Delay_ms(100);
month_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto month_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(200);
if (PORTB.F3 == 0) month = UP_BUTTON(month);
if (Bcd2Dec(month) == 13) month = Dec2Bcd(1);
if (PORTB.F4 == 0) month = DOWN_BUTTON(month);
if (Bcd2Dec(month) == 0 ) month = Dec2Bcd(12);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,10,month);
BLINK_CURSOR();
}
}
/* Cai dat nam (YEAR) --------------------------------------------------------*/
void SET_YEAR() {
Delay_ms(200);
year_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto year_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) year = UP_BUTTON(year);
if (Bcd2Dec(year) == 100) year = Dec2Bcd(0);
if (PORTB.F4 == 0) year = DOWN_BUTTON(year);
if (Bcd2Dec(year) == 101) year = Dec2Bcd(99);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,15,year);
BLINK_CURSOR();
}
}
/* Cai dat lich duong CALENDAR ---------------------------------------------- */
void SET_2() {
Lcd_Cmd(Lcd_CLEAR);
LCD_Out(1,1,"Cai dat Lich:");
LCD_Out(2,1,day_txt);
BCD(2,7,date);
LCD_Chr_Cp('/');
BCD(2,10,month);
LCD_Out_Cp("/20");
BCD(2,15,year);
Delay_ms(200);
Set_2l:
if (PORTB.F2 == 0) goto Set_2l;
SET_DAY();
SET_DATE();
SET_MONTH();
SET_YEAR();
}
/* Cai dat gio bao thuc ------------------------------------------------------*/
void TIME_ALARM() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Out(1,1,"Gio rung Chuong:");
BCD(2,6,hour_al);
Lcd_Out(2,8,":");
BCD(2,9,minute_al);
Delay_ms(200);
Set_4l:
if (PORTB.F2 == 0) goto Set_4l;
Delay_ms(200);
hour_al_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto hour_al_lb;
}
Delay_ms(200);
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) hour_al = UP_BUTTON(hour_al);
if (Bcd2Dec(hour_al) == 24) hour_al = Dec2Bcd(0);
if (PORTB.F4 == 0) hour_al = DOWN_BUTTON(hour_al);
if (Bcd2Dec(hour_al) == 101 ) hour_al = Dec2Bcd(23);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,6,hour_al);
BLINK_CURSOR();
}
Delay_ms(200);
minute_al_lb:
if (PORTB.F2 == 0) {
CLEAR_TIMER0();
goto minute_al_lb;
}
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(100);
if (PORTB.F3 == 0) minute_al = UP_BUTTON(minute_al);
if (Bcd2Dec(minute_al) == 60) minute_al = Dec2Bcd(0);
if (PORTB.F4 == 0) minute_al = DOWN_BUTTON(minute_al);
if (Bcd2Dec(minute_al) == 101 ) minute_al = Dec2Bcd(59);
Lcd_Cmd(LCD_CURSOR_OFF);
BCD(2,9,minute_al);
BLINK_CURSOR();
}
}
/* Cai dat bao thuc ----------------------------------------------------------*/
void SET_3() {
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
Lcd_Out(1,1,"Cai dat Chuong:");
Delay_ms(200);
Set_3l:
if (PORTB.F2 == 0) goto Set_3l;
Delay_ms(200);
while ((delay10s_2 < 5) && (PORTB.F2)) {
Delay_ms(200);
if ((PORTB.F3 == 0) || (PORTB.F4 == 0)) {
if (type_al == 0) type_al = 1;
else type_al =0;
CLEAR_TIMER0();
}
if (type_al == 0) Lcd_Out(2, 1,"Khong");
if (type_al == 1) Lcd_Out(2, 1,"Co ");
}
if (type_al == 1) TIME_ALARM();
}
/* Xu ly nut bam Set ---------------------------------------------------------*/
void SET_BUTTON() {
INTCON.INTE = 0; // OFF ngat ngoai
PORTE = 0x04; // Bat LED 3
INTCON.T0IE = 1; // Kich hoat Timer0 dem thoi gian tre man hinh 30s
SET_1();
SET_2();
SET_3();
PORTE = 0x00; // OFF LED 3
Lcd_Cmd(Lcd_CLEAR); // Xoa man hinh
Lcd_Cmd(Lcd_CURSOR_OFF); // OFF con tro man hinh
CLEAR_TIMER0(); // Xoa bo dem thoi gian tre man hinh
SET_DATA(); // Cap nhat du lieu
}
/* Nhay bao chuong -----------------------------------------------------------*/
void BLINK_ALARM() {
INTCON.INTE = 0; // OFF ngat ngoai
PORTE = 0x00;
Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF);
Lcd_Out(1,1,"Bay gio la ");
BCD(1,12,hour_al);
Lcd_Out(1,14,":");
BCD(1,15,minute_al);
LCD_Out(2,1,day_txt);
BCD(2,7,date);
LCD_Chr_Cp('/');
BCD(2,10,month);
LCD_Out_Cp("/20");
BCD(2,15,year);
while ((count_al < 100) && (PORTB.F1 == 1) && (PORTB.F2 == 1) && (PORTB.F3 == 1) && (PORTB.F4 == 1)) {
Delay_ms(200);
PORTE = ~PORTE;
count_al++;
}
Lcd_Cmd(Lcd_CLEAR);
PORTE = 0x00;
}
/* Quet phim -----------------------------------------------------------------*/
void CYCLE_BUTTON() {
while(1) {
INTCON.T0IE = 0;
INTCON.INTE = 1;
if (flag_time == 1) {
UPDATE_TIME();
flag_time = 0;
}
Delay_ms(200);
if (PORTB.F1 == 0) MODE_BUTTON();
if (PORTB.F2 == 0) SET_BUTTON();
if ((hour == hour_al) && (minute == minute_al) && (second == 0x00)) BLINK_ALARM();
count_al = 0;
}
}
/* XU LY NGAT ************************************************** ***************/
void interrupt() {
if (INTCON.T0IF) {
delay10s_1++;
if (delay10s_1 == 200) {
delay10s_2++;
delay10s_1 = 0;
}
INTCON.T0IF = 0;
}
if (INTCON.INTF) {
flag_time = 1;
INTCON.INTF = 0; // Xoa co ngat ngoai
}
}
/* CHUONG TRINH CHINH ************************************************** *******/
void MAIN() {
INIT_MAIN();
CYCLE_BUTTON();
}
/* KET THUC ************************************************** *****************/
Comment