I have simplified the listing to bare minimum where I can still observe this behaviour.
When this program is running, it works as expected. Button press causes LED to show up.
The problem exist on first run: when I run it first time (powering up) or resetting the processor the body of if (!(PINC & (1<<PC0)))
is executed despite that the condition is not met (button not pressed).
Code:
#include <avr/io.h>
#include <util/delay.h>
int main(void) {
DDRB |= (1<<PB5); // pin for LED as output
DDRC &= ~(1<<PC0); // to ensure PC0 is input - btn here, shorts to gnd when pressed
DDRD |= (1<<PD0)|(1<<PD1); // debug LED1 and LED2
while(1) {
PORTC |= (1<<PC0); // pull-up
asm( "nop " );
asm( "nop " );
if (!(PINC & (1<<PC0))) { //btn pressed? shorts to gnd
PORTB |= (1<<PB5); // LED on <-- executed after RESET!
// here comes extra code to move stepper motor
}
else {
PORTD |= (1<<PD1); // dbg LED on
}
_delay_ms(2000);
PORTB &= ~(1<<PB5);
PORTD &= ~(1<<PD1);
}
}
Is here something I am not aware of?
Extra info: this would be a small nuisance if comes as LEDs only, but I want some step motor movement (or rather - NO movement). This execution is undesired just after powering the processor up.
EDIT
More info: The hardware is Arduino UNO SMD.
Code is written and compiled with Arduino IDE.
Wires connected between pushbutton switch and the PC0 (or A0, using Arduino description) with about 15cm long wires. This pushbutton shorts PC0 to gnd.
No external (hardware) pullup/pulldown.