Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] I am getting mad with with AVR-GCC . Why while(*p) {data(*p++);} not working?

Status
Not open for further replies.

vinodstanur

Advanced Member level 3
Joined
Oct 31, 2009
Messages
751
Helped
114
Reputation
234
Reaction score
114
Trophy points
1,333
Location
Kerala (INDIA)
Activity points
7,054
I am new to avr-gcc.
I am just trying an lcd program for atmega16.

Now,

data('H'); data('E'); data('L'); data('L'); data('O');

The above code is working fine!!!




But...
below code is not

const char *p = "HELLO";
while(*p) data(*p++);

below code is also not working,
char *p = "HELLO";
while(*p) data(*p++);



But the below code is WORKING!!
const char *p = "HELLO";
data(*p++);
data(*p++);
data(*p++);
data(*p++);
data(*p++);

!!!!!

Why ???????
 
Last edited:

p is a pointer and p++ increments the pointer address , how do you expect to get the value pointed by p without using the * (as you do in data(p++))

---------- Post added at 23:48 ---------- Previous post was at 23:42 ----------

c = *p++; // assign to c the value pointed by p, and then increment the address p
c = *++p; // increment the address p, then assign c the value pointed to by p
c = ++*p; // increment the value pointed to by p,then assign it to c, leaving the address of p untouched
c = (*p)++ // assign c the value pointed to by p, then increment the value pointed to by p leaving the address of p untouched
 

Sir it was a typing mistake..
I really mean data(*p++) is also not working!!!!!!!!

I tried a lot...

my function
void string(const char *p) {
data(*p++);
}

is not working!!!!

if I call it with
string("HELLO WORLD");

it seems strange ...
 

I'm not very clear on the problem, I don't understand what you are trying to do.

Post your string function and the way you call it.
Does string("HELLO WORLD"); work or not?

this while(*p) data(*p++); works fine for me.
 
My complete code:

Code:
#include<avr/io.h>
#define F_CPU 12e6
#include <util/delay.h>
#define RS 6 //PD6
#define EN 7 //PD7
#define databits PORTC

void port_init()
{
    DDRC = 0xff;
   	DDRD = (1 << RS)|(1 << EN);
}

void LCD_STROBE(void)
{
    PORTD |= (1 << EN);
    _delay_us(1);
    PORTD &= ~(1 << EN);
}

void data(unsigned char c)
{
    PORTD |= (1 << RS);
    _delay_us(50);
    databits = (c >> 4);
    LCD_STROBE();
    databits = (c);
    LCD_STROBE();
}

void cmd(unsigned char c)
{
   	PORTD &= ~(1 << RS);
    _delay_us(50);
    databits = (c >> 4);
    LCD_STROBE();
    databits = (c);
    LCD_STROBE();
}

void clear(void)
{
    cmd(0x01);
    _delay_ms(5);
}

void lcd_init()
{
    _delay_ms(15);
    cmd(0x38);
    _delay_ms(1);
    cmd(0x38);
    _delay_us(100);
    cmd(0x38);
    cmd(0x28);			// Function set (4-bit interface, 2 lines, 5*7Pixels)
    cmd(0x28);			// Function set (4-bit interface, 2 lines, 5*7Pixels)
    cmd(0x0c);			// Make cursorinvisible
    clear();			// Clear screen 
    cmd(0x6);			// Set entry Mode(auto increment of cursor)
}

void string(const char *q)
{
while(*q)
	data(*q++);
}

int main()
{
    _delay_ms(50);
    port_init();
    lcd_init();
    cmd(0x80);
	data('H');
	_delay_ms(1000); //to cofirm H is printed on LCD (Working fine)
	string("HELLO WORLD"); //NOT WORKING FOR ME :-( DONT KNOW WHY!!
	while(1);
	return 0;
}


command line I used:
Code:
avr-gcc -mmcu=atmega16 -Os lcd.c
avr-objcopy -j .text -O ihex a.out a.hex


---------- Post added at 23:28 ---------- Previous post was at 23:24 ----------

If some one could compile it using avr-gcc and provide the hex, then I could confirm if it is some thing wrong with my packge or not,,,
 
Last edited:

This is the compiled hex View attachment m16.zip

You don't have a loop inside string

Code:
void string(const char *q)
{
   while (*q)  data(*q++);
}

Was that what you tried and wasn't working?
 
this while(*p) data(*p++); works fine for me
Do you mean with AVR-GCC?

