chuong trinh Dong ho 16f877a

22
Dong ho: #include <16f877a.h> #fuses hs,nowdt #use delay(clock=8000000) #include <ds1307.c> #include <hienthilcd.c> byte sec,phut,gio,thu,ngay,thang,nam; byte const map[10]={0,1,2,3,4,5,6,7,8,9}; byte temp_sec_slow, temp_sec_high, phut_low, phut_high, gio_low, gio_high, thu_a; byte ngay_thap, ngay_cao, thang_thap, thang_cao, nam_thap, nam_cao; float sec_low, sec_high, phut_chuc, phut_donvi, gio_chuc, gio_donvi, thu_b, ngay_chuc; float ngay_donvi, thang_chuc, thang_donvi, nam_chuc, nam_donvi; //hang don vi va hang chuc //************************************** byte mode; // Che do int1 blink_sec, blink_phut, blink_gio, blink_ngay, blink_thang, blink_nam, blink_thu; #bit SW_mode = 0x08.0 // Cho che do #bit SW_tang = 0x08.1 // Chinh tang #bit SW_giam = 0x08.2 // Chinh giam #bit SW_clear = 0x08.3 // Ngung chinh //************************************** void read_rtc(); void bcd_to_dec(); //chuyen tu bcd sang thap phan void display(); // hien thi void key_test(); void chinhgiay(); void chinhphut(); void chinhgio(); void chinhngay(); void chinhthang(); void chinhnam(); void chinhthu(); void set_blink(); //*********************

Transcript of chuong trinh Dong ho 16f877a

Page 1: chuong trinh Dong ho 16f877a

Dong ho:

#include <16f877a.h>#fuses hs,nowdt#use delay(clock=8000000)#include <ds1307.c>#include <hienthilcd.c>byte sec,phut,gio,thu,ngay,thang,nam;byte const map[10]={0,1,2,3,4,5,6,7,8,9};byte temp_sec_slow, temp_sec_high, phut_low, phut_high, gio_low, gio_high, thu_a;byte ngay_thap, ngay_cao, thang_thap, thang_cao, nam_thap, nam_cao;

float sec_low, sec_high, phut_chuc, phut_donvi, gio_chuc, gio_donvi, thu_b, ngay_chuc;float ngay_donvi, thang_chuc, thang_donvi, nam_chuc, nam_donvi; //hang don vi va hang chuc

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

byte mode; // Che doint1 blink_sec, blink_phut, blink_gio, blink_ngay, blink_thang, blink_nam, blink_thu;

#bit SW_mode = 0x08.0 // Cho che do#bit SW_tang = 0x08.1 // Chinh tang#bit SW_giam = 0x08.2 // Chinh giam#bit SW_clear = 0x08.3 // Ngung chinh

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

void read_rtc();void bcd_to_dec(); //chuyen tu bcd sang thap phanvoid display(); // hien thivoid key_test();void chinhgiay();void chinhphut();void chinhgio();void chinhngay();void chinhthang();void chinhnam();void chinhthu();void set_blink();

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

