bermain RTC dengan code vision

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

http://www.kei-elektronik.com/

Iklan

AVR SERIAL ATMEGA16 + BORLAND DELPHI

apdcomport merupakan component delphi untuk komunukasi serial, dapat di download di http://sourceforge.net/projects/tpapro.

dibandingkan dengan mscomm dan comport. apdcomport selangkah lebih maju, dan bisa dikatakan keren. tapi disini saya akan menggunakan komponen comport dan apdcomport.

agar cepat bisa mengerti langsung saja kita buat project.

jam digital menggunakan rtc ds1307 terhubung dengan komputer menggunakan component comport dan apdcomport.

tampilan program delphi anu teu acan jadi.

ApdComPort1.Open:=false;// digunakan untuk disconect serial

ApdComPort1.Open:=true; //digunakan untuk connect serial

ApdComPort1.Output:=’A’  //digunakan untuk mengeluarkan data ke serial

hardwarena pake mikrokontroler atmega16+rtc ds1307

tah ieu tampilan program delphi anu tos janten.

download kabeh source codena. keheula keur di jieun uingna keur streesssss euy. bagi bagi atuh euy mun nu boga project teh.

wassalam

dhani nugraha.

lcd rtc ds1307 atmega8 avr gcc

rtclcd

#define F_CPU 8000000UL
#include “util/delay.h”
#include “stdlib.h”
#include “avr/io.h”
#include “avr/pgmspace.h”
#include “avr/interrupt.h”
#include “avr/wdt.h”
//============================================================

#define ddr_scl_hi DDRC|=(1<<0)     //I2C eeprom & rtc
#define ddr_scl_lo DDRC&=~(1<<0)
#define scl_hi PORTC|=(1<<0)
#define scl_lo PORTC&=~(1<<0)
#define ddr_sda_hi DDRC|=(1<<1)
#define ddr_sda_lo DDRC&=~(1<<1)
#define sda_hi PORTC|=(1<<1)
#define sda_lo PORTC&=~(1<<1)

#define ACK        1
#define NO_ACK    0

#define LCD_RS_HI PORTB|=(1<<0)       //LCD
#define LCD_RS_LO PORTB&=~(1<<0)

#define LCD_EN_HI PORTB|=(1<<1)
#define LCD_EN_LO PORTB&=~(1<<1)

#define LCD_D4_HI PORTB|=(1<<4)
#define LCD_D4_LO PORTB&=~(1<<4)
#define LCD_D5_HI PORTB|=(1<<5)
#define LCD_D5_LO PORTB&=~(1<<5)
#define LCD_D6_HI PORTB|=(1<<6)
#define LCD_D6_LO PORTB&=~(1<<6)
#define LCD_D7_HI PORTB|=(1<<7)
#define LCD_D7_LO PORTB&=~(1<<7)

volatile unsigned char second,minute,hour,day,date,month,year;
const char hextab[]=”0123456789ABCDEF”;
//================================================================SERIAL

#define line1 0x00
#define line2 0x40

//===============================================================
// lcd
//==================================================================
void LCD_STROBE()
{
LCD_EN_HI;
LCD_EN_LO;
}

void lcd_write(unsigned char datalcd)
{
if(datalcd & 0x80) {LCD_D7_HI;} else {LCD_D7_LO;}
if(datalcd & 0x40) {LCD_D6_HI;} else {LCD_D6_LO;}
if(datalcd & 0x20) {LCD_D5_HI;} else {LCD_D5_LO;}
if(datalcd & 0x10) {LCD_D4_HI;} else {LCD_D4_LO;}
LCD_STROBE();
if(datalcd & 0x08) {LCD_D7_HI;} else {LCD_D7_LO;}
if(datalcd & 0x04) {LCD_D6_HI;} else {LCD_D6_LO;}
if(datalcd & 0x02) {LCD_D5_HI;} else {LCD_D5_LO;}
if(datalcd & 0x01) {LCD_D4_HI;} else {LCD_D4_LO;}
LCD_STROBE();
_delay_ms(5);
}