I guess the problem is related to the necessity to generate LPM instructions for const data located in Flash memory. The *p++ syntax is generally O.K., but it's not working for Flash memory without special compiler prerequisites. But I'm not familiar with AVR-GCC and don't know how they are solving this problems. You should find informations about handling of flash memory pointers in the documentation.

When facing problems like this, I would generally inspect the generated assembly code to understand why it's not working. Or use a hardware debugger or possibly a simulator.

P.S.: Sounds like a more trivial problem. But then the observations in post #1 aren't fully correct?
You don't have a loop inside string
 
Yes I meant AVR-GCC.
He is not using flash memory data, the const data is still in RAM memory but can't be changed.
In order to use flash he needs to include #include <avr/pgmspace.h> and use the PROGMEM

https://www.avrfreaks.net/index.php...ic&t=38003&start=all&postdays=0&postorder=asc

---------- Post added at 00:40 ---------- Previous post was at 00:38 ----------

AM AGAIN SORRY...IT WAS ALSO A TYPING MISTAKE....pls don't mind

Should I recompile it using the string function with the while (*q) addition?
 
Yes I meant AVR-GCC.
He is not using flash memory data, the const data is still in RAM memory but can't be changed.
In order to use flash he needs to include #include <avr/pgmspace.h> and use the PROGMEM

https://www.avrfreaks.net/index.php...ic&t=38003&start=all&postdays=0&postorder=asc

---------- Post added at 00:40 ---------- Previous post was at 00:38 ----------



Should I recompile it using the string function with the while (*q) addition?
Yes Sir,
I would be happy if you could ones again compile it with while(*p)
 

I can't imagine there is something wrong with your gcc, since you said that data('H'); worked fine but you only had problem to print a string.
Try to recompile with the while inside the string function.

Are you using winAVR as standalone or with AVRstudio?

---------- Post added at 00:58 ---------- Previous post was at 00:55 ----------

I'm using AVRstudio v4.18 with winavr20090313
 

I can't imagine there is something wrong with your gcc, since you said that data('H'); worked fine but you only had problem to print a string.
Try to recompile with the while inside the string function.

Are you using winAVR as standalone or with AVRstudio?

---------- Post added at 00:58 ---------- Previous post was at 00:55 ----------

I'm using AVRstudio v4.18 with winavr20090313

I really don't know WinAVR or avrstudio. Am new to avr..
Now using avr-gcc and the command line which I posted in previous post,,,
Here is a screenshot of terminal...
avrgcc.png
 

char *p = "HELLO";
data(*p++);data(*p++);data(*p++);data(*p++);data(*p++);

Above sample is WORKING FINE!!

Now it is becoming hard to find what is the exact problem...
At present, it seems some thing like,
inside while condition, *p is not possible..
(in my case)

Because I checked below code:

char *p = "HELLO";
while(*p) {data('A'); p++};
this is printing more that 5 'A' ...
exactly more than 32 As, since I could observe lcd is filled with letter A,,.
 

Have you recompiled it making sure that you use

Code:
void string(const char *q)
{
   while (*q)  data(*q++);
}

You are using a linux OS, I have never used any AVR tool-chain in a linux environment so I have no idea about it

Avrstudio can be download from Atmel but it is intended for windows.


---------- Post added at 01:29 ---------- Previous post was at 01:27 ----------

char *p = "HELLO";
data(*p++);data(*p++);data(*p++);data(*p++);data(*p++);

Above sample is WORKING FINE!!

Now it is becoming hard to find what is the exact problem...
At present, it seems some thing like,
inside while condition, *p is not possible..
(in my case)

Because I checked below code:

char *p = "HELLO";
while(*p) {data('A'); p++};
this is printing more that 5 'A' ...
exactly more than 32 As, since I could observe lcd is filled with letter A,,.

Use a null terminated string, while(*p) will continue until it finds a null character
 

Yes I have recompiled it with while(*p)...

still not working...
lcd is showing many symbols and all...Entire screen...


but this is printing zero in LCD...

char *p = "HELLO";
data(*(p + 5) + '0');

Above code is printing NUMBER 0 in lcd...So it confirms that the string is terminating with null!!
 
Last edited:

But is there a chance for such a bug in avr-gcc since it is much popular already...

I wan;t to find some one who already have avr-gcc in their system to compile it for me...Then only I could confirm what is wrong..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top