0% found this document useful (0 votes)
83 views9 pages

ECE 341 2013 in Class Midterm1

This document contains a midterm exam for an ECE 341 class. It has 10 questions covering various topics related to assembly language, C programming, PIC microcontrollers, bitwise operations, and checksum calculations. Some of the questions ask the student to write code snippets in assembly or C to perform operations like toggling port values, bit shifts, and delays. Other questions test understanding of concepts like overflow, carry, PIC architecture, and computing checksum values. The exam is out of a total of 108 points and covers material from the textbook and previous quizzes.

Uploaded by

LukeLi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views9 pages

ECE 341 2013 in Class Midterm1

This document contains a midterm exam for an ECE 341 class. It has 10 questions covering various topics related to assembly language, C programming, PIC microcontrollers, bitwise operations, and checksum calculations. Some of the questions ask the student to write code snippets in assembly or C to perform operations like toggling port values, bit shifts, and delays. Other questions test understanding of concepts like overflow, carry, PIC architecture, and computing checksum values. The exam is out of a total of 108 points and covers material from the textbook and previous quizzes.

Uploaded by

LukeLi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ECE 341 Midterm In Class

Name: _____________________
Date: 2/14/13, Thursday, 4:00 p.m. 5:15 p.m.
Total Grade Points: 13 grade points
Total Points: 108 points
How assembly language and C do the following. (24%)
Clock Frequency and time calculations example 3-14
1. (12%) Write the assembly instructions / functions for the following operations. Note you do
not have to write a working program. For example, part (a) can be just an instruction and you
do not have to include ORG, EQU etc.
(a) Turn on bit 3 of PORTC. pg 143
BSF PORTC, 3
(b) Turn off bit 3 of PORTC.
BCF PORTC, 3
(c) Have a delay of between 1 ms and 2 ms (it can be anything like a 4 digits 1xxx s).
(d) Add up three integers a, b, and c each between decimal 15 and 30 (you specify a, b and c
yourself, in hex or in decimal as you like). Pg 156
MOVLW d15
ADDLW d20
ADDLW d25
2. (12%) Write the C instructions / functions for the following operations. Note you do not have
to write a working program. For example, part (a) can be a #define instruction and an
instruction to set a variable to 1 in C language. You do not need to include the instruction
#include <PIC18F458.h>
(e) Turn on bit 3 of PORTC. Pg 262
#define input PORTCbits.RB3

input = 1;
(f) Turn off bit 3 of PORTC.
#define input PORTCbits.RB3

input = 0;
(g) Have a delay of between 100 ms and 200 ms pg257
void MSDelay (unsigned int);
MSDelay(150) //150ms delay
void MSDelay (unsigned int itime)
{
unsigned int i; unsigned char j;
for (i=0; i<itime; i++)
for(j=0; j < 165; j++)
}
(h) Add up three integers a, b, and c each between decimal 15 and 30 (you specify a, b and c
yourself, in hex or in decimal as you like).

int a,b,c, sum;


int a = 15;
int b = 15 ;
int c = 15;
sum = a + b + c;
Toggling data in PORTB (20%)
The book had shown how PORTB data can be toggled between 0x55 and 0xAA (which is ones
complement of 0x55).
3. (12%) Show in C language three ways of toggling data in PORTC between 0x42 and its
ones complement with 300 ms delay. What is the value of that ones complement? How
should you set TRISC in such code? (hint: look at examples of chapter 7. You do not have to
include whole program such as #include <P18F458.h>, void main (void) etc.). You can
assume code of MSDelay as follows without rewriting that. Pg 254, 268, 269
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}

42h = 66 decimal = 0100 0010


