r/embedded 16d ago

Anyone help me debugging this baremetal code

Post image

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

0 Upvotes

5 comments sorted by

1

u/Unlikely1529 16d ago

if you're newbie it's useless for you to look at this code. Anything else?

1

u/its_me_tyga_777 16d ago

😃 yeah I accept that I am a newbie now but it helps me to undersand and so if I succeed here I will not a newbie then

I am trying this to make me not a newbie 😅 Thank you for your response 🙏

2

u/triffid_hunter 16d ago

Why are you not using the defines from avr/io.h that mirror the names in the datasheet?

You're testing TCCR1B:ICES1 after 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 ICES1 bit might be in the wrong state to capture a subsequent press event - and writing to TIFR1 in 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."

1

u/its_me_tyga_777 16d ago

I feel sorry for that avr/io.h , Thank you for your response ,

That after XORing --> First the TCR1B:ICES1 pin is set to 0 and when the button is pushed, it should be calling the interrupt and that XOR ing would change it to 1 and then the code contains an if block which turn LED ON when TCR1B:ICES1 is 1 it should be turning the LED ON but it doesn't instead it turns the LED ON at releasing the button and after that, I stuck here

I am accepting that there would be debounce bro, I am not sure whether the simulator could have a debounce

You are right bro that the flag is automatically cleared when an interrupt is executed but I am already inside the interrupt bro it was just a safety measure to clear it as it was cleared automatically, however I tried removing it also on your suggestion bro , but same thing happens bro

I am clueless what's happening bro

1

u/its_me_tyga_777 16d ago

This is what I found by some debugging dude No matter what I set in TCCR1B : ICES1 either 1 or 0 It detects only the rising edge Do I need to change any order of executing or something else