coding query

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

Hi all I was just wondering if someone could point out to me why code 1 doesn't work yet code 2 does. I'm using studio4 with WinAVR, code 1 generates an error when building/compiling.

CODE 1

#define BIT0 0x01;
.
.
.
void toggel_led1()
{
	if((PORTB & BIT0) == 0) /*LED is off */
	{
		Led1_on;
	}
	else
	{
		Led1_off;
	}
}

CODE 2

void toggel_led1()
{
	if((PORTB & 0x01) == 0) /*LED is off */
	{
		Led1_on;
	}
	else
	{
		Led1_off;
	}
}

Thanks in advance for the feedback

Last Edited: Sun. Jul 30, 2006 - 07:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you define BIT0 and use BIT1 ?

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

Ooops!! that was just a typo in the post, has been edited/corrected

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

BIT0 is a macro, that is, the preprocessor simply replaces its
text into the source code. So what you're actually compiling in
the first case is:

void toggel_led1()
{
   if((PORTB & 0x01;) == 0) /*LED is off */
   {
      Led1_on;
   }
   else
   {
      Led1_off;
   }
}

See the extra semicolon?

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

p.s.: an empty parameter list

void toggle_led1()

is something very different than a parameter list
declared "void"

void toggle_led1(void)

in C. The first declares/defines a function where nothing
is known about parameters, the second declares/defines
a function that takes no parameters.

This is one of the differences to C++: both are identical
there, because they don't have to maintain the "we don't
know yet about that function's parameter list" heritage
from old K&R C there.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Thanks, thats fixed it :D

And thanks for the info on the (void) parameter list, I will use it in future, in actually was taught C++ first the C, I guess habbits have carried over