[vc_row][vc_column width=”1/1″][vc_tour][vc_tab title=”Introduction ” tab_id=”1395588859-1-33″][vc_column_text]The Maxim DS1302 clock module can supply date and time information such asyear, month, day, hour, minute, second, week display. It has a button battery. In this project, we will show how to use RTC DS1302 to provide date/time information to Arduino.
[/vc_column_text][/vc_tab][vc_tab title=”Schematic” tab_id=”1395588859-2-0″][vc_column_text]
DS1302 data sheet can be found at : http://www.maxim-ic.com/datasheet/index.mvp/id/2685/t/al[/vc_column_text][/vc_tab][vc_tab title=”Wire Diagram” tab_id=”1395589244913-2-0″][vc_column_text]CE (DS1302 pin5) -> Arduino D5
IO (DS1302 pin6) -> Arduino D6
SCLK (DS1302 pin7) -> Arduino D7
Vcc2 (DS1302 pin1) -> Arduino +5 v
GND (DS1302 pin4) -> Arduino GND[/vc_column_text][/vc_tab][vc_tab title=”Sample Code” tab_id=”1395589471148-3-9″][vc_column_text]Before use, first extract the files to the following libraries under ‘arduino-0023\libraries’ folder.
Code Source: http://quadpoint.org/projects/arduino-ds1302
Increases the serial port to adjust the time code
* / # Include <stdio.h> # Include <string.h> # Include <DS1302.h> / * Interface Definition CE (DS1302 pin5) -> Arduino D5 IO (DS1302 pin6) -> Arduino D6 SCLK (DS1302 pin7) -> Arduino D7 * / uint8_t CE_PIN = 5; uint8_t IO_PIN = 6; uint8_t SCLK_PIN = 7; / * Date variable buffer * / char buf [50]; char day [10]; / * Serial data cache * / String comdata = ""; int numdata [7] = {0}, j = 0, mark = 0; / * Create DS1302 object * / DS1302 rtc (CE_PIN, IO_PIN, SCLK_PIN); void print_time () { / * Get the current time from the DS1302 * / Time t = rtc.time (); / * The week from digital to name * / memset (day, 0, sizeof (day)); switch (t.day) { case 1: strcpy (day, "Sunday"); break; case 2: strcpy (day, "Monday"); break; case 3: strcpy (day, "Tuesday"); break; case 4: strcpy (day, "Wednesday"); break; case 5: strcpy (day, "Thursday"); break; case 6: strcpy (day, "Friday"); break; case 7: strcpy (day, "Saturday"); break; } / * The date code format to make up for output buf * / snprintf (buf, sizeof (buf), "% s% 04d-% 02d-% 02d% 02d:% 02d:% 02d", day, t.yr, t.mon, t.date, t.hr, t. min, t.sec); / * Output the date to the serial port * / Serial.println (buf); } void setup () { Serial.begin (9600); rtc.write_protect (false); rtc.halt (false); } void loop () { / * When the serial port has data, the data is spliced into a variable comdata * / while (Serial.available ()> 0) { comdata + = char (Serial.read ()); delay (2); mark = 1; } / * A comma-separated string comdata decomposition, the decomposition results become converted into digital to numdata [] array * / if (mark == 1) { Serial.print ("You inputed:"); Serial.println (comdata); for (int i = 0; i <comdata.length (); i + +) { if (comdata [i] == ',' | | comdata [i] == 0x10 | | comdata [i] == 0x13) { j + +; } else { numdata [j] = numdata [j] * 10 + (comdata [i] - '0 '); } } / * Make up the converted numdata time format, write DS1302 * / Time t (numdata [0], numdata [1], numdata [2], numdata [3], numdata [4], numdata [5], numdata [6]); rtc.time (t); mark = 0; j = 0; / * Empty comdata variable to wait for the next input * / comdata = String (""); / * Empty numdata * / for (int i = 0; i <7; i + +) numdata [i] = 0; } / * Print the current time * / print_time (); delay (1000);
Open the Arduino serial terminal, you can see the current time. If you need to adjust the time,
only need to enter the current date and time in port, separated by commas per the following format:
Year, month, day, hour, minute, second, day of week
Number of weeks: Sunday = 1, MOnday = 2, … Saturday = 7
For example, today is at 11:23:40 on November 17, 2011 Thursday
You can enter 2011,11,17,11,22,40,5.[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.