Going postal over DS1302 code

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please, anything but postal!

No help, otherwise.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That one is easy.

See the Excerp from the DS1302 datasheet below...

The data will probably be PACKED BCD. First, seperate the two nibbles into seperate variables.
You will have to shift the high nibble down to the 4 least significant bits.

So, lets take the number 3 in BCD representation. That will be 0000-0011b. To display this number, simply OR 0x30 with it.

All BCD digits, when ORed with 0x30, form the ASCII equivelent decimal character.

BCD    LOGIC   HEX    HEX   ASCII
0000  OR WITH  0x30 = 0x30 =  0
0001  OR WITH  0x30 = 0x31 =  1
0010  OR WITH  0x30 = 0x32 =  2
0011  OR WITH  0x30 = 0x33 =  3
0100  OR WITH  0x30 = 0x34 =  4
0101  OR WITH  0x30 = 0x35 =  5
0110  OR WITH  0x30 = 0x36 =  6
0111  OR WITH  0x30 = 0x37 =  7
1000  OR WITH  0x30 = 0x38 =  8
1001  OR WITH  0x30 = 0x39 =  9

Which translates to the correct ASCII representation.

So the number representing the BCD number 3 is simply logically ORed with 0x30.

Going fromASCII to BCD...

ASCII   LOGIC   HEX    HEX     BCD
0x30  AND WITH  0x0F = 0x00 =  0000
0x31  AND WITH  0x0F = 0x01 =  0001
0x32  AND WITH  0x0F = 0x02 =  0010
0x33  AND WITH  0x0F = 0x03 =  0011
0x34  AND WITH  0x0F = 0x04 =  0100
0x35  AND WITH  0x0F = 0x05 =  0101
0x36  AND WITH  0x0F = 0x06 =  0110
0x37  AND WITH  0x0F = 0x07 =  0111
0x38  AND WITH  0x0F = 0x08 =  1000
0x39  AND WITH  0x0F = 0x09 =  1001

Attachment(s): 

You can avoid reality, for a while.  But you can't avoid the consequences of reality! - C.W. Livingston

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
The data will probably be PACKED BCD. First, seperate the two nibbles into seperate variables.
You will have to shift the high nibble down to the 4 least significant bits.

Well, that's exactly what I'm doing.
The problem is that somewhere, something is very misinterpreting the values.

Currently when I read from It it goes this way:
0 1 2 3 4 5 6 7 8 9 A B C D E F
And then increments the higher nybble. Oh well, guess I'll just go nuts...bwihiihii!

ka7ehk: If you insist...

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Solved. Most likely. Seems my DS1302 is NOT running in BCD 0-9 but in binary...Why is the universe strange on me?

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.