void lcd_clrscr()
{
LCD_RS_LO;
lcd_write(0x1);
_delay_ms(2);
}

void lcd_putc(unsigned char c)
{
LCD_RS_HI;
lcd_write(c);
}

void lcd_puts(const char *s)
{
register char c;

while ( (c = *s++) ) {
lcd_putc(c);
}
}

void lcd_puts_pp(const char *progmem_s)
{
register char c;
for(;;)
{
c = pgm_read_byte(progmem_s++);
if(c==0) break;
lcd_putc(c);
}
}

void lcd_goto(unsigned char poslcd)
{
LCD_RS_LO;
lcd_write(0x80 + poslcd);
}

void initlcd()
{
LCD_RS_LO;
_delay_ms(15);
LCD_D4_HI;
LCD_D5_HI;
LCD_STROBE();
_delay_ms(5);

LCD_STROBE();
_delay_us(100);
LCD_STROBE();
_delay_ms(5);
LCD_D4_LO;
LCD_STROBE();
_delay_us(40);
lcd_write(0x28);
lcd_write(0x0C);
lcd_write(0x06);
lcd_write(0x01);
_delay_ms(10);
}
//=============================================
//i2c manual
//============================================
void i2c_start()
{
wdt_reset();

ddr_scl_hi;
ddr_sda_hi;
scl_hi;
sda_hi;
_delay_us(5);
sda_lo;
_delay_us(5);
scl_lo;
_delay_us(5);
}

void i2c_stop()
{
wdt_reset();

ddr_sda_hi;
sda_lo;
_delay_us(5);
scl_hi;
_delay_us(5);
sda_hi;
_delay_us(5);
}

void i2c_transmit_byte(unsigned char datai2c)
{
unsigned char indexi2c;
ddr_scl_hi;
ddr_sda_hi;
for(indexi2c = 8; indexi2c > 0; indexi2c–)
{
wdt_reset();

if(datai2c & (1 << (indexi2c – 1)))
{
sda_hi;
}
else
{
sda_lo;
}
scl_hi;
_delay_us(5);
scl_lo;
}
scl_hi;
_delay_us(5);
scl_lo;
_delay_us(5);
}

unsigned char i2c_receive_byte(unsigned char reply)
{
unsigned char indexi2c;
unsigned char datai2c=0;
ddr_sda_lo;
ddr_scl_hi;
scl_lo;

for(indexi2c = 8; indexi2c > 0; indexi2c–)
{
wdt_reset();

scl_hi;
_delay_us(5);
if(bit_is_set(PINC,1))
{datai2c |= (1 << (indexi2c – 1));}
else
{datai2c&= ~(1 << (indexi2c – 1));}
scl_lo;
_delay_us(5);
}
ddr_sda_hi;
if(reply)
{sda_lo;}
else
{sda_hi;}
_delay_us(5);
scl_hi;
_delay_us(5);
scl_lo;
_delay_us(5);
return datai2c;
}

char dec2bcd(char num)
{
return ((num/10 * 16) + (num % 10));
}

char bcd2dec(char num)
{
return ((num/16 * 10) + (num % 16));
}

void dectobcdrtc()
{
wdt_reset();

hour=dec2bcd(hour);
minute=dec2bcd(minute);
second=dec2bcd(second);
date=dec2bcd(date);
month=dec2bcd(month);
year=dec2bcd(year);
}
void bcdtodecrtc()
{
// hour  =((hour & 0xF0) >> 4)*10 + (hour & 0x0F);
wdt_reset();

hour=bcd2dec(hour);
minute=bcd2dec(minute);
second=bcd2dec(second);
date=bcd2dec(date);
month=bcd2dec(month);
year=bcd2dec(year);
//minute  =((minute & 0xF0) >> 4)*10 + (minute & 0x0F);
//second  =((second & 0xF0) >> 4)*10 + (second & 0x0F);
//date  =((date & 0xF0) >> 4)*10 + (date & 0x0F);
//month  =((month & 0xF0) >> 4)*10 + (month & 0x0F);
//year  =((year & 0xF0) >> 4)*10 + (year & 0x0F);
}

