This tutorial shows you how to set the shield's real time clock, its DS1302. Don't be scared by the "DS"... yes, this is a chip from Dallas's 1-wire family , but you don't have to deal with the usual 1-Wire programming things. You DON'T need the general Arduino "OneWire" library.
A detail: You will see "one wire" and "one wire interface" in the nuElectronics documentation. This is not always connected with "1-Wire" (a Dallas trademark) at all, and even when you are connecting a 1-Wire device to a nuElectronics "one wire" connection point, you won't often (ever?) get dragged into some of the more complex 1-Wire issues. Don't get me wrong... I like 1-Wire... it is powerful. But to get everything you can from 1-Wire gets dangerously close to Serious Work. neElectonics lets you use some of the (marvelous) 1-Wire devices without the work! (I've written a brief note about the use of the terms, if you want to read more.)
The following is tested, and differs in some respects from the material nuElectronics has put online for your use.
When run, the following will start spitting out (to the serial monitor) a date/time about once per second. If you enter a line, STARTING WITH a t followed by a space, then, say 2010 5 10 1 12 15 0, and then press enter, you will, if you've entered a valid line, reset the date/time in the RTC. The spitting out of date/times will resume, with your new settings as the starting point. See the inline comments for the meaning of the string of digits, but note in particular....
Here's the code....
/*SetRTC 6 Jly 2010 */ //Yes... all three needed for following... #include <avr/pgmspace.h> #include <sensor_pff.h> #include <DS1302.h> DS1302 ds1302; RTC rtc; byte bLine; char buff[128]; /* i/o buffer */ // === START OF GET_LINE === //The following looks worse than it is. It fetches a line //of text from the serial monitor, and puts it someplace //for further use. You don't need to worry about the //internals. static void get_line (char *buff, byte len) { byte c; int idx = 0; for (;;) {//1 Start infinite loop... will only exit via "break" if(Serial.available()){//2 c = Serial.read(); if (((byte)c >= ' ') && (idx < len - 1)) {//3 buff[idx++] = c; Serial.print(c); }//3 }else break;//2 }//1 (ends "for...") buff[idx] = 0; Serial.print(c); Serial.print('\n'); } // === END OF GET_LINE === // === START OF CONVERT_RTC ==== //As with "get_line", the following looks worse than //it is. It fetches a line of text from the //serial monitor, and puts it someplace for further //use. You don't need to worry about the internals. /* convert string "<year> <day in month> <month> <dow> <hour> <min> <sec>" to RTC value, return 1 if successful. e.g., to set RTC for... 2010, 5th of October, Monday, 12:15:00 ... you enter.... t 2010 5 10 1 12 15 0 Note the space after the t. Important. ("Monday=1" is arbitrary, up to you. "Day" numbers are 1-7. Make, say, Thursday "day 1"... if you wish to be odd! You can MIS-set a dow to, for instance, "0"... but rollover, back to "1" will occur at midnight at the end of day "7") The error checking isn't 100% reliable. */ byte convert_RTC(char *ptr, RTC* rtc_p) { int val[7]; byte i = 0; int value=0; while(*ptr++ && i<7) { if( *ptr >='0' && *ptr<='9' ) value = value * 10 + *ptr - '0'; else if (*ptr == ' '|| !*ptr){ val[i++] = value; value = 0; } } /* convert string "<year> <day in month> <month> <dow> <hour> <min> <sec>" to RTC value, return 1 if successful. e.g., to set RTC for... 2010, 5th of October, Monday, 12:15:00 ... you enter.... 2010 5 10 1 12 15 0*/ if (i==7) { rtc_p->year = (unsigned int) val[0]; rtc_p->mday = (byte) val[1]; rtc_p->month = (byte) val[2]; rtc_p->wday = (byte) val[3]; rtc_p->hour = (byte) val[4]; rtc_p->min = (byte)val[5]; rtc_p->sec = (byte) val[6]; return 1; } else return 0; } // === END OF CONVERT_RTC ==== void setup() { byte res; bLine=6; Serial.begin(9600); Serial.println("RTC setting utility."); } void loop() { char *ptr; long p1, p2; byte res; unsigned short w; if(Serial.available() == FALSE){ ds1302.gettime(&rtc); bLine++; if (bLine==7){ Serial.println("Yr / day of month / month , day of week , time"); bLine=0; } sprintf_P(buff, PSTR("%u/%u/%u, %u, %02u:%02u:%02u"), rtc.year, rtc.mday, rtc.month, rtc.wday, rtc.hour, rtc.min, rtc.sec); Serial.println(buff); delay(1002); } else { Serial.println();// Make gap, to show starting from new date/time Serial.println(); get_line(buff, sizeof(buff)); ptr = buff; switch (*ptr++) { case 't' : /* Process line in form.... t [<year> <day of month> <mon> <dow> <hour> <min> <sec>] .. which SETS rtc. Not all invalid command lines result in a "false" return from convert_RTC*/ if (convert_RTC(ptr, &rtc)) { ds1302.settime(&rtc); } //Needs(??) something like "else report command was //malformed? } } }
The "get_line" function has been modified from what is in the nuElectronics demos because, I think, I needed to do that to get it to work with the Arduino serial monitor. For more details on that, and other general details, see my page about my working environment.
Further to the Arduino ideas the page you are reading now will take you to, I have posted a series of essays which try to help you become a better Arduino programmer and engineer... but, for the best result, you will have to buckle down and work your way through them in sequence. The "How To's" here can be accessed in whatever order you like.
Feel free to use this information in programming courses, etc, but a credit of the source would be appreciated. If you simply copy the pages to other web pages you will do your readers a disservice: Your copies won't stay current. Far better to link to these pages, and then your readers see up-to-date versions. For those who care- thank you- I have posted a page with more information on what copyright waivers I extend, and suggestions for those who wish to put this material on CDs, etc.
See the discussion near the bottom of the "top level" page covering the bulk of my Arduino contributions. There is information there, too, about things like "May I copy your material?", and the system of file names I am trying to work to.
If you visit 1&1's site from here, it helps me. They host my website, and I wouldn't put this link up for them if I wasn't happy with their service... although I was less than pleased the other day to have what I was doing interrupted by a telephone call from their sales team, trying to get me to extend my involvement. Sigh. Hardly a rare event, but I'd thought 1&1 were a bit classier that some of the people who have my telephone number.
Click here to visit editor's Sheepdog Software (tm) freeware, shareware pages.
Click here to visit the homepage of my biggest site.
Click here to visit the homepage of Sheepdogsoftware.co.uk. Apologies if the "?Frmar3ne1rtc2" I added to that link causes your browser problems. Please let me know, if so?
To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite the page's URL, "ar3ne1rtc2.htm".
Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.
. . . . . P a g e . . . E n d s . . . . .