mình đang làm đồ án đồng hồ số dùng pic 16f877a và rtc ds1307, đã code xong phần hiển thị hết rồi, nhưng khi mô phỏng thấy phần năm hàng chục và hàng đơn vị bị đảo lộn cho nhau, nhưng mình xem lại code thì thấy hoàn toàn đúng hay trình mình kém không biết sai ở đâu hết? mong mấy bạn giúp mình với
em không biết cách up lên diễn đàn , mod đừng xóa bài em nha
dong ho.rar
Code:
#include <16f877a.h> #include <def_877a.h> #include <ds1307.c> #fuses nowdt,noprotect,nolvp,xt,hs #use delay(clock=4000000) #use fast_io(b) #use fast_io(d) byte sec,min,hour,date,month,year; byte const maled[10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90 }; byte gic,gid,pc,pd,gc,gd,dac,dad,moc,mod,ye1,ye2,ye3,ye4; byte chinh_giay,chinh_phut,chinh_gio,chinh_nam,chinh_ngay,chinh_thang; int i; int16 j; #bit hourc =0x08.5 #bit hourd =0x08.4 #bit minc =0x08.3 #bit mind =0x08.2 #bit secc =0x08.1 #bit secd =0x08.0 #bit dated =0x09.2 #bit datec =0x09.1 #bit monthd =0x07.7 #bit monthc =0x07.2 #bit y4 =0x05.5 #bit y3 =0x09.0 #bit y2 =0x05.3 #bit y1 =0x05.2 //nam hang ngan #bit sw1=0x07.0 #bit sw2=0x07.1 #bit sw3=0x07.5 #bit sw4=0x07.6 void hienthi(); void read_time(); void update_time(); void update_ds1307(); void set_sec(); void set_min(); void set_hour(); void set_date(); void set_month(); void set_year(); void main() { j++; set_tris_d(0x00); set_tris_b(0x00); set_tris_e(0x00); set_tris_a(0x01); set_tris_c(0x63); init_ds1307(); chinh_giay=0; chinh_phut=0; chinh_gio=0; chinh_ngay=0; chinh_thang=0; chinh_nam=0; while(1) { read_time(); hienthi(); if(sw1==0) { if(chinh_giay==0&&sw4==1) { while(sw1==0) {} set_sec(); chinh_giay=1;} else if(chinh_phut==0 && sw4==1) { while(sw1==0){} set_min();chinh_phut=1; } else if(chinh_gio==0 && sw4==1) { while(sw1==0){} set_hour();chinh_gio=1; } else if(chinh_ngay==0 && sw4==1) { while(sw1==0){} set_date();chinh_ngay=1; } else if(chinh_thang==0 && sw4==1) { while(sw1==0){} set_month();chinh_thang=1; } else if(chinh_nam==0 && sw4==1) { while(sw1==0){} set_year();chinh_nam=1; } else if(chinh_giay==1&&chinh_phut==1&&chinh_gio==1&&chinh_ngay==1&&chinh_thang==1&&chinh_nam==1) {chinh_giay=0;chinh_phut=0;chinh_gio=0;chinh_ngay=0;chinh_thang=0;chinh_nam=0;} } if(sw4==0) { chinh_giay=0;chinh_phut=0;chinh_gio=0;chinh_ngay=0;chinh_thang=0;chinh_nam=0; } } } void read_time() { sec=read_ds1307(0); min=read_ds1307(1); hour=read_ds1307(2); date=read_ds1307(4); month=read_ds1307(5); year=read_ds1307(6); update_time(); } void update_ds1307() { write_ds1307(0,sec); write_ds1307(1,min); write_ds1307(2,hour); write_ds1307(4,date); write_ds1307(5,month); write_ds1307(6,year); } void update_time() { gd=sec&0x0f; gc=(sec&0xf0)>>4; pd=min&0x0f; pc=(min&0xf0)>>4; gid=hour&0x0f; gic=(hour&0x30)>>4; dad=date&0x0f; dac=(date&0x30)>>4; mod=month&0x0f; moc=(month&0x10)>>4; ye3=year&0x0f; ye4=(year&0xf0)>>4; } void set_sec() { gd=1;gc=1;pd=1;pc=1;gid=1;gic=1; sec=read_ds1307(0); gd=sec&0x0f; gc=(sec&0xf0)>>4; while(sw1==1 && sw4==1) { portb=maled[gd];secd=0;delay_us(100);secd=1; portb=maled[gc];secc=0;delay_us(100);secc=1; if(sw2==0) { while(sw2==0){} if(gc==5 && gd==10) {gc=0;gd=0;} else if(gd==10) {gc++;gd=0;} else gd++; } if(sw3==0) { while(sw3==0){} if(gc==0&&gd==0) {gc=5;gd=9;} else if(gd==0) {gc--;gd=9;} else gd--; } sec=(gc<<4)+gd; write_ds1307(0,sec); delay_us(8); } } void set_min() { gd=1;gc=1;pd=1;pc=1;gid=1;gic=1; min=read_ds1307(1); pd=min&0x0f; pc=(min&0xf0)>>4; while(sw1==1 && sw4==1) { portb=maled[pd];mind=0;delay_us(100);mind=1; portb=maled[pc];minc=0;delay_us(100);minc=1; if(sw2==0) { while(sw2==0){} if(pc==5 && pd==10) {pc=0;pd=0;} else if(pd==10) {pc++;pd=0;} else pd++; } if(sw3==0) { while(sw3==0){} if(pc==0&&pd==0) {pc=5;pd=9;} else if(pd==0) {pc--;pd=9;} else pd--; } min=(pc<<4)+pd; write_ds1307(1,min); delay_us(8); } } void set_hour() { gd=1;gc=1;pd=1;pc=1;gid=1;gic=1; hour=read_ds1307(2); gid=hour&0x0f; gic=(hour&0x30)>>4; while(sw1==1 && sw4==1) { portb=maled[gid];hourd=0;delay_us(100);hourd=1; portb=maled[gic];hourc=0;delay_us(100);hourc=1; if(sw2==0) { while(sw2==0){} if(gic==2 && gid==4) {gic=0;gid=0;} else if(gid==10) {gic++;gid=0;} else gid++; } if(sw3==0) { while(sw3==0){} if(gic==0&&gid==0) {gic=2;gid=3;} else if(gid==0) {gic--;gid=9;} else gid--; } hour=(gic<<4)+gid; write_ds1307(2,hour); delay_us(8); } } void set_date() { dac=1;dad=1;moc=1;mod=1;ye1=1;ye2=1;ye3=1;ye4=1; date=read_ds1307(4); month=read_ds1307(5); dad=date&0x0f; dac=(date&0x30)>>4; while(sw1==1 &&sw4==1) { portb=maled[dad];dated=0;delay_us(100);dated=1; portb=maled[dac];datec=0;delay_us(100);datec=1; if(sw2==0) { while(sw2==0){} if(month==1 || month==3 ||month==5 || month==7 ||month==8 || month==10 || month==12) { if(dac==3 && dad==1) {dac=0;dad=0;} else if(dad==10) {dac++;dad=0;} else dad++; } else if(month==4 || month==6 || month==9 || month==11 ||month==8 || month==10) { if(dac==3 && dad==0) {dac=0;dad=0;} else if(dad==10) {dac++;dad=0;} else dad++; } } if(sw3==0) { while(sw3==0){} if(month==1 || month==3 ||month==5 || month==7 ||month==8 || month==10 || month==12) { if(dac==0 && dad==0) {dac=3;dad=1;} else if(dad==0) {dac--;dad=9;} else dad--; } else if(month==4 || month==6 ||month==9 || month==11 ||month==8 || month==10) { if(dac==0 && dad==0) {dac=3;dad=0;} else if(dad==0) {dac--;dad=9;} else dad--; } } date=(dac<<4)+dad; write_ds1307(4,date); delay_us(8); } } // void set_month() { dac=1;dad=1;moc=1;mod=1;ye1=1;ye2=1;ye3=1;ye4=1; month=read_ds1307(5); mod=month&0x0f; moc=(month&0x10)>>4; while(sw1==1&&sw4==1) { portb=maled[mod];monthd=0;delay_us(100);monthd=1; portb=maled[moc];monthc=0;delay_us(100);monthc=1; if(sw2==0) { while(sw2==0){} if(moc==1 && mod==2) {moc=0;mod=0;} else if(mod==10) {moc++;mod=0;} else mod++; } if(sw3==0) { while(sw3==0){} if(moc==0 && mod==0) {moc=1;mod=2;} else if(mod==0) {moc--;mod=9;} else mod--; } month=(moc<<4)+mod; write_ds1307(5,month); delay_us(8); } } void set_year() { dac=1;dad=1;moc=1;mod=1;ye1=1;ye2=1;ye3=1;ye4=1; year=read_ds1307(6); ye3=(year&0xf0)>>4; ye4=year&0x0f; while(sw1==1 && sw4==1) { portb=maled[ye4];y4=0;delay_us(100);y4=1; portb=maled[ye3];y3=0;delay_us(100);y3=1; portb=0xc0;y2=0;delay_us(100);y2=1; portb=0xa4;y1=0;delay_us(100);y1=1; if(sw2==0) { while(sw2==0){} if(ye3==9&&ye4==9) { ye3=0;ye4=0; } else if(ye4==10) { ye4=0;ye3++; } else ye4++; } if(sw3==0) { while(sw3==0){} if(ye3==0&&ye4==0) { ye3=9;ye4=9; } else if(ye4==0) {ye3--;ye4=9;} else ye4--; } year=(ye3<<4)+ ye4; write_ds1307(6,year); delay_us(8); } } void hienthi() { for(i=1;i<30;i++) { portb=maled[gd];secd=0;delay_us(100);secd=1; portb=maled[gc];secc=0;delay_us(100);secc=1; portb=maled[pd];mind=0;delay_us(100);mind=1; portb=maled[pc];minc=0;delay_us(100);minc=1; portb=maled[gid];hourd=0;delay_us(100);hourd=1; portb=maled[gic];hourc=0;delay_us(100);hourc=1; portb=maled[dad];dated=0;delay_us(100);dated=1; portb=maled[dac];datec=0;delay_us(100);datec=1; portb=maled[mod];monthd=0;delay_us(100);monthd=1; portb=maled[moc];monthc=0;delay_us(100);monthc=1; portb=maled[ye3];y3=0;delay_us(100);y3=1; portb=maled[ye4];y4=0;delay_us(100);y4=1; portb=0xc0;y2=0;delay_us(100);y2=1; portb=0xa4;y1=0;delay_us(100);y1=1; } }
dong ho.rar
Comment