I am using a mbed board(which uses microcontroller LPC1768). In mbed, p1.18, p1.20, p1.21, p1.23 are connected to LED1, LED2, LED3 and LED4 on the board respectively.
Follow the code, It set the p1.18, p1.20, p1.21, p1.23 as output first, then LED1, LED2, LED3 and LED4 should be on, after a while, these four LEDs should be off.
However, when I download the program to the mbed, LED2 and LED4 are on and off, but the LED1 and LED3 are always off.
It’s a very weird behaviour. I tried to use bit-banding but the result was same. Then I tried to swap some lines. As a result some other LEDs did not blink :
I also try to simulate this program on keil uvision5. I compiled the program using keil Compiler ‘V5.06 update 6 (build 750)’, optimiztion set to -O0. In simulation, it works well, the corresponding value of LED1 and LED3 in FIOPIN register are on and off. But after download the program to mbed, it cannot work correctly, LED1 and LED3 still cannot blink.
But everytime I create a new uvision project and paste the code to the new project, using v5.06 compiler and set the optimization to -O0, it works well, all LEDs blink. However, when i add some new lines to the code, and then compile, LED1 and LED3 cannot blink again… It’s weird.
I searched this problem on Internet. Someone said using FIOPIN to change the status of the GPIO is not recommended, because it will cause undesired or unexpected result. But I don’t know why?
In the code below first a variable is modified step by step and then the FIOPIN register is set in a single assignment. It’s amazing that in this case all LEDs blink correctly: