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

Consider Example 9-29 on page 364 that shows how a C program toggles only the PO

ID: 3551538 • Letter: C

Question

Consider Example 9-29 on page 364 that shows how a C program toggles only the PORTB.4 bit continuously every 50 ms. It uses Timer 0 16-bit mode; the 1:4 prescaler to create the delay and assume XTAL = 10MHz. This is the example you have had in both exercise 3 and exercise 4 already.


#include <p18f4580.h>

void T0Delay(void);

#define mybit PORTBbits.RB4

void main(void)

{

TRISBbits.TRISB4=0;

while(1)

{

mybit^=1;

T0Delay();

}

}

void T0Delay()

{

T0CON=0x01;

TMR0H=0x85;

TMR0L=0xEE;

T0CONbits.TMR0ON=1;

while(INTCONbits.TMR0IF==0);

T0CONbits.TMR0ON=0;

INTCONbits.TMR0IF=0;

}

(5%) Explain what the while (1) loop in line 6 of the code does.

(5%) Explain what the while(INTCONbits.TMR0IF==0); loop in line 18 does.

(5%) In what situation will line 19 be executed? Is it possible that line 19 never gets executed with the current code? Explain!

(5%) How long (in seconds) does line 18 run? Show the calculation.

(5%) What does line 8 do? Explain!

(5%) How many times is PORTB.4 bit flipped (toggled) every second? In other words, how often does PORTB.4 change from 0 to 1 or from 1 to 0 in every second? Show the calculation.

(5%). Think of PORTB.4 as a square wave (switching between 0 and 1). What is the frequency of this square wave? Show the calculation.

Explanation / Answer

1.)

Toggles mybit(portB pin 4) after every Xms. Where Xms is the delay provided by the routine T0Delay

2.)

Waits for the TMR0IF flag to set. that is wait until the timer count is overflowed.

3.)

Yes it executes, right after the flag TMR0IF flag is set implicating the overflow of the timer0.

4.)

Initial value of timer counter is TMR0H = 0x85 & TMR0L = 0xEE. So it has to count 0xFFFF-0x85EE + 1 times to overflow. that is 0x7A12 = 31250 times.

The prescaler is 4 -> 31250*4 instruction cyles -> 125000 instrction cycles.

Instruction cyles is XTAL/4 -> 2.5MHz -> 0.4us

So total time for waiting is 0.4*125000 = 50000us = 50ms = 0.05s

5.)

Toggles the value on mybit ie PortB pin 4

6.)

As calculated above T0Delay provides a delay of 50ms. So PortB4 is toggled every 50ms ie 20 times is a second

7.)

value on the portB4 stays 0 for 5 ms and then toggles to 1 , waits for 50 ms and process repeats.

so the time period of the sqaure wave = high time+ low time = 100ms.

So frequency is 1/timeperiod = (1/100ms) = 10Hz


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote