1Which register is primarily used to configure the operation of Timer0 in PIC18 microcontrollers?
Programming Timer 0 in C
Easy
A.TMR0L
B.INTCON
C.ADCON0
D.T0CON
Correct Answer: T0CON
Explanation:
The T0CON (Timer0 Control) register is used to configure Timer0's mode, prescaler, and to turn it on or off.
Incorrect! Try again.
2When Timer0 is configured in 16-bit mode, which pair of registers holds the count value?
Programming Timer 0 in C
Easy
A.TMR1L and TMR1H
B.PORTA and PORTB
C.TMR0L and TMR0H
D.T0CON and INTCON
Correct Answer: TMR0L and TMR0H
Explanation:
In 16-bit mode, the lower 8 bits of the count are stored in TMR0L and the upper 8 bits are stored in TMR0H, forming a single 16-bit counter.
Incorrect! Try again.
3What is the function of the prescaler in Timer0?
Programming Timer 0 in C
Easy
A.To select the timer's mode (8-bit or 16-bit)
B.To enable the timer interrupt
C.To set the timer's initial value
D.To divide the clock frequency before it reaches the timer
Correct Answer: To divide the clock frequency before it reaches the timer
Explanation:
The prescaler divides the input clock frequency by a selectable value (e.g., 2, 4, 8...), which allows the timer to count more slowly and measure longer time intervals.
Incorrect! Try again.
4To turn on Timer0, which bit in the T0CON register must be set to 1?
Programming Timer 0 in C
Easy
A.GIE
B.TMR0ON
C.T0CS
D.PSA
Correct Answer: TMR0ON
Explanation:
The TMR0ON (Timer0 On) bit in the T0CON register acts as the master switch for Timer0. Setting it to 1 enables the timer to start counting.
Incorrect! Try again.
5Which register is used to control and configure Timer1 in a PIC18 microcontroller?
Programming Timer 1 in C
Easy
A.T1CON
B.TMR1L
C.PIR1
D.T0CON
Correct Answer: T1CON
Explanation:
The T1CON (Timer1 Control) register is used to configure various settings for Timer1, including its clock source, prescaler, and enabling the timer.
Incorrect! Try again.
6Timer1 is a 16-bit timer. Which two 8-bit registers are combined to store its value?
Programming Timer 1 in C
Easy
A.T1CON and T1GCON
B.TMR1H and TMR1L
C.PIR1 and PIE1
D.TMR0H and TMR0L
Correct Answer: TMR1H and TMR1L
Explanation:
TMR1L holds the lower 8 bits (Least Significant Byte) and TMR1H holds the upper 8 bits (Most Significant Byte) of the 16-bit Timer1 count.
Incorrect! Try again.
7To use the internal instruction cycle clock () as the source for Timer1, what should the TMR1CS bit in the T1CON register be set to?
Programming Timer 1 in C
Easy
A.This bit does not control the clock source
B.It depends on the prescaler
C.1
D.0
Correct Answer: 0
Explanation:
Clearing the TMR1CS bit (setting it to 0) selects the internal clock () as the clock source for Timer1. Setting it to 1 would select an external clock source on the T1CKI pin.
Incorrect! Try again.
8What is a primary difference between Timer1 and Timer0 in many PIC18 devices?
Programming Timer 1 in C
Easy
A.Timer0 is faster than Timer1
B.Only Timer1 can be used with interrupts
C.Timer0 has more prescaler options
D.Timer1 is always 16-bit, while Timer0 can be 8-bit or 16-bit
Correct Answer: Timer1 is always 16-bit, while Timer0 can be 8-bit or 16-bit
Explanation:
A key architectural difference is that Timer1 is a dedicated 16-bit timer/counter, whereas Timer0 can be configured to operate in either an 8-bit or 16-bit mode.
Incorrect! Try again.
9What is an Interrupt Service Routine (ISR)?
PIC18 interrupts
Easy
A.A special function that the processor executes in response to an interrupt
B.The main loop of a program
C.A hardware register that stores interrupt flags
D.A command to reset the microcontroller
Correct Answer: A special function that the processor executes in response to an interrupt
Explanation:
An ISR is a piece of code written to handle a specific interrupt. When the interrupt occurs, the main program is paused, and the CPU jumps to the ISR to perform a task.
Incorrect! Try again.
10Which bit in the INTCON register must be set to enable all unmasked interrupts globally?
PIC18 interrupts
Easy
A.INTE (External Interrupt Enable)
B.PEIE (Peripheral Interrupt Enable)
C.TMR0IE (Timer0 Interrupt Enable)
D.GIE (Global Interrupt Enable)
Correct Answer: GIE (Global Interrupt Enable)
Explanation:
The GIE bit acts as a master switch for all interrupts. If GIE is cleared (set to 0), no interrupts will be processed, regardless of individual interrupt enable bits.
Incorrect! Try again.
11In a PIC18 microcontroller, what is an interrupt vector?
PIC18 interrupts
Easy
A.A register that enables interrupts
B.A variable that stores the interrupt type
C.A specific memory address where the ISR for an interrupt is located
D.The priority level of an interrupt
Correct Answer: A specific memory address where the ISR for an interrupt is located
Explanation:
An interrupt vector is a fixed memory address that the CPU automatically jumps to when a specific interrupt occurs. This address contains the starting point of the Interrupt Service Routine (ISR).
Incorrect! Try again.
12What is the purpose of an interrupt flag bit, such as TMR0IF?
PIC18 interrupts
Easy
A.To disable all interrupts globally
B.To set the priority of an interrupt
C.To enable a specific interrupt
D.To indicate that a specific interrupt event has occurred
Correct Answer: To indicate that a specific interrupt event has occurred
Explanation:
An interrupt flag is a bit in a register that is automatically set by the hardware when the corresponding interrupt condition (e.g., a timer overflow) is met.
Incorrect! Try again.
13To enable the Timer0 overflow interrupt, which two bits must be set?
Programming Timer Interrupt
Easy
A.TMR0ON and T0CS
B.GIE and TMR0IE
C.GIE and INTE
D.PEIE and TMR1IE
Correct Answer: GIE and TMR0IE
Explanation:
You must enable the specific interrupt source (TMR0IE in the INTCON register) and also enable interrupts globally (GIE in the INTCON register).
Incorrect! Try again.
14When a Timer0 overflow interrupt occurs, which flag bit is set in the INTCON register?
Programming Timer Interrupt
Easy
A.TMR0IF
B.ADIF
C.TMR1IF
D.INT0IF
Correct Answer: TMR0IF
Explanation:
The TMR0IF (Timer0 Interrupt Flag) bit is automatically set by the hardware when Timer0 overflows from its maximum value (0xFF or 0xFFFF) back to 0.
Incorrect! Try again.
15What is a critical step that must be performed inside a timer's Interrupt Service Routine (ISR) before exiting?
Programming Timer Interrupt
Easy
A.Set the timer's prescaler
B.Re-enable global interrupts
C.Clear the timer's interrupt flag bit
D.Reload the T0CON register
Correct Answer: Clear the timer's interrupt flag bit
Explanation:
The interrupt flag (e.g., TMR0IF) must be cleared in software within the ISR. If it is not cleared, the processor will think the interrupt is still pending and will immediately re-enter the ISR upon exit, causing an infinite loop.
Incorrect! Try again.
16To use the Timer1 overflow interrupt, which bit must be set in the PIE1 (Peripheral Interrupt Enable) register?
Programming Timer Interrupt
Easy
A.ADIE
B.TMR2IE
C.RCIE
D.TMR1IE
Correct Answer: TMR1IE
Explanation:
The TMR1IE bit in the PIE1 register specifically enables the interrupt for Timer1 overflow. You would also need to set PEIE and GIE in the INTCON register.
Incorrect! Try again.
17Which register contains the enable bit (INT0IE) and flag bit (INT0IF) for the external interrupt INT0?
Programming External Hardware Interrupt
Easy
A.INTCON
B.PIE1
C.T0CON
D.INTCON2
Correct Answer: INTCON
Explanation:
The INTCON register is the primary control register for core interrupts, including the external interrupt INT0. It holds the enable bit INT0IE and the flag bit INT0IF.
Incorrect! Try again.
18What does an external hardware interrupt allow the microcontroller to do?
Programming External Hardware Interrupt
Easy
A.Convert an analog voltage to a digital number
B.Measure the passage of time accurately
C.Communicate with a computer via USB
D.Respond to an event from an external device, like a button press
Correct Answer: Respond to an event from an external device, like a button press
Explanation:
External hardware interrupts are designed to detect a signal change on a specific input pin, allowing the MCU to react immediately to external events without constantly checking (polling) the pin's status.
Incorrect! Try again.
19The INTEDG0 bit in the INTCON2 register is used to configure what aspect of the INT0 external interrupt?
Programming External Hardware Interrupt
Easy
A.The interrupt's flag
B.The interrupt's priority level
C.The triggering edge (rising or falling)
D.Enabling or disabling the interrupt
Correct Answer: The triggering edge (rising or falling)
Explanation:
The INTEDG0 (Interrupt Edge) bit selects whether the INT0 interrupt is triggered on a rising edge (0 to 1 transition) or a falling edge (1 to 0 transition) of the signal on the INT0 pin.
Incorrect! Try again.
20After an external hardware interrupt is triggered and the processor jumps to the ISR, what must be done to the interrupt flag (e.g., INT0IF) before exiting the ISR?
Programming External Hardware Interrupt
Easy
A.Nothing, the flag is not used for external interrupts
B.It must be cleared in software
C.It must be set to 1 in software
D.It is cleared automatically by hardware
Correct Answer: It must be cleared in software
Explanation:
Similar to timer interrupts, the external interrupt flag (INT0IF, INT1IF, etc.) must be manually cleared by your code within the ISR. If not cleared, the CPU will continuously re-enter the ISR after exiting.
Incorrect! Try again.
21A PIC18 is running with an oscillator frequency () of 20 MHz. What value should be loaded into the 8-bit TMR0 register to achieve a 1 ms delay using a 1:64 prescaler?
Programming Timer 0 in C
Medium
A.200
B.125
C.78
D.178
Correct Answer: 178
Explanation:
The instruction cycle frequency is MHz. The timer clock period is . To get a 1 ms (1000 µs) delay, we need counts. Since Timer0 is an 8-bit up-counter, the initial value to load for it to overflow after 78 counts is .
Incorrect! Try again.
22In a PIC18 microcontroller with interrupt priority enabled (IPEN=1), an external interrupt INT1 (configured as high priority) and a Timer0 overflow interrupt (configured as low priority) occur at the exact same time. Which Interrupt Service Routine (ISR) will be executed first?
PIC18 interrupts
Medium
A.The Timer0 ISR
B.Neither will execute until acknowledged
C.The processor will enter a debug state
D.The INT1 ISR
Correct Answer: The INT1 ISR
Explanation:
When interrupt priority is enabled (IPEN=1), the PIC18 hardware automatically services the high-priority interrupt first. The low-priority interrupt will be held pending and will only be serviced after the high-priority ISR has completed and returned.
Incorrect! Try again.
23To configure Timer1 on a PIC18 to operate as a 16-bit timer using the internal clock (), with a 1:8 prescaler, and to turn it on, which hexadecimal value should be written to the T1CON register?
Programming Timer 1 in C
Medium
A.0x01
B.0x31
C.0x85
D.0xB1
Correct Answer: 0xB1
Explanation:
The T1CON register value 0xB1 (binary 10110001) correctly sets the required bits: RD16=1 (16-bit mode), T1CKPS<1:0>=11 (1:8 prescaler), TMR1CS=0 (internal clock source), and TMR1ON=1 (Timer1 enabled).
Incorrect! Try again.
24After enabling the Timer0 interrupt (TMR0IE=1 and GIE=1) and configuring Timer0, what is the most critical action to perform inside the Timer0 Interrupt Service Routine (ISR) to prevent it from being continuously re-triggered?
Programming Timer Interrupt
Medium
A.Reload the TMR0 register with the initial value.
B.Clear the TMR0IF flag in the INTCON register.
C.Re-write the configuration to the T0CON register.
D.Disable and then re-enable the GIE bit.
Correct Answer: Clear the TMR0IF flag in the INTCON register.
Explanation:
The Timer0 Interrupt Flag (TMR0IF) is set by hardware when the timer overflows. It must be cleared by software inside the ISR. If it is not cleared, the processor will continuously re-enter the ISR as soon as it exits, effectively halting the main program.
Incorrect! Try again.
25You need to trigger an interrupt on the falling edge of a signal connected to the INT0 pin on a PIC18. Which register bit must be specifically configured for this purpose?
Programming External Hardware Interrupt
Medium
A.Set the INT0IF bit in the INTCON register.
B.Clear the RBPU bit in the INTCON2 register.
C.Clear the INTEDG0 bit in the INTCON2 register.
D.Set the INTEDG0 bit in the INTCON2 register.
Correct Answer: Clear the INTEDG0 bit in the INTCON2 register.
Explanation:
The INTEDG0 bit in the INTCON2 register controls the edge detection for the INT0 external interrupt. Clearing this bit (INTEDG0 = 0) configures the interrupt to trigger on a falling edge. Setting it would configure it for a rising edge.
Incorrect! Try again.
26Consider the following C code for a PIC18: T0CON = 0b10000101;. What is the resulting configuration of Timer0?
The value 0b10000101 (0x85) corresponds to: TMR0ON=1 (On), T08BIT=0 (16-bit mode), T0CS=0 (internal clock), PSA=0 (prescaler assigned), and T0PS<2:0>=101 (1:64 prescaler).
Incorrect! Try again.
27What is the primary purpose of the PIE1 (Peripheral Interrupt Enable 1) register in a PIC18 microcontroller?
PIC18 interrupts
Medium
A.To set the priority level of all peripheral interrupts.
B.To globally enable or disable all interrupts on the chip.
C.To individually enable or disable specific peripheral interrupts like Timer1 or USART.
D.To store the status flags of peripheral interrupts like TMR1IF.
Correct Answer: To individually enable or disable specific peripheral interrupts like Timer1 or USART.
Explanation:
The PIE registers contain individual enable bits for each peripheral interrupt source. While the Global Interrupt Enable (GIE) bit acts as a master switch, each peripheral's interrupt must also be enabled in its corresponding PIE register (e.g., TMR1IE in PIE1 for Timer1).
Incorrect! Try again.
28A PIC18 uses a 16 MHz crystal. To generate a delay of 50 ms using Timer1 in 16-bit mode with a 1:8 prescaler, what initial value should be loaded into the TMR1 register pair (TMR1H:TMR1L)?
Programming Timer 1 in C
Medium
A.0xB1E0
B.0xF858
C.0x9E58
D.0x3CB0
Correct Answer: 0x9E58
Explanation:
The timer clock is (, giving a period of 2 µs per count. For a 50 ms delay, counts are needed. For a 16-bit timer that counts up, the initial value should be , which is 0x9E58 in hexadecimal.
Incorrect! Try again.
29In a PIC18 C program, you have an ISR defined as void __interrupt(low_priority) low_isr(void). Which register configuration is required for this ISR to handle a Timer1 overflow interrupt?
Programming Timer Interrupt
Medium
A.Set TMR1IE=0 in PIE1.
B.Set TMR1IP=1 in IPR1 and IPEN=1 in RCON.
C.Set TMR1IP=0 in IPR1 and IPEN=1 in RCON.
D.Set IPEN=0 in RCON.
Correct Answer: Set TMR1IP=0 in IPR1 and IPEN=1 in RCON.
Explanation:
The low_priority keyword tells the compiler to use the low-priority vector. For this to work, interrupt priorities must be enabled (IPEN=1 in RCON). To assign the Timer1 interrupt to the low-priority level, its corresponding priority bit, TMR1IP in the IPR1 register, must be cleared to 0.
Incorrect! Try again.
30Why might a 100nF capacitor be placed between an external interrupt pin (e.g., INT0) and ground when the interrupt is triggered by a mechanical switch?
Programming External Hardware Interrupt
Medium
A.To act as a simple hardware debouncing filter.
B.To amplify the signal from the switch.
C.To invert the logic level of the switch.
D.To limit the current drawn by the input pin.
Correct Answer: To act as a simple hardware debouncing filter.
Explanation:
Mechanical switches exhibit 'contact bounce,' which creates multiple rapid signal transitions. This can trigger numerous false interrupts. A capacitor, along with a pull-up resistor, forms a low-pass RC filter that smooths out these rapid transitions, ensuring the microcontroller sees only one clean signal edge.
Incorrect! Try again.
31If Timer0 is configured in 8-bit mode with a 1:256 prescaler and an internal clock of , what is the maximum delay that can be generated in a single overflow cycle? Assume = 16 MHz.
Programming Timer 0 in C
Medium
A.8.192 ms
B.16.384 ms
C.65.536 ms
D.32.768 ms
Correct Answer: 16.384 ms
Explanation:
With a 16 MHz , the instruction clock is 4 MHz. With a 1:256 prescaler, the timer increments every . In 8-bit mode, the timer overflows after 256 counts (from 0 to 255). Therefore, the maximum delay is or 16.384 ms.
Incorrect! Try again.
32What happens if a high-priority interrupt occurs while a low-priority ISR is already executing in a PIC18 (with IPEN=1)?
PIC18 interrupts
Medium
A.The low-priority ISR completes fully before the high-priority ISR is serviced.
B.The low-priority ISR is immediately preempted, and the high-priority ISR begins execution.
C.The processor resets because two ISRs cannot be active.
D.The high-priority interrupt is ignored and its flag is lost.
Correct Answer: The low-priority ISR is immediately preempted, and the high-priority ISR begins execution.
Explanation:
A key feature of the PIC18's two-level interrupt system is that a high-priority interrupt can preempt (interrupt) a currently executing low-priority ISR. The processor saves the context of the low-priority ISR, jumps to the high-priority ISR, and resumes the low-priority ISR after the high-priority one is complete.
Incorrect! Try again.
33When using Timer1 as an asynchronous counter for external events (TMR1CS=1, T1SYNC=1), what is a major advantage of this mode?
Programming Timer 1 in C
Medium
A.The count value can be read more quickly.
B.It synchronizes automatically with the main program execution.
C.It consumes less power than synchronous mode.
D.It can count events even while the microcontroller is in sleep mode.
Correct Answer: It can count events even while the microcontroller is in sleep mode.
Explanation:
In asynchronous mode, Timer1 uses an external clock source that is independent of the main system clock. This allows the timer to continue running and counting external events even when the main CPU core is put into a low-power sleep mode, which is very useful for applications like real-time clocks.
Incorrect! Try again.
34Analyze the following PIC18 ISR code snippet. What is a potential issue with it? void __interrupt() my_isr(void) { if (PIR1bits.TMR1IF) { // Toggle an LED PORTBbits.RB0 = ~PORTBbits.RB0; // Reload Timer1 TMR1 = 0xC2F7; } }
Programming Timer Interrupt
Medium
A.The TMR1 register cannot be written to inside an ISR.
B.The TMR1IF flag is not cleared.
C.The __interrupt keyword is invalid for Timer1.
D.The GIE bit should be disabled at the start of the ISR.
Correct Answer: The TMR1IF flag is not cleared.
Explanation:
The interrupt flag (TMR1IF) that caused the ISR to execute is never cleared in software. As soon as the ISR returns, the processor will see that the flag is still set and immediately re-enter the ISR. This will starve the main program of any processing time. The line PIR1bits.TMR1IF = 0; must be added.
Incorrect! Try again.
35To enable both the external interrupt on pin INT2 and the "interrupt-on-change" for PORTB (pins RB7:RB4), which combination of interrupt enable bits must be set?
Programming External Hardware Interrupt
Medium
A.INT2E in PIE1 and RBIE in INTCON
B.INTEDG2 in INTCON2 and RBPU in INTCON2
C.INT2IP in INTCON3 and RBIP in INTCON2
D.INT2IE in INTCON3 and RBIE in INTCON
Correct Answer: INT2IE in INTCON3 and RBIE in INTCON
Explanation:
The enable bits for different interrupts are spread across several registers. The INT2 External Interrupt Enable bit (INT2IE) is located in the INTCON3 register, while the RB Port Change Interrupt Enable bit (RBIE) is located in the INTCON register. Both must be set to 1.
Incorrect! Try again.
36A programmer wants to generate a time delay by polling the TMR0IF flag instead of using an interrupt. Which C code snippet correctly implements this blocking delay?
This code correctly implements polling. The while(!INTCONbits.TMR0IF); line creates an empty loop that halts program execution until the TMR0IF flag is set to 1 by the hardware. Once the loop is exited, INTCONbits.TMR0IF = 0; clears the flag to prepare for the next delay measurement.
Incorrect! Try again.
37What are the memory addresses for the high-priority and low-priority interrupt vectors in a standard PIC18 microcontroller?
PIC18 interrupts
Medium
A.0x0004 (reset) and 0x0008 (general)
B.0x0008 (high) and 0x0018 (low)
C.0x0018 (high) and 0x0008 (low)
D.0x0000 (reset) and 0x0004 (interrupt)
Correct Answer: 0x0008 (high) and 0x0018 (low)
Explanation:
When an interrupt occurs, the PIC18's program counter is forced to a specific, predefined address in program memory called the interrupt vector. For high-priority interrupts, this address is 0x0008. For low-priority interrupts (when IPEN=1), it is 0x0018.
Incorrect! Try again.
38In a C program for a PIC18, how would you read the entire 16-bit value of a running Timer1 into an unsigned int variable named timer_value in a single, atomic operation provided by the XC8 compiler?
Modern compilers like XC8 are aware of the 16-bit nature of registers like TMR1. When you write timer_value = TMR1;, the compiler generates the correct assembly sequence to read TMR1L first (which latches the value of TMR1H into a buffer) and then read TMR1H, ensuring a correct 16-bit read without risk of rollover during the read operation.
Incorrect! Try again.
39An application uses a Timer1 interrupt every 10ms to increment a counter. The main loop must perform an action every 1 second. What is the most robust and efficient way to implement this logic?
Programming Timer Interrupt
Medium
A.Inside the ISR, if the counter reaches 100, set a global flag and reset the counter. The main loop polls this flag.
B.In the main loop, create a 1-second software delay and then check the value of the counter.
C.Inside the ISR, if the counter reaches 100, perform the 1-second action directly inside the ISR.
D.In the main loop, continuously check if counter >= 100. If so, perform the action and reset the counter.
Correct Answer: Inside the ISR, if the counter reaches 100, set a global flag and reset the counter. The main loop polls this flag.
Explanation:
ISRs should be as short as possible to minimize latency for other interrupts. Performing long tasks inside an ISR is bad practice. The best method is to use the ISR to handle the time-critical event (counting) and signal the main loop using a flag. The main loop can then perform the longer, non-time-critical task when it detects the flag.
Incorrect! Try again.
40You have configured the INT0 external interrupt for a rising edge. In the ISR, you clear the INT0IF flag. However, the interrupt triggers again immediately upon exit. Assuming no switch bounce, what is a likely cause?
Programming External Hardware Interrupt
Medium
A.The priority bit INT0IP was set incorrectly.
B.The INT0IE bit was accidentally cleared in the ISR.
C.The signal on the INT0 pin remained high after the initial rising edge.
D.The GIE bit was not cleared inside the ISR.
Correct Answer: The signal on the INT0 pin remained high after the initial rising edge.
Explanation:
This is a classic issue. The interrupt flag (INT0IF) is cleared in the ISR, but the physical pin is still at the logic level that caused the interrupt. Due to a mismatch between the internal state and the external pin level, the flag can be immediately set again upon returning from the interrupt. The external signal must return to its inactive state (low, in this case) before the ISR completes.
Incorrect! Try again.
41A PIC18F running at MHz is configured with Timer0 in 16-bit mode with a 1:64 prescaler. To generate a precise interrupt every 500 ms, an ISR reloads Timer0 with a value TMR0_PRELOAD and uses a software counter that triggers an event after N interrupts. Which combination of TMR0_PRELOAD and N achieves this target?
Programming Timer 0 in C
Hard
A.TMR0_PRELOAD = 64286, N = 25
B.TMR0_PRELOAD = 62411, N = 10
C.All of the above are valid combinations.
D.TMR0_PRELOAD = 59286, N = 5
Correct Answer: All of the above are valid combinations.
Explanation:
The correct option follows directly from the given concept and definitions.
Incorrect! Try again.
42Consider the following PIC18 C code snippet intended to configure Timer0 in 16-bit mode.
c
T0CON = 0b00000011; // 16-bit, FOSC/4, Prescaler 1:16
TMR0H = 0x0B;
TMR0L = 0xDC;
T0CONbits.TMR0ON = 1;
According to the PIC18 datasheet, when writing to TMR0H, the value is held in a temporary register until TMR0L is written. What is the potential issue if a high-priority interrupt occurs between the TMR0H write and the TMR0L write, and the ISR also modifies Timer0?
Programming Timer 0 in C
Hard
A.The TMR0H write will be lost, and Timer0 will start counting from 0x00DC.
B.The temporary holding register for TMR0H is overwritten by the ISR, leading to an incorrect starting count when the main code resumes and writes to TMR0L.
C.A write to TMR0L in the ISR will incorrectly load the TMR0H with the value from the main code's temporary register, corrupting the ISR's intended timer value.
D.There is no issue, as the hardware protocol for 16-bit timer writes is inherently atomic and protected from interrupts.
Correct Answer: A write to TMR0L in the ISR will incorrectly load the TMR0H with the value from the main code's temporary register, corrupting the ISR's intended timer value.
Explanation:
The PIC18 hardware uses a temporary holding register (TMPH) for TMR0H. A write to TMR0H loads TMPH. A subsequent write to TMR0L causes the value from TMPH to be loaded into the actual TMR0H register. If an interrupt occurs after TMR0H = 0x0B; and the ISR writes its own 16-bit value to Timer0 (e.g., TMR0H=0xFF; TMR0L=0xFF;), the ISR's write to TMR0L will trigger the transfer of the main code's0x0B from TMPH into TMR0H, not the ISR's 0xFF. This corrupts the timer's state within the ISR. The safe procedure is to disable interrupts before a 16-bit write and re-enable them after.
Incorrect! Try again.
43A developer configures Timer0 as an 8-bit external event counter using the T0CKI pin. They set T0CON = 0b01101000;. However, they observe that the timer is not incrementing on events at T0CKI, but is instead incrementing at a rate related to the system clock. What is the most likely cause of this misbehavior?
Programming Timer 0 in C
Hard
A.The TRIS direction for the T0CKI pin was not set to input.
B.The PSA (Prescaler Assignment) bit is set to 1, which assigns the prescaler to the Watchdog Timer, causing Timer0 to use the raw FOSC/4 clock.
C.The TMR0ON bit was not set, so the timer is disabled.
D.The T0CS bit is set incorrectly; it should be 0 for counter mode.
Correct Answer: The PSA (Prescaler Assignment) bit is set to 1, which assigns the prescaler to the Watchdog Timer, causing Timer0 to use the raw FOSC/4 clock.
Explanation:
The configuration T0CON = 0b01101000 translates to: TMR0ON=0, T08BIT=1, T0CS=1 (transition on T0CKI), T0SE=0, PSA=1 (Prescaler assigned to WDT), T0PS=000. The key issue is PSA=1. When the prescaler is assigned to the Watchdog Timer, Timer0 ignores the prescaler settings and effectively uses the internal FOSC/4 clock source, bypassing the T0CKI input. The developer likely intended PSA=0.
Incorrect! Try again.
44Given T0CON = 0b10000101;, and a PIC18 running at MHz. The timer is running. At a certain point, a read of the 16-bit timer value is performed using this sequence: low_byte = TMR0L; high_byte = TMR0H;. What is a potential critical flaw in this reading method?
Programming Timer 0 in C
Hard
A.Reading TMR0H first is required to latch the low byte.
B.If TMR0L overflows to 0x00 after it is read but beforeTMR0H is read, the captured high_byte will be one higher than it should have been relative to the low_byte, resulting in an incorrect time value.
C.This method is correct and has no flaws.
D.16-bit reads must be disabled by setting RD16 in T1CON before reading TMR0.
Correct Answer: If TMR0L overflows to 0x00 after it is read but beforeTMR0H is read, the captured high_byte will be one higher than it should have been relative to the low_byte, resulting in an incorrect time value.
Explanation:
The configuration T0CON=0b10000101 enables Timer0 in 16-bit mode. When reading a 16-bit timer that is actively running, a rollover can occur between reading the low and high bytes. For instance, if the timer value is 0x1AFF and you read TMR0L to get 0xFF, a tick might occur causing the timer to become 0x1B00. Then, when you read TMR0H, you get 0x1B. The combined result is 0x1BFF, which is incorrect. A safe method is to read H-L-H and check if the two high byte reads are consistent.
Incorrect! Try again.
45A PIC18 is configured with Timer1 in 16-bit Asynchronous Counter mode to count external events while the CPU is in SLEEP mode. The configuration is T1CON = 0b00000111;. The oscillator for Timer1 is a low-power 32.768 kHz watch crystal connected to T1OSI and T1OSO. What happens to the TMR1 register value if the main CPU is put to sleep using the SLEEP() instruction?
Programming Timer 1 in C
Hard
A.Timer1 continues to count events from its independent oscillator, allowing the PIC to wake up on a Timer1 overflow interrupt.
B.Timer1 resets to 0x0000 upon entering sleep mode and resumes counting on wakeup.
C.Timer1's behavior is undefined in sleep mode and depends on the specific PIC18 derivative.
D.Timer1 stops counting because the main oscillator is turned off during sleep.
Correct Answer: Timer1 continues to count events from its independent oscillator, allowing the PIC to wake up on a Timer1 overflow interrupt.
Explanation:
The configuration T1CON = 0b00000111 enables Timer1 using its own dedicated external crystal oscillator (TMR1CS1:TMR1CS0=01, T1OSCEN=1). A key feature of this mode is its ability to operate independently of the main CPU clock. When the CPU enters SLEEP, the main oscillator may stop, but the dedicated Timer1 oscillator continues to run, allowing it to keep time and generate an interrupt to wake the processor.
Incorrect! Try again.
46A scenario where Timer1 is used with the CCP module in Capture mode to measure the period of an incoming signal. Configuration: Timer1 is free-running, clocked by , with MHz. An engineer observes that for a high-frequency input signal, the captured values (CCPR1H:CCPR1L) occasionally seem to give a very large period measurement instead of a small one. What is the most plausible cause?
Programming Timer 1 in C
Hard
A.The period of the input signal is slightly longer than the overflow period of Timer1, causing a Timer1 overflow (TMR1IF) to occur between two consecutive captures.
B.The input signal frequency is higher than the Nyquist rate, causing aliasing.
C.The prescaler for Timer1 is set too high, reducing the resolution and causing measurement errors.
D.An interrupt for the CCP capture event is not being serviced fast enough, causing the CCP1IF flag to be missed before the next capture event occurs.
Correct Answer: The period of the input signal is slightly longer than the overflow period of Timer1, causing a Timer1 overflow (TMR1IF) to occur between two consecutive captures.
Explanation:
In period measurement, the period is CurrentCapture - PreviousCapture. If the time between two captures is greater than the time it takes for the 16-bit Timer1 to overflow (count from 0 to 65535), the simple subtraction will be incorrect. For example, if the first capture is at 65000 and the second occurs after an overflow at 1000, the result 1000 - 65000 is wrong. To handle this, the software must also check and account for the Timer1 overflow flag (TMR1IF) between captures.
Incorrect! Try again.
47Timer1 is configured with T1CON = 0b10010001; and MHz. What is the maximum resolution (i.e., the time duration of a single timer tick) of Timer1 in this configuration, and what is its total overflow period if it starts from 0x0000?
Programming Timer 1 in C
Hard
A.Resolution: 4.0 µs, Period: 262.144 ms
B.Resolution: 0.5 µs, Period: 32.768 ms
C.Resolution: 1.0 µs, Period: 65.536 ms
D.Resolution: 0.25 µs, Period: 16.384 ms
Correct Answer: Resolution: 0.25 µs, Period: 16.384 ms
Explanation:
Decoding T1CON = 0b10010001: RD16=1, T1CKPS=01 (Prescaler 1:2), TMR1CS=0 (Clock is FOSC/4). The instruction clock is . The timer's input clock is prescaled: . The resolution (tick time) is the reciprocal of this frequency: µs. The overflow period for a 16-bit timer is ms.
Incorrect! Try again.
48A developer is using Timer1's Gate Control feature (T1GCON) to measure the width of a high pulse on the T1G pin. The configuration is: T1GCON = 0b10010001;, Timer1 is on and clocked internally. What does this specific T1GCON configuration imply about how the measurement is performed?
Programming Timer 1 in C
Hard
A.Timer1 is toggled on/off by each successive falling edge on the T1G pin.
B.Timer1 runs only when the T1G pin is high; the gate event is level-sensitive.
C.Timer1 starts counting on a rising edge detected by the CCP2 module and stops counting on a falling edge.
D.A single rising edge on T1G starts Timer1, and it runs until manually stopped.
Correct Answer: Timer1 runs only when the T1G pin is high; the gate event is level-sensitive.
Explanation:
Decoding T1GCON = 0b10010001: TMR1GE=1 (gate enabled), T1GPOL=0 (active-high), T1GTM=0 (toggle mode disabled), T1GSPM=1 (single-pulse mode enabled), T1GSS=01 (gate source is T1G pin). When Toggle Mode (T1GTM) is disabled, the gate acts as a level-sensitive enable. T1GPOL=0 means it's active-high. Therefore, Timer1 will increment only during the time the T1G pin is held high. This setup is ideal for measuring the width of a high pulse.
Incorrect! Try again.
49A PIC18 has interrupt priority enabled (IPEN = 1). A low-priority Timer0 interrupt (TMR0IP = 0) and a high-priority external interrupt (INT0IP = 1) have their flags set at the exact same time. The high-priority ISR for INT0 takes 20 instruction cycles. Global interrupts are enabled. What is the execution flow?
PIC18 interrupts
Hard
A.Only the high-priority ISR executes; the low-priority interrupt flag is ignored until the next polling cycle.
B.Both ISRs execute sequentially, with the high-priority ISR running first, followed immediately by the low-priority ISR.
C.The low-priority ISR starts first, is preempted by the high-priority ISR, and then resumes.
D.The processor vectors to the high-priority interrupt vector (0x0008), executes the high-priority ISR, returns, and then immediately vectors to the low-priority interrupt vector (0x0018) to execute the low-priority ISR.
Correct Answer: The processor vectors to the high-priority interrupt vector (0x0008), executes the high-priority ISR, returns, and then immediately vectors to the low-priority interrupt vector (0x0018) to execute the low-priority ISR.
Explanation:
When multiple interrupt flags are set simultaneously with IPEN=1, the hardware services the highest priority interrupt first. The CPU vectors to the high-priority location (0x0008) for INT0. During the high-priority ISR, low-priority interrupts are masked. Upon returning from the high-priority ISR (with RETFIE), the global interrupt enable is restored. The CPU's interrupt logic then immediately detects the still-pending low-priority TMR0IF flag and vectors to the low-priority location (0x0018) to service it.
Incorrect! Try again.
50In a PIC18 system with interrupt priorities enabled (IPEN=1), an engineer writes a low-priority ISR. To ensure atomicity of a certain operation within this ISR, they decide to temporarily disable high-priority interrupts. What is the correct and safest way to achieve this inside the low-priority ISR?
PIC18 interrupts
Hard
A.Use a software flag (semaphore) that the high-priority ISR must check before proceeding.
B.GIEH = 0; at the start of the critical section and GIEH = 1; at the end.
C.This is not possible; a low-priority ISR can never block a high-priority interrupt.
D.INTCONbits.GIE = 0; at the start and INTCONbits.GIE = 1; at the end.
Correct Answer: GIEH = 0; at the start of the critical section and GIEH = 1; at the end.
Explanation:
When the CPU enters a low-priority ISR, the GIEH (Global Interrupt Enable High) bit remains set, allowing high-priority interrupts to preempt it. To create a critical section within the low-priority ISR that cannot be interrupted, the GIEH bit must be manually cleared (GIEH = 0; or BCF INTCON, 7). After the critical section, it must be set again (GIEH = 1; or BSF INTCON, 7) to restore normal priority behavior.
Incorrect! Try again.
51A developer is debugging a PIC18 system. After a brown-out event, they find the device is stuck in a loop. Upon examining the RCON register, they find its value is 0b00001001. What can be concluded about the sequence of reset events?
PIC18 interrupts
Hard
A.A Watchdog Timer timed out, followed by a software reset.
B.A Brown-out Reset occurred, followed by a Watchdog Timer time-out.
C.A Brown-out Reset occurred, but the NOT_POR bit indicates a MCLR reset happened before that.
D.A Power-on Reset occurred, followed by a Brown-out Reset.
Correct Answer: A Brown-out Reset occurred, followed by a Watchdog Timer time-out.
Explanation:
The RCON register value 0b00001001 decodes as active-low bits NOT_BOR=0 and NOT_TO=0. NOT_BOR=0 implies a Brown-out Reset has occurred. NOT_TO=0 implies a Watchdog Timer time-out has occurred. NOT_POR=1 indicates no Power-on Reset has happened since the flags were last cleared. These flags are cumulative. This state shows that both a BOR and a WDT time-out have happened since the last POR, which is a plausible failure sequence.
Incorrect! Try again.
52A system has two active interrupts: a high-priority ADC interrupt (ADIP=1) and a low-priority UART receive interrupt (RCIP=0), with IPEN=1. The UART ISR is currently executing a critical section where it has temporarily disabled high-priority interrupts by clearing GIEH. During this critical section, an ADC conversion completes, setting the ADIF flag. What happens immediately after GIEH is set back to 1 at the end of the critical section?
PIC18 interrupts
Hard
A.The high-priority ADC ISR immediately preempts the low-priority UART ISR at the instruction right after GIEH is set.
B.The low-priority UART ISR completes its execution fully, and then the high-priority ADC ISR is serviced.
C.The processor completes the current instruction in the UART ISR and then vectors to the ADC ISR.
D.The ADIF flag is ignored because it was set while GIEH was 0.
Correct Answer: The high-priority ADC ISR immediately preempts the low-priority UART ISR at the instruction right after GIEH is set.
Explanation:
Interrupt flags like ADIF are set by hardware regardless of the state of the interrupt enable bits. When GIEH is set back to 1 inside the low-priority ISR, the interrupt controller immediately sees the pending high-priority ADIF flag. This causes an immediate preemption. The processor will complete the instruction that sets GIEH, push the program counter, and vector to the high-priority ADC ISR, pausing the execution of the low-priority ISR.
Incorrect! Try again.
53In a PIC18 Timer0 ISR, the following C code is used to reload the timer. The system clock is MHz, Timer0 is in 8-bit mode with a 1:256 prescaler.
c
void interrupt tc_int(void) {
if (TMR0IF) {
// ... toggle LED ...
TMR0L = 100;
TMR0IF = 0;
}
}
What is the primary issue with this ISR code that will lead to significant cumulative timing error (jitter and drift)?
Programming Timer Interrupt
Hard
A.The TMR0IF flag is cleared after reloading the timer, which is the wrong order.
B.TMR0L = 100; is the wrong value for a 10 ms delay.
C.An 8-bit timer cannot produce a 10 ms delay with the given clock and prescaler.
D.The time taken for the ISR to execute before TMR0L is reloaded is not accounted for, causing the actual period to be longer than intended.
Correct Answer: The time taken for the ISR to execute before TMR0L is reloaded is not accounted for, causing the actual period to be longer than intended.
Explanation:
When the TMR0IF flag is set, Timer0 has already overflowed and continued counting from 0. The ISR takes time (latency + execution) to reach the TMR0L = 100; line. During this time, the timer has been incrementing. Reloading a fixed value does not compensate for this variable delay. The result is that the actual period will be (intended period) + (ISR latency), causing jitter and drift over time. A more accurate method is to add the desired count value to the current TMR0L value to compensate for the time already passed.
Incorrect! Try again.
54To create a software PWM signal, a developer uses a single Timer1 overflow interrupt. To achieve a 25% duty cycle on a 20ms period, the pin must be high for 5ms and low for 15ms. What is the most efficient and accurate interrupt-driven method?
Programming Timer Interrupt
Hard
A.In the Timer1 ISR, alternate the Timer1 reload value to create a 5ms interval, then a 15ms interval.
B.Use a second timer (e.g., Timer0) to fire a one-shot interrupt in 5ms to set the pin low.
C.Inside the Timer1 ISR, use a software delay loop for 5ms before setting the pin low.
D.In the main loop, continuously poll the TMR1 register and set the pin low when it reaches the 5ms count value.
Correct Answer: In the Timer1 ISR, alternate the Timer1 reload value to create a 5ms interval, then a 15ms interval.
Explanation:
The most efficient use of a single timer for software PWM is to modulate its overflow period within the ISR. At the start of the PWM cycle (rising edge), the ISR reloads Timer1 with a value that will cause an overflow after the 'on' time (5ms). In the next ISR, the pin is set low, and Timer1 is reloaded with a value for the 'off' time (15ms). This cycle repeats, creating a precise, non-blocking PWM signal with a single hardware timer.
Incorrect! Try again.
55A developer sets up Timer0 and Timer1 interrupts, with Timer0 as high priority and Timer1 as low priority (IPEN=1). Timer0 ISR takes 15 µs and interrupts every 25 µs. Timer1 ISR takes 40 µs and interrupts every 100 µs. At time t=0, both events occur. What percentage of CPU time is spent executing the main (non-interrupt) code? Assume no other tasks.
Programming Timer Interrupt
Hard
A.40%
B.10%
C.60%
D.0%
Correct Answer: 0%
Explanation:
The correct option follows directly from the given concept and definitions.
Incorrect! Try again.
56An external interrupt INT1 is configured as edge-triggered. The ISR for INT1 takes 50 µs to execute. The external signal connected to the INT1 pin is a square wave with a period of 40 µs (25 kHz frequency). What will be the observed behavior of the system?
Programming External Hardware Interrupt
Hard
A.The first interrupt will be serviced, but subsequent interrupts occurring during the ISR execution will be missed entirely.
B.The ISR will execute for every edge of the input signal, and the main program will run very slowly.
C.The first interrupt is serviced. Any subsequent valid edges that occur while the ISR is running will set the INT1IF flag again, causing the ISR to be re-entered immediately after it finishes.
D.The system will crash due to a stack overflow.
Correct Answer: The first interrupt is serviced. Any subsequent valid edges that occur while the ISR is running will set the INT1IF flag again, causing the ISR to be re-entered immediately after it finishes.
Explanation:
Edge-triggered interrupt flags (INTxIF) are latched by hardware when a valid edge occurs, regardless of whether the CPU is already in an ISR. Since the signal period (40 µs) is shorter than the ISR execution time (50 µs), a new interrupt edge will occur before the ISR completes. When the ISR returns (using RETFIE), the INT1IF flag will already be set again, causing the CPU to immediately re-enter the ISR. The main code will get little to no execution time.
Incorrect! Try again.
57A project uses Interrupt-on-Change for port B (RBIE). Pin RB4 is for a button input, and RB5 is an LED output controlled by writing to LATB in the main loop. The ISR is intended only for RB4. The ISR code is:
c
void interrupt low_priority isr(void) {
if (RBIF) {
if (PORTBbits.RB4 == 0) { / handle button / }
RBIF = 0; // Clear interrupt flag
}
}
Why might this ISR be triggered unexpectedly?
Programming External Hardware Interrupt
Hard
A.The code does not handle contact bounce from the button.
B.The RBIF flag should be cleared before checking the pin state.
C.The ISR must read from the PORTB register to clear the mismatch condition before clearing the RBIF flag.
D.A write to LATB in the main loop to control RB5 can cause RBIF to be set.
Correct Answer: The ISR must read from the PORTB register to clear the mismatch condition before clearing the RBIF flag.
Explanation:
The Interrupt-on-Change feature triggers on a mismatch between the physical pin states and the value last read from PORTB. The interrupt condition is cleared only by reading the PORTB register, which updates the internal latch. Simply clearing the RBIF flag is not sufficient. If the main code changes RB5 via LATB, a mismatch occurs, setting RBIF. If the ISR only clears the flag but never reads PORTB, the mismatch condition persists, and the interrupt may re-trigger immediately upon returning.
Incorrect! Try again.
58The INT0 external interrupt is configured with INTCONbits.INT0E = 1;, INTCONbits.INT0IF = 0;, and INTCON2bits.INTEDG0 = 0; (falling edge). The main program enters SLEEP mode. A single, clean 100 ns negative pulse occurs on the RB0/INT0 pin. The PIC's instruction cycle time is 250 ns ( MHz). What is the expected outcome?
Programming External Hardware Interrupt
Hard
A.The device wakes up, but the INT0IF flag is not set, so the ISR is not executed.
B.The pulse is too short to be detected by the interrupt logic, and the device remains in sleep.
C.The device's behavior is unpredictable, as the pulse width is shorter than one instruction cycle.
D.The device wakes up from sleep and immediately executes the INT0 ISR.
Correct Answer: The device wakes up from sleep and immediately executes the INT0 ISR.
Explanation:
The external interrupt inputs on PIC microcontrollers have Schmitt Trigger buffers and synchronization circuits designed to detect very short edges. The minimum pulse width requirement is typically in the tens of nanoseconds, which is much shorter than an instruction cycle. The logic is asynchronous to the main clock. The 100 ns pulse is sufficient to trigger the edge-detection logic, set the INT0IF flag, wake the processor from sleep, and cause it to vector to the ISR.
Incorrect! Try again.
59In a PIC18 system using high-priority interrupts, what is the primary purpose of the RETFIE FAST instruction compared to the standard RETFIE?
PIC18 interrupts
Hard
A.It restores the WREG, STATUS, and BSR registers from shadow registers instead of the stack, but does not re-enable global interrupts.
B.It restores key working registers (WREG, STATUS, BSR) from dedicated shadow registers and re-enables global high-priority interrupts (GIEH) in a single, atomic instruction.
C.It executes in fewer clock cycles than RETFIE.
D.It automatically clears the interrupt flag of the serviced interrupt.
Correct Answer: It restores key working registers (WREG, STATUS, BSR) from dedicated shadow registers and re-enables global high-priority interrupts (GIEH) in a single, atomic instruction.
Explanation:
The PIC18 architecture provides shadow registers for WREG, STATUS, and BSR to speed up context switching for high-priority interrupts. RETFIE FAST is designed for this feature. In a single instruction, it restores these three key registers from their shadow copies and re-enables high-priority interrupts by setting GIEH. This is faster and more efficient than a standard RETFIE, which would require manual POP instructions in the ISR for context restoration.
Incorrect! Try again.
60A PIC18's INT0 interrupt is configured for a falling-edge trigger from a 1 kHz square wave. The INT0 ISR increments a 16-bit volatile integer count. The main loop toggles an LED and resets count to 0 when count >= 1000. The LED toggles approximately every 2 seconds instead of the expected 1 second. What is the most probable cause?
Programming External Hardware Interrupt
Hard
A.Compiler optimization is removing the volatile qualifier.
B.The INT0 interrupt is triggering on both rising and falling edges.
C.The ISR is taking too long, causing it to miss every other interrupt.
D.The main loop's check and reset of count is not an atomic operation. An interrupt can occur after count is read but before it is reset, causing increments to be lost.
Correct Answer: The main loop's check and reset of count is not an atomic operation. An interrupt can occur after count is read but before it is reset, causing increments to be lost.
Explanation:
This is a classic race condition. The main loop performs a non-atomic read-modify-write operation. An INT0 interrupt can occur between the if (count >= 1000) check and the count = 0; assignment. For example, count is 1000, the if is true. An interrupt fires, making count 1001. The main code then resumes and sets count to 0. The increment from 1000 to 1001 is lost. Over time, these lost counts accumulate, causing the LED to toggle much slower than expected. This operation needs to be protected in a critical section.