Home > Komputer, Robot, Tips & Trik > Bahasa C untuk jam digital berbasis LCD

Bahasa C untuk jam digital berbasis LCD

Jam digital adalah jam yang ditampilkan dalam format digital, penampil menggunakan LCD karakter 16×2. saat ini banyak digunakan Real time clock IC DS1307 produksi Maxim. DS1307 adalah IC serial RTC dimana alamat dan data ditransmisikan secara serial melalui sebuah jalur data dari dua arah I2C. karena menggunakan jalur data I2C, maka hanya memerlukan 2 buah pin untuk komunikasi yaitu pin untuk data dan pin untuk sinyal clock.

Tulisan disini tidak membahas secara detil pembuatanya, tapi hanya sedikit memberikan contoh penulisan program menggunakan bahasa c pada aplikasi jam digital berbasis LCD karakter 16×2

berikut contoh penulisan program bahasa c

———————————————————————————————————————–

#include “LCDM.h”

#define sda p3_2
#define sda_d pd3_2
#define scl p3_3
#define scl_d pd3_3
#define see 0xa0
#define DS1307 0xd0
#define seconds 0x00
#define minutes 0x01
#define hours 0x02
#define day 0x03
#define date 0x04
#define month 0x05
#define year 0x06
#define control 0x07

const unsigned char msg1[] = “digital Clock”;
const Zero_offset = 5;

unsigned char Detik, Menit, Jam;
unsigned char Hari, Tanggal, Bulan, Tahun;

void AturClock_Ext();
void Atur_sfr();

void Atur_sfr(){

 p3         = 0xff;
 pd3        = 0x0f;
 prcr    = 4;
 pd0        = 0xf1;
 prcr    = 0;
 adcon0  = 0x8e;
 adcon1  = 0x30;
 adcon2  = 0x01;
 adst    = 1;

}

void AturClock_Ext(){

Sam("FCLR I");/*Nonaktifkan Interrupt*/
prc0 = 1;  /*Write protect dimatikan*/
cm13 = 1;  /*P46 dan P47 sebagai Xin-Xout*/
cm15 = 1;  /*Kapasitas drive Xin-Xout dipilih HIGH*/
cm05 = 0;  /*Aktifkan clock utama */
cm16 = 0;  /*frkuensi clock tanpa pembagian*/
cm17 = 0;  /*frkuensi clock tanpa pembagian*/
cm06 = 0;  /*CM16 dan CM17 enable*/
asm("nop");
asm("nop");
asm("nop");
asm("nop");
ocd2 = 0;  /*Dipilih clock utama(dari crystal)*/
prc0 = 0;  /*Write protect diaktifkan*/

}

void Wait1uS(void){

asm("nop");    /* +1cycle */
asm("nop");    /* +1cycle = 16cycle */
asm("nop");    /* +1cycle */
asm("nop");    /* +1cycle = 16cycle */
asm("nop");    /* +1cycle */
asm("nop");    /* +1cycle = 16cycle */

}

void delay_us(unsigned int x){

 while(x-- > 0) Wait1uS();

}

void Init_Bus_I2C(){

 scl_d = 0;
 sda_d = 0;

}

void Start_I2C(){

scl       = 0;
scl_d     = 1;
delay_us(1);
sda        = 1;
sda_d    = 1;
delay_us(2);
scl        = 1;
delay_us(2);
sda        = 0;
sda_d    = 1;
delay_us(4);
scl        = 0;

}

void Stop_I2C(){

scl        = 0;
scl_d    = 1;
delay_us(1);
sda        = 0;
sda_d    = 1;
delay_us(1);
scl        = 1;
delay_us(2);
sda        = 1;
sda_d    = 1;
delay_us(2);
scl        = 0;

}

void Clock_I2C(){

scl_d = 1;
scl = 0;
delay_us(5);
scl = 1;
delay_us(5);
scl = 0;

}

unsigned char Out_I2C_8Bit(unsigned char data){

 unsigned char i = 0x80;
 unsigned char ret = 0;
 while (i){
sda        = 0;
if (data & i) sda_d = 0;
else {
sda_d = 1;
asm("nop");
asm("nop");
asm("nop");
}
delay_us(5);
scl = 1;
delay_us(3);
scl = 0;
i >>= 1;
delay_us(1);
}
sda_d = 0;
delay_us(5);
scl   = 1;
if (sda) ret = 1;
delay_us(5);
scl = 0;
delay_us(5);

}

