반응형

아두이노 우노는 자체적으로 타임키퍼라는 부품을 포함하고 있지만 아두이노에 전원이 연결된 

시간만 측정하고 있으므로 Real Time을 측정하는 부품을 포함하고 있지 않다.

이에 대하여 RTC 모듈이라는 부품을 통해서 현재 시간을 설정하고 알 수 있다.


DS1302 라는 칩이 그 역할을 하며 CR 2032 3V 배터리로 시간설정을 기억한다.

모듈의 핀배열은 사진에 보이는 위에서 아래로 다음과 같다.

VCC, GND, CLK, DAT, RST 이다.


##DS1302 데이터시트##

DS1302.pdf


##DS1302 라이브러리##

DS1302.zip


프로그래밍을 위해 위의 라이브러리 파일을 아두이노 설치 폴더 하위의 libraries 폴더에

압축을 해제하여 복사해 넣는다.


##아두이노와의 연결##

 RTC Module

 Arduino UN

 GND

 GND 

 VCC

 5V

 CLK

 D4

 DAT

 D3

 RST

 D2


##소스##

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* Define the DIO pins used for the RTC module */
#define SCK_PIN 4
#define IO_PIN 3
#define RST_PIN 2
 
/* Include the DS1302 library */
#include <DS1302.h>
 
/* Initialise the DS1302 library */
DS1302 rtc(RST_PIN, IO_PIN, SCK_PIN);
 
void setup()
{
  /* Clear the 1302's halt flag */
  rtc.halt(false);
  /* And disable write protection */
  rtc.writeProtect(false);
  /* Initialise the serial port */
  Serial.begin(9600);
  
  /* Set the time and date */
  rtc.setDOW(THURSDAY);
  rtc.setTime(21,48,0);
  rtc.setDate(27,8,2015); 
}
 
/* Main program */
void loop()
{
  /* Read the time and date once every second */
  while(1)
  {
    Serial.print("It is ");
    Serial.print(rtc.getDOWStr());
    Serial.print(" ");
    Serial.print(rtc.getDateStr());
    Serial.print(" ");
    Serial.print("and the time is: ");
    Serial.println(rtc.getTimeStr());
 
    /* Wait before reading again */
    delay (1000);
  }
}
cs


RTC 모듈의 시간을 설정하고 모듈에 설정된 시간을 읽어와서 시리얼 모니터를 통해 뿌려주는 소스이다.

Set the time and date 부분의 시간은 자신의 현재시간으로 수정해 주어야 한다.

Set the time and date 부분은 한 번 업로드 후 주석 처리 후 재업로드 해야한다. 

그렇지 않으면 다음 전원 연결 시 시간이 다시 소스에 설정된 시간으로 바뀌게 된다.


##결과##



반응형

+ Recent posts