Tuesday, July 14, 2009

ATmega 32 & DS1307 Digital Clock

This projects called Digital Clock used ATmega 32 and RTC DS 1307.

FEATURES OF DS1307
1. Real time clock counts seconds,minutes,hours, date of month,moth, day of week and year with leap year compensation valid up to 2100.
2. 56 byte nonvolatile RAM for general data storage
3. 2-wrire interface (I2C)
4. Automatic power fail detect
5. Comsumes less than 500 nA for battery back-up at 25′C
With this features, DS 1307 is great rtc can we use to build a perfect digital clock.


Here is the schematic for DS1307 Connection to ATmega 32:









The clock display used LCD, C Port for LCD, B.0 Port for SCL and B.1 Port for SDA Port 0.

Below is the sourcecode to read RTC DS1307 used CodeVision AVR Evaluation 2.03.9

Source 1 :

**************************************************************

#include

// I2C Bus functions
#asm
.equ __i2c_port=0×18 ;PORTB
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include

// Standard Input/Output functions
#include
#include
#define ADDA_ADDR 0×90
#define EEPROM_ADDR 0xA0
#define RTC_ADDR 0xD0
unsigned char data_rtc[8];
unsigned char kata1[16];
unsigned char kata2[16];
unsigned char kata3[16];
unsigned char kata4[16];
unsigned char kata5[16];
unsigned char kata6[16];

// Declare your global variables here

unsigned char bcd2dec(unsigned char input){
unsigned char tmp_data, tmp1;

tmp_data = input;
tmp1 = tmp_data % 16;
if (tmp_data > 15) tmp_data = tmp_data / 16;
else tmp_data = 0;
tmp_data = (tmp_data * 10)+tmp1;
return tmp_data;
}

