3 (10 points) The following program configures the PIC16F887 to generate a Timer
ID: 3875597 • 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. Background: 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 0 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 ; Oxffff ; 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
TMR1ON=1; //The timer is enable
TMRICS=0; //Internal clock source
TICKPS0=0; // Prescalar value set to "00"
TICKPS1=0; // Which means 1:1 ( no division)
Int count=0;
Void main()
{
TRISC=0; // Configuring ouput port
PORTC.F0=1; //led connected to 1st of port C
TMR1H=0x00; // Initial count value in timer 1 register
TMR1L = 0x00;
T1CON= 0x00;
While(1)
{
While(TMR1IF==0)
Count=count+1;
If(count==217) // for .14 sec
{
PORTC.F0=~PORTC.F0;
count=0;
}
PIR1.F0=0;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.