void read_rtc() { sec = read_ds1307(0); phut = read_ds1307(1); gio = read_ds1307(2); thu_b = read_ds1307(3); ngay = read_ds1307(4); thang = read_ds1307(5); nam = read_ds1307(6); ngay_thap = ngay & 0x0f; ngay_cao = (ngay & 0x30)>>4; thang_thap = thang & 0x0f; thang_cao = (thang & 0x10)>>4;

Page 2: chuong trinh Dong ho 16f877a

nam_thap = nam & 0x0f; nam_cao = (nam & 0xf0)>>4; gio_low = gio & 0x0f; gio_high = (gio & 0x30)>>4; temp_sec_slow = sec & 0x0f; //don vi temp_sec_high = (sec & 0x70)>>4; //chuc phut_low = phut & 0x0f; //don vi phut_high = (phut & 0x70)>>4; //chuc }

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

void bcd_to_dec() { sec_low = map[temp_sec_slow]; sec_high = map[temp_sec_high]; phut_donvi = map[phut_low]; phut_chuc = map[phut_high]; gio_donvi = map[gio_low]; gio_chuc = map[gio_high]; ngay_chuc = map[ngay_cao]; ngay_donvi = map[ngay_thap]; thang_chuc = map[thang_cao]; thang_donvi = map[thang_thap]; nam_chuc = map[nam_cao]; nam_donvi = map[nam_thap]; }

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

void display()

{ lcd_gotoxy(16,2); printf(lcd_putc,"%1.0f",sec_low); lcd_gotoxy(15,2); printf(lcd_putc,"%1.0f",sec_high); lcd_gotoxy(14,2); printf(lcd_putc,":"); lcd_gotoxy(13,2); printf(lcd_putc,"%1.0f",phut_donvi); lcd_gotoxy(12,2); printf(lcd_putc,"%1.0f",phut_chuc); lcd_gotoxy(11,2); printf(lcd_putc,":"); lcd_gotoxy(10,2); printf(lcd_putc,"%1.0f",gio_donvi); lcd_gotoxy(9,2); printf(lcd_putc,"%1.0f",gio_chuc); lcd_gotoxy(1,1); printf(lcd_putc,"%1.0f",ngay_chuc); lcd_gotoxy(2,1); printf(lcd_putc,"%1.0f",ngay_donvi); lcd_gotoxy(3,1); printf(lcd_putc,":"); lcd_gotoxy(4,1); printf(lcd_putc,"%1.0f",thang_chuc); lcd_gotoxy(5,1); printf(lcd_putc,"%1.0f",thang_donvi); lcd_gotoxy(6,1); printf(lcd_putc,":"); lcd_gotoxy(7,1); printf(lcd_putc,"%1.0f",nam_chuc); lcd_gotoxy(8,1); printf(lcd_putc,"%1.0f",nam_donvi); lcd_gotoxy(1,2); printf(lcd_putc,"THU:"); lcd_gotoxy(5,2); printf(lcd_putc,"%1.0f",thu_b); lcd_gotoxy(6,2); printf(lcd_putc," "); if(thu_b==1) { lcd_gotoxy(5,2); printf(lcd_putc,"CN"); }

Page 3: chuong trinh Dong ho 16f877a

}

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

void key_test(){if (SW_mode==0) { delay_ms(20); if(SW_mode==0){ while(SW_mode==0){;} mode++; } }else if (SW_clear==0) { mode=0; blink_sec=0; blink_phut=0; blink_gio=0; blink_ngay=0; blink_thang=0; blink_nam=0; }

switch (mode) { case 1: { blink_sec =1; blink_phut=0; blink_gio=0; blink_ngay=0; blink_thang=0; blink_nam=0; chinhgiay(); break; } case 2: { blink_phut =1; blink_sec=0; blink_gio=0; blink_ngay=0; blink_thang=0; blink_nam=0; chinhphut(); break; } case 3: { blink_gio=1; blink_sec=0; blink_phut=0;

Page 4: chuong trinh Dong ho 16f877a

blink_ngay=0; blink_thang=0; blink_nam=0; chinhgio(); break; } case 4: { blink_thu=1; blink_sec =0; blink_phut=0; blink_gio=0; blink_ngay=0; blink_thang=0; blink_nam=0; chinhthu(); break; } case 5: { blink_sec =0; blink_phut=0; blink_gio=0; blink_ngay=1; blink_thang=0; blink_nam=0; blink_thu=0; chinhngay(); break; } case 6: { blink_sec =0; blink_phut=0; blink_gio=0; blink_ngay=0; blink_thang=1; blink_nam=0; blink_thu=0; chinhthang(); break; } case 7: { blink_sec =0; blink_phut=0; blink_gio=0; blink_ngay=0; blink_thang=0; blink_nam=1; blink_thu=0; chinhnam(); break; } case 8:

Page 5: chuong trinh Dong ho 16f877a

{ mode=0; blink_sec =0; blink_phut=0; blink_gio=0; blink_ngay=0; blink_thang=0; blink_nam=0; blink_thu=0; break; } }}

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

void chinhgiay(){ set_blink(); if (SW_tang==0)// nut tang duoc nhan { delay_ms(1); if (sec<0x30) { write_ds1307(0,0x00); } else { write_ds1307(0,0x00); phut++; write_ds1307(1,phut); } } }

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

void chinhphut(){ set_blink(); if (SW_tang==0) { delay_ms(1); if (phut==0x09) { write_ds1307(1,0x10); } else if (phut==0x19) { write_ds1307(1,0x20); } else if (phut==0x29) { write_ds1307(1,0x30);

Page 6: chuong trinh Dong ho 16f877a

} else if (phut==0x39) { write_ds1307(1,0x40); } else if (phut==0x49) { write_ds1307(1,0x50); } else if (phut==0x59) { write_ds1307(1,0x00); } else { phut++; write_ds1307(1,phut); } } else if (SW_giam==0) { delay_ms(1); if (phut==0x50) { write_ds1307(1,0x49); } else if (phut==0x40) { write_ds1307(1,0x39); } else if (phut==0x30) { write_ds1307(1,0x29); } else if (phut==0x20) { write_ds1307(1,0x19); } else if (phut==0x10) { write_ds1307(1,0x09); } else if (phut==0x00) { write_ds1307(1,0x59); } else { phut--; write_ds1307(1,phut); } }

Page 7: chuong trinh Dong ho 16f877a

}

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

void chinhgio(){ set_blink(); if (SW_tang==0) { delay_ms(1); if (gio==0x09) {write_ds1307(2,0x10);} else if (gio==0x19) {write_ds1307(2,0x20);} else if (gio==0x23) {write_ds1307(2,0x00);} else {gio++; write_ds1307(2,gio);} } else if (SW_giam==0) { delay_ms(1); if (gio==0x20) {write_ds1307(2,0x19);} else if (gio==0x10) {write_ds1307(2,0x09);} else if (gio==0x00) {write_ds1307(2,0x23);} else {gio--; write_ds1307(2,gio);} }}

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

void chinhthu(){ set_blink(); if (SW_tang==0) { delay_ms(1); if (thu==0x07) {write_ds1307(3,0x01);} else {thu++; write_ds1307(3,thu);} } else if (SW_giam==0) { delay_ms(1); if (thu==0x01) {write_ds1307(3,0x07);} else {thu--; write_ds1307(3,thu);} }}//**************************************************

void chinhngay(){ set_blink(); if (SW_tang==0) { delay_ms(1); if (ngay==0x09) {write_ds1307(4,0x10);} else if (ngay==0x19) {write_ds1307(4,0x20);} else if (ngay==0x29) {write_ds1307(4,0x30);} else if (ngay==0x32) {write_ds1307(4,0x01);}

Page 8: chuong trinh Dong ho 16f877a

else {ngay++; write_ds1307(4,ngay);} } else if (SW_giam==0) { delay_ms(1); if (ngay==0x01) {write_ds1307(4,0x32);} else if (ngay==0x30) {write_ds1307(4,0x29);} else if (ngay==0x20) {write_ds1307(4,0x19);} else if (ngay==0x10) {write_ds1307(4,0x09);} else {ngay--; write_ds1307(4,ngay);} }}

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

void chinhthang(){ set_blink(); if (SW_tang==0) { delay_ms(1); if (thang==0x09) {write_ds1307(5,0x10);} else if (thang==0x12) {write_ds1307(5,0x01);} else {thang++; write_ds1307(5,thang);} } else if (SW_giam==0) { delay_ms(1); if (thang==0x10) {write_ds1307(5,0x09);} else if (thang==0x01) {write_ds1307(5,0x12);} else {thang--; write_ds1307(5,thang);} }}

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

void chinhnam(){ set_blink(); if (SW_tang==0) { delay_ms(1); if (nam==0x09) { write_ds1307(6,0x10); } else if (nam==0x19) { write_ds1307(6,0x20); } else if (nam==0x29) { write_ds1307(6,0x30); } else if (nam==0x39)

Page 9: chuong trinh Dong ho 16f877a

{ write_ds1307(6,0x40); } else if (nam==0x49) { write_ds1307(6,0x50); } else if (nam==0x59) { write_ds1307(6,0x60); } else if (nam==0x69) { write_ds1307(6,0x70); } else if (nam==0x79) { write_ds1307(6,0x80); } else if (nam==0x89) { write_ds1307(6,0x90); } else if (nam==0x99) { write_ds1307(6,0x00); } else { nam++; write_ds1307(6,nam); } } else if (SW_giam==0) { delay_ms(1); if (nam==0x90) { write_ds1307(6,0x89); } else if (nam==0x80) { write_ds1307(6,0x79); } else if (nam==0x70) { write_ds1307(6,0x69); } else if (nam==0x60) { write_ds1307(6,0x59); } else if (nam==0x50) { write_ds1307(6,0x49);

Page 10: chuong trinh Dong ho 16f877a

} else if (nam==0x40) { write_ds1307(6,0x39); } else if (nam==0x30) { write_ds1307(6,0x29); } else if (nam==0x20) { write_ds1307(6,0x19); } else if (nam==0x10) { write_ds1307(6,0x9); } else if (nam==0x00) { write_ds1307(6,0x99); } else { nam--; write_ds1307(6,nam); } }}

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

void set_blink(){if (blink_sec==1) { lcd_gotoxy(15,2); printf(lcd_putc," "); // hai khoang trong delay_ms(250); lcd_gotoxy(15,2); printf(lcd_putc,"%1.0f",sec_high); lcd_gotoxy(16,2); printf(lcd_putc,"%1.0f",sec_low); } if (blink_phut==1) { lcd_gotoxy(12,2); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(12,2); printf(lcd_putc,"%1.0f",phut_chuc); lcd_gotoxy(13,2); printf(lcd_putc,"%1.0f",phut_donvi); }

Page 11: chuong trinh Dong ho 16f877a

if (blink_gio==1) { lcd_gotoxy(9,2); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(9,2); printf(lcd_putc,"%1.0f",gio_chuc); lcd_gotoxy(10,2); printf(lcd_putc,"%1.0f",gio_donvi); }

if (blink_thu==1) { if (thu_b==1){ lcd_gotoxy(5,2); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(5,2); printf(lcd_putc,"CN"); } else{ lcd_gotoxy(5,2); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(5,2); printf(lcd_putc,"%1.0f",thu_b); } }

if (blink_ngay==1) { lcd_gotoxy(1,1); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(1,1); printf(lcd_putc,"%1.0f",ngay_chuc); lcd_gotoxy(2,1); printf(lcd_putc,"%1.0f",ngay_donvi); } if (blink_thang==1) { lcd_gotoxy(4,1); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(4,1); printf(lcd_putc,"%1.0f",thang_chuc); lcd_gotoxy(5,1); printf(lcd_putc,"%1.0f",thang_donvi); } if (blink_nam==1) { lcd_gotoxy(7,1); printf(lcd_putc," "); delay_ms(250); lcd_gotoxy(7,1);

Page 12: chuong trinh Dong ho 16f877a

printf(lcd_putc,"%1.0f",nam_chuc); lcd_gotoxy(8,1); printf(lcd_putc,"%1.0f",nam_donvi); }}

//*************************************************void main(){byte u;mode=0;blink_sec=0;blink_phut=0;blink_gio=0;blink_thu=0;blink_ngay=0;blink_thang=0;blink_nam=0;set_tris_b(0x00);u = read_ds1307(0);sec = u & 0x7f; //mo RTCwrite_ds1307(0,sec);lcd_init();init_ds1307();while(true){read_rtc();bcd_to_dec();display();key_test();}}

Hien thi lcd:

#include <16f877a.h>#fuses hs#use delay(clock=8000000)#define rs pin_b4#define enabled pin_b5#define db4 pin_b0#define db5 pin_b1#define db6 pin_b2#define db7 pin_b3 char const lcd_type=2; char const lcd_init_string[4]={0x20 | lcd_type<<2,0x0c, 1, 6}; //////////////////////////// void make_out_data(char buffer_data) { output_bit(db4, bit_test(buffer_data,0)); output_bit(db5, bit_test(buffer_data,1));

Page 13: chuong trinh Dong ho 16f877a

output_bit(db6, bit_test(buffer_data,2)); output_bit(db7, bit_test(buffer_data,3)); }///////////////////////////void lcd_send_nibble(char buffer_nibble) { make_out_data(buffer_nibble); delay_us(10); output_high(enabled); delay_us(10); output_low(enabled); }/////////////////////////void lcd_send_byte( char address, char n) { output_low(rs); delay_ms(1); output_bit(rs,address);//lcd.rs=address; delay_us(40); output_low(enabled);//lcd.enabled=0; lcd_send_nibble(n>>4); lcd_send_nibble(n&0x0f); delay_ms(1); }//////////////////////////void lcd_init() { char i; output_low(rs);//lcd.rs=0; output_low(enabled);//lcd.enable=0; delay_ms(500); lcd_send_nibble(2); for(i=0;i<=3;++i) lcd_send_byte(0,lcd_init_string[i]); }///////////////////////////void lcd_gotoxy(char x, char y) { char address; switch(y) { case 1 : address=0x80; break; case 2 : address=0xc0; break; } address=address+(x-1); lcd_send_byte(0,address); }////////////////////////void lcd_putc(char c) { lcd_send_byte(1,c); }/////////////////////////main(){

Page 14: chuong trinh Dong ho 16f877a

lcd_init(); while(1) { lcd_send_byte(0,1); lcd_gotoxy(2,1); printf (lcd_putc,"TRUONG CAO DANG"); lcd_gotoxy(5,2); printf (lcd_putc,"KT-KT KG"); delay_ms(3500); lcd_send_byte (0,1); lcd_gotoxy(5,1); printf (lcd_putc,"THONG BAO"); lcd_gotoxy(4,2); printf (lcd_putc,"TUYEN SINH"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(5,1); printf (lcd_putc,"CAC NGANH"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(5,1); printf (lcd_putc,"KE TOAN"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(5,1); printf (lcd_putc,"QUAN TRI"); lcd_gotoxy(4,2); printf (lcd_putc,"KINH DOANH"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(1,1); printf (lcd_putc,"QUAN TRI DICH VU"); lcd_gotoxy(2,2); printf (lcd_putc,"DU LICH,LU HANH"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(5,1); printf (lcd_putc,"KHACH SAN"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(1,1); printf (lcd_putc,"T.ANH THUONG MAI"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(6,1); printf (lcd_putc,"TIN HOC"); lcd_gotoxy(6,2); printf (lcd_putc,"UNG DUNG"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(3,1); printf (lcd_putc,"KI THUAT O TO"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(1,1);

Page 15: chuong trinh Dong ho 16f877a

printf (lcd_putc,"KI THUAT CO KHI"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(3,1); printf (lcd_putc,"DIEN-DIEN TU"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(4,1); printf (lcd_putc,"CO-DIEN TU"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(6,1); printf (lcd_putc,"DIEN TU"); lcd_gotoxy(3,2); printf (lcd_putc,"TRUYEN THONG"); delay_ms(3500); lcd_send_byte(0,1); lcd_gotoxy(2,1); printf (lcd_putc,"DICH VU THU Y"); delay_ms(3500); }}

Nhiet do len lcd:

#include <16f877a.h>#device adc=10#fuses hs#use delay(clock=8000000)#define rs pin_b4#define enabled pin_b5#define db4 pin_b0#define db5 pin_b1#define db6 pin_b2#define db7 pin_b3char const lcd_type=2;char const LCD_INIT_STRING[4]={0X20|(LCD_type<<2),0x0c,0x01,0x06};//ham suat 4 bit du lieu ra 4 chan rb0, rb1, rb2, rb3void make_out_data(char buffer_data){ output_bit(db4,bit_test(buffer_data,0)); output_bit(db5,bit_test(buffer_data,1)); output_bit(db6,bit_test(buffer_data,2)); output_bit(db7,bit_test(buffer_data,3));}//ham gui nua byte du lieuvoid lcd_send_nibble(char buffer_nibble){ make_out_data(buffer_nibble); delay_us(10); output_high(enabled); delay_us(10);

Page 16: chuong trinh Dong ho 16f877a

output_low(enabled);}//ham goi 1 byte du lieuvoid lcd_send_byte(char address,char n){ output_low(rs); delay_ms(1); output_bit(rs,address); delay_us(40); output_low(enabled); lcd_send_nibble(n>>4); lcd_send_nibble(n&0x0f); delay_ms(1);}//ham khoi tao lcdvoid lcd_init(){char i;output_low(rs);output_low(enabled);delay_ms(500);lcd_send_nibble(2);for(i=0;i<=3;++i)lcd_send_byte(0,lcd_init_string[i]);}//di den toa do xy de hien thi du lieuvoid lcd_gotoxy(char x,char y){ char address; switch(y) { case 1:address=0x80;break; case 2:address=0xc0;break; } address=address+(x-1); lcd_send_byte(0,address);}//ham gui ki tu hien thi len lcdvoid lcd_putc(char c){ lcd_send_byte(1,c);}//ham dich chuyenvoid lcd_shiftleft(){ lcd_send_byte(0,0x18);}void lcd_shiftright(){ lcd_send_byTE(0,0x1c);}void lcd_moveleft(char p){ char i; for(i=0;i<p;i++)

Page 17: chuong trinh Dong ho 16f877a

{ lcd_shiftleft(); delay_ms(200); }}void lcd_moveright(char p){ char i; for(i=0;i<p;i++) { lcd_shiftright(); delay_ms(200); }}main(){float tam,nhietdo; lcd_init(); setup_adc(adc_clock_internal); setup_adc_ports(an0_an1_an3); set_adc_channel(3); set_tris_d(0x00); output_d(0x00); delay_ms(10); while(1) { tam=(read_adc()-558.558); nhietdo=(tam/2.048); lcd_gotoxy(1,1); printf(lcd_putc," NHIET DO:"); lcd_gotoxy(12,1); printf(lcd_putc,"%1.0f",nhietdo); lcd_gotoxy(14,1); lcd_putc(223); lcd_gotoxy(15,1); printf(lcd_putc,"C"); if(nhietdo>40){ output_high(pin_d0); output_low(pin_d1); output_high(pin_d3); output_low(pin_d2); delay_ms(500); output_low(pin_d0); } else if((nhietdo>=30)&&(nhietdo<=40)){ output_high(pin_d1); output_low(pin_d0); output_low(pin_d3); output_low(pin_d2); delay_ms(500); output_low(pin_d1); } else if(nhietdo<30){ output_high(pin_d2);

Page 18: chuong trinh Dong ho 16f877a

output_low(pin_d0); output_low(pin_d1); output_low(pin_d3); delay_ms(500); output_low(pin_d2); } }}

Nhiet do len led:

#include <16f877a.h>#device adc=10#fuses hs#use delay(clock=8000000)main(){float tam;int8 nhietdo; setup_adc(adc_clock_internal); setup_adc_ports(an0_an1_an3); set_adc_channel(3); set_tris_c(0x00); set_tris_b(0x00); output_b(0x00); output_c(0x00); delay_ms(10); while(1) { tam=(read_adc()-558.558); nhietdo=(tam/2.048); output_b(nhietdo/10); output_c(nhietdo%10); }}