You are on page 1of 9

임베디드 마이크로프로세서

프로그래밍 실전

제작 : 네 로 테 크
강의 : 김 종 형
Embedded AVR Programming

ATMEGA128의 실전 Ⅱ

1. RTC란
2. DS1302를 이용한 RTC 실습

2006-
2006-04-
04-08 -2-
Embedded AVR Programming

1-1 RTC (Real Time Clock)이란?

⊙ 실시간 시계. 전원 공급이 안 되어도 현재의 시간을 지키는 시계

⊙ DS1302란?

- Trickle charge RTC (세류 충전 계시 칩, 혹은 트리클 충전 계시 칩)

- Trickle => 물방울, 졸졸 흐르는 시내물.

적은 전류로 충전(==> 동작)하는 RTC라고 생각하면 됨

- DS1302는 내부에 여러 개의 데이터 영역을 가지고 있으며, 각각의 영역마다 초, 분, 시, 년,


월, 일 등의 데이터를 기록하고, 자동으로 갱신시켜 줌. 유저는 단지 이 데이터를 읽어와 사용

2006-
2006-04-
04-08 -3-
Embedded AVR Programming

1-2 DS1302를 이용한 RTC

⊙ 핀 정리

1 : VCC2 : 메인 전원용 VCC와 연결


2, 3 : X1, X2 : 32768 크리스털과 연결
4 : GND : 전원 그라운드
5 : #RST : 칩 선택용이니 CS(chip enable)
6 : I/O : 시리얼 데이터를 주고 받는 포트
7 : SCLK : 시리얼 클럭. 데이터를 주고 받을 때 사용하는 클럭
8 : VCC1 : 백업용 배터리를 연결

2006-
2006-04-
04-08 -4-
Embedded AVR Programming

1-2 DS1302를 이용한 RTC

⊙ 회로도
X1

U5
32.768KHz 2 6
3 X1 I/O
X2 1
7 VCC2 VCC
SCLK
5
VCC RST

8 4
D1 VCC1 GND
1N5819
D2 DS1302SN/SOIC
RTC.BAT
VCC.BAT
1N5819 BT1
TL5101

JP12
#BIT.RST 1 2 BIT.IO
#BIT.SLK 3 4
5 6
HEADER/2X3
I2C/RTC

2006-
2006-04-
04-08 -5-
Embedded AVR Programming

1-2 DS1302를 이용한 RTC

⊙ DS1302의 내부 데이터 영역도

2006-
2006-04-
04-08 -6-
Embedded AVR Programming

1-2 DS1302를 이용한 RTC

⊙ 명령 및 어드레스, 데이터 송수신 순서

2006-
2006-04-
04-08 -7-
Embedded AVR Programming

1-3 DS1302를 이용한 RTC 실습 1


☞ DS1302를 이용한 Real Time Clock 를 LCD로 표시하는 프로그램
#include <mega128.h> // timer 0 overflow ISR
#include <stdio.h> interrupt [TIM0_OVF] void timer0_ovf_isr(void)
#include <delay.h> {
#include "am_lcd.h" TCNT0 = 6; // reload for 1ms ticks
#include "am_lcd.c" if(++timecount == 500) // 500ms 마다 업데이트
{
// DS1302 Real Time Clock functions tick1flag = 1;
#asm timecount = 0; // clear for next 500ms
.equ __ds1302_port=0x12 ;PORTD }
.equ __ds1302_io=4 }
.equ __ds1302_sclk=5
.equ __ds1302_rst=2 void main(void)
#endasm {
#include <ds1302.h> unsigned char h,m,s,i;

char rtc_buf[16]; // lcd display temp buffer MCUCR=0xc0; // am_lcd함수 사용시


char lcd_state;
bit tick1flag=0; //Port D
PORTD = 0xff;
DDRD = 0xff;
unsigned int timecount = 0; // global time counter
TCNT0 = 0x00; // reset TCNT0
TCCR0 = 0x04; // count with cpu clock/64 (64/16MHz=4ms)
TIMSK = 0x01; // enable TCNT0 overflow

2006-
2006-04-
04-08 -8-
Embedded AVR Programming

1-3 DS1302를 이용한 RTC 실습 1


☞ DS1302를 이용한 Real Time Clock 를 LCD로 표시하는 프로그램
#asm("sei"); while(1)
{
// DS1302 Real Time Clock initialization if (tick1flag) // 0.5초마다 업데이트
// Trickle charger: On {
// Trickle charge resistor: None if(i==0)
// Trickle charge diode(s): 1 {
rtc_init(1,1,0); i=1;
lcd_state = 43; // "X"를 표시
/* initialize the LCD for 2 lines & 16 columns */ }
else
lcd_init(); {
i=0;
Gotoxy(0,0); lcd_state = 120; // "+"를 표시
_lcd_str(" ::AVRMall::"); }
Gotoxy(0,1); lcd_clear();
_lcd_str("Real Time Clock 1"); Gotoxy(0,0);
_lcd_str(" M128 RTC TEST1 ");
rtc_set_time(12,0,0); sprintf(rtc_buf,"%c TIME %2d:%2d:%2d",lcd_state
//rtc_set_date(15,6,4); ,h,m,s,lcd_state);
Gotoxy(0,1); // LCD 둘째 줄 왼쪽 처음으로 이동
delay_ms(2000); lcd_str(rtc_buf); // LCD에 tpbuf 표시
tick1flag = 0;
}
rtc_get_time(&h,&m,&s); // DS1302에서 시간데이터 얻기
}
}

2006-
2006-04-
04-08 -9-

You might also like