Set, Clear, Toggle and Check Bit Value in C

In the embedded system design, the system needs to check the input state and then send signal to other devices, or change the state of the outputs. Since each GPIO pin is connected to a different device (some pins are input and other pins may be output or other functions), it is not possible to check the pin state by comparing with a constant value. Setting, clearing and toggling pins also have the same situation: constant values can not be assigned directly to the port DATA register. Because each GPIO pin is associated with the corresponding bit in the port DATA register, a bitwise operation can be used to solve these issues.

EmbeddedSystem01
Figure 1
: Example of Embedded System

 

 


Bit Access Functions

The following functions get or set a particular bit of a variable can make programs easier to read.