We want to use Timer 1 as a time base and generate a 100 s pulse every 1 ms. To
ID: 3592265 • Letter: W
Question
We want to use Timer 1 as a time base and generate a 100 s pulse every 1 ms. To do this we will configure Timer 1 to alternate counting 900 s and 100 s intervals, with the output on RB15 respectively low for 900 s and then high for 100 . We will use Timer 1 interrupts and toggle RB15 in the ISR (assume RB15 is configured as a digital output) In the following assume a PIC24F J64GA002 microcontroller operating with a 16 MHz instruction cycle clock, FcY (a) Show the code needed to initialize the Timer 1 interrupts. Timer 1 should be assigned its natural priority. (b) Show the code needed to initialize and enable Timer 1. We want to be as accurate as possible. (c) Write the Timer 1 ISR for this application. Use the ISR macro.Explanation / Answer
a) Timer has its natural priorirty as 3. Hence the code needed to to initialize Timer 1 interrupt is-
IPC0bits.T1IP = 0x03; /* set priority as 3 */
IFS0bits.T1IF = 0; /* clear timer1 interrupt status flag */
IEC0bits.T1IE = 1; /* enable timer1 interrupt */
b) f = 16 MHz, hence timer will get 8 MHz T = 0.125 micro-second. So for 900 micro second count will be 900/0.125 = 7200 = 0x1C20.
For 100 micro second, count = 800 = 0x320. Hence for 900 micro-second the timer will be initialized as-
T1CON = 0x00; /* clear and stop the timer1 */
TMR1 = 0x00; /* clear the timer count start value */
PR1 = 0x1C20; /* load count value */
T1CONbits.TCKPS = 0; /* set 1:1 prescaling */
for 100 micro second counting just change PR1 = 0x320 in place of 0x1C20.
c) Timer1 ISR is written below-
void _ISR _T1Interrupt( int count_value )
{
T1CON = 0x00; /* clear and stop the timer1 */
TMR1 = 0x00; /* clear the timer count start value */
PR1 = count_value; /* load count value */
T1CONbits.TCKPS = 0; /* set 1:1 prescaling */
IPC0bits.T1IP = 0x03; /* set priority as 3 */
IFS0bits.T1IF = 0; /* clear timer1 interrupt status flag */
IEC0bits.T1IE = 1; /* enable timer1 interrupt */
T1CONbits.TON = 1; /* start the timer */
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.