strange char array parsing problem

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When I try to parse an array of chars over to a particular function, they always default to a char array of chars with hex value 0xFF, no matter what I parse to it. I also parse over 2 integers, and they go over fine.

I have run simulations on AVR Studio and the char arrays parse over fine, except when I load it onto my atmega644p, no matter what I parse over to the function, it always reads the char array I parsed to it as all chars of hex value 0xFF.

It used to work fine when I was running it on a atmega8.

Does anyone have any ideas?

Below is the function prototype and also the line I used to call it.

void lcd_write (char *to_print, u8 length, u8 position)
lcd_write("peekaboo",8,20);

In summary, I am trying to parse an array of char's to a function, and when the other function reads what it was parsed, the contents have changed from what I parsed it, to an array of 0xFF char's.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you sure you're compiling for a mega644P? Double check your compiler settings.

BTW, in order to parse your post, I had to change all occurences of "parse" to "pass". In this context it was quite confusing at first.

Jim

Your message here - reasonable rates.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes its compiling for a atmega644p, here is my make file below. I reinstalled the lasted version of winavr to get 644p support. It compiles without any errors or warnings.

main: lcd_16x2.c delay.c test.c
	avr-gcc -g -Wall -mmcu=atmega644p -I. lcd_16x2.c delay.c test.c -o test.elf
	avr-objcopy -j .text -O ihex test.elf test.hex
	avrdude -p m644P -c usbtiny -e -U flash:w:test.hex

The char array is used to drive an LCD, and it works fine when I manually set the char array (to whatever word I want to write on the LCD) within the function. Here is the function:

void lcd_write (char *toprint, u8 length, u8 position)
{

	lcd_set_ddram(position);
	u8 i;
	char temp[length];
	
	/*temp[0] = word[0];
	temp[1] = word[1];
	temp[2] = word[2];
	temp[3] = word[3];
	temp[4] = word[4];
	temp[5] = word[5];
	temp[6] = word[6];*/
	
	temp[0] = 'm';
	temp[1] = 'a';
	temp[2] = 't';
	temp[3] = 't';
	temp[4] = 'h';
	temp[5] = 'e';
	temp[6] = 'w';
	
	for (i = 0; i < length; i++)
	{
		lcd_write_char(toprint[i]);
	}
	
}

When I load this, it displays 'matthew on the lcd fine, but if I were to pass through the char array 'matthew' like this:

lcd_write("matthew",7,2);

It will not work, because its passed through to the function, hence my problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps you have an option to "store string literals in flash" selected in your makefile. In this case you would be taking a Flash address (from your string literal), and trying to read from RAM. (remember that Flash and RAM are on separate busses on the AVR)

In fact I'm fairly certain of it. I bet that the following actually works.

   char temp[8] = "matthew";
.
.
.   
   lcd_write(temp, 7, 20);

Also note (not related to your problem) you don't need "length" to be passed... a double quoted string will always be terminated with a NULL (0).

This would be functionally the same

void lcd_write (char *toprint, u8 position)
{

   lcd_set_ddram(position);

   while(*toprint)
     lcd_write_char(*toprint++);   
} 

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok, I got it, thanks glitch for pointing me in the correct direction. It was looking for it in RAM. I used this tutorial on PROGMEM here to fix it. Its working like a dream now. Cheers.

https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38003