S
ssylee
I have set my year value in the register on my RTC (DS1302) to be 2008
using this code:
// This function sets the year on the RTC
// Note: The year takes in only the last two digits of the year only
// and valid for only 2000 to 2099
void set_year(unsigned int year)
{
unsigned int year_temp;
unsigned char year_write = 0;
unsigned char year_tens;
unsigned char year_ones;
// Obtain the last two digits of the year
year_temp = year - 2000;
// Obtain the ones of the year and write it to year_write
year_ones = year_temp % 10;
year_write = year_write | year_ones;
// Obtain the tens of the year and write it to year_write
year_tens = year_temp / 10;
year_write = year_write | (year_tens << 4);
// Write the byte year_write to the register
writebyte(WRITEYEAR, year_write);
}
However, the chip can't store all four digits. So I subtract 2000 to
store the last two digits of the year, and add 2000 back to the value
retrieved from the real time clock register carrying the information
about the year.
The following function accesses the year information from the
register:
// This function accesses the register carrying the year and returns
// it to the main function in the format of "20xx", with the "xx"
determined
// by the contents of the register
unsigned int access_year(void)
{
unsigned char register_data;
unsigned int return_year = 0;
unsigned char temp;
// retrieve the byte in the register
register_data = readbyte(READYEAR);
// process the first digit of the year
temp = register_data & 0x0f;
return_year += temp;
// process the second digit of the year
temp = register_data & 0xf0;
return_year += (temp >> 4)*10;
// add 2000 to return_year to make it 20xx
return_year += 2000;
// return return_year to complete function call
return return_year;
}
However, I'm getting a value of 216 displayed on the LCD. readbyte()
and writebyte() functions have been tested to work properly. It would
be much appreciated to get some feedback, as I have made sure that I
have anded and or'ed the variables properly. Could there be a problem
with the conversion between unsigned int and unsigned char? For a
specific LCD writing function, it uses type int for the data rather
than unsigned int. Could that be a source of problem as well?
using this code:
// This function sets the year on the RTC
// Note: The year takes in only the last two digits of the year only
// and valid for only 2000 to 2099
void set_year(unsigned int year)
{
unsigned int year_temp;
unsigned char year_write = 0;
unsigned char year_tens;
unsigned char year_ones;
// Obtain the last two digits of the year
year_temp = year - 2000;
// Obtain the ones of the year and write it to year_write
year_ones = year_temp % 10;
year_write = year_write | year_ones;
// Obtain the tens of the year and write it to year_write
year_tens = year_temp / 10;
year_write = year_write | (year_tens << 4);
// Write the byte year_write to the register
writebyte(WRITEYEAR, year_write);
}
However, the chip can't store all four digits. So I subtract 2000 to
store the last two digits of the year, and add 2000 back to the value
retrieved from the real time clock register carrying the information
about the year.
The following function accesses the year information from the
register:
// This function accesses the register carrying the year and returns
// it to the main function in the format of "20xx", with the "xx"
determined
// by the contents of the register
unsigned int access_year(void)
{
unsigned char register_data;
unsigned int return_year = 0;
unsigned char temp;
// retrieve the byte in the register
register_data = readbyte(READYEAR);
// process the first digit of the year
temp = register_data & 0x0f;
return_year += temp;
// process the second digit of the year
temp = register_data & 0xf0;
return_year += (temp >> 4)*10;
// add 2000 to return_year to make it 20xx
return_year += 2000;
// return return_year to complete function call
return return_year;
}
However, I'm getting a value of 216 displayed on the LCD. readbyte()
and writebyte() functions have been tested to work properly. It would
be much appreciated to get some feedback, as I have made sure that I
have anded and or'ed the variables properly. Could there be a problem
with the conversion between unsigned int and unsigned char? For a
specific LCD writing function, it uses type int for the data rather
than unsigned int. Could that be a source of problem as well?