void read_rtc(void){
unsigned char i, tmp_data;

i2c_start();
i2c_write(RTC_ADDR);
i2c_write(0);
i2c_stop();
i2c_start();
i2c_write(RTC_ADDR | 1);
for (i=0; i<6; tmp_data =" bcd2dec(i2c_read(1));" tmp_data =" bcd2dec(i2c_read(0));">

unsigned char dec2bcd(unsigned char input){
unsigned char tmp_data;

if (input > 9) tmp_data = ((input / 10)*16) + (input % 10);
else
tmp_data = input;
return tmp_data;
}

void write_rtc(unsigned char alamat, unsigned char data){
i2c_start();
i2c_write(RTC_ADDR);
i2c_write(alamat);
if (alamat <>

unsigned char read_nvram(unsigned char alamat){
unsigned char tmp_data;

i2c_start();
i2c_write(RTC_ADDR);
i2c_write(alamat);
i2c_stop();
i2c_start();
i2c_write(RTC_ADDR | 1);
tmp_data = i2c_read(0);
i2c_stop();
return tmp_data;
}

unsigned char read_EEPROM(unsigned char alamat){
unsigned char data;
i2c_start();
i2c_write(EEPROM_ADDR);
i2c_write(alamat);
i2c_start();
i2c_write(EEPROM_ADDR | 1);
data = i2c_read(0);
i2c_stop();
return data;
}

void write_EEPROM(unsigned char alamat, unsigned char nilai){
i2c_start();
i2c_write(EEPROM_ADDR);
i2c_write(alamat);
i2c_write(nilai);
i2c_stop();

delay_ms(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=0×00;
UCSRB=0×18;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;

// I2C Bus initialization
i2c_init();

// LCD module initialization
lcd_init(16);
putchar(’p');
while (1)
{
read_rtc();
//0123456789ABCDEF
sprintf(kata1,”%2d:”,data_rtc[2],);
sprintf(kata2,”%2d:”,data_rtc[1],);
sprintf(kata3,”%2d”,data_rtc[0],);
sprintf(kata4,”%2d-”,data_rtc[4],);
sprintf(kata5,”%2d-”,data_rtc[5],);
sprintf(kata6,”%2d”,data_rtc[6],);
lcd_clear();
lcd_gotoxy(3,0);lcd_puts(kata1);
lcd_gotoxy(6,0);lcd_puts(kata2);
lcd_gotoxy(9,0);lcd_puts(kata3);
lcd_gotoxy(3,1);lcd_puts(kata4);
lcd_gotoxy(6,1);lcd_puts(kata5);
lcd_gotoxy(9,1);lcd_puts(kata6);
delay_ms(100);

}
}

Source 2 :

**************************************************************

#include

// I2C Bus functions
#asm
.equ __i2c_port=0×18 ;PORTB
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include

// Standard Input/Output functions
#include
#include
#define ADDA_ADDR 0×90
#define EEPROM_ADDR 0xA0
#define RTC_ADDR 0xD0
unsigned char data_rtc[8];
unsigned char data_t[16];

// Declare your global variables here

unsigned char bcd2dec(unsigned char input){
unsigned char tmp_data, tmp1;

tmp_data = input;
tmp1 = tmp_data % 16;
if (tmp_data > 15) tmp_data = tmp_data / 16;
else tmp_data = 0;
tmp_data = (tmp_data * 10)+tmp1;
return tmp_data;
}

void read_rtc(void){
unsigned char i, tmp_data,y;

i2c_start();
i2c_write(RTC_ADDR);
i2c_write(0);
i2c_stop();
i2c_start();
i2c_write(RTC_ADDR | 1);
for (i=0; i<6; tmp_data =" bcd2dec(i2c_read(1));" tmp_data =" bcd2dec(i2c_read(0));">

unsigned char dec2bcd(unsigned char input){
unsigned char tmp_data;

if (input > 9) tmp_data = ((input / 10)*16) + (input % 10);
else
tmp_data = input;
return tmp_data;
}

void write_rtc(unsigned char alamat, unsigned char data){
i2c_start();
i2c_write(RTC_ADDR);
i2c_write(alamat);
if (alamat <>

unsigned char read_nvram(unsigned char alamat){
unsigned char tmp_data;

i2c_start();
i2c_write(RTC_ADDR);
i2c_write(alamat);
i2c_stop();
i2c_start();
i2c_write(RTC_ADDR | 1);
tmp_data = i2c_read(0);
i2c_stop();
return tmp_data;
}

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=0×00;
UCSRB=0×18;
UCSRC=0×86;
UBRRH=0×00;
UBRRL=0×47;

// I2C Bus initialization
i2c_init();

// LCD module initialization
lcd_init(16);
while (1)
{
read_rtc();
//0123456789ABCDEF
data_t[0]=(data_rtc[2]/10)|0×30;
data_t[1]=(data_rtc[2]%10)|0×30;
data_t[2]=(data_rtc[1]/10)|0×30;
data_t[3]=(data_rtc[1]%10)|0×30;
data_t[4]=(data_rtc[0]/10)|0×30;
data_t[5]=(data_rtc[0]%10)|0×30;

data_t[6]=(data_rtc[4]/10)|0×30;
data_t[7]=(data_rtc[4]%10)|0×30;
data_t[8]=(data_rtc[5]/10)|0×30;
data_t[9]=(data_rtc[5]%10)|0×30;
data_t[10]=(data_rtc[6]/10)|0×30;
data_t[11]=(data_rtc[6]%10)|0×30;

data_t[12]=(data_rtc[3]/10)|0×30;
data_t[13]=(data_rtc[3]%10)|0×30;

lcd_clear();
lcd_gotoxy(3,0);lcd_putchar(data_t[0]);
lcd_gotoxy(4,0);lcd_putchar(data_t[1]);
lcd_gotoxy(5,0);lcd_putchar(’:');
lcd_gotoxy(6,0);lcd_putchar(data_t[2]);
lcd_gotoxy(7,0);lcd_putchar(data_t[3]);
lcd_gotoxy(8,0);lcd_putchar(’:');
lcd_gotoxy(9,0);lcd_putchar(data_t[4]);
lcd_gotoxy(10,0);lcd_putchar(data_t[5]);

lcd_gotoxy(0,1);lcd_putchar(data_t[12]);
lcd_gotoxy(1,1);lcd_putchar(data_t[13]);

lcd_gotoxy(3,1);lcd_putchar(data_t[6]);
lcd_gotoxy(4,1);lcd_putchar(data_t[7]);
lcd_gotoxy(5,1);lcd_putchar(’:');
lcd_gotoxy(6,1);lcd_putchar(data_t[8]);
lcd_gotoxy(7,1);lcd_putchar(data_t[9]);
lcd_gotoxy(8,1);lcd_putchar(’:');
lcd_gotoxy(9,1);lcd_putchar(’2′);
lcd_gotoxy(10,1);lcd_putchar(’0′);
lcd_gotoxy(11,1);lcd_putchar(data_t[10]);
lcd_gotoxy(12,1);lcd_putchar(data_t[11]);

delay_ms(200);

}
}

No comments:

Post a Comment