2019-03-31 05:58 PM
Hi,
I am working on a project, turning on an LED in push-pull mode when a switch in pull-up mode is pressed. The LED only lights up once. I am using IAR and C.
Everything works perfectly fine except that, sometimes when the LED is connected to power with the switch NOT pressed, the LED would flash.
Our group suspects it to be a problem of the circuit because it's a complicated circuit and we're only using some of its functions. But because it's my first time to use STM8S, I am not so confident that my program is flawless.
Could anyone helps me check if there's any problem with the program?
Thank you very much!
2019-04-06 08:01 AM
Hello Corvette!
Do you still have the problem?
Well, I don't know the usage of IAR compiler and it's definitions or macros, and apart from the fact that your program doesn't really make sense (in any way) - but it should be functional, I'm curious how the following instruction from your code translates to assembler:
PC_ODR_bit.ODR5 = !PD_IDR_bit.IDR5; //PC5 reads if Switch PD5 is pressed
Could you post the compiler's output?
And maybe you should simply try an if-else in your lightup(void) function:
if (PD_IDR_bit.IDR5) {
//button not pressed
} else {
//button pressed
//set here the LEDs output ...
}
Apart from the program, the internal pull-up resistor of the port is about 45 kΩ, so if you have anything externally that could pull down the pin, parallel to the button, you could get false triggers.