if..else statement error

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

hi all,
what is the problem with my coding? when i simulate it using avrstudio, i found that the simulator keep looping within the ifelse statement and wont go to test the PORTC pin1.
can anyone help me on this?

thank you.

include 

int main (void)
{
	int temp;

	DDRA = 0xFF;
	DDRC = 0x00;

	PORTA = 0X00;
	for(;;)
	{
		temp = (PINC & 0x01);

		if (!(temp ==0))
		{
			PORTA = 0XF0;
		}
		else
		{
			PORTA = 0X0F;
		}
	}

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

The "optimiser" has come to your help and decided that some bits of your code are supeflous, after all it knows best.

Declare temp as volatile to show the "optimiser" who is the boss. Also you may want an 8 bit integer for temp

   volatile uint8_t temp; 

Do you have the necessary pull down resistors on portc? Which chip are you using?

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

optimizer should not play a role here as temp is being loaded from a volatile, and then evaluated. The code should work as expected. It may however have been optimized to a SBIS or SBIC instruction, foregoing "temp" altogether.

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

js wrote:

Do you have the necessary pull down resistors on portc? Which chip are you using?

i'm using atmega32, and using DIP switch on PORTC. when i simulate it, i'm wonder that why it wont go to the temp = (PORTC & 0x01); that step.it just skip off that step.

why is it happen?

thank you.

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

the assembly to c mapping is not necessarily 1:1. Thus it may look like something is being skipped when you trace through in the C view, but it is actually getting executed. looking at the generated assembly code would reveal the truth of what is going on.

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

If you are using portc make sure that the JTAG fuse is turned off or you will lose pc2-pc5.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Enable an internal pull-up on PORTC.0 if you want to read the switch. (you normally wire the switch between PORTC.0 and GND)

DDRC  &= ~(1<<0);      // input (power-on default)
PORTC |= (1<<0);       // enable pull-up for bit #0

Studio should notice that you have PINC.0 high. But of course you can simulate the switch by clicking on PINC.0 box in the i/o view.

'temp' may well be optimised away but the program should work correctly anyway.

If you wire your switch between PORTC.0 and 5V:

1. Studio will not know.
2. You MUST have an external pull-down resistor between PORTC.0 and GND.

David.

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

Quote:
the assembly to c mapping is not necessarily 1:1. Thus it may look like something is being skipped when you trace through in the C view, but it is actually getting executed. looking at the generated assembly code would reveal the truth of what is going on.

Well this is what I get when I build it for mega16:

int main (void) 
{ 
   int temp; 

   DDRA = 0xFF; 
  6c:	8f ef       	ldi	r24, 0xFF	; 255
  6e:	8a bb       	out	0x1a, r24	; 26
   DDRC = 0x00; 
  70:	14 ba       	out	0x14, r1	; 20

   PORTA = 0X00; 
  72:	1b ba       	out	0x1b, r1	; 27
   { 
      temp = (PINC & 0x01); 

      if (!(temp ==0)) 
      { 
         PORTA = 0XF0; 
  74:	90 ef       	ldi	r25, 0xF0	; 240
      } 
      else 
      { 
         PORTA = 0X0F; 
  76:	8f e0       	ldi	r24, 0x0F	; 15
   PORTA = 0X00; 
   for(;;) 
   { 
      temp = (PINC & 0x01); 

      if (!(temp ==0)) 
  78:	98 9b       	sbis	0x13, 0	; 19
  7a:	02 c0       	rjmp	.+4      	; 0x80 
      { 
         PORTA = 0XF0; 
  7c:	9b bb       	out	0x1b, r25	; 27
  7e:	fc cf       	rjmp	.-8      	; 0x78 
      } 
      else 
      { 
         PORTA = 0X0F; 
  80:	8b bb       	out	0x1b, r24	; 27
  82:	fa cf       	rjmp	.-12     	; 0x78 

I'd switch to the interleaved C/Asm view and single step that. If it doesn't keep doing the SBIS and then one of the OUT/RJMPs then I'd get concerned. Otherwise I would put it down to optimisation and if you were concerned about the C not stepping as expected then give in and build it -O0. But it's a far better idea to learn to debug optimised code.

Cliff