Sei sulla pagina 1di 5

ECE 341 Quiz 3

Name:
Date: 2/4/14, Tuesday, week 5,
Due Date: 2/7/14, Friday, week 5
Total Grade Points: 3 grade points
Total Points: 117 points
A. (45%) Consider Example 7-1 on page 253 of the book of a for lop.
This example asked you to run C code on MPLAB to send values 00-FF to PORT B.
1. #include <P18F458.h>
2. void main(void)
3. {
4.
unsigned char z;
5.
TRISB = 0;
6.
for(z=0;z<=255;z++)
7.
PORTB = z;
8.
while(1);
9. }
(a) (7%) What happens to the code if you want to run that in PIC18F4321? Do you possibly need
any change?
Yes the header file needs to say #include <P18F4321.h> instead of #include <P18F458.h>

(b) (8%) If you change the inside of for loop from z<=255 to z <= 100, will this code still work (with
possibly less iterations?)?
The code wont send all the values from 00-FF. It will only send 00-64 hex to PORTB because it
only iterates 101 times from 0-100.

(c) (10%) What happens if you replace the line 7 PORTB = z; by z = PORTB;? Mention if
you need also some code change.
If you say z = PORTB then youre not going to output anything in your SFR PORTB.
PORTB is you special function register and in order for you to see an output in that
specific register you must have it the way it is. Z is just a variable storing data.

(d) (10%) Show how you do this if you want to replace the original for loop (of lines 6 and
7) by a while loop (you still have while(1) in line 8).

#include <P18F458.h>
void main(void)
{
unsigned char z;
z = 0;
TRISB = 0;
while (z <=255)
{
PORTB = z;
z++;
}
while(1);
}

(e) (10%) Show how you can do this if you want PORTB = z; for even z and PORTC = z; for
odd z. The range of z is from 0 till 255 as original code.
#include <P18F458.h>
void main(void)
{
unsigned char z;
z = 0;
TRISB = 0;
TRISC = 0;
for(z=0;z<=255;z++)
{
if (z%2 == 0)
PORTB = z;
else
PORTC = z;
}
while(1);
}

B.
1.
2.
3.
4.
5.
6.
7.

(33%) Consider Example 7-2 on page 254 of the book of reading from an array.
#include <P18F458.h>
void main(void)
{
unsigned char mynum[]= "012345ABCD";
unsigned char z;
TRISB = 0;
for(z=0;z<10;z++)

8. PORTB = mynum[z];
9. while(1);
10.
}
(a) (5%) In the original code, what is sent to PORTB when z = 9?
PORTB = char D (hex) = 14 (decimal). Since it starts off at z = 0 by the 10 th
iteration z = 9, which is character D in the array.
(b) (8%) If line 4 is changed to unsigned char mynum[]= "012345AB"; what is
sent to PORTB when z = 9?
Since it is out of the bounds of the array I would assume that I should get an
array out of bounds error, but when I tried doing this in MPLAB I didnt get an
error. I guess C doesnt check if an index is within the boundaries of an array.
What's happening line is that we have now accessed beyond the part of
memory that is allocated for the stack. As a result we are indexing into a part
of memory that is not allocated to the process or is allocated in a read only
fashion. So it generates a random character each time.
(c) (8%) If line 4 is changed to unsigned char mynum[]= "012345ABCDEFGH";
what is sent to PORTB when z = 9?
PORTB = char D (hex) = 14 (decimal). Since it starts off at z = 0 by the 10 th
iteration z = 9, which is character D in the array.
(d) (12%) How can you change line 7 the for loop so that it can read the whole
mynum[] array whether it is "012345ABCD", "012345AB",
"012345ABCDEFGH", or "012345ABCDEFGHIJKLMNOPQRSTUVWXYZ"?
I would modify line 7 to read:
for(z = 0; z < sizeof(mynum); z++)

C. (39%) Consider Example 7-19 on page 268 of using Bitwise AND, OR, XOR,
>>, and << etc.
1. #include <P18F458.h>
2. void main (void)
3. {
4. TRISB = 0;
5. TRISC = 0;
6. TRISD = 0;
7. PORTB = 0x35 & 0x0F;
8. PORTC = 0x04 | 0x68;
9. PORTD = 0x54 ^ 0x78;
10.
PORTB = ~0x55;

11.
12.
13.
14.
15.

PORTC = 0x9A >> 3;


PORTD = 0x77 >> 4;
PORTB = 0x6 << 4;
while(1);
}

(a) (8%) What is in PORTD after line 9? Explain using the operation of XOR. Do
not just quote that MPLAB runs this way.
Since logic operators act on a bit by bit pieces when you XOR the following
you are actually XORing each bit of it. Knowing the rules of XOR here is what I
have.
54H = 01010100
78H = 01111000
------------------------2C = 00101100
(b) (8%) What is in PORT C after line 11? Explain!.
I created an equation for shifting that I use. If its shifting to the left we
multiply y by 2x to give our new output and if its shifting to the right we
divide y by 2x to give our new output.
y<< x left shift
y>>x right shift
Line 11 we have 9A as our y value and 3 as our x value and we shift right so
we divide 9A by 23 (8) and our result is 13hex.
(c) (8%) If line 11 is changed to PORTC = 0x9A >> 8; What happens to the code?
What will be in PORT C after line 11?
Since we shift to the right 8 times we are going to be left with all 0s in all 8
bits so our PORTC will have a value of 0x00.
(d) (15%) What happen to PORTB, PORTC and PORTD after execution of lines 7, 8,
and 9 if lines 7, 8, and 9 change to:
PORTC = 0x35 ^ 0x0F;
35h = 00110101
XOR
0Fh = 00001111
3Ah = 00111010
PORTD = 0x04 & 0x68;
04h = 00000100
AND
68h = 01101000
00h = 00000000

PORTB = 0x54 | 0x78;


54h = 01010100
OR
78h = 01111000
7Ch = 01111100

Potrebbero piacerti anche