In embedded systems, hardware registers are used to control devices. Why is this method used?
Think about how the CPU communicates with hardware devices and the speed needed.
Hardware registers are special memory locations mapped to devices. They allow the CPU to quickly change device behavior by reading or writing small amounts of data directly.
What is the output of this embedded C code controlling a hardware register?
volatile unsigned int *REG = (unsigned int *)0x40021000; *REG = 0x01; if (*REG & 0x01) { printf("Device ON\n"); } else { printf("Device OFF\n"); }
Check what value is written and then read back from the register.
The code writes 0x01 to the register and then checks if the least significant bit is set. Since it is, it prints "Device ON".
Find the error in this code that tries to set a hardware register bit:
unsigned int *REG = 0x40021000; *REG |= 0x02;
Look at how the pointer is assigned the address.
The pointer is assigned an integer without casting to a pointer type, causing a compilation error. It should be cast to (unsigned int *).
Choose the correct code snippet to set bit 3 of a hardware register at address 0x40021000.
Remember to use volatile pointer and bitwise OR to set bits.
Option C correctly declares a volatile pointer to the register and sets bit 3 using bitwise OR. Others either misuse pointer or variable types or use incorrect operations.
Given the following code, how many bits are set to 1 in the hardware register after execution?
volatile unsigned int *REG = (unsigned int *)0x40021000; *REG = 0x0F; *REG &= ~(1 << 1); *REG |= (1 << 4);
Track the bits step-by-step: start with 0x0F, clear bit 1, then set bit 4.
Start: 0x0F = 00001111 (bits 0,1,2,3 set). Clear bit 1: 00001111 & ~(00000010) = 00001101 (bits 0,2,3 set). Set bit 4: 00001101 | 00010000 = 00011101 (bits 0,2,3,4 set). Total 4 bits set.