In Program 11-3C (Page 438 or PDF p. 452), you see when Timer 0 and Timer 1 over
ID: 3862411 • Letter: I
Question
In Program 11-3C (Page 438 or PDF p. 452), you see when Timer 0 and Timer 1 overflows, the ISR (Interrupt Service Routine) are like below:
#include <p18F458.h>
#define myPB6bit PORTBbits.RB6
void chk_isr(void);
void T0_ISR(void);
void T1_ISR(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
if(INTCONbits.TMR0IF == 1)
T0_ISR();
if(PIR1bits.TMR1IF == 1)
T1_ISR();
}
#pragma code My_HiPrio_Int = 0x0008
void My_HiPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
void main(void)
{
TRISBbits.TRISB6 =0;
TRISCbits.TRISC0 =1;
TRISD =0;
T0CON = 0x08;
TMR0H = 0;
TMR0L = 0;
T1CON = 0x06;
TMR0H = 255;
TMR0L = -200;
INTCONbits.TMR0IF = 0;
PIR1bits.TMR1IF = 0;
INTCONbits.TMR0IE =1;
PIE1bits.TMR1IE = 1;
T0CONbits.TMR0ON =1;
T1CONbits.TMR1ON =1;
INTCONbits.PEIE =1;
INTCONbits.GIE =1;
while(1);
}
void T0_ISR(void)
{
PORTD++;
TMR0H=0;
TMR0L=0;
INTCONbits.TMR0IF=0;
}
void T1_ISR(void)
{
myPB6bit = ~ myPB6bit;
TMR1H=255;
TMR1L=-200;
PIR1bits.TMR2IF=0;
}
A.
Here PORTD++ increments PORTD as 0, 1, 2, 3, …, until 255,and then 0, 1, again etc. for every timer 0 overflow (from FFFG 0r 65535 to 0).
Modify the code to add a switch SW so that
When SW = 0, PORTD increments 0, 1, 2, …, 255, 0, 1, 2, …, etc.
When SW = 1, PORTD increments by 2 as: 0, 2, 4, 6, …, 252, 254, 0, 2, 4, 6, etc.
B.
Modify the code of program 11-3C to have T0_ISR called every 100 ms (milliseconds). Show the calculation.
C.
Further modify the code of program 11-3C so that the first call of T0_ISR has PORTD = 1; the second call of T0_ISR has PORTD = 10 binary = 2 decimal = 0x2, etc. (then PORTS = 4; PORTD = 8; until PORTD = 128 for next T0_ISR call, followed by PORTD = 1; etc.)
Explanation / Answer
#include <p18F458.h>
#define myPB6bit PORTBbits.RB6
void chk_isr(void);
void T0_ISR(void);
void T1_ISR(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
if(INTCONbits.TMR0IF == 1)
T0_ISR();
if(PIR1bits.TMR1IF == 1)
T1_ISR();
}
#pragma code My_HiPrio_Int = 0x0008
void My_HiPrio_Int(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
void main(void)
{
TRISBbits.TRISB6 =0;
TRISCbits.TRISC0 =1;
TRISD =0;
T0CON = 0x08;
TMR0H = 0;
TMR0L = 0;
T1CON = 0x06;
TMR0H = 255;
TMR0L = -200;
INTCONbits.TMR0IF = 0;
PIR1bits.TMR1IF = 0;
INTCONbits.TMR0IE =1;
PIE1bits.TMR1IE = 1;
T0CONbits.TMR0ON =1;
T1CONbits.TMR1ON =1;
INTCONbits.PEIE =1;
INTCONbits.GIE =1;
while(1);
}
void T0_ISR(void)
{
PORTD++;
TMR0H=0;
TMR0L=0;
INTCONbits.TMR0IF=0;
}
void T1_ISR(void)
{
myPB6bit = ~ myPB6bit;
TMR1H=255;
TMR1L=-200;
PIR1bits.TMR2IF=0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.