Attiny84A need clarification about integer comparing

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,

 

I have question regarding my program. It's simple program, TIMER0 is configured as CTC and every 1ms it subtracts uint32_t time1 variable. When time1 variable reaches 0 task1 is executed which simply toggles PORTA0 and time1 is again set to begginer value.

Program works great when time1 is smaller than 255, when it's bigger it sometimes execute if statement which goes as : if (time1 == 0) {} when time1 is 255. I don't understand why that's happening. I avoided problem by configure if statement as follows: if (time1 == 0 && time1 != 255) {} and now it works as intendent but i still don't understand why sometimes if statement set 255 == 0 as true.

Clarification would be helpfull.

 

you can see program in attachement.

Attachment(s): 

This topic has a solution.
Last Edited: Sun. Jan 12, 2020 - 11:33 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You’re not accessing the variable time1 atomically. I wrote a tutorial on the ‘traps when using interrupts’. Google this for the solution.

Last Edited: Sat. Jan 11, 2020 - 11:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks on answer. I looked up your tutorial and it's very helpfull. I understand now why this happens :D I disabled interrupts before IF statement and enabled after and now it works.

 

Thanks again :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't forget to mark the solution.