7 years, 8 months ago.

FRDM K64F: "calling" the pins in sequence

Hi everyone! :D

Another noobish question for you. Is it possible to create a loop that uses the various output pins of the board in sequence? I mean, the pin callouts for the FRDM k64f are D0, D1 and so on... If they were just integer numbers I can create a loop with a counter that uses the pins in sequence, while if they're called with the letter D in front I don't know if that's possible to do.

The evil plan is that of recreate a simple exercise in which a few LED lights will turn on in sequence, but I don't think that writing something of the type below is smart enough xD

led1 =!led1 wait(...) led2=!led2 wait(...)

[...]

led8 = !led8

Thanks in advance :)

2 Answers

7 years, 8 months ago.

Just FYI, D0, D1, etc. already map to integers. They are just not sequential :-) You could do something like this though:

uint8_t pins[8] = { D0, D1, D2, D3, D4, D5, D6, D7 };

// loop over all pins in the array
for (size_t ix = 0; ix < sizeof(pins) / sizeof(pins[0]); ix++) {
  // toggle the pin
  pins[ix] = !pins[ix];
}

Accepted Answer

You need to create a DigitalOut for each pin (another array and loop) and then toggle those, rather than operating on the pin numbers themselves.

posted by Colin Hogben 11 Aug 2016

Also, for the K64F at least, the pin names are larger than will fit in 8 bits; you should use the PinName type, not uint8_t.

posted by Colin Hogben 11 Aug 2016

And it wouldn't actually work since writing to that array won't do anything.

posted by Erik - 11 Aug 2016
7 years, 8 months ago.

You can make an array of DigitalOuts which would allow for that. However this is already done for you in BusOut, which is probably the easiest solution for you: https://developer.mbed.org/handbook/BusOut

Exactly what I was looking for :) Thank you very much!

posted by Nicolò Bagarin 09 Nov 2016