Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 5 months ago.
enumerate IO pins.
Hi,
I' m trying to optimize my "hello_world code:
int main()
{
uart1.baud(921600);
while(1) {
myled1 = 1;
uart1.printf("LED1 aan\r");
wait(0.2);
myled1 = 0;
myled2 = 1;
uart1.printf("LED2 aan\r");
wait(0.2);
myled2 = 0;
myled3 = 1;
uart1.printf("LED3 aan\r");
wait(0.2);
myled3 = 0;
myled4 = 1;
uart1.printf("LED4 aan\r");
wait(0.2);
myled4 = 0;
}
} // endmain
How can I implement a for() loop to sequentially perform the same action: I tried:
/* Libraries en classes insluiten */
#include "mbed.h"
/* Definieer seriële poorten */
Serial uart1( p9, p10); // tx, rx communicatie via seriele poort
/* Definieer digitale IO */
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);
/* Start hoofprogramma */
int main()
{
uart1.baud(921600);
while(1) {
for ( int i = 1; i < 5; ++i) {
myled[i] = 1;
uart1.printf("LED %i aan\r", i);
wait(0.2);
myled[i] = 0;
}
}
} // endmain
Which of course doesn't work. How do I mplement such a feature? Can I define a vector with the four leds in it?
1 Answer
9 years, 5 months ago.
Yes, you can define an array of LED DigitalOut pins or you can use the BusOut type. See here for a similar example.
Thank you Wim,
That was exactly what I was looking for.
I was already irritating myself with a solution using PortOut() with integer constants holding binary patterns for every separate LED. But this is far easier.
Can I address LEDS and PINS on a LPC1768 also using the Px.y syntax where X is port and Y is pin, like some other mbed controllers do?
Regards, marout
posted by 01 May 2016You can select any LPC1768 pin by using pinnames like Px_y. For example P0_1 for Port 0, pin 1. The pinnames shown on the LPC1768 platform page (eg p9, p10, I2C_SDA, USB_TX etc) are just shortcuts for the actual pinnames. These shortcuts are defined in mbed source PinNames.h.
posted by 01 May 2016