void lcdhex(unsigned char datana)
{
unsigned char tempbyte;
tempbyte=datana & 0xf0;
tempbyte=tempbyte >> 4;
lcd_putc(hextab[tempbyte]);
tempbyte=datana & 0x0f;
lcd_putc(hextab[tempbyte]);
}

void rtc_lcd()
{
lcd_goto(line1);
lcdhex(hour);
lcd_putc(‘:’);
lcdhex(minute);
lcd_putc(‘:’);
lcdhex(second);
lcd_goto(line2);
lcdhex(date);
lcd_putc(‘:’);
lcdhex(month);
lcd_putc(‘:’);
lcdhex(year);
}

void rtc_uart()
{
lcd_goto(line1);
lcd_putc((hour / 10)   + 48);
lcd_putc((hour % 10)   + 48);
lcd_putc(58);
lcd_putc((minute / 10)   + 48);
lcd_putc((minute % 10)   + 48);
lcd_putc(58);
lcd_putc((second / 10)   + 48);
lcd_putc((second % 10)   + 48);

lcd_goto(line2);
lcd_putc((date / 10)   + 48);
lcd_putc((date % 10)   + 48);
lcd_putc(58);
lcd_putc((month / 10)   + 48);
lcd_putc((month % 10)   + 48);
lcd_putc(58);
lcd_putc((year / 10)   + 48);
lcd_putc((year % 10)   + 48);

}

void gettimertc()
{
i2c_start();
i2c_transmit_byte(0xd0);
i2c_transmit_byte(0);
i2c_start();
i2c_transmit_byte(0xd1);
second=i2c_receive_byte(ACK);
minute=i2c_receive_byte(ACK);
hour=i2c_receive_byte(ACK);
day=i2c_receive_byte(ACK);
date=i2c_receive_byte(ACK);
month=i2c_receive_byte(ACK);
year=i2c_receive_byte(NO_ACK);
i2c_stop();
_delay_ms(10);
//bcdtodecrtc();

}

void settimertc()
{
dectobcdrtc();
i2c_start();
i2c_transmit_byte(0xd0);
i2c_transmit_byte(0);
i2c_transmit_byte(second);
i2c_transmit_byte(minute);
i2c_transmit_byte(hour);
i2c_transmit_byte(0);
i2c_transmit_byte(date);
i2c_transmit_byte(month);
i2c_transmit_byte(year);
i2c_stop();
_delay_ms(10);

}

int main()
{
DDRB=255;
initlcd();
for(;;)
{
gettimertc();
rtc_lcd();
_delay_ms(500);
}
}


jika ada masalah dengan rtc ds1307, biasanya penyebabnya gara gara crsytalnya, ganti saja sama crystal jam weker, fungsi lcd yang dibuat, lumayan enak, manis. soalnya port yang terhubung ke lcd bisa bebas. tidak seperti compiler yang heheh. ga usah disebut lah.

avr gcc merupakan c garis keras, soalnya kita mengotak ngatik register sendiri, jadi kalo kita terbiasa dengan c garis keras, lumayan besar manfaatnya. nanti bisa kamu rasain sendiri.

RTC DS1307 PAKAI CRYSTAL JAM WEKER

Satu hari ngoprek-ngoprek IC rtc DS 1307, ternyata sangat memusingkan,  masalahnya ada pada crystal yang dipakainya yaitu crystal DS1307 32,768KHz, akhirnya karena ngga jalan jalan, JAM WEKER pun jadi korban, di cabutlat crytal 32,768KHz dari jam weker,

Dan ketika crystal tersebut di pasangkan ke IC rtc ds1307, ternyata rtc nya pun jalan. HIdup jam weker.

rtc