I am using a library the return a "byte HEX" value via I2C from a DS3231 real time clock chip then I am storing the value in an "int registerValue" but the value is in HEX.
When I display the value on the LCD it prints the HEX value not the int value.
int registerValue = RTC.readRTC(ALM1hrReg);
I have tried using int(RTC.readRTC(ALM1hrReg)); but it does not work. I have also tried atoi(); and that doesn't work either.
I can get the value to display properly as an int if I put the HEX in my Serial.println(registerValue, HEX); function but the value is still a HEX stored in the variable and I do many operations with it later on.
How can I convert the value before storing it do I don't need the HEX in my Serial.print(); function?
It appears that RTC.readRTC returns the number in binary coded decimal, not hexidecimal.
The first hex digit is the 10's digit, the second is the 1's, in decimal format, so to convert back to binary take the first hex digit, multiply by 10, and add the second hex digit.
I added this to your code, see if it works:
// RTC.readRTC returns a byte in BCD (binary coded decimal)
// convert from BCD to Binary
ALM2hrVal = (ALM2hrVal / 16 * 10) + ALM2hrVal % 16;
ALM2minVal = (ALM2minVal / 16 * 10) + ALM2minVal % 16;
#include <DS3232RTC.h> //http://github.com/JChristensen/DS3232RTC
#include <Wire.h> //http://arduino.cc/en/Reference/Wire
//Alarm 1 hour/minute registers
int ALM1hrReg = ALM1_MATCH_HOURS+1;
int ALM1minReg = ALM1_MATCH_HOURS;
//Alarm 2 hour/minute registers
int ALM2hrReg = 0x0C;
int ALM2minReg = 0x0B;
void setup(void)
{
Serial.begin(9600);
// Set alarms in DS3231 Alarm registers
RTC.setAlarm(ALM1_MATCH_HOURS, 0, 15, 23, 0);
RTC.setAlarm(ALM2_MATCH_HOURS, 13, 22, 0);
//clear the alarm flags
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
// Read Alarm 1 registers
int ALM1hrVal;
int ALM1minVal;
ALM1hrVal = RTC.readRTC(ALM1hrReg);
ALM1minVal = RTC.readRTC(ALM1minReg);
// Print Alarm 1 values via Serial
Serial.print("ALM1 = ");
Serial.print(ALM1hrVal, HEX);
Serial.print(":");
Serial.println(ALM1minVal, HEX);
delay(100); // for some reason the compiler craps out without this delay
// Read Alarm 1 registers
int ALM2hrVal;
int ALM2minVal;
ALM2hrVal = RTC.readRTC(ALM2hrReg);
ALM2minVal = RTC.readRTC(ALM2minReg);
// RTC.readRTC returns a byte in BCD (binary coded decimal)
// convert from BCD to Binary
ALM2hrVal = (ALM2hrVal / 16 * 10) + ALM2hrVal % 16;
ALM2minVal = (ALM2minVal / 16 * 10) + ALM2minVal % 16;
// Print Alarm 2 values via Serial
Serial.print("ALM2 = ");
Serial.print(ALM2hrVal);
Serial.print(":");
Serial.println(ALM2minVal);
delay(1000);
}
void loop(void)
{
}
Another method, without having to use the math equations, would be to do the math in the print statements, but you seemed to want to avoid complications in the print statements themselves.
david_2018:
It appears that RTC.readRTC returns the number in binary coded decimal, not hexidecimal.
The first hex digit is the 10's digit, the second is the 1's, in decimal format, so to convert back to binary take the first hex digit, multiply by 10, and add the second hex digit.
Yes! The data sheets also say that the value of 'hour alarm' is in BCD format:
I also figured out if I convert the returned data to a string with a HEX type, then convert it to an int that works as well.
int registerValue = String(RTC.readRTC(ALM2hrReg), HEX).toInt();
thanks for the other suggestions. I will try them out but they look a little more advanced/complicated than my shitty non-efficient multiple conversion method but it would be good to understand for the future!