| Munish Kumar 03/03/10 01:06 Modified: 03/03/10 01:06 Read: 671 times Chandigarh India |
#173759 - Problem with strings in C |
Hi,
I have written code for reading time from DS1307 RTC, convert BCD to decimal (12:34:56 in BCD to 12:34:56 in dec) & then that decimal to an ASCII string & after that copy the individual strings ("12" "34" "56") to a str_buffer with proper formatting e.g. "12:34:56 PM". Finally I send this str_buffer to a graphical LCD. The way I am doing this is: static unsigned char xdata str_buffer[20]; //not sure if static reqd, its in main() ... strcpy(str_buffer,dec2ascii(read_hour())); //read_hour() returns 12 in dec (tested) strcat(str_buffer,":"); strcat(str_buffer,dec2ascii(read_min())); //dec2ascii() returns ptr to "34" strcat(str_buffer,":"); strcat(str_buffer,dec2ascii(read_sec())); LCD_sendstring(str_buffer); Now the problem I am facing is that as soon as LCD_sendstring(str_buffer) is called, everyting else disappears from LCD! even though the Keil debugger shows "12" being copied/concatenated to str_buffer on using strcpy/strcat(str_buffer,dec2ascii(12)); while the following code works: strcpy(str_buffer,"11")); strcat(str_buffer,":"); strcat(str_buffer,"22"); strcat(str_buffer,":"); strcat(str_buffer,"33"); LCD_sendstring(str_buffer);& prints 11:22:33 on LCD. On dubugging yet another test code: strcpy(str_buffer,dec2ascii(11)); strcat(str_buffer,":"); strcat(str_buffer,dec2ascii(22)); strcat(str_buffer,":"); strcat(str_buffer,dec2ascii(33)); LCD_sendstring(str_buffer);I found that "11:22:33" is passed to LCD_sendstring() but still disappearing on LCD! Here is my dec2ascii() (it's in another file containing DS1307 subroutines) /*------------------------------------------------------------------------
Converts 2 digit decimal to ACSII string... 26 to "26"
returns pointer to converted string i.e 2
--------------------------------------------------------------------------*/
unsigned char* dec2ascii (unsigned char dec)
{
code unsigned char asciitable[10]={'0','1','2','3','4','5','6','7','8','9'}; //look up table for ascii values
static unsigned char buffer[2]; //to hold the 2 ascii codes ... MUST be static
buffer[0]=asciitable[(dec-dec%10)/10]; //tens
buffer[1]=asciitable[dec%10]; //ones
return buffer;
}
my typical tested & working routines to read from RTC: unsigned char read_hour(void)
{ unsigned char temp;
temp = readbyte(HR_RTC);
if(temp & (1<<6)) //12 hr mode is selected if set
return(bcd2dec(temp & ~(1<<6) & ~(1<<5))); //strip the 6th & 5th bits: AM/PM & 12hr/24hr mode & convert the remaining
else
return(bcd2dec(temp));
}
unsigned char read_min(void)
{ return(bcd2dec(readbyte(MIN_RTC)));
}
unsigned char read_sec(void)
{ return(bcd2dec(readbyte(SEC_RTC)));
}
What could be wrong with my code? |
| Topic | Author | Date |
| Problem with strings in C | MUNISH KUMAR | 03/03/10 01:06 |
| You missed a fundamental point on how 'C' does strings | Andy Neil | 03/03/10 01:13 |
| I think you don't need to be mysterious here, Andy | Jan Waclawek | 03/03/10 01:28 |
| Strings in 'C' *must* be NUL-terminated | Andy Neil | 03/03/10 01:52 |
| A little whitespace goes a long way | Andy Neil | 03/03/10 01:18 |
| conversion on top of conversion | Jan Waclawek | 03/03/10 01:30 |
| Null termination | MUNISH KUMAR | 03/03/10 12:48 |
| Think about it... | Andy Neil | 03/03/10 15:08 |
| so is your problem solved or not? | Jan Waclawek | 03/03/10 16:18 |
| "lucky" or "unlucky"? | Andy Neil | 03/03/10 17:59 |
| rather | Erik Malund | 03/03/10 18:05 |
| Yes - that's what I meant! | Andy Neil | 03/03/10 19:15 |
| That, in fact, IS it | MUNISH KUMAR | 03/04/10 01:49 |
| how true | Jan Waclawek | 03/04/10 01:56 |
| Debugger != Simulator | Andy Neil | 03/04/10 02:35 |
| if that is true, find another job! | Erik Malund | 03/04/10 05:35 |
| Does Keil initialise non-static data? | Jan Waclawek | 03/04/10 05:45 |
| the very first thing ... | Erik Malund | 03/04/10 06:22 |
| this is not the case.. | Jan Waclawek | 03/04/10 06:29 |
| Mr C hater: | Erik Malund | 03/04/10 06:49 |
| this IS part of the C-hatred | Jan Waclawek | 03/04/10 07:31 |
| but you do | Erik Malund | 03/04/10 08:05 |
| and what are the unzeroed values, then? | Jan Waclawek | 03/04/10 08:51 |
| I have no idea | Erik Malund | 03/04/10 09:02 |
| Many Reasons for not clearing (all of) RAM | Andy Neil | 03/04/10 10:22 |
| I actually gave an example. | Erik Malund | 03/04/10 10:36 |
| Another Example... | Michael Karas | 03/05/10 05:47 |
| what I wold do | Erik Malund | 03/05/10 07:04 |
| I Elect to Totally Disagree | Michael Karas | 03/05/10 07:13 |
| a reply | Erik Malund | 03/05/10 07:20 |
| I'll take the time to reply when... | Michael Karas | 03/05/10 07:30 |
| It's too easy | Andy Neil | 03/05/10 08:12 |
Auro variables are always undefined | David Prentice | 03/05/10 09:50 |
| Seconded! | Andy Neil | 03/05/10 08:02 |
| makes assumptions? | Erik Malund | 03/05/10 08:47 |



