Go Down

Topic: [Solved] using a mask bit dallas rtc (Read 629 times) previous topic - next topic

grendle

Apr 25, 2013, 04:33 am Last Edit: May 03, 2013, 06:00 pm by grendle Reason: 1
Hello all,
if i have a byte, say dec17 , B00010001, but bit 6 is a mask bit so if i set bit6 high i get B01010001(dec83), which totally changes my original 17. im thinking im to use |= to change bit 6, but im not having any luck doing it. this is for an rtc which uses bcd. i have my bcd code in place
Code: [Select]
writeRegister(MINUTES_REG,decToBcd(17));
void writeRegister(byte thisRegister, char thisValue)
{
 digitalWrite(SLAVESELECT, HIGH);
 SPI.transfer(thisRegister+DS1305_ADDRESS_OFFSET);
 SPI.transfer(thisValue);
 digitalWrite(SLAVESELECT, LOW);
}
byte decToBcd(byte val)
{
 return ( (val/10*16) + (val%10) );
}

and the clock is set and running fine, to enable alarms bit 6 in hours,days, and minutes register has to be "1". im not using a library for the device (ds1305)http://datasheets.maximintegrated.com/en/ds/DS1305.pdf but am looking at the ds1302 library to see how the bits are handled but im not understanding it. thank you.

lloyddean

#1
Apr 25, 2013, 07:15 am Last Edit: Apr 25, 2013, 07:44 am by lloyddean Reason: 1
Untested but something along the line of -

Code: [Select]

#define setBit(VALUE, BIT)       ((VALUE) |=  (1 << BIT))
#define clearBit(VALUE, BIT)     ((VALUE) &= ~(1 << BIT))

Grumpy_Mike

Quote
but bit 6 is a mask bit so if i set bit6 high i get B01010001(dec83), which totally changes my original 17.

Yes it will. The number 17 is only one way of interpreting what is after all only a pattern of bits. But if you set bit 6 then all the other bits remain the same and that is what matters if I understand your context correctly.

grendle


Quote
but bit 6 is a mask bit so if i set bit6 high i get B01010001(dec83), which totally changes my original 17.

Yes it will. The number 17 is only one way of interpreting what is after all only a pattern of bits. But if you set bit 6 then all the other bits remain the same and that is what matters if I understand your context correctly.


ya but the clock then runs with "83" (or whatever 83 converts to from bcd).


Untested but something along the line of -

Code: [Select]

#define setBit(VALUE, BIT)       ((VALUE) |=  (1 << BIT))
#define clearBit(VALUE, BIT)     ((VALUE) &= ~(1 << BIT))



im gonna give this a shot. thank you.

AWOL

Quote
(or whatever 83 converts to from bcd).

Woah!
Don't confuse hex with BCD or decimal.

0b10000011 is 0x83 and BCD 83, but 131 decimal.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up