Global Pin Assignments

26 Nov 2009

Hi Everyone,

I have a question on the Mbed pin assignments. I am writing a program to interface with an ADC. I have split the program over 3 files, the first sets up the ADC, the second reads it and the third (main.cpp) takes in tha data and prints it to a pc.

I know I can use global variables by using the extern declaration, but is there some way I can make global pin assignments as the same pins are used in the setting up and reading of the ADC, it seems a bit silly to have to redeclare them all using different names!

Regards

Martin 

26 Nov 2009 . Edited: 26 Nov 2009

Hi Martin,

To allow multiple users of an interface over different files, you can use extern as you say. In the following example, the objects are created and pins are assigned in one place (the .cpp file), and the externs in the header file allow any other file to #include them and therefore get access to them.

[interfaces.cpp]

#include "interfaces.h"

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

[interfaces.h]

#ifndef INTERFACES_H
#define INTERFACES_H

#include "mbed.h"

extern DigitalOut led1;
extern DigitalOut led2;
extern DigitalOut led3;
extern DigitalOut led4;
 
#endif 

See the whole program for a full example:

GlobalPinAssignments

But i'm not sure if this exactly answers your question? If not, can you provide the question by way of an example?
Simon
26 Nov 2009

Hi,

Simon beat me to it.

I had this kicking around, just cleaned it up and tested it.

GlobalObjects

Cheers,
Chris

26 Nov 2009

Thanks guys, this is exactly what I was on about.

I was putting the pin number in the extern statement again, e.g.  extern DigitalOut mypin(15)

Oops, just tried it as you suggested and it works fine.

26 Nov 2009

You can also pass pointers of DigitalOut or AnalogOut instances to your functions in other files.

BTW, this is something I noticed in some of the samples on the site... often the classes inherit from SPI or I2C etc. which can make things a bit complicated if you e.g. connect several devices to the bus. Here's what I'm doing in my class:

// use an existing I2C instance
MMCx12xM(I2C &i2c);
// create an I2C instance on specified pins
MMCx12xM(PinName sda, PinName scl);