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.
10 years, 4 months ago.
GPIO Difficulties
Greetings,
I have the following code:
include the mbed library with this snippet
#include "mbed.h" DigitalIn dig_in1(PTB0); DigitalOut led(LED_BLUE); Serial pc(USBTX, USBRX); int main() { int dig_in1_status = 0; while(1) { if(dig_in1 != dig_in1_status) { dig_in1_status = dig_in1; pc.printf("Digital Input 1 Status %d\n\r", dig_in1_status); if(dig_in1_status == 1) { led = 1; pc.printf("Digital Input 1 On\n\r"); } else { led = 0; pc.printf("Digital Input 1 Off\n\r"); } wait(0.25); } } }
My expectation is that when I short PTB0 to ground, I get the LED to light up and a serial output that "Digital Input 1 On" and vice versa when I release the pin.
The LED is working as expected but the text output to the com port seems to be reversed. Anyone know why? Also, on startup, the LED blinks once and I think the logic asserts which I don't want to happen.
Any ideas how to correct that?
Thanks!
2 Answers
10 years, 4 months ago.
It's basic problem.
Mechanical switch is bounding at each on/off.
So if you get change port status ,wait for 30-50mS till stable.
And
if(dig_in1 != dig_in1_status) { dig_in1_status = dig_in1;
You read dig_in1 twice,each status is not same status so that Switch is Bounding.
I don't follow. If the switch is bouncing, why would the LED out work as expected (On when asserted and held, off when let go) but the serial text be opposite?
posted by 30 Jun 2014my comment means
int main() { int temp1,temp2; int dig_in1_status = 0; while(1) { do{ temp1 = dig_in1; wait(0.050); temp2 = dig_in1; }while (temp1 != temp2);// wait for stable if(temp1 != dig_in1_status) { dig_in1_status = temp1; pc.printf("Digital Input 1 Status %d\n\r", dig_in1_status); if(dig_in1_status == 1) { led = 1; pc.printf("Digital Input 1 On\n\r"); } else { led = 0; pc.printf("Digital Input 1 Off\n\r"); } wait(0.25); } } }
10 years, 4 months ago.
The LED is reversed: On the KL25 (and quite some other boards also btw), the LED is on when supplied with a low signal, and off when it gets a high signal.
I wouldn't know why it then works inverted from how you expect, are you sure that is the case?
This would somewhat explain it. I thought I was going nuts! Right now, I have PTB0 connected to nothing and LED is off. I guess I should clear up my nomenclature. Right now, when I connect PTB0 to ground, I am considering that a "low". Perhaps I should be considering that a "high" then?
posted by 30 Jun 2014No that shouldn't be the case. But are you sure PTB0 is now a high? That would be the case if PullUps are enabled, but I don't actually know if it enables those by default.
Maybe check if you print directly the dig_in1.read() value (you need to add .read if you print it with printf, otherwise it will print the address of the dig_in1 object).
posted by 30 Jun 2014
What's your target ? what is the logic for LED (how is connected) ?
posted by Martin Kojtal 30 Jun 2014The LED is the tri-color LED built into the KL25Z FRDM board. Basically, the function I want is that if I assert a digital input to ground, the LED lights up and I get a message out of the USB/Serial port. When the digital input to ground is released, I want the LED to turn off and a different message to display. The LED part works fine. The message to the serial port seems reversed.
posted by Control Guy 30 Jun 2014