Nah silahkan kunjungi terlebih dahulu untuk lebih memahami halaman ini. Praktek yang akan dilakukan dalam percobaan kali ini adalah membuat jam yang dapat di atur waktunya jika mengalami perbedaan waktu.
Cobalah Listing Program yang ada di bawah ini :
--------------------------------------------------------------------------------------
#include <LiquidCrystal.h>
LiquidCrystal lcd(43, 45, 47, 49,51,53); // RS,E,D4,D5,D6,D7
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
int ct1=1;
int ta=9 ; int tb=8; int tc=7;
int t1,t2,t3,j,mn,d,tg,bl,th,hk;
String hari;
String b1,b2,baris1,baris2;
int k=1; int tm=1;
int j1,j2,m1,m2,d1,d2;
int ct2,ct3;
boolean lt1= false;
void setup() {
Wire.begin();
lcd.begin(16,2);
pinMode(ta,INPUT); pinMode(tb,INPUT);pinMode(tc,INPUT);
pinMode (18,OUTPUT); digitalWrite(18,0); // Untuk Memberi Tegangan 0 Volt ke RTC
pinMode (19,OUTPUT); digitalWrite(19,1); // Untuk Memberi Tegangan 5 Volt ke RTC
}
void loop() {
delay(100);
t1= digitalRead(ta);
t2= digitalRead(tb);
t3= digitalRead(tc);
if (k==1) {
Wire.beginTransmission(DS1307_ADDRESS);
byte zero = 0x00;
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
d = bcdToDec(Wire.read());
mn = bcdToDec(Wire.read());
j = bcdToDec(Wire.read() & 0b111111); //24 hour time
hk = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
tg = bcdToDec(Wire.read());
bl = bcdToDec(Wire.read());
th = bcdToDec(Wire.read());
j1= j/10; j2=j%10; m1=mn/10; m2=mn%10; d1=d/10; d2=d%10;
baris1=hari+ " "+ String (tg) +"-"+ String (bl)+"-"+ String (th) ;
baris2= "Jam : "+String (j1)+String (j2) +":"+ String (m1)+String (m2)+":"+String (d1)+String (d2) ;
if (t1 ==1 && lt1 == false) {
ct1+=1; if (ct1>19) {k=2;tm=1;ct1=1; lt1= true ;} }
else ct1=1;
}
if (k==2) {
if (tm==1){
baris1 ="Memasuki Menu ";
baris2 = "Pengaturan Alat ";
ct1 +=1;
if (ct1 > 10) {ct1=0; tm=2; }
}
if (tm==2){
baris1 ="2. Rubah Tanggal ";
baris2 = "3. Merubah Jam ";
if (t2==1 ) {tm=7; }
if (t3==1 ) {tm=3; }
if (t1==0) lt1= false;
}
if (tm==3){
baris1 ="Memasuki Menu ";
baris2 = "Merubah Data Jam ";
ct1 +=1;
if (ct1 > 10) {ct1=0; tm=4; }
}
if (tm==4){
baris1 ="Jam : "+String(j) ;
baris2 ="1.Next 2&3 Atur";
if (t2==1 ) {j+=1; if(j>23) j=0;}
if (t3==1 ) {j-=1; if(j<0) j=23;}
if (t1==1 && lt1== false) { tm=3; lt1=true; }
if (t1==0) lt1= false;
}
if (tm==5){
baris1 ="Menit : "+String(mn) ;
baris2 ="1.Next 2&3 Atur";
if (t2==1 ) {mn+=1; if(mn>59) mn=0;}
if (t3==1 ) {mn-=1; if(mn<0) mn=59;}
if (t1==1 && lt1 == false) { tm=4; lt1=true; }
if (t1==0) lt1= false;
}
if (tm==6){
baris1 ="Data dirubah...?";
baris2= "2. Ya 3. Tidak ";
if (t1==0) lt1= false;
if (t3==1 ){ tm=1;k=1;}
if (t2==1 ) {tm=1;k=1;
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(d));
Wire.write(decToBcd(mn));
Wire.write(decToBcd(j));
Wire.write(decToBcd(hk));
Wire.write(decToBcd(tg));
Wire.write(decToBcd(bl));
Wire.write(decToBcd(th));
Wire.write(zero); //start
Wire.endTransmission();
delay (100);
}}
if (tm==7){
baris1 ="Memasuki Menu ";
baris2 = "Merubah Tanggal";
ct1 +=1;
if (ct1 > 10) {ct1=0; tm=8; }
}
if (tm==8){
baris1 ="Tanggal : "+ String(tg) ;
baris2 ="1.Next 2&3 Atur";
if (t2==1 ) {tg+=1; if(tg>31) tg=1;}
if (t3==1 ) {tg-=1; if(tg<1) tg=31;}
if (t1==1 && lt1 == false) { tm=9; lt1=true; }
if (t1==0) lt1= false;
}
if (tm==9){
baris1 = "Bulan : "+ String(bl) ;
baris2 = "1.Next 2&3 Atur";
if (t2==1 ) {bl+=1; if(bl>12) bl=1;}
if (t3==1 ) {bl-=1; if(bl<1) bl=12;}
if (t1==1 && lt1 == false) { tm=10; lt1=true; }
if (t1==0) lt1= false;
}
if (tm==10){
baris1 = "Tahun : "+ String(th) ;
baris2 = "1.Next 2&3 Atur";
if (t2==1 ) {th+=1; if(th>99) th=1;}
if (t3==1 ) {th-=1; if(th<1) th=99;}
if (t1==1 && lt1 == false) { tm=11; lt1=true; }
if (t1==0) lt1= false;
}
if (tm==11){
baris1 = "Hari : "+ String(hari) ;
baris2 = "1.Next 2&3 Atur";
if (t2==1 ) {hk +=1; if(hk>6 ) hk=1;}
if (t3==1 ) {hk -=1; if(hk<1) hk=6;}
if (t1==1 && lt1 == false) { tm=6; lt1=true; }
if (t1==0) lt1= false;
}
delay (155);
} // END OF K2
if(b1!= baris1 || b2!=baris2 ) {
b1= baris1 ; b2=baris2 ;
lcd.clear() ;
lcd.setCursor(0,0);
lcd.print(b1);
lcd.setCursor(0,1);
lcd.print(b2);
}
if (hk==0) hari="Minggu" ;
if (hk==1) hari="Senin" ;
if (hk==2) hari="Selasa" ;
if (hk==3) hari="Rabu" ;
if (hk==4) hari="Kamis" ;
if (hk==5) hari="Jum'at" ;
if (hk==6) hari="Saptu" ;
// end loop
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
--------------------------------------------------------------------------------------
Tidak ada komentar:
Posting Komentar