Complement = 1011 1101 = 189 decimal = BDh
4h + B = F, 2h + D = F, therefore, complement of 42h = BDh
TRISC should be set to 0 because it is an output
#include <P18F458.h>
void MSDelay(unsigned int)
void main(void)
{
TRISC = 0;
while(1)
{
PORTC = 0x42;
MSDelay(300);
PORTC = 0xBD;
MSDelay(300);
}

TRISC = 0;
PORTC = 0x42
while(1)
{

PORTC = ~PORTC;
MSDelay(300);
}

TRISC = 0;
PORTC = 0x42
while(1)
{

PORTC = PORTC^0xFF;
MSDelay(300);
}
4. (8%) Show in assembly language two different ways of toggling between 0x57 and its
ones complement. For simplicity, delay is not needed here. An assembly instruction you
learned in chapter 2 before page 60 can be used as one of such way. Pg 54, 173
(1)

(2)

MOVLW 0x57
XORLW 0xFF

Right shift (>>) and left shit (<<) (20%)


In chapter 7.3 (and example 7-19 in quiz 3) you have learned right shift and left shift. You
remembered on page 267 that 0x9A >> 3 = 0x13, 0x77 >> 4 = 0x07, and 0x6 << 4 = 0x60.
5. (10%) You know right shift 1 bit means dividing an integer by 2.
(a) Does this mean right shift is division exactly by 2? Explain this by shifting 0x9A 1 bit, 2
bits and 3 bits and show what values are 0x9A >> 1, 0x9A >> 2 and 0x9A >> 3.
No, it is exactly division by 2 ONLY when the number being divided is even. For
example:
0x9A = 154 (decimal).
0x9A >>1 = 4D = 77 (decimal).
0x9A>>2 = 4D>>1 = 0x26 = 38(decimal). In this case, the answer should have
been exactly 38.5 but Hex and binary values dont use decimal points so for odd
values it is always off by +0.5.
0x9A>>3 = 0x26>>1 = 0x13 = 19 (decimal).
(b) Is it true that right shifting will always generate an integer of smaller or the same
magnitude? When will n >> 1 be the same as n?
No, the only time n>>1 will result in n, is if n = 0. For example: 0>>1 = 0
(c) Given a general nonzero integer n with 0 < n < 255, what is / compute the minimum
number of right shifts r to cause n >> r to become 0 (so this means n >> (r - 1) is still
nonzero. What is that value?).
r = 8 because 255 in binary is 8 bits long. For any value n, the general idea is to
convert the number to binary, count the number of bits and r = #number of bits.
(d) In particular, for 0x9A and 0x77, how many right shifts do you need to make them 0?
0x9A = 1001 1010. Because 9A is 8 bits long, it would take 8 shifts to make them 0.
0x77 = 0111 0111. Because it is only 7 bits long it will take only 7 shifts to make it 0.
6. (10%) You know left shift 1 bit means multiplying by 2.
(a) You have learned 0x6 << 4 = 0x60. Calculate 0x6 << n for n = 1 up to 8.
0x6<<1 = 0xC
0x6<<2 = 0x18
0x6<<3 = 0x30
0x6<<4 = 0x60
0x6<<5 = 0xC0
0x6<<6 = 0x80 C = 1
0x6<<7 = 0x00 C= 3
0x6<<8 = 0x00 C =6
(b) Can you conclude that left shift always create a bigger integer (of twice as big)? Why?
Yes, because when you left shift you are always adding a 1 to the most significant bit.
For example: 10<<1 = 100. The only exception is if you are left shifting 0. 0<<n = 0.

(c) When do left shift 0x6 << n generate strange results? (there may be several n in (a) that
this happens). Explain why you get odd results for such n.
You start generating strange results when you go above n = 6 because the Hex value goes
above 255. After n >= 6, a carry is generated.
PIC with n bits (16%)
7. (16%) Knowledge and sense of PIC microcontroller family. Pg 28
(a) Why PIC 18 is called 8 bits microcontroller?
It has an 8 bit architecture meaning it reads and executes on an 8 bit basis (CPU can only
work on 8 bits of data at a time). All the registers are also 8 bits wide.
(b) Is (are) any other PIC microcontroller of 8 bits?
Yes PIC10xxx, 12xxx, 14xxx, 16xxx, 17xxx, and 18xxx
(c) Are there PIC microcontrollers of 16 bits, 32 bits?
Yes for example PIC24Fxxxx are all 16 bits and the PIC32MXxxxx are all 32 bits.
(d) Are there PIC microcontrollers of other bits combination such as 4 bits, 12 bits?
No there arent.
(e) PIC18F458 in the book consists of 40 pins. Does this mean all PIC18 family
microcontrollers are of 40 pins?
No the PIC18F2420 uses 28 pins.
Overflow, Carry, and Checksum (20%)
Overflow is covered in Mazidi pages 169 and 170 when in signed 8 bits numbers, two positive integers
add up to negative or two negative integers add up to positive.
You know signed 8 bits range from -128 to +127 and unsigned 8 bits range from 0 to 255.

When you add up 120 + 120, you get 240 or -16. This case you get an overflow of two positive
integers add to a negative integer -16.

120 = 0111 1000


+
120 = 0111 1000
240 = 1111 0000
Since D7 is 1 (N = 1) and OV = 1 we know its an unsigned positive number. Had
we not known OV = 1 we would take 2s comp and get -16
1111 0000 = 240 but computer thinks its a negative number so it takes the
negative 2s comp
0000 1111 = 1s comp
0000 0001 + 1
0001 0000 = -16

N Flag is the D7 bit. Overflow flag is set to 1 if there is a carry from D6 to D7 or from D7 out, but not
both. Meaning there will be overflow when a signed number goes above +127 or below -128. When
overflow is 1 it inverts the N (negative) flag so if N=1 and OV =1 then think of it as an unsigned number,
but if N=0 and OV = 1 then it is a negative number.

8. (8%) Overflow: Suppose we have signed 8 bit numbers and consider adding decimal 40
several times, 40, 40 + 40, 40 + 40 + 40, 40 + 40 + 40 + 40, until 40 + 40 + 40 + 40 + 40 + 40 (40*6
literally). We do not go further since 40 + 40 + 7 times is 280 > 256 even for unsigned
integer. Show when overflow happens in this sequence of adding 40 several times and
also what are the values when there are overflows (which means OV flag is on).
0010 1000 = 40
+
0010 1000 = 40
----------------------0101 0000 = 80
+
0010 1000 = 40
----------------------0111 1000 = 120
+
0010 1000 = 40
----------------------1010 0000 = 160
Since N = 1 (D7) and OV = 1 (D6 carry to D7) we know its a positive number,
but the CPU thinks its a negative value so it takes the negative 2s
complement of it
1010 0000 = 160
0101 1111 = 1s comp
0000 0001 + 1
0110 0000 = -96
**DO WE CONTINUE OR STOP and IF we continue to we use the -96 and add
40 or what?***

9. (6%) Carry: Now consider unsigned integer as you did in Quiz 1, #1 (hence 120 + 120 =
240, not -16). Consider the same sequence 40, 40 + 40, 40 + 40 + 40, 40 + 40 + 40 + 40 (4 times),
until 40 + 40 + +.. + 40 (7 times) and 40 + 40 + + 40 (8 times). Show when carry C = 1 in this sequence.

