banyak banget yang pakai code vision. saya juga gaul atuh demi mendapatkan pasar. ayo kita bermain main RTC DS 1307 dengan code vision. itu sangat gampang sekali.
mikro yang dipakai atmega 16, xtal 11.0592
ini simulasi nya di proteus
untuk contoh source nya ini
#include <mega16.h>
#include <delay.h>
#asm
.equ __i2c_port=0x15 ;PORTC
.equ __sda_bit=1
.equ __scl_bit=0
#endasm
#include <i2c.h>
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
#include <stdio.h>
unsigned char hour,minute,second,date,month,year,day;
// Declare your global variables here
unsigned char dec2bcd(unsigned char num)
{
return ((num/10 * 16) + (num % 10));
}
unsigned char bcd2dec(unsigned char num)
{
return ((num/16 * 10) + (num % 16));
}
void dectobcdrtc(void)
{
hour=dec2bcd(hour);
minute=dec2bcd(minute);
second=dec2bcd(second);
date=dec2bcd(date);
month=dec2bcd(month);
year=dec2bcd(year);
}
void bcdtodecrtc(void)
{
hour=bcd2dec(hour);
minute=bcd2dec(minute);
second=bcd2dec(second);
date=bcd2dec(date);
month=bcd2dec(month);
year=bcd2dec(year);
}
//=================================================
void gettimertc(void)
{
i2c_start();
i2c_write(0xd0);
i2c_write(0);
i2c_stop();
i2c_start();
i2c_write(0xd1);
second=i2c_read(1);
minute=i2c_read(1);
hour=i2c_read(1);
day=i2c_read(1);
date=i2c_read(1);
month=i2c_read(1);
year=i2c_read(0);
i2c_stop();
delay_ms(10);
bcdtodecrtc();
}
void settimertc(void)
{
dectobcdrtc();
i2c_start();
i2c_write(0xd0);
i2c_write(0);
i2c_write(second & 0x7F);
i2c_write(minute);
i2c_write(hour);
i2c_write(day);
i2c_write(date);
i2c_write(month);
i2c_write(year);
i2c_stop();
delay_ms(10);
}
void rtc_lcd(void)
{
lcd_gotoxy(0,0);
lcd_putchar((hour / 10) + 48);
lcd_putchar((hour % 10) + 48);
lcd_putchar(58);
lcd_putchar((minute / 10) + 48);
lcd_putchar((minute % 10) + 48);
lcd_putchar(58);
lcd_putchar((second / 10) + 48);
lcd_putchar((second % 10) + 48);
lcd_gotoxy(0,1);
lcd_putchar((date / 10) + 48);
lcd_putchar((date % 10) + 48);
lcd_putchar(58);
lcd_putchar((month / 10) + 48);
lcd_putchar((month % 10) + 48);
lcd_putchar(58);
lcd_putchar((year / 10) + 48);
lcd_putchar((year % 10) + 48);
}
void rtc_uart(void)
{
putchar((hour / 10) + 48);
putchar((hour % 10) + 48);
putchar(58);
putchar((minute / 10) + 48);
putchar((minute % 10) + 48);
putchar(58);
putchar((second / 10) + 48);
putchar((second % 10) + 48);
putchar(13);
putchar(10);
putchar((date / 10) + 48);
putchar((date % 10) + 48);
putchar(58);
putchar((month / 10) + 48);
putchar((month % 10) + 48);
putchar(58);
putchar((year / 10) + 48);
putchar((year % 10) + 48);
putchar(13);
putchar(10);
}
void main(void)
{
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47;
// I2C Bus initialization
i2c_init();
// LCD module initialization
lcd_init(16);
DDRA.0=0;
DDRA.1=0;
DDRA.2=0;
DDRA.3=0;
PORTA.0=1;
PORTA.1=1;
PORTA.2=1;
PORTA.3=1;
while (1)
{
// Place your code here
gettimertc();
rtc_lcd();
rtc_uart();
//delay_ms(1000);
if(PINA.0==0)
{
gettimertc();
hour++;
if(hour>23)hour=0;
settimertc();
}
if(PINA.1==0)
{
gettimertc();
minute++;
if(minute>59)minute=0;
settimertc();
}
};
}
bisa setting jam dan menit. sok sudah complete tah. kalo ga jalan dengan rtc ds1307. itu biasanya masalahnya:
1. RTC BELUM DISET
2. IC DS1307 RUSAK’
3. XTAL RTC JELEK (TIDAK MEMENUHI DATASHEET).
4. SESUAIKAN PROGRAM YANG DIBUAT DENGAN FUSEBIT MICRCONTROLERNYA.
sekian dari saya. anda maju, saya senang. anda kaya saya senang.
trims
dhani nugraha