year information not displayed properly

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?
 
M

MisterE

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?

It must be your LCD function. 2008 as an integer has its least significant
byte of 0xD8 which is 216 in decimal, which is what you are seeing. Your
problem must be the LCD writing function is somewhere using char 8bit
datatypes and not multibyte integers.

And everyone else will bitch at you because you need to take this problem to
a group about programming for whatever cpu/mcu/lcd you are using.
 
S

ssylee

It must be your LCD function. 2008 as an integer has its least significant
byte of 0xD8 which is 216 in decimal, which is what you are seeing. Your
problem must be the LCD writing function is somewhere using char 8bit
datatypes and not multibyte integers.

And everyone else will bitch at you because you need to take this problem to
a group about programming for whatever cpu/mcu/lcd you are using.

I personally found this to be a programming problem. Below shows my
LCD display code that deals with data:

void LCD4_OUT_data(int number, char position)
{
const int size = 10;
char text_array[size];
int n;

n = sprintf(text_array, "%u", number);
if (n < 0 || n >= size){
return;
}

LCD4_CMD(position);
LCD4_OUT(text_array);
LCD4_OUT(" ");

}

It doesn't appear that the integer got converted to unsigned char
anywhere. It got converted to string, that's for sure.
 
G

Guest

I personally found this to be a programming problem. Below shows my
LCD display code that deals with data:

...

It doesn't appear that the integer got converted to unsigned char
anywhere. It got converted to string, that's for sure.

Then the error must be after the return from access_year() and before
the call to LCD4_OUT_data().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top