0010 1000 = 40
+
0010 1000 = 40
----------------------0101 0000 = 80
+

0010 1000 = 40
----------------------0111 1000 = 120
+
0010 1000 = 40
----------------------1010 0000 = 160
+
0010 1000 = 40
----------------------1100 1000 = 200
+
0010 1000 = 40
----------------------1111 0000 = 240
+
0010 1000 = 40
----------------------0001 0010 1100 = 280 we have C = 1 (7*40)
+
0000 0001 1000 = 40
----------------------0001 0100 0000 = 320 we have C = 1 (8*40)

10. (6%) Checksum: checksum is covered on pages 274-275 (ex 7-26 till 7-28). Compute the
checksum of 40 + .. + 40 (3 times), 40 + .. + 40 (4 times), 40 + .. + 40 (5 times), and 40
+ .. + 40 (6 times). Use Hex in your calculation (so 40 = 28H)
To get checksum byte we take the 2s complement of the hex value after
dropping the carry
***USE XOR FF for 1s comp and then add 1***
28h = 40
+
28h = 40
+
28h = 40
------------78h = 120 this is the sum. To get the checksum byte we take 2s
complement of 78h
0111 1000 = 78h
1000 0111 = 1s comp of 78h
0000 0001 + 1
1000 1000 = 88h = 2s comp of 78h = checksum byte

Perform checksum operation we add checksum byte to the sum and we are
supposed to get 00h
78h + 88h = 100h. Drop carry we are left with 00 so data isnt corrupt.
28h = 40
+
28h = 40
+
28h = 40
+
28h = 40
------------A0h = 160 this is the sum. To get the checksum byte we take 2s
complement of A0h
1010 0000 = A0h
0101 1111 = 1s comp
0000 0001 + 1
0110 0000 = 60h = 2s comp of A0h = checksum byte
Perform checksum operation we add checksum byte to the sum and we are
supposed to get 00h
A0h + 60h = 100h. Drop carry we are left with 00 so data isnt corrupt.
28h = 40
+
28h = 40
+
28h = 40
+
28h = 40
+
28h = 40
------------C8h = 200 this is the sum. To get the checksum byte we take 2s complement
of C8h
1100 1000 = C8h
0011 0111 = 1s comp
0000 0001 + 1
0011 1000 = 38h = 2s comp of C8h = checksum byte
Perform checksum operation we add checksum byte to the sum and we are
supposed to get 00h
C8h + 38h = 100h. Drop carry we are left with 00 so data isnt corrupt.

28h = 40
+
28h = 40
+
28h = 40
+
28h = 40
+
28h = 40
+
28h = 40
------------F0h = 240 this is the sum. To get the checksum byte we take 2s complement
of F0h
1111 0000 = F0h
0000 1111 = 1s comp
0000 0001 + 1
0001 0000 = 10h = 2s comp of F0h = checksum byte
Perform checksum operation we add checksum byte to the sum and we are
supposed to get 00h
F0h + 10h = 100h. Drop carry we are left with 00 so data isnt corrupt.

Simple XOR calculation

11. (8%) You learned XOR as Exclusive OR or

(this is covered in pages 172 and 173 of

Mazidi).

By definition, A

B = AB + BA.

(a) Suppose A = 100 decimal, compute / find B so that A


You can do 100 xor 120 = 28 = B
A = 0110 0100 = 100
B = 0001 1100 = 28
C = 0111 1000 = 120

(b) Conversely, if A = 120, compute / find B so that A


120 xor 100 = 28 = B

B = C = 120.

B = 100.

You might also like