More Related Content Similar to Avr lecture8 (20) Avr lecture83. --33--20062006--0404--0808
Embedded AVR ProgrammingEmbedded AVR Programming
1-1 RTC (Real Time Clock)이란?
⊙ 실시간 시계. 전원 공급이 안 되어도 현재의 시간을 지키는 시계
⊙ DS1302란?
- Trickle charge RTC (세류 충전 계시 칩, 혹은 트리클 충전 계시 칩)
- Trickle => 물방울, 졸졸 흐르는 시내물.
적은 전류로 충전(==> 동작)하는 RTC라고 생각하면 됨
- DS1302는 내부에 여러 개의 데이터 영역을 가지고 있으며, 각각의 영역마다 초, 분, 시, 년,
월, 일 등의 데이터를 기록하고, 자동으로 갱신시켜 줌. 유저는 단지 이 데이터를 읽어와 사용
4. --44--20062006--0404--0808
Embedded AVR ProgrammingEmbedded 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 : 백업용 배터리를 연결
5. --55--20062006--0404--0808
Embedded AVR ProgrammingEmbedded AVR Programming
1-2 DS1302를 이용한 RTC
⊙ 회로도
U5
DS1302SN/SOIC
X1
2
X2
3
GND
4
RST
5
I/O
6
SCLK
7 VCC2
1
VCC1
8
X1
32.768KHz
BT1
TL5101
JP12
HEADER/2X3
1 2
3 4
5 6
RTC.BAT
#BIT.RST
#BIT.SLK
BIT.IO
VCC
I2C/RTC
VCC.BAT
D1
1N5819
D2
1N5819
VCC
8. --88--20062006--0404--0808
Embedded AVR ProgrammingEmbedded AVR Programming
1-3 DS1302를 이용한 RTC 실습 1
☞ DS1302를 이용한 Real Time Clock 를 LCD로 표시하는 프로그램
// timer 0 overflow ISR
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0 = 6; // reload for 1ms ticks
if(++timecount == 500) // 500ms 마다 업데이트
{
tick1flag = 1;
timecount = 0; // clear for next 500ms
}
}
void main(void)
{
unsigned char h,m,s,i;
MCUCR=0xc0; // am_lcd함수 사용시
//Port D
PORTD = 0xff;
DDRD = 0xff;
TCNT0 = 0x00; // reset TCNT0
TCCR0 = 0x04; // count with cpu clock/64 (64/16MHz=4ms)
TIMSK = 0x01; // enable TCNT0 overflow
#include <mega128.h>
#include <stdio.h>
#include <delay.h>
#include "am_lcd.h"
#include "am_lcd.c"
// DS1302 Real Time Clock functions
#asm
.equ __ds1302_port=0x12 ;PORTD
.equ __ds1302_io=4
.equ __ds1302_sclk=5
.equ __ds1302_rst=2
#endasm
#include <ds1302.h>
char rtc_buf[16]; // lcd display temp buffer
char lcd_state;
bit tick1flag=0;
unsigned int timecount = 0; // global time counter
9. --99--20062006--0404--0808
Embedded AVR ProgrammingEmbedded AVR Programming
1-3 DS1302를 이용한 RTC 실습 1
☞ DS1302를 이용한 Real Time Clock 를 LCD로 표시하는 프로그램
while(1)
{
if (tick1flag) // 0.5초마다 업데이트
{
if(i==0)
{
i=1;
lcd_state = 43; // "X"를 표시
}
else
{
i=0;
lcd_state = 120; // "+"를 표시
}
lcd_clear();
Gotoxy(0,0);
_lcd_str(" M128 RTC TEST1 ");
sprintf(rtc_buf,"%c TIME %2d:%2d:%2d",lcd_state
,h,m,s,lcd_state);
Gotoxy(0,1); // LCD 둘째 줄 왼쪽 처음으로 이동
lcd_str(rtc_buf); // LCD에 tpbuf 표시
tick1flag = 0;
}
rtc_get_time(&h,&m,&s); // DS1302에서 시간데이터 얻기
}
}
#asm("sei");
// DS1302 Real Time Clock initialization
// Trickle charger: On
// Trickle charge resistor: None
// Trickle charge diode(s): 1
rtc_init(1,1,0);
/* initialize the LCD for 2 lines & 16 columns */
lcd_init();
Gotoxy(0,0);
_lcd_str(" ::AVRMall::");
Gotoxy(0,1);
_lcd_str("Real Time Clock 1");
rtc_set_time(12,0,0);
//rtc_set_date(15,6,4);
delay_ms(2000);