unsigned char In_I2C_8Bit(){

 unsigned char i = 0x80;
 unsigned char Temp = 0x00;
 while (i){
Temp = Temp << 1;
scl = 1;
if (sda == 0) Temp = Temp & 0xfe;
else Temp = Temp | 0x01;
i = i >> 1;
scl = 0;
 }
 return(Temp);

}

unsigned char Write_I2C(unsigned char ID, unsigned char Addr, unsigned char Data){

 unsigned char Temp;
 unsigned char ret;
 Start_I2C();
 while(1){
 if ((ret = Out_I2C_8Bit(ID &0xfe)) == 1) break;
 if ((ret = Out_I2C_8Bit(Addr)) == 1) break;
 if ((ret = Out_I2C_8Bit(Data)) == 1) break;
 break;
 }
 Stop_I2C();
 return(ret);

}

unsigned char Read_I2C(unsigned char ID, unsigned char Addr){

 unsigned char i, ret;
 Start_I2C();
 while(1){
 if ((ret = Out_I2C_8Bit(ID&0xfe)) == 1) break;
 if ((ret = Out_I2C_8Bit(Addr)) == 1) break;
 Start_I2C();
 if ((ret = Out_I2C_8Bit(ID|0x01)) == 1) break;
 ret = In_I2C_8Bit();
 break;
 }
 Stop_I2C();
 return(ret);

}

void Init_DS1307(){

 Write_I2C(DS1307,0x07,0x10);

}

void Set_Waktu(unsigned char j, unsigned char m, unsigned char d){

 Write_I2C(DS1307,seconds,d);
 Write_I2C(DS1307,minutes,m);
 Write_I2C(DS1307,hours,j);

}

unsigned char BCD2Bin(unsigned char x){

 return ((x >> 4) * 10) + (x & 0x0f);

}

unsigned char Bin2BCD(unsigned char x){

 return ((x / 10) << 4) + (x % 10);

}

void Ambil_Waktu(){

 Detik   = BCD2Bin(Read_I2C(DS1307,seconds) & 0x7f);
 Menit   = BCD2Bin(Read_I2C(DS1307,minutes) & 0x7f);
 Jam     = BCD2Bin(Read_I2C(DS1307,hours) & 0x3f);
 Tanggal = BCD2Bin(Read_I2C(DS1307,date) & 0x3f);
 Bulan   = BCD2Bin(Read_I2C(DS1307,month) & 0x1f);
 Tahun   = BCD2Bin(Read_I2C(DS1307,year) & 0xff);
 Hari    = BCD2Bin(Read_I2C(DS1307,day) & 0x0f);

}

void Tampilkan_Waktu(){

 Baris1();
 GeserCursor_Kanan();
 GeserCursor_Kanan();
 GeserCursor_Kanan();
 GeserCursor_Kanan();
 Kirim_Karakter(Jam/10+'0');
 Kirim_Karakter(Jam%10+'0');
 Kirim_Karakter(':');
 Kirim_Karakter(Menit/10+'0');
 Kirim_Karakter(Menit%10+'0');
 Kirim_Karakter(':');
 Kirim_Karakter(Detik/10+'0');
 Kirim_Karakter(Detik%10+'0');

}

void main(void){

 unsigned int i;
 AturClock_Ext();
 Atur_sfr();
 Init_LCD();
 Init_Bus_I2C();
 Init_DS1307();
 Set_Waktu(0x10,0x15,0x00);
 while (1){
Ambil_Waktu();
Tampilkan_Waktu();
for (i=0;i<5;i++) Delay(50000);
 }

}

——————————————————————————————————————–

jika ada yang kurang dalam penulisan program ini mohon dimaafkan dikarenakan keterbatasan ilmu yang dimiliki oleh penulis. dan masih banyak kekurangan disini, untuk itu penulis mohon maaf yang sebesar-besarnya. terimakasih & semoga bermanfaat.

by gp
#include “LCDM.h”#define sda p3_2
#define sda_d pd3_2
#define scl p3_3
#define scl_d pd3_3
#define see 0xa0
#define DS1307 0xd0
#define seconds 0x00
#define minutes 0x01
#define hours 0x02
#define day 0x03
#define date 0x04
#define month 0x05
#define year 0x06
#define control 0x07const unsigned char msg1[] = “digital Clock”;
const Zero_offset = 5;unsigned char Detik, Menit, Jam;
unsigned char Hari, Tanggal, Bulan, Tahun;void AturClock_Ext();
void Atur_sfr();void Atur_sfr(){
p3 = 0xff;
pd3 = 0x0f;
prcr = 4;
pd0 = 0xf1;
prcr = 0;
adcon0 = 0x8e;
adcon1 = 0x30;
adcon2 = 0x01;
adst = 1;
}

