Sie sind auf Seite 1von 4

ECE 341 Quiz 4

Name: ___________________________________
Date: 3/7/13, Thursday, week 9, 5 5:25 p.m.
Total Grade Points: 3 grade points
Total Points: 121 points
This quiz emphasizes the PIC timers we covered after midterm. Please have your book or PDf file ready
when taking the quiz. Note: do not just answer yes or no to the questions. You need to explain why your
answer is that way.
1. (35%) 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.
A. #include <p18f4580.h>
1. void T0Delay(void);
2. #define mybit PORTBbits.RB4
3. void main(void)
4.
{
5.
TRISBbits.TRISB4=0;
6.
while(1)
7.
{
8.
mybit^=1;
9.
T0Delay();
10.
}
11.
}
12. void T0Delay()
13.
{
14.
T0CON=0x01;
15.
TMR0H=0x85;
16.
TMR0L=0xEE;
17.
T0CONbits.TMR0ON=1;
18.
while(INTCONbits.TMR0IF==0);
19.
T0CONbits.TMR0ON=0;
20.
INTCONbits.TMR0IF=0;
21.
}

(a) (5%) Explain what the while (1) loop in line 6 of the code does.
It continuously toggles PORTB.4 since its always true. Within the loop it calls the delay which
delays each toggle by 50ms.
(b) (5%) Explain what the while(INTCONbits.TMR0IF==0); loop in line 18 does.
It keeps monitoring the Timer0 Interrupt Flag until it rolls over (reaches 0) in order for it to go on
to the next set of instructions.

(c) (5%) In what situation will line 19 be executed? Is it possible that line 19 never gets
executed with the current code? Explain!
Once the Timer0 Interrupt Flag becomes 0 (rolls over) Line 19 will execute and it will
stop the timer. The only way to avoid this is if we never turn on Timer0.
(d) (5%) How long (in seconds) does line 18 run? Show the calculation.
FFFF-85EE = 7A11 = 31249 + 1 = 31250
Timer Delay = 31250*4*.4*10-6 = 50 ms
(e) (5%) What does line 8 do? Explain!

Line 8 mybit references to PORTB.4 and uses an XOR to toggle all the bits of PORTB.4
(f) (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.
It toggles every 50 ms because the next instruction calls the delay function which is a
50ms delay. It uses 85h in the High bit and EEh in the low bit. FFFFh 85EEh = 7A11h
= 31249+1 = 31250. The timer delay = 31250*4(prescaler)*0.4us = 50ms
So within 1 second we have 1s/0.05s = 20 times.
(g) (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.
Frequency = 1/(2*Timer Delay) = 1/100ms = 10Hz
B. (25%) Think of example 9-29 again. We want to consider modifying the code without
adding or removing a line (so we can change say T0CON=0x01; to T0CON = 0x65;. But
we are not removing this line T0CON=0x01; or adding another line T1CON= 0x32; for
Timer 1 say.).
(a) (15%) The code shows you how to toggle PORTB.4 every 50 ms. Modify the code (without
adding or removing a line, but changing some lines) to show a way of toggling every x ms
where x is an integer bigger than 3,000 picked by you or explain why this can not be done.
By changing the prescaler to 1:256 (T0CON = 0x67) we can get a delay of:

FFFFh 85EEh = 7A11h = 31249+1 = 31250.


The timer delay = 31250*256(prescaler)*0.4us = 3200ms
(b) (10%) Repeat the same question as in (a) with x changed to an integer bigger than 4,000
picked by you.
The largest value of timer delay we can get with the current 85EEh for TMR0H/L is 3200ms
since 1:256 is the largest we can prescale to. In order to get greater than 3200ms we must use
a smaller value for our TMR0H/L bits. If we were to use 0000h we would get the largest
possible delay by keeping our prescaler at 1:256 (T0CON = 0x67)
FFFFh 0000h = FFFFh = 65535+1 = 65536
Timer Delay = 65536*256(prescaler)*0.4 = 6710ms
C. (25%) Modify Example 9-29 and remove or add line(s) if necessary this time
for the 8 bit timer (not 16 bit timer).
D. #include <p18f4580.h>
22. void T0Delay(void);
23. #define mybit PORTBbits.RB4
24. void main(void)
25.
{
26.
TRISBbits.TRISB4=0;
27.
while(1)
28.
{
29.
mybit^=1;
30.
T0Delay();
31.
}
32.
}
33. void T0Delay()
34.
{
35.
T0CON=0x01;
36.
TMR0H=0x85;
37.
TMR0L=0xEE;

38.
39.
40.
41.
42.

T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}

(a) (5%) Show the modified code for 8 bits timer.


****largest possible period we can generate with XTAL = 10Mhz is setting
TMR0L to 0. We can only get up to 26ms.****
E. #include <p18f4580.h>
1. void T0Delay(void);
2. #define mybit PORTBbits.RB4
3. void main(void)
4.
{
5.
TRISBbits.TRISB4=0;
6.
while(1)
7.
{
8.
mybit^=1;
9.
T0Delay();
10.
}
11.
}
12. void T0Delay()
13.
{
14.
T0CON=0x41;
8-bit, 1:4 prescale
15.
TMR0H=0x85;
16.
TMR0L=0x00;
17.
T0CONbits.TMR0ON=1;
18.
while(INTCONbits.TMR0IF==0);
19.
T0CONbits.TMR0ON=0;
20.
INTCONbits.TMR0IF=0;
21.
}

(b)(5%) Do you have both TMR0H and TMR0L for 8 bits timer (line 15 and line 16
in Question A earlier)? Explain!
No only TMR0L because it is only 8 bits and TMR0L takes 8 bits. TMR0H takes
another 8 bits which is only needed if its a 16 bits timer.
(c) (5%) Can you still have PORTB.4 toggling every 50 ms for the 8 bits timer
version here? Explain!
NO. Largest possible period we can generate with XTAL = 10Mhz is setting
TMR0L to 0. We can only get up to 26ms.
(d) (10%) For 8 bits timer, can you toggle PORTB.4 every y s (microseconds), where y is between 5
and 20? Explain! (show the code if this can be done or show the reason if this can not be done).
You can get about 15s delay by:
Value = 15*10^-6/(4*.4*10^-6) = 9.375
FF TMR0L = (8) + 1 = 9 = Value.
TMR0L = FF 8 = F7
So we set TMR0L to F7 and keep the rest of the code the same as in part a.
F. (36%) Timer as counter

Examples 9-34 till 9-37 on pages 369 till page 372 show the timer as a counter with
1-Hz, 64-Hz, 60-Hz as the frequency of external clocks.

(a) (10%) Is it possible to have an external clock of 128 Hz (which is not among the list of 1, 64, and
60 we saw in the 3 examples)? If this is possible, show which line(s) of which example can be
modified to make this possible. Otherwise explain why this is impossible.
Line 5 should be changed to T0CON = 0x66 so we set the prescale to 1:128 to get 128 Hz instead
of 1 Hz.
(b) (10%) Explain if it is possible to have an external clock of 50 Hz. Answer in a way similar to
(a).
If we changed Line 7 from TMR0L = -60 to TMR0L = -50. The counter then would have to count
up 50 pulses each second.
(c) (8%) Is it possible to have an external clock of higher frequencies between 500 and 1000 Hz?
Explain!
Yes by adjusting the prescaler appropriately you could acquire such frequencies.
(d) (8%) Is it possible to have an external clock of frequency lower than 1Hz (such as 0.5 Hz etc.)?
Explain!
No because anything less than 1Hz would be recognized as 0.

Das könnte Ihnen auch gefallen