Saturday, November 20, 2010

Jam Digital RTC DS1307 ATmega8535

Saya mencoba untuk share tentang pembutan jam digital dengan RTC DS1307, kenapa menggunakan DS1307, karena bila power padam jam tetap jalan dan komunikasi data menggunakan I2C sehingga menghemat pin.
berikut ini source code nya:
#include <mega8535.h>

// I2C Bus functions
#asm
   .equ __i2c_port=0x18 ;PORTB
   .equ __sda_bit=1
   .equ __scl_bit=0
#endasm
#include <delay.h>
#include <bcd.h>
#include <i2c.h>


unsigned char tahun,bulan,tanggal,hari,jam,menit,detik,temp;
// DS1307 Real Time Clock functions
#include <ds1307.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

// External Interrupt 2 service routine  // menampilkan jam secara otomatis melalui fasilitas interupsi
interrupt [EXT_INT2] void ext_int2_isr(void)
{
// Place your code here
    tahun = rtc_read(0x06);
    bulan = rtc_read(0x05);
    tanggal = rtc_read(0x04);
    hari = rtc_read(0x03);
    jam = rtc_read(0x02);
    menit = rtc_read(0x01);
    detik = rtc_read(0x00);
    temp = jam >> 4;
    temp = temp + 0x30;
    lcd_gotoxy(4,0), lcd_putchar(temp);
    temp = jam & 0x0f;
    temp = temp + 0x30;
    lcd_putchar(temp);
    temp = menit >> 4;
    temp = temp + 0x30;
    lcd_gotoxy(7,0), lcd_putchar(temp);
    temp = menit & 0x0f;
    temp = temp + 0x30;
    lcd_putchar(temp);
    temp = detik >> 4;
    temp = temp + 0x30;
    lcd_gotoxy(10,0), lcd_putchar(temp);
    temp = detik & 0x0f;
    temp = temp + 0x30;
    lcd_putchar(temp);
  
    lcd_gotoxy(0,1);
    temp = hari;
    if (temp == 1) lcd_putsf("Ahad            ");
    if (temp == 2) lcd_putsf("Senin           ");
    if (temp == 3) lcd_putsf("Selasa          ");
    if (temp == 4) lcd_putsf("Rabu            ");
    if (temp == 5) lcd_putsf("Kamis           ");
    if (temp == 6) lcd_putsf("Jum'at          ");
    if (temp == 7) lcd_putsf("Sabtu           ");
  
    lcd_gotoxy(7,1);
    temp = tanggal >> 4;
    temp = temp + 0x30;
    lcd_putchar(temp);
    temp = tanggal & 0x0f;
    temp = temp + 0x30;
    lcd_putchar(temp);
  
    lcd_putchar('/');
  
    temp = bulan >> 4;
    temp = temp + 0x30;
    lcd_putchar(temp);
    temp = bulan & 0x0f;
    temp = temp + 0x30;
    lcd_putchar(temp);
  
    lcd_putchar('/');
  
    temp = tahun >> 4;
    temp = temp + 0x30;
    lcd_putchar(temp);
    temp = tahun & 0x0f;
    temp = temp + 0x30;
    lcd_putchar(temp);
    
  
    temp = (tanggal - 1) * 5;
    subuh = pntr[temp + 0];
    duhur = pntr[temp + 1];
    asar = pntr[temp + 2];
    maghrib = pntr[temp + 3];
    isya = pntr[temp + 4];
}

void main(void)
{
PORTA=0x00;
DDRA=0x00;

PORTB=0x00;
DDRB=0xB0;

PORTC=0x03;
DDRC=0x03;

PORTD=0x80;
DDRD=0x80;

TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;



// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: On
// INT1 Mode: Falling Edge
// INT2: On
// INT2 Mode: Falling Edge
GICR|=0xE0;
MCUCR=0x0A;
MCUCSR=0x00;
GIFR=0xE0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// I2C Bus initialization
i2c_init();

// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: On
// Square wave frequency: 1Hz
rtc_init(0,1,0);
temp = rtc_read(0);
if ((temp & 0x80) == 0x80) rtc_write(0,0);

// LCD module initialization
lcd_init(16);

// Global enable interrupts

bulan = rtc_read(0x05);
tanggal = rtc_read(0x04);
jam = rtc_read(0x02);
menit = rtc_read(0x01);
 
#asm("sei")
lcd_gotoxy(0,0), lcd_putsf("      :  :      ");

while (1) ;
}

jam digital akan ditampilkan secara otomatis melalui fasilitas interupsi external yang akan terjadi setiap detik, karena interupsi external dihubungkan ke SQR out dati RTC.
berikut ini gambar rangkaiannya
terimakasih telah berkunjung, bila ada saran untuk perbaikan konten, dipersilahkan...