Page 1 of 1

How to read the code

Posted: Mon Jul 30, 2012 7:49 pm
by cGiesen
Hello,

I start reading the code. Normaly I code in VB, C is long time ago ;)

I have problem to understand rows like this:

#define LEDPIN_TOGGLE PINB |= (1<<7); PINC |= (1<<7);

When I read the referenz this is not allowed ;)
Syntax
#define constantName value
Note that the # is necessary.
Example
#define ledPin 3
// The compiler will replace any mention of ledPin with the value 3 at compile time.
Tip
There is no semicolon after the #define statement. If you include one, the compiler will throw cryptic errors further down the page.


But it works, so it could not be an error ;)

Can I have help please.

cu
Carsten

Re: How to read the code

Posted: Tue Jul 31, 2012 12:54 pm
by treym
A Brief Tutorial on Programming the ATMega (Arduino) without Arduino Software : http://brittonkerin.com/cduino/lessons.html

A Complete Tutorial : http://www.atmel.com/Images/8271s.pdf

Re: How to read the code

Posted: Tue Jul 31, 2012 2:00 pm
by cGiesen
The first link is interesting.

The second link is 'only' a datasheet

But both now answer to my question.

Re: How to read the code

Posted: Wed Aug 01, 2012 1:38 am
by treym
cGiesen wrote: #define LEDPIN_TOGGLE PINB |= (1<<7); PINC |= (1<<7);

When I read the referenz this is not allowed ;)

Can I have help please.
Carsten

as you have difficulties with text reading , yes i can help you and send you a training about how to search the internet ... i can also do the search for you

cGiesen wrote:The second link is 'only' a datasheet


PINC is describe page 12

Re: How to read the code

Posted: Wed Aug 01, 2012 2:09 am
by mr.rc-cam
Example
#define ledPin 3
// The compiler will replace any mention of ledPin with the value 3 at compile time.
Tip
There is no semicolon after the #define statement. If you include one, the compiler will throw cryptic errors further down the page.

In this example a semicolon is not allowed at the end of the statement. That is because ledpin is an alias for the value you provided, and if you added the semicolon then every instance of ledPin will be "3;" which is not a valid number. This will cause compiler errors.

#define LEDPIN_TOGGLE PINB |= (1<<7); PINC |= (1<<7);

The semicolon is valid at the end because the alias for LEDPIN_ is a valid C statement. In other words, when the compiler sees LEDPIN_, it substitutes this with "TOGGLE PINB |= (1<<7); PINC |= (1<<7);"
If the trailing semicolon is not provided it is not a correctly terminated C statement and compiler errors will occur.

Re: How to read the code

Posted: Wed Aug 01, 2012 6:45 am
by cGiesen
#define LEDPIN_TOGGLE PINB |= (1<<7); PINC |= (1<<7);

The semicolon is valid at the end because the alias for LEDPIN_ is a valid C statement. In other words, when the compiler sees LEDPIN_, it substitutes this with "TOGGLE PINB |= (1<<7); PINC |= (1<<7);"
If the trailing semicolon is not provided it is not a correctly terminated C statement and compiler errors will occur.


Thanks, now I understand. I read a lot in the last two days, but no example like this.

You make my day.

Re: How to read the code

Posted: Wed Aug 01, 2012 12:41 pm
by treym
i did no realize this was about macro , sorry for missing the point ..