3 years, 5 months ago.

configuring inputs/outputs on condition?

hi i wanted to do some variants - configurations of program depending on board elements. wanted to send number that corespondes with configuration and it will work a little different depending on configuration. one configuration supposed to work with 2 more outputs - or all other without them.

is it possible to configure for example output in if statement like:

if (konfiguracja==0x00||konfiguracja==0x01||konfiguracja==0x03)DigitalOut wy5(PA_12,PullUp);

or maybe disabling outputs if something is configuring?

1 Answer

3 years, 5 months ago.

Hello,

You can try to use the C preprocessor for example as follows:

#define KONFIGURACJA  1

#if KONFIGURACJA == 0
DigitalOut  wy5(PA_12, PullNone);
DigitalOut  wy6(PA_13, PullNone);
#elif KONFIGURACJA == 1
DigitalOut  wy5(PA_12, PullUp);
DigitalOut  wy6(PA_13, PullDown);
#elif KONFIGURACJA == 2
DigitalOut  wy6(PA_13, PullDown);
#elif KONFIGURACJA == 3
DigitalOut  wy5(PA_12, OpenDrainPullUp);
#endif

Accepted Answer

should i do it in additional header file or somewhere in main? im just starting with mikrokontrolers and programming so im green in it.

posted by J B 25 Apr 2018

it works! thank you

posted by J B 25 Apr 2018

To answer your question, in main.cpp will work fine as you've probably discovered.

You could put it in a separate header file and include that in main if you wanted to, it would work but for this specific use that would be a bad idea.

The way c works is that the #include lines in a file are effectively replaced with he contents of those files when compiling. So putting the code in a header and including it ends up being exactly the same as including the code directly.

Where headers are useful is if you want to include the same lines in multiple files. e.g. if you put function declarations in a header then you can access those functions from any file that includes the header.

Where headers cause problems (and why a header isn't a good idea for this situation) is that if you create anything (in this case an output pin) in a header and then include that header in two different .c / .cpp files then it will try to create it twice. It will then realize that your program includes two output pins with the same name and report an error.

If you want an output pin visible in two different c files (e.g. so you can keep this configuration detail out of main.cpp) then you use the extern keyword in the header to indicate that a DigitalOut with that name exists but is defined somewhere else. You then put the definition of the pins in a .c file somewhere. This also works for variables, if you want a global variable visible to multiple .c files e.g.

pinSetup.h

// ensure this doesn't somehow get included twice in the same c file...
#ifndef __PINSETUP_H__
#define __PINSETUP_H__ 

//needed for the definition of DigitalOut
#include "mbed.h"

#define KONFIGURACJA  1
 
extern DigitalOut  wy5;
extern DigitalOut  wy6;

#endif

pinSetup.cpp

#include "mbed.h"
#include "pinSetup.h"

#if KONFIGURACJA == 0
DigitalOut  wy5(PA_12, PullNone);
DigitalOut  wy6(PA_13, PullNone);
#elif KONFIGURACJA == 1
DigitalOut  wy5(PA_12, PullUp);
DigitalOut  wy6(PA_13, PullDown);
#elif KONFIGURACJA == 2
DigitalOut  wy6(PA_13, PullDown);
#elif KONFIGURACJA == 3
DigitalOut  wy5(PA_12, OpenDrainPullUp);
#endif

main.cpp

#include "mbed.h"
#include "pinSetup.h"

// output pins are already defined...

posted by Andy A 25 Apr 2018