Unit 3 - Practice Quiz
1 Which register is primarily used to configure a GPIO pin on a PIC microcontroller as either an input or an output?
2 To configure pin RB3 of a PIC microcontroller as an output, what value should be written to the corresponding bit in the TRISB register?
3 What is the main purpose of placing a resistor in series with an LED when connecting it to a PIC microcontroller pin?
4 When interfacing a simple push-button switch to a PIC input pin, what component is used to ensure a defined logic level (HIGH or LOW) when the switch is open?
5 Which register is used to read the current logic state (HIGH or LOW) of the physical pins of a GPIO port?
6 To configure pin RA5 of a PIC microcontroller as an input, what value should be written to the corresponding bit in the TRISA register?
7 In C programming for PIC, which bitwise operator is used to set a specific bit of a register to 1 without affecting the other bits?
^)
&)
~)
|)
8 To implement a simple 4-bit binary up-counter on pins RB0-RB3, what is the most common operation performed on the PORTB register inside the main loop?
PORTB = ~PORTB)
PORTB--)
PORTB++)
PORTB = 0)
9 If an LED is connected between a PIC I/O pin and Ground (sourcing current), what logic level must the pin be set to in order to turn the LED ON?
10 A switch is connected to an input pin with a pull-up resistor. The other end of the switch is connected to Ground. What logic level will the PIC read when the switch is pressed (closed)?
11 Which C statement correctly clears bit 2 (the third bit from the right) of PORTB without changing any other bits?
PORTB = PORTB | (1<<2);
PORTB = ~(1<<2);
PORTB = PORTB & (~(1<<2));
PORTB = PORTB ^ (1<<2);
12 If you want to configure all 8 pins of PORTB as outputs, what value should you load into the TRISB register?
13 Writing a value to the PORTB register has an immediate effect on which pins?
14 If an LED is connected between a PIC I/O pin and VDD (sinking current), what logic level must the pin be set to in order to turn the LED ON?
15
What is the function of the bitwise XOR (^) operator when used on an I/O register bit?
16 If an 8-bit binary counter is implemented on PORTB, what is the maximum decimal value it can display before it overflows (resets to 0)?
17 To read the state of a switch connected to pin RC4, which specific register and bit would you need to check in your code?
18 The acronym TRIS, used for the data direction register in PIC microcontrollers, stands for what?
19
Consider the instruction TRISB = 0b11110000;. How are the pins of PORTB configured?
20 To start a binary counter from the beginning, what initial value should be written to the PORT register connected to the LEDs?
21
In a PIC microcontroller, after a Power-On Reset (POR), the TRISB register is typically initialized to 0xFF. If you then immediately execute the instruction PORTB = 0x55;, what will be the state of the PORTB data latch and the physical pins?
0x55.
01010101.
22
Consider the following C code snippet for a PIC18F microcontroller: TRISB = 0b11110000;. Which of the following statements accurately describes the configuration of PORTB?
23 Which of the following C expressions is the most efficient and standard way to toggle the state of pin RC2 (bit 2 of PORTC) without affecting any other pins on PORTC, while avoiding Read-Modify-Write issues?
24 You need to interface an LED with a forward voltage of 2.0V and a desired forward current of 15mA to a PIC GPIO pin. The PIC operates at a VDD of 5.0V and the pin's output high voltage () is guaranteed to be at least 4.2V. What is the minimum standard E24 series resistor value you should use?
25
A 4-bit binary counter is implemented on pins RB0-RB3 of a PIC microcontroller. The counter increments every second. If the current value displayed by the LEDs connected to these pins is 1110 (binary 14), what will be the value displayed after 3 seconds?
26 A push-button switch is connected to pin RA0 of a PIC microcontroller. A 10k pull-up resistor is connected between RA0 and VDD (5V). When the switch is not pressed, it is an open circuit. When pressed, it connects the pin to Ground. What logic level will the microcontroller read at RA0 when the switch is not pressed, and what level when it is pressed?
27
When performing a read-modify-write operation (e.g., PORTBbits.RB0 = 1;) on an output pin in a PIC18F microcontroller, why is it generally recommended to write to the LATB register instead of the PORTB register?
28 You want to use pin RA0 on a PIC16F877A as a digital input. This pin is also shared with the analog function AN0. What is the correct sequence of register configuration to ensure it functions purely as a digital input?
29
A byte variable port_val representing a PORT register is initialized to 0b10101100. The following sequence of C operations is performed on it:
1. port_val &= ~(1 << 2);
2. port_val |= 0x03;
3. port_val ^= (1 << 7);
What is the final hexadecimal value of port_val?
30
An LED is connected between a PIC's GPIO pin (RD0) and VDD (5V), with a current-limiting resistor in series. This is a "current sinking" configuration. To turn the LED ON, what value must be written to the LATD0 bit, and why?
0 to LATD0 to create a path to ground, allowing current to flow from VDD through the LED.
1 to LATD0 to source current from the pin to the LED.
1 to LATD0 to match the VDD potential, preventing current flow.
0 to LATD0 to put the pin in a high-impedance state.
31
An 8-bit binary counter is implemented on PORTB. The counter is incremented inside a timer interrupt service routine that executes exactly every 10 milliseconds. What is the total time it will take for the counter to cycle from 0x00 through 0xFF and back to 0x00?
32 You are implementing a software debouncing routine for a switch. A common technique involves detecting a pin state change, waiting for a short delay (e.g., 20ms), and then checking the pin state again. Why is this second check necessary?
33
On many modern PIC microcontrollers, what is the primary purpose of the WPUx (Weak Pull-Up) register?
34
You have configured the upper nibble of PORTD (RD4-RD7) as inputs and the lower nibble (RD0-RD3) as outputs. Which C code snippet correctly reads the state of the input pins and stores their 4-bit value in a variable input_val?
35
You want to set PORTC bits 4 and 5 to 1, and clear bits 0 and 1, without affecting any other bits (2, 3, 6, 7). Which single line of C code achieves this? Assume the initial state of PORTC is unknown.
36 To drive a 7-segment display using fewer I/O pins, a technique called multiplexing is often used. This involves rapidly switching between digits while displaying the correct segments for each. What principle allows the human eye to perceive this as a steady, complete number?
37
Consider the following C code intended to implement a simple 8-bit counter on PORTB.
c
void main() {
TRISB = 0x00;
LATB = 0;
while(1) {
PORTB = PORTB + 1;
_delay_ms(100);
}
}
What potential issue exists in this code, particularly if PORTB pins were driving capacitive loads?
while(1) loop will cause the microcontroller to hang.
LATB should be used instead of TRISB to initialize the output value.
PORTB for a read-modify-write operation, which can lead to errors.
38 A switch is connected to an external interrupt pin (e.g., INT0) to detect a button press. If no debouncing (either hardware or software) is implemented, what is the most likely consequence when the button is pressed once?
39
A PIC's PORTB pin, RB0, is configured as an output (TRISB0 = 0) and is driving a high signal (LATB0 = 1). What happens to the physical state of the pin if the program then executes TRISBbits.TRISB0 = 1; without changing LATB0?
LATB0 is automatically cleared to 0.
40
In a PIC C program, you see the line: LATA = (sensor_val > 100) ? 0xFF : 0x00;. What does this code accomplish?
sensor_val.
sensor_val is greater than 100, and turns them all off otherwise.
sensor_val is greater than 100.
41
A PIC microcontroller toggles an LED on RB7 using PORTB ^= 0x80;. Pin RB0 is also configured as an output and drives a highly capacitive load, causing its voltage to rise slowly. The initial state is LATB = 0x00. The code PORTB |= 0x01; is executed to turn on RB0. Immediately after, PORTB ^= 0x80; is executed to toggle RB7. Due to the slow rise time, the voltage at pin RB0 is still logic low when the toggle instruction reads the port. What will be the final state of the LATB register?
42
A pin RA0 on a PIC16F887 is configured as a digital output by setting TRISAbits.TRISA0 = 0;. An LED is connected to this pin. The following code is executed: LATA = 0x01;. However, the LED does not light up, and the voltage at RA0 measures near 0V. Which register is the most likely cause of this malfunction, assuming no hardware faults?
43
Pin RC0 of a PIC is configured as a digital input (TRISCbits.TRISC0 = 1;). It is connected to a 5V source via a 1kΩ resistor. After the system stabilizes, the code reconfigures the pin to a digital output (TRISCbits.TRISC0 = 0;) without first writing to LATCbits.LATC0. What is the immediate voltage level at the RC0 pin after it is switched to an output?
44
You need to create a C preprocessor macro SET_NIBBLE(reg, val) that writes a 4-bit value val to the upper nibble (bits 7-4) of an I/O register reg without altering the lower nibble (bits 3-0). Which of the following implementations is both correct and avoids potential side-effects of multiple evaluations of reg?
#define SET_NIBBLE(reg, val) reg = (reg | 0xF0) & ((val << 4) | 0x0F)
#define SET_NIBBLE(reg, val) reg = (reg & 0x0F) | (val << 4)
#define SET_NIBBLE(reg, val) reg &= 0x0F; reg |= (val << 4)
#define SET_NIBBLE(reg, val) reg = (reg & 0xF0) | (val & 0x0F)
45
Consider a Charlieplexing setup with 3 LEDs (L1, L2, L3) connected among three PIC I/O pins (P1, P2, P3). L1 is between P1(Anode) and P2(Cathode). L2 is between P2(Anode) and P1(Cathode). L3 is between P1(Anode) and P3(Cathode). To light up only L3, what must be the configuration of the TRIS and LAT registers for these three pins?
TRIS={P1=1, P2=0, P3=0}; LAT={P1=X, P2=1, P3=0}
TRIS={P1=0, P2=1, P3=0}; LAT={P1=1, P2=X, P3=1}
TRIS={P1=0, P2=1, P3=0}; LAT={P1=1, P2=X, P3=0}
TRIS={P1=0, P2=0, P3=1}; LAT={P1=1, P2=0, P3=X}
46
A 4-bit binary up-counter is to be implemented on pins RD3:RD0. However, to prevent simultaneous switching transients, the output must be a Gray code representation of the count. If the current binary count is 0110 (6), what single C expression will correctly calculate the next Gray code value to be written to LATD after the count increments to 0111 (7)?
gray_code = count ^ (count >> 1); where count is 7
gray_code = ~count; where count is 7
gray_code = count & (count - 1); where count is 7
gray_code = (count << 1) | (count >> 3); where count is 7
47
A switch is monitored on pin RB0. A software debouncing routine is implemented within a 1ms timer ISR. The routine uses a static 8-bit counter, debounce_count. If RB0 is low, the counter increments; if high, it resets. The switch is considered "pressed" only when debounce_count reaches 50. Ignoring counter overflow, what is the minimum duration the switch must be held stable in the low state for a press to be registered?
48
On a PIC16F690, pins RA0 and RA1 are configured as digital outputs (TRISA<1:0> = 0b00). You observe that writing LATA = 0x03 fails to make RA1 high. The ANSEL register has been correctly cleared for these pins. What is the most probable cause related to another peripheral?
RA1 as SDO.
RA1.
RA0 and RA1.
49
You are driving eight high-efficiency LEDs, each requiring 15 mA, directly from the 8 pins of PORTB on a PIC18F45K22. The datasheet specifies a maximum current sourced by any I/O pin as 25 mA, and a maximum total current sourced by PORTB as 100 mA. What is the most likely outcome of this design?
50
An application uses an interrupt service routine (ISR) to toggle an LED on RC0 every 1ms. The main loop independently sets pin RC1 high to indicate a status. Which code implementation presents the highest risk of a race condition leading to incorrect I/O behavior?
LATCbits.LATC1=1; / ISR: LATCbits.LATC0 ^= 1;
LATCbits.LATC1=1; / ISR: PORTCbits.RC0 ^= 1;
PORTCbits.RC1=1; / ISR: PORTCbits.RC0 ^= 1;
PORTCbits.RC1=1; / ISR: LATCbits.LATC0 ^= 1;
51
You are multiplexing a 4-digit, common-cathode 7-segment display. The segments (a-g) are connected to PORTD and the digit select lines (D1-D4) are connected to RA0-RA3. To display the number '2' on the 3rd digit (D3), what must be the state of PORTD and PORTA<3:0>? (Assume segment 'a' is bit 0, 'g' is bit 6, and the hex code for '2' is 0x5B).
PORTD = 0x5B; PORTA<3:0> = 0b1011;
PORTD = ~0x5B; PORTA<3:0> = 0b0100;
PORTD = 0x5B; PORTA<3:0> = 0b0100;
PORTD = ~0x5B; PORTA<3:0> = 0b1011;
52
The following C code is intended to implement a simple 8-bit binary counter on PORTB. What is the subtle error in this code that will lead to incorrect counting behavior, especially on older PICs or with noisy I/O?
c
TRISB = 0x00;
LATB = 0x00;
while(1) {
PORTB++;
delay_ms(100);
}
delay_ms(100) is too short for the port to stabilize.
LATB register should be used instead of PORTB for the increment operation.
TRISB register is cleared inside the loop, causing issues.
53
In a 4x4 keypad matrix, rows (R1-R4) are connected to RB0-RB3 and columns (C1-C4) to RB4-RB7. To detect if the key at R2 and C3 is pressed, which scan step is correct? (Assume rows are outputs, columns are inputs with pull-ups, and a '0' is driven to activate a row).
TRISB=0b11110000. Write LATB so RB1=0. Read RB6. If low, key is pressed.
TRISB=0b11110000. Write LATB so RB2=0. Read RB5. If low, key is pressed.
TRISB=0b00001111. Write LATB so RB6=0. Read RB1. If low, key is pressed.
TRISB=0b11110000. Write LATB so RB1=1. Read RB6. If low, key is pressed.
54
Immediately after a Power-on Reset (POR) on most PIC microcontrollers, what is the default state of the TRIS registers, and what is the primary reason for this design choice?
1) and the upper nibble is cleared (0) for mixed-mode operation.
0), configuring pins as outputs to ensure a known starting state.
1), configuring pins as inputs to prevent contention with external circuitry.
55
A standard PIC I/O pin does not have a true open-drain output mode, but it can be simulated to achieve a wired-AND logic function with another device. How can this be accomplished using the TRIS and LAT registers with an external pull-up resistor?
LAT bit to 1 and toggle TRIS bit: TRIS=0 for logic high, TRIS=1 for logic low.
LAT bit to 0 and toggle TRIS bit: TRIS=0 for logic high, TRIS=1 for logic low.
TRIS bit to 0 and toggle the LAT bit between 0 and 1.
LAT bit cleared (0) and toggle TRIS bit: TRIS=0 to drive low, TRIS=1 to release high.
56
In a time-critical application, you need to set RB7, clear RB6, and toggle RB5 of PORTB as close to a single atomic operation as possible. Given the initial LATB value is 0b11010101, which C instruction correctly performs this action?
LATB = (LATB & 0x9F) | 0x80;
LATB = (LATB & 0xDF) ^ 0x80;
LATB = (LATB | 0x80) & (~0x40) ^ 0x20;
LATB ^= 0xE0;
57 You are controlling an LED's brightness using software PWM. The PWM period is 10ms. To make the LED appear at 25% of its maximum brightness to the human eye, what should the approximate ON-time (duty cycle) be?
58
A 16-bit counter is implemented using count_L for the low byte (on PORTB) and count_H for the high byte (on PORTC), both are unsigned char. Which C code snippet correctly and most compactly handles the carry-over when incrementing?
count_L++; if(count_L == 0) count_H++;
count_L++; count_H++;
count_L++; if(count_L > 255) count_H++;
count_L++; count_H += (count_L == 0);
59
A switch with a pull-up resistor is connected to pin RB4 on a PIC18F, which is configured for Interrupt-on-Change (IOC). You want to trigger an interrupt only when the switch is pressed (pin goes from high to low). How should the IOCBP (positive edge) and IOCBN (negative edge) registers be configured for bit 4?
IOCBP4 = 1; IOCBN4 = 0;
IOCBP4 = 1; IOCBN4 = 1;
IOCBP4 = 0; IOCBN4 = 0;
IOCBP4 = 0; IOCBN4 = 1;
60
When defining a pointer to a GPIO register like PORTB in C (e.g., #define PORTB_p ((unsigned char*)0xF81)), why is it critically important to include the volatile keyword in the pointer's type declaration (e.g., volatile unsigned char*)?