Now. This little code uses the fine DS1302 library of oods_52 posted in the projects section. BUt that's not the problem. The problem lies within the routine i use to set the minutes.
When I press the button it goes into the if(...){...}. It reads the data correctly. (I can see this, as I'm using something to get the data out of the thing. ). Then, it increments it, does some processing with it, and goes bad.
The bad part: It does NOT make the lower part (temp1) 0 after going to 10, instead, it happily goes over 10, to 16 even and completely ignores the higher part of the byte (temp).
if(B_SET_MIN()) { wait(_ms(B_WAIT)); while(B_SET())wdt_reset(); wait(_ms(B_WAIT));//wait for the button to unbounce temp = read_byte(min_r); //read the seconds temp1= temp & 0x0F;//extract the lower part //minutes temp = temp & 0x70;//extract the higher part temp = temp / 0x10;//the tens of minutes //and make it into the lower nibble if(++temp1 == 10){temp++;temp1=0;}; //increment the number of minutes. if it equals ten, set it to zero, and increment the tens of minutes if(temp == 6){temp = 0;temp1=0;};//if the number of tens of minutes equals 6, then set all of the stuff to zero... temp = temp * 0x10; //restore the tens of minutes into the higher nybble write_byte(sec_w,0x00);//reset the seconds wait(_ms(10));//wait a liiiitle while write_byte(min_w,temp | temp1);//write the minutes... }
Did I understand the datasheet of the dreaded DS1302 correctly, that it has the output registers in BCD and NOT encoded as binary? Cause it's acting really strange...
Can some of the elder gurues find a bug?
Thanks,
David Gustafik