Hello all,
I am working on a program on a PIC18 processor that takes and inputed character string and is supposed to return a two digit year from an RS232 read. Here is the code and the calling function:
Code:
void read_rs232_date(struct datetime* input_datetime) {
char string_datetime[12];
char datetime_number[2];
unsigned int32 leap_year_determinator;
rs232_read_successful = TRUE;
get_string(string_datetime, 13);
datetime_number[0] = string_datetime[8];
datetime_number[1] = string_datetime[9];
The problem is with the atoi function, it is supposed to return 04, but it keeps returning 45 or 46. The really weird thing is that it works fine until the system clock reads 10pm. Before 22:00:00 the clock is fine and the atoi function returns the proper 2 digit year, after 22:00:00, it returns the 45 or 46 when the inputted string is 04.
What am I doing wrong?
Here is the code for the atoi function:
Code:
//////////////////////////////////////////////////////////
// This function is copied from standard lib //
//////////////////////////////////////////////////////////
int8 atoi(char *s) {
int8 result;
int8 base, index;
char c;
index = 0;
base = 10;
result = 0;
// Omit all preceeding alpha characters
if(s)
c = s[index++];
if (c >= '0' && c <= '9') { // Check for hexa number
if (c == '0' && (s[index] == 'x' || s[index] == 'X')) {
base = 16;
index++;
c = s[index++];
}
// The number is a decimal number
if (base == 10) {
while (c >= '0' && c <= '9') {
result = 10*result + (c - '0');
c = s[index++];
}
} else if (base == 16) { // The number is a hexa number
while (c = TOUPPER(c), (c >= '0' && c <= '9') || (c >= 'A' && c<='F')) {
if (c >= '0' && c <= '9')
result = (result << 4) + (c - '0');
else
result = (result << 4) + (c - 'A' + 10);
c = s[index++];
}
}
}
return(result);
}