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.

embedded C junior doubts?

Status
Not open for further replies.

mImoto

Full Member level 4
Joined
Feb 21, 2002
Messages
210
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
1,733
Hello,

I have this two doubts and I don't know where to find an answer.

First:

I would like that if pcfg_temp is different from 0b00000111 or different from 0b00001110 or different from 0b00000100 or different from
0b00000010 then something happens. I have used the following coding but I am not sure.

if (pcfg_temp != ( (0b00000111) || (0b00001110) || (0b00000100) || (0b00000010) ) )
{
.....
}

My doubt is that the result of (0b00000111) || (0b00001110) || (0b00000100) || (0b00000010) ) is boolean so maybe this is not the correct way. Any suggestion?

And second:

void function()
{
union READ_DATA tris_desired;
union READ_DATA tris_now;
union READ_DATA bidir_now;
union READ_DATA tris_final;
union READ_DATA error_final;
BYTE tris_temp = 0x00;
BYTE port_id = 0x00;
BYTE pin_id = 0x00;


tris_desired.ReadData = 0x00;
tris_now.ReadData = 0x00;
bidir_now.ReadData = 0x00;
tris_final.ReadData = 0x00;
error_final.ReadData = 0x00;
....

}

If I use this then the compiler works ok but if I use instead->


void function()
{
union READ_DATA tris_desired;
union READ_DATA tris_now;
union READ_DATA bidir_now;
union READ_DATA tris_final;
union READ_DATA error_final;


tris_desired.ReadData = 0x00;
tris_now.ReadData = 0x00;
bidir_now.ReadData = 0x00;
tris_final.ReadData = 0x00;
error_final.ReadData = 0x00;


BYTE tris_temp = 0x00;
BYTE port_id = 0x00;
BYTE pin_id = 0x00;
....

}

The compiler tells me that tris_temp, etc are not declared ¿why?.

Thanks a lot in advance,

mimoto
 

Try this:

if((pcfg_temp != 0x07) && (pcfg_temp != 0x0d) && (pcfg_temp != 0x04) && (pcfg_temp != 0x02))
{
do something;
}

shouldn't you define your union:

union READ_DATA
{
BYTE tris_desired;
BYTE tris_now;
BYTE bidir_now;
BYTE error_final;
};

in C you have to declare all variables in a function before any code, thats why the compiler complains.
 

btbass said:
Try this:

if((pcfg_temp != 0x07) && (pcfg_temp != 0x0d) && (pcfg_temp != 0x04) && (pcfg_temp != 0x02))
{
do something;
}

BTW, all those constants (0x07, 0x0d, 0x04,...) have their meaning, so it is better to do something like:

#define LAMP_ON 0x07

if (pcfg_temp != LAMP_ON )
{
do something
}

After six months it is hard to remember what 0x07 means.

Tom
 

of course I agree, but he had not defined the constants.
#define is not so good these days?
Better is:

const unsigned char LAMP_ON = 0x07;
 

btbass said:
of course I agree, but he had not defined the constants.
#define is not so good these days?
Better is:

const unsigned char LAMP_ON = 0x07;

1. He should have defined those constants. That is the point.

2. I think not. #define just declares a simbol, while
const unsigned char LAMP_ON = 0x07;
defines a data variable which will be placed in ROM. Access to data in ROM cost some time in execution.

3. We are talking about C not C++.

Tom
 

I have a solution which will be pleased to all of you :

use enum...

why... ?

simple :

Enumerations provide a convenient way to associate constant values with names, an alternative to #define with the advantage that the values can be generated for you. Although variables of enum types may be declared, compilers need not check that what you store in such a variable is a valid value for the
enumeration. Nevertheless, enumeration variables offer the chance of checking and so are often better
than #defines. In addition, a debugger may be able to print values of enumeration variables in their

enum escapes { BELL = '\a', BACKSPACE = '\b', TAB = '\t',
NEWLINE = '\n', VTAB = '\v', RETURN = '\r' };
symbolic form.
 

manitooo said:
Enumerations provide a convenient way to associate constant values with names, an alternative to #define with the advantage that the values can be generated for you.

For discontinued values use #define since autogenerating values for enums can be error prone (if you miss order of simblos in enums declaration). Otherwise, enums are convenient way to assign meaningful names to constants.

Tom
 

In embedded C you should use #define NAME
const will allocate memory for it in your program memory and most µC don't have allot of space!
by using #define the preprocessor will substitute the value before you compile so actually it is the same as writing it with the value. But it's a bit more clear.

I think enum's are handled the same way as defines but i'm not sure about that.

const might be good in C++ but the power of const isn't in defining constants but in defining const member functions, const iterators, ...

Antharax
 

emum types usually are defined as integer (2 bytes long!) If you use #define's you can define single byte constants. It is very important, when you have 8bit controller with small memory.
 

The reason I suggested const is that when you do static analysis with lint or use a debugger, then you see the const name.
If you use #define, you just see the meaningless number.
If memory is a problem, use #define.
If you do static analysis or use a debugger, use const.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top