3 (10 points) The following program configures the PIC16F887 to generate a Timer
ID: 2267073 • Letter: 3
Question
3 (10 points) The following program configures the PIC16F887 to generate a Timer 1 overflow interrupt once every 0.14 seconds. Your job is to fill in all of the missing blanks in this program to make it operate as intended. Be sure to read the comments beside each line of C code to help you know what to fill in the blanks. You should consult Section 6 (Timer 1 Module) of the PIC16F887 data sheet. Backqround: Through the use of the 16-bit Timer 1 overflow (wraps around from 0xFFFF->0x0000) we can generate a specific delay by writing an initial value to the Timer 1 (TMR1) register, which causes the timer to start counting from this initial value upward at the designated tick rate. When the timer overflows, the desired waiting time has completed. For example, if the Timer 1 tick rate is set to 1 microsecond, we can wait for 25 ms by writing an initial value of 216-25000 40536 to TMRI, since 1 microsecond * 2500025 ms. Next we should clear the Timer 1 overflow interrupt flag (TMR1IF) and then either wait for TMR11F to become set in a loop, or we could enable Timer 1 interrupts and allow our program to go on and do other things until a Timer 1 overflow interrupt occurs. We will use the interrupt-driven approach in the program below // HW6 Prob 3 Demonstrating the use of Timer 1 Overflow Interrupts to generate a prescribed delay // In this example program, an LED connected to RDO is toggled every 0.14 s by the interrupt handler #include void main(void) // Make RDO an output. // Turn off LED on RDO // Set internal clock frequency to Fosc 8 MHz IRCF2- 1; IRCF1- 1; IRCFO-1; TMR1GE:- ; TMR1ON//Enable TIMER 1 (See Fig. 6-1 in PIC16F887 Data Sheet) // Select internal clock whose frequency is Fosc/4 for Timer1 // Set prescale to divide by 8 yielding a clock tick period of 4 seconds TICKPS1 ; TICKPSO ; = 0xffff ; I/ Schedule first TMR1 overflow interrupt to occur in 0.14 s. // Clear TMR1 overflow interrupt flag // Enable TMR1 overflow interrupt // Enable all peripheral interrupts // Globally Enable all interrupts // Main Program Idle Loop GIE while(1);Explanation / Answer
Answer:-3) The blank lines can be filled as-
TRISD = 0x00; //make port D pins as output(all pins, along with pin zero)
PORTD = 0x00; //make LED at port D pin0 off
TMR1GE = 0; //Gate is not active now, T1CON register bit6
TMR1ON = 1; //Timer 1 is ON now, T1CON register bit0
TMR1CS = 0; //Internal Clock(Fosc/4), T1CON register bit1
T1CKPS1 = 1; //, T1CON register bit5
T1CLPS0 = 1; //set 1:8 prescaling, T1CON register bit4
TMR1 = 0xFFFF - 0x7747 //to set delay of 0.14s count must be 35000 in decimal
TMR1IF = 0; //timer1 interrupt flag is cleared, PIR1 register bit0
TMR1IE = 1; //again enable timer1 interrupt, PIE1 register bit0
PEIE = 1; //enable all peripheral interrupts, INTCON register bit6
GIE = 1; //globally enable all interrupts, INTCON register bit7
In the the function:-
RD0 = RD0 ^ 1; //performing XOR operation
TMR1 = 0xFFFF - 0x7747 //to set delay of 0.14s again after the overflow
TMR1IF = 0; //timer1 interrupt flag is cleared, PIR1 register bit0
Answer:-4) We have already used 1:8 prescaling and with clock of 8 MHz, we will get 1 MHz clock.
Again timer will use 1/4 MHz i.e. 4 us, with this to get a delay of 0.28s, timer needs to count 70000.
As we are using 16-bit timer we can count upto 65536 and hence maximum delay is 0.262144s only.
So 0.28s delay can't be produced with timer1 with clock source of 8 MHz.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.