|
|
View previous topic :: View next topic |
Author |
Message |
Ed Arnold Guest
|
Set time / date on DS1307 |
Posted: Sun Dec 09, 2001 1:30 pm |
|
|
I am using stuctures on a DS1307 RTC. I want to use 3 push-buttons to set the time and date on this RTC. 1 button for increment, 1 button for decrement, and 1 for enter/next value. The time structure looks like this:
struct time
{
int hr;
int min;
int sec;
}t, *p_t;
I have all of the read, write and control fuctions for the RTC. What I need is a better understanding of pointers and stuctures. I can better explain what I want with my non-working code fuction. I hope someone can show me how to make this work. The code here is only for set time, but you can get the general idea, I hope.
set_time_date()
{
int i=0;
int const LIMIT[5] = {23,59,12,31,99}; //used to force limits
clock_read(&t,&d);
printf(lcd_putc,"\fSet Time");
lcd_goto_xy(15,1);
printf(lcd_putc,"\%x:\%x", t.hr, t.min);
while(i<2) //how many times enter/next value is pressed
{
switch(KPbuff[Kout]) //keypress routine found in rtcc
{
case 'E': //this is enter keypress
*p_t++; //increment to next element in structure
i++;
break;
case 'U':
*p_t=--(*p_t)\%LIMIT[i]; //this won't compile but I want
//it to decrement what p_t points to.
lcd_goto_xy(15,1);
printf(lcd_putc,"\%x:\%x", t.hr, t.min);
break;
case 'D':
*p_t=++(*p_t)\%LIMIT[i];
lcd_goto_xy(15,1);
printf(lcd_putc,"\%x:\%x", t.hr, t.min);
break;
default:
break;
}
}
I realize this code may not be possible, but I am sure there is a simple way to do it. I want to keep this function simple. I don't want to use up alot of RAM or program ROM to accomplish this. Again thanks to the group for any help on this.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1532 |
|
|
Tomi Guest
|
Re: Set time / date on DS1307 |
Posted: Mon Dec 10, 2001 4:07 am |
|
|
First of all, there is a problem about the increment/decrement usage of your code. I think you want to select the next struct element with this: *p_t++; But it is illegal because the (*p_t) is not an integer type (e.g. char, int, int16, etc.). (*p_t) is a structure. (Note: in your original concept you must declare an independent char* pointer (e.g. myptr), use the "myptr = (char *)p_t" at start and then use myptr++ to access the next element in the struct. p_t++ means the next struct in an array and not the next element in the struct - the pointer is incremented by 3 in your example.)
Next, using the \% (modulo) operator is not a really good idea. It means a division (unless the second parameter for the \% is 2**N) but the division is always a pain using small microcontrollers like PICs. Use a simple compare what is a subtraction.
Next, I think you have choosen a difficult way. It would be better to use a simple char array.
Finally, "\%x" writes a value in hex. If your numbers are figured as binary (not BCD) use "\%02u". If your numbers are BCDs, you have a small problem with incr/decr.
All in all, here is a modified code: (note that I have changed the incr/decr operators; I think "U" is "up" and "d" is "down" ; sorry, I removed the lcd_putc from printf to test the code)
char t[3]; // instead of struct:t[0]=hour, etc.
printf(...... , "\%02u:\%02u", t[0], t[1]);
case 'E': //this is enter keypress
i++; // increment the index
if (i>2) i=0; // if index overflow: back to hours
break;
case 'U': // up???
t[i]++; // increment the appropr. array element
if (t[i] > LIMIT[i]) t[i] = 0; // instead of modulo
printf(..... , "\%02u:\%02u", t[0], t[1]);
break;
case 'D': // down???
if (t[i] > 0) t[i]--; // normal decr.
else t[i] = LIMIT[i]; // =0 before decr.: load it from LIMIT
printf(..... , "\%02u:\%02u", t[0], t[1]);
break;
___________________________
This message was ported from CCS's old forum
Original Post ID: 1539 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|