Embedded C Programming Flashcards
Cards pertaining to Embedded Programming Containing Various Lessons Learned throughout My Experiences
UnSet a specific bit in a byte (set to 0)
targetByte &= ~(1 «_space;n); //Shifts a 1 n bits left then inverts then AND’s the values and sets the source byte to the result.
Toggle a specifc bit in a byte (1=0 | 0=1)
targetByte ^= 1 «_space;n; //Exclusive OR the bit shifted 1 with the source byte.
Bitwise “OR” Operator and its’ use.
Or Operator is the pipe : “ | “
It is used to set individual bits, for example
REG = (1«19) | (1«12); // 19th and 12th bits are set to ‘1’ , rest are Zeros.
Set 3rd bit of byte to 1 using bit shift…
(1«3)
Operator for inverting bits
”~”
Bit-wise Negation Operator
Set 11th and 4th bit of a register to 1…
REG |= (1 «_space;11) | (1 «_space;4);
Syntax and format of ternary operator…
result = binaryCondition ? valueReturnedIfTrue : valueReturnedIfFalse;
”~”
Bit-wise Negation Operator, used for the inversion of bits.
Check value of individual bit in byte
(targetByte»_space; n) & 1; //Compares shifted value to see if AND’d byt with 0x01 returns a 1
Set a specific bit in a byte
targetByte |= (1<
How do you set a subset of bits in a value?
Bitwide OR'ing uint8_t mask = 0x0F; //00001111b uint8_t value = 0x55; //01010101b output = mask | value; value; output = 0x05; //00000101b
Proper use scenarios for the “voltaile” keyword
A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:
- Memory-mapped peripheral registers
- Global variables modified by an interrupt service routine
- Global variables accessed by multiple tasks within a multi-threaded application
How do you extract a certain subset of bits from a value?
Bitwide ANDing uint8_t mask = 0x0F; //00001111b uint8_t value = 0x55; //01010101b output = mask & value; output = 0x05; //00000101b
common uses : extracting individual bytes from a word (would utilize shifting as well)
Binary “AND” operator and it’s uses
“&” Binary AND Operator
Used to check condition of individual bits, usually performed using bit masking. For example:
reg1 = 0x2A
a = reg1 & (1 «_space;3); //0x2A & 0x
”|=” uses
Used to set individual bits of a byte without interfering with the status of other bits. This is done by masking