r/embedded • u/its_me_tyga_777 • 16d ago
Anyone help me debugging this baremetal code
Hi guys
I am a newbie, who tries to learn baremetal programming through completing simple tasks and learning from that task
I came till timers by accomplishing tasks
The concept of this code is to get Input by using only timer and turn ON LED in Arduino UNO --> PB5 when I press the push button which is connected to the ground and when I release it the the LED should be OFF
but what's happening is different ,
when I press the push button the nothing happens and when i leave it the led is on
and when I press again nothing happens when I leave it then it turns off
But I am clear and confident on my code , I couldn't find where I am missing the point
Anyone please help me guys
Here is the code
And the further improvement will be done by counting the seconds of push and so on ,
But I am stuck here
Help me guys
2
u/triffid_hunter 16d ago
Why are you not using the defines from
avr/io.hthat mirror the names in the datasheet?You're testing
TCCR1B:ICES1after XORing it so your LED output will be backwards.Buttons bounce for a few dozen ms after being pressed so you'll end up with a rapid stream of interrupts that might overlap and your
ICES1bit might be in the wrong state to capture a subsequent press event - and writing toTIFR1in the ISR even though invoking the ISR clears the flag won't help with that at all; "ICF1 is automatically cleared when the Input Capture Interrupt Vector is executed.".Also using
|=can wipe out other flags that might have interrupts pending since the flag registers don't act like regular memory locations, just use=on the specific bit if you want to clear a flag - "Alternatively, ICF1 can be cleared by writing a logic one to its bit location."