Tuesday 22 March 2016

avr - Clearing Flag Bits By Writing 1


Most of the interface devices have various flag bits (that can also raise interrupts if that functionality is provided) used to check the status of the device/operation.


Usually they are cleared by writing a '1' to said bit.


Just some questions regarding this ...




  1. Why are they cleared by writing a 1? I mean if the flag is already set to 1 by the device, won't it make sense to clear it by writing a 0 to it?




  2. Lets say i have a 8 bit status register with bits distributed as (picked from datasheet of NRF24L01+)





enter image description here


If bit 5 (TX_DS) gets set (ie. turns to 1) and I want to clear it by writing 1 to it, what value do I write to the register.



Answer





  1. No, because then you could possibly clear other bits you didn't intend to by accident. It's a little hard to verbalize, but it is very easy to write a 0 simply by, well, writing a 0. You must deliberately choose to write a 1.





  2. Since every position expects a 1 in order to clear it, write a 1 only to bit 5:


    STATUS = _BV(TX_DS);


No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...