반응형


character LCD를  위 그림처럼 연결합니다.



아두이노와 DS1302 RTC를 아래처럼 연결해줍니다.  이전에는 VCC를 3.3V에 연결해도 잘 동작했는데.. LCD를 연결해서 그런지 제대로 동작하지 않아서 VCC와 GND를 디지털핀에 연결해주고 각각 핀상태를 HIGH와 LOW상태로 만들었습니다..


DS1302 RTC  ----- Arduino UNO

RST                    8 

DAT                   9

CLK                    10

GND                  7

VCC                   6



아래 주소에서 라이브러리를 다운로드 받아 압축을 풀어서

http://playground.arduino.cc/uploads/Main/DS1302RTC.zip


아래 위치에 복사해줍니다..



아래 주소에서 라이브러리를 다운로드 받아 압축을 풀어서 

https://github.com/PaulStoffregen/Time


아래 위치에 복사해줍니다..



아두이노에 다음 소스코드를 업로드합니다.

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <LiquidCrystal.h>
  
LiquidCrystal lcd(12115432);  
  
 
#include <Time.h>
#include <DS1302RTC.h>
 
// Set pins:  CE(reset),IO(dat),CLK
DS1302RTC RTC(8910);
 
  
void setup() {  
  
  lcd.begin(162); //16열 2줄짜리 LCD임을 설정해줌.  
  
 
 
  // Setup Serial connection
  Serial.begin(9600);
  
  Serial.println("DS1302RTC Read Test");
  Serial.println("-------------------");
  
  
  // Activate RTC module
digitalWrite(7, LOW);
pinMode(7, OUTPUT);//GROUND pin
 
digitalWrite(6, HIGH);
pinMode(6, OUTPUT);//VCC pin
 
  Serial.println("RTC module activated");
  Serial.println();
  delay(500);
  
  if (RTC.haltRTC()) {
    Serial.println("The DS1302 is stopped.  Please run the SetTime");
    Serial.println("example to initialize the time and begin running.");
    Serial.println();
  }
  if (!RTC.writeEN()) {
    Serial.println("The DS1302 is write protected. This normal.");
    Serial.println();
  }
 
 time_t t;
 tmElements_t tm;
 
        
  delay(5000);
 
  
  //시간을 맞춰야할때 주석을 풀어서 설정합니다..
  /*
  tm.Year =  CalendarYrToTm(2016);
  tm.Month = 6;
  tm.Day = 9;
  tm.Hour = 21;
  tm.Minute = 00;
  tm.Second = 00;
  t = makeTime(tm); 
  if(RTC.set(t) == 0) { // Success
    setTime(t);
    Serial.print("RTC set to: ");
    printDateTime(t);
    Serial.println("");
  }
  else
    Serial.println("RTC set failed!");
    */
  
  
}  
  
  
  
void loop() {  
  
  tmElements_t tm;
  char mdate[255],mtime[255];
  
  //현재 날짜 / 시간을 가져옴
  if (! RTC.read(tm)) {
    sprintf( mdate, "%04d-%02d-%02d",
             tmYearToCalendar(tm.Year), tm.Month,  tm.Day);
 
   sprintf(mtime, "%02d:%02d:%02d",
             tm.Hour, tm.Minute, tm.Second );
  } else {
    Serial.println("DS1302 read error!  Please check the circuitry.");
    Serial.println();
    delay(9000);
  }
 
   lcd.setCursor(00); 
  lcd.print(mdate); 
   Serial.println(mdate); 
 
   lcd.setCursor(01); 
  lcd.print(mtime); 
   Serial.println(mtime); 
 
   
  // Wait one second before repeating :)
  delay (2000);
  
}
 
void print2digits(int number) {
  if (number >= 0 && number < 10)
    Serial.write('0');
  Serial.print(number);
}
 
 
//print date and time to Serial
void printDateTime(time_t t)
{
    printDate(t);
    Serial.print(' ');
    printTime(t);
}
 
//print time to Serial
void printTime(time_t t)
{
    printI00(hour(t), ':');
    printI00(minute(t), ':');
    printI00(second(t), ' ');
}
 
//print date to Serial
void printDate(time_t t)
{
    printI00(day(t), 0);
    Serial.print(monthShortStr(month(t)));
    Serial.print(year(t));
}
 
//Print an integer in "00" format (with leading zero),
//followed by a delimiter character to Serial.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
    if (val < 10) Serial.print('0');
    Serial.print(val);
    if (delim > 0) Serial.print(delim);
    return;
}
cs







반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts