How to read the code

This forum is dedicated to all issues and questions related to your individual setups and configurations
Post Reply
cGiesen
Posts: 188
Joined: Wed Jul 18, 2012 7:53 am
Location: Bochum, Germany

How to read the code

Post 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

User avatar
treym
Posts: 258
Joined: Sat Jul 21, 2012 12:28 am

Re: How to read the code

Post 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

cGiesen
Posts: 188
Joined: Wed Jul 18, 2012 7:53 am
Location: Bochum, Germany

Re: How to read the code

Post by cGiesen »

The first link is interesting.

The second link is 'only' a datasheet

But both now answer to my question.

User avatar
treym
Posts: 258
Joined: Sat Jul 21, 2012 12:28 am

Re: How to read the code

Post 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

mr.rc-cam
Posts: 457
Joined: Wed Jul 27, 2011 11:36 pm

Re: How to read the code

Post 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.

cGiesen
Posts: 188
Joined: Wed Jul 18, 2012 7:53 am
Location: Bochum, Germany

Re: How to read the code

Post 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.

User avatar
treym
Posts: 258
Joined: Sat Jul 21, 2012 12:28 am

Re: How to read the code

Post by treym »

i did no realize this was about macro , sorry for missing the point ..

Post Reply