Would it be possible to define an array of DigitalOut pins?
Topic last updated
11 Jun 2012, by
Daniel Dai.
5 replies
array,
Digital I/O
Hi, All,
I am wrting a programe to control a group of stepper motors and each motor requires 4 wires (CLK, ENaBle, DIRection, FULlstep).
Now I havev to define the DigitalOut pins as follows:
DigitalOut clk1(p21);
DigitalOut enb1(p23);
DigitalOut dir1(p25); / / 0 clockwise, 1 anti-clockwise
DigitalOut ful1(p27);
DigitalOut clk2(p22);
DigitalOut enb2(p24);
DigitalOut dir2(p26); / / 0 clockwise, 1 anti-clockwise
DigitalOut ful3(p28);
In the program, I have to repeat the samilar codes several times.
I wonder if it is allowed to define an array of DigitalOut, something like
DigitalOut clk[2](p21,p22);
DigitalOut enb[2](p23, p24);
Thus I can reuse the codes by access the I/O pins by index, rather by the name. e.g. clk[0] will be p21, clk[1] will be p22.
However, compiling the codes above reports errors:
'A value of type "PinName" cannot be used to initilize an entity of type mbed::DigitalOut[2]'
Is there any construction function of DigitalOut to get this job done? Any comments are apprecaited indeed.
Dan
Replies
Find a way walk around. Using an array of pointer.
// variable definition
DigitalOut clk1[p21];
DigitalOut clk2[p22];
DigitalOut *p[2];
In the programme,
main()
{ ...
// initialized the pointers
p[0]=&clk1;
p[1]=&clk2;
....
// access to clk1, 2 .... n, and reverse it
for (i=0;i<n;i++)
*p[i]=!(*p[i]);
...
}
I see it is a bit odd, but it works. Please let me know if you have any better and clear solution.
Hi,
In my code i'm using a simple static array like (no pointers required):
DigitalOut Do[6] = {
DigitalOut(p11), //DO1 - Lower Cylinder
DigitalOut(p12), //DO2 - Upper Cylinder
DigitalOut(p13), //DO3 - Slack
DigitalOut(p14), //DO4 - Lower Clamp
DigitalOut(p15), //DO5 - Upper Clamp
DigitalOut(p16) //DO6 - Seal Pressure
};
int Dos = (sizeof(Do) / sizeof(Do[0])) - 1;
for (int i=0; i<Dos; i++) {
Do[i]=1;
}
wvd_vegt
Hi,
In my code i'm using a simple static array like (no pointers required):
DigitalOut Do[6] = {
DigitalOut(p11), //DO1 - Lower Cylinder
DigitalOut(p12), //DO2 - Upper Cylinder
DigitalOut(p13), //DO3 - Slack
DigitalOut(p14), //DO4 - Lower Clamp
DigitalOut(p15), //DO5 - Upper Clamp
DigitalOut(p16) //DO6 - Seal Pressure
};
...
wvd_vegt
Appreciated. That's much better and clever.
You don't even have to call the constructors explicitly. The following should work as well:
DigitalOut Do[6] = {
p11, //DO1 - Lower Cylinder
p12, //DO2 - Upper Cylinder
p13, //DO3 - Slack
p14, //DO4 - Lower Clamp
p15, //DO5 - Upper Clamp
p16 //DO6 - Seal Pressure
};
You don't even have to call the constructors explicitly.
DigitalOut Do[6] = {
p11, //DO1 - Lower Cylinder
...
p16 //DO6 - Seal Pressure
};
What is the difference between using
DigitalOut p11;
and
DigitalOut(p11);
and
DigitalOut varName(p11);
Please log in to post a reply.
Hi, All,
I am wrting a programe to control a group of stepper motors and each motor requires 4 wires (CLK, ENaBle, DIRection, FULlstep).
Now I havev to define the DigitalOut pins as follows:
In the program, I have to repeat the samilar codes several times.
I wonder if it is allowed to define an array of DigitalOut, something like
Thus I can reuse the codes by access the I/O pins by index, rather by the name. e.g. clk[0] will be p21, clk[1] will be p22.
However, compiling the codes above reports errors:
'A value of type "PinName" cannot be used to initilize an entity of type mbed::DigitalOut[2]'
Is there any construction function of DigitalOut to get this job done? Any comments are apprecaited indeed.
Dan