void AturClock_Ext(){
asm(“FCLR I”); /* Nonaktifkan Interrupt*/
prc0 = 1; /* Write protect dimatikan */
cm13 = 1; /* P46 dan P47 sebagai Xin-Xout */
cm15 = 1; /* Kapasitas drive Xin-Xout dipilih HIGH*/
cm05 = 0; /* Aktifkan clock utama */
cm16 = 0; /* frkuensi clock tanpa pembagian*/
cm17 = 0; /* frkuensi clock tanpa pembagian*/
cm06 = 0; /* CM16 dan CM17 enable */
asm(“nop”);
asm(“nop”);
asm(“nop”);
asm(“nop”);
ocd2 = 0; /* Dipilih clock utama (dari crystal) */
prc0 = 0; /* Write protect diaktifkan */
}

void Wait1uS(void){
asm(“nop”); /* +1cycle */
asm(“nop”); /* +1cycle = 16cycle */
asm(“nop”); /* +1cycle */
asm(“nop”); /* +1cycle = 16cycle */
asm(“nop”); /* +1cycle */
asm(“nop”); /* +1cycle = 16cycle */
}

void delay_us(unsigned int x){
while(x– > 0) Wait1uS();
}

void Init_Bus_I2C(){
scl_d = 0;
sda_d = 0;
}

void Start_I2C(){
scl = 0;
scl_d = 1;
delay_us(1);
sda = 1;
sda_d = 1;
delay_us(2);
scl = 1;
delay_us(2);
sda = 0;
sda_d = 1;
delay_us(4);
scl = 0;
}

void Stop_I2C(){
scl = 0;
scl_d = 1;
delay_us(1);
sda = 0;
sda_d = 1;
delay_us(1);
scl = 1;
delay_us(2);
sda = 1;
sda_d = 1;
delay_us(2);
scl = 0;
}

void Clock_I2C(){
scl_d = 1;
scl = 0;
delay_us(5);
scl = 1;
delay_us(5);
scl = 0;
}

unsigned char Out_I2C_8Bit(unsigned char data){
unsigned char i = 0x80;
unsigned char ret = 0;
while (i){
sda = 0;
if (data & i) sda_d = 0;
else {
sda_d = 1;
asm(“nop”);
asm(“nop”);
asm(“nop”);
}
delay_us(5);
scl = 1;
delay_us(3);
scl = 0;
i >>= 1;
delay_us(1);
}
sda_d = 0;
delay_us(5);
scl = 1;
if (sda) ret = 1;
delay_us(5);
scl = 0;
delay_us(5);
}

unsigned char In_I2C_8Bit(){
unsigned char i = 0x80;
unsigned char Temp = 0x00;
while (i){
Temp = Temp << 1;
scl = 1;
if (sda == 0) Temp = Temp & 0xfe;
else Temp = Temp | 0x01;
i = i >> 1;
scl = 0;
}
return(Temp);
}

unsigned char Write_I2C(unsigned char ID, unsigned char Addr, unsigned char Data){
unsigned char Temp;
unsigned char ret;
Start_I2C();
while(1){
if ((ret = Out_I2C_8Bit(ID &0xfe)) == 1) break;
if ((ret = Out_I2C_8Bit(Addr)) == 1) break;
if ((ret = Out_I2C_8Bit(Data)) == 1) break;
break;
}
Stop_I2C();
return(ret);
}

unsigned char Read_I2C(unsigned char ID, unsigned char Addr){
unsigned char i, ret;
Start_I2C();
while(1){
if ((ret = Out_I2C_8Bit(ID&0xfe)) == 1) break;
if ((ret = Out_I2C_8Bit(Addr)) == 1) break;
Start_I2C();
if ((ret = Out_I2C_8Bit(ID|0x01)) == 1) break;
ret = In_I2C_8Bit();
break;
}

Stop_I2C();
return(ret);
}

void Init_DS1307(){
Write_I2C(DS1307,0x07,0x10);
}

void Set_Waktu(unsigned char j, unsigned char m, unsigned char d){
Write_I2C(DS1307,seconds,d);
Write_I2C(DS1307,minutes,m);
Write_I2C(DS1307,hours,j);
}

unsigned char BCD2Bin(unsigned char x){
return ((x >> 4) * 10) + (x & 0x0f);
}

