Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assume that for a PIC24FJ64GA002 the pin for CN13/RB13 has a pushbutton switch c

ID: 3592011 • Letter: A

Question

Assume that for a PIC24FJ64GA002 the pin for CN13/RB13 has a pushbutton switch connected to it and the change notication interrupt for RB13 is enabled. For the following code snippet, how many times does the CNInterrupt ISR execute if the pushbutton is presses and released one time? Assume no switch bounce and explain your answer. The ISR is void _ISR _CNInterrupt(void) { DISABLE_RB13_CN_INTERRUPT(); // _CN13IE = 0; _CNIF = 0; } The main routine is int main(void){ CONFIG_RB13_AS_DIG_INPUT(); ENABLE_RB13_CN_INTERRUPT(); // _CN13IE = 1; _CNIF = 0; _CNIP = 2; _CNIE = 1; while(1); // forever loop }

Explanation / Answer

If an event occurs that triggers one of the configured interrupts, the corresponding interrupt flag will be set When an interrupt flag is set, if that interrupt is enabled AND its priority is higher than the code currently being executed, the corresponding Interrupt Service Routine (ISR) is executed The ISR deals with the cause of the interrupt, CLEARS THE INTERRUPT FLAG, and the foreground code continues to execute where it left off Timers So far, the “events” we have looked at that trigger interrupts have been external The PIC’s built-in timers can also generate interrupts: A timer is just a counter that starts at 0, counts up to a predetermined value then resets to zero The PIC can be configured so an interrupt is thrown each time the predetermined value is reached This generates a periodic interrupt, which can be extremely useful! Timer Terminology – 1 TMRx: 16-bit register containing current count of TimerX. We will work with Timer2 and Timer3 Tick: One count of a timer Period Register (PRx): 16-bit register containing the # of ticks TimerX counts before it resets to 0. Max value is 0xFFFF (65,535) Timeout Period (TTxIF): Amount of time that passes before TimerX resets to 0 Timer Terminology – 2 Internal Clock (FCY): For our purposes, always the PIC’s internal 40MHz clock Prescaler (PRE): # of clock cycles that occur for the timer to count up 1 tick (1, 8, 64, or 256) TxIF: Interrupt flag for TimerX TxCON: Configuration register for TimerX Timer Clarification Although the PIC clock (FCY) is always 40 MHZ the timers do not always count ticks at this frequency! By changing the prescaler value, the timer could take either 1, 8, 64, or 256 clock periods for a single tick. So, to calculate total time between interrupts, always consider both PRx and PRE: TTxIF = (PRx + 1) x PRE / FCY The interrupt flag is not set every time the clock ticks! It is set only when the timer has counted the number of ticks in the period register (PRx)! Timer Diagram Assuming the following timer 2 settings: TMR2 = 0 initially PRE = 8 PR2 = 1 25 ns, 1 clock period 200 ns, 8 clock periods, 1 timer tick 40MHZ 400 ns, 16 clock periods, 2 timer ticks, 1 timeout period Interrupt generated and TMR2 reset to 0 Timer Configuration Step 1: Set configuration register (TxCON=?). For now, these values will always be the same except for the prescaler (shown in yellow) Step 2: Set # of ticks for period register (PRx=?) Step 3: Clear the timer Register (TMRx=0) Step 4: Start the timer (TxCONbits.TON = 1) Example: T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF | T2_32BIT_MODE_OFF | T2_SOURCE_INT | T2_PS_1_8; PR2 = 2; TMR2 = 0; T2CONbits.TON = 1; Timer Diagram TCY TCY Ignore for now =0 =0 Determining PRx and PRE The application usually dictates TTxIF and the programmer must figure out what values for PRx and PRE will deliver this timeout period Often, we assume PRE and calculate PRx: PRx = (TTxIF x FCY / PRE) – 1 Always double-check that PRx <= 65535! In the lab, there are utility functions: PRE = getTimerPrescale(TxCONbits); PRx = msToU16Ticks (TTxIF, PRE) – 1; PRx = usToU16Ticks (TTxIF, PRE) – 1; Ch. 9 Problem #20 (modified) How many PIC24 Timer2 ticks are equal to 70µs assuming a prescale of 1 and a FCY of 40MHz? PR2 = (TT2IF x FCY / PRE) – 1 PR2 = (70x10-6 x 40x106 / 1) – 1 PR2 = 2800 – 1 PR2 = 2799 ticks Ch. 9 Problem #22 (modified) What is the maximum PIC24 Timer2 timeout period assuming a prescale of 64 and a FCY of 40MHz? Max timeout occurs when PR2 = 0xFFFF = 65535, so.... TTxIF = (PRx + 1) x PRE / FCY TTxIF = (65535 + 1) x 64 / (40x106) TTxIF = 0.1049 =