ECE 341 2013 in Class Midterm1
ECE 341 2013 in Class Midterm1
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).
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
(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.
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.
Mazidi).
By definition, A
B = AB + BA.
B = C = 120.
B = 100.