unsigned char Bin2BCD(unsigned char x){
return ((x / 10) << 4) + (x % 10);
}

void Ambil_Waktu(){
Detik = BCD2Bin(Read_I2C(DS1307,seconds) & 0x7f);
Menit = BCD2Bin(Read_I2C(DS1307,minutes) & 0x7f);
Jam = BCD2Bin(Read_I2C(DS1307,hours) & 0x3f);
Tanggal = BCD2Bin(Read_I2C(DS1307,date) & 0x3f);
Bulan = BCD2Bin(Read_I2C(DS1307,month) & 0x1f);
Tahun = BCD2Bin(Read_I2C(DS1307,year) & 0xff);
Hari = BCD2Bin(Read_I2C(DS1307,day) & 0x0f);
}

void Tampilkan_Waktu(){
Baris1();
GeserCursor_Kanan();
GeserCursor_Kanan();
GeserCursor_Kanan();
GeserCursor_Kanan();
Kirim_Karakter(Jam/10+’0′);
Kirim_Karakter(Jam%10+’0′);
Kirim_Karakter(‘:’);
Kirim_Karakter(Menit/10+’0′);
Kirim_Karakter(Menit%10+’0′);
Kirim_Karakter(‘:’);
Kirim_Karakter(Detik/10+’0′);
Kirim_Karakter(Detik%10+’0′);
}

void main(void){
unsigned int i;
AturClock_Ext();
Atur_sfr();
Init_LCD();
Init_Bus_I2C();
Init_DS1307();
Set_Waktu(0x10,0x15,0x00);
while (1){
Ambil_Waktu();
Tampilkan_Waktu();
for (i=0;i<5;i++) Delay(50000);
}
}

  1. September 10, 2009 at 10:38

    thank’s your post, i like this

  2. arief
    June 3, 2010 at 19:50

    aku bisa mnta kirimin prgrmnya ke email aku gak?

    • indragp2009
      June 5, 2010 at 15:54

      boleh, alamatnya?

  3. Adit
    June 7, 2010 at 02:55

    Buat setting timer n read timer ds 1307 pake perintah apa y di micronya..?? ni tampilan pengen saya masukkan k 7 segment..bukan lcd…gmn caranya y..?? maklum pemula.thx

  4. zaqi
    June 9, 2010 at 14:38

    mas bagaimana cara menampilkan hasil konversi counter ke LCD,. kemudian disertai keyped juga sebagai pengontrolnya,.. terimakasih

  5. June 9, 2010 at 21:58

    bagus banget nih artikel boleh minta listing program jam digital dengan menggunakan ATMega8535 beserta kalender digitalnya ga….??? kalo punya kirim aja ke email aku ya…..
    ini alamat emailku
    abayudistira@gmail.com atau abayudistira@yahoo.co.id
    terima kasih sebelumnya

  6. dwi
    June 13, 2010 at 18:09

    Nice Posting,Bro..!!BTW aku bolemnta kirimin prgrmnya ke email aku jg ga?Makasi

  7. bent
    June 21, 2010 at 19:02

    Hmm great.. Bs mnta program keseluruhan? Saya ingin modifikasi untuk bel otomatis. Tp menggunakan DS1302, apakah sama? Saya menggunakan ATMega8535, LCD display 16×2, dan RTC DS1302. Bs mnta tlg krm program nya gak mas ke email taurus.thecrew@gmail.com ?
    Makasih banyak sebelumnya..

  8. hanif
    November 13, 2010 at 22:02

    mas,bisa minta program lgkapnya/?
    umpama program ini di aplikasikan ke lcd nika 3310,kira2 yg dirubah apa aja?
    hanief_nooer@yahoo.com

  9. jimmy
    January 11, 2011 at 21:38

    saya juga minta donk souce codenya kalo boleh
    kirim ke email saya jimmy_chenchen@yahoo.com
    terima kasih sebelumnya

  10. ricco21
    May 27, 2011 at 14:53

    saya mau tnya in di port mana ya?programnya?
    klo boleh saya minta bantuannya,,,cara mensetting programnya pada Ic
    terima kasih🙂

  11. chyko
    May 28, 2011 at 00:29

    minta programnya ke email boleh gak???
    santikopurnomo@yahoo.com

  12. wahyu.renamber@yahoo.com
    November 2, 2011 at 16:26

    aq blh mnta program buat jam digital tdak?

  1. July 17, 2010 at 23:50

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: