Sunteți pe pagina 1din 7

ECE 341 Midterm Take Home

Date: 2/14/14, Thursday, week 6


Due Date: 2/21/14, Thursday, week 7
Total Grade Points: 12 grade points
Total Points: 107 points
How assembly language and C do the following. (40%)
For the same two problems you have in the midterm in class, now write the whole program and
show how you run those, and prove / verify your answer is right
1. (20%) Write an assembly program that incorporates the assembly instructions / functions for
the following operations. Prove or verify your code works (with screen shot, display of
registers, memory, or hardware etc.) You can do this in more than one program if necessary.
Turn in the results in the word document and also separately the assembly code.
It may be very hard to verify the delay of 1ms. So feel free to change the question to have delay
of 2 seconds say (using one or more loops).
(a) Turn on bit 3 of PORTC.

(b) Set PORTC to input only.

(c) Have a delay of between 1 ms and 2 ms (it can be anything like a 4 digits 1xxx s).
1

(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).

2. (20%) Write a C program that incorporates the C instructions / functions for the following
operations. Prove or verify your code works (with screen shot, display of registers, memory,
or hardware etc.) You can do this in more than one program if necessary. Turn in the results
in the word document and also separately the C code.
It may be very hard to verify the delay of 100 ms. So feel free to change the question to have
delay of 2 seconds say (using one or more loops)..
a) Turn on bit 3 of PORTC.

b) Set PORTC to input only.

c) Have a delay of between 100 ms and 200 ms (it can be anything like a 3 digits 1xx s).

d) Add up ten integers each between decimal 5 and 10 (you specify these integers yourself, in
hex or in decimal as you like).

3.

(15%) Explain in detail how Example 7-24 on page 272 works. Answer this question by
first explaining what packed BCD, unpacked BCD, and ASCII mean or stand for. Then
explain how each of the line works (how the data 0x29 change in the code and eventually
becomes 2 and 9).
#include <P18F458.h>
void main(void)
{
unsigned char x, y, z;
unsigned char mybyte = 0x29;
TRISB = 0;
TRISC = 0;
x = mybyte & 0x0F;
PORTB = x | 0x30;
y = mybyte & 0xF0;
y = y >> 4;
PORTC = y | 0x30;
}
4. (15%) Consider Example 5-30 on page 182 of the book that wrote an assembly program to
find the number of 1s in a given byte.
Explain using the assembly code below how the assembly code can find the number of 1s in a
given bytes. You do not have to run the assembly code to verify for this question.

The variable R1 initialized at 0 is used to count the number of 1s. We also create a STATUS file
C to use it to check if a bit is 1 or 0. We set the COUNT constant to 8 so we could check each bit
of the VALREG constant to see if its a 1 or 0. In this case 97 hex is moved into the constant
VALREG. By using RLCF we rotate left with a carry for the value stored in VALREG. We then
branch to the label NEXT if the value is 0 in the STATUS file C and decrement the COUNT
variable once (so from 8 it goes down to 7). Then we go branch back to the AGAIN label and
repeat the process. If the STATUS file C was checked to be 1, then we increment R1 by 1 and
move to the next instruction which repeats the process previously mentioned. Once COUNT has
reached 0 the program will stop and R1 (loc 0x20) will contain the number of 1s added up.
R1
EQU
COUNT
EQU
VALREG EQU

AGAIN
NEXT

0x20
0x21
0x22

BCF
STATUS,C
CLRF
R1
MOVLW
0x8
MOVWF
COUNT
MOVLW
0x97
MOVWF
VALREG
RLCF
VALREG,F
BNC
NEXT
INCF
R1,F
DECF
COUNT,F
BNZ AGAIN

5. (16%) In midterm in class, question 10, you were asked to find / calculate B so that A

B = 120. when A = 100 in part (a) or A B = 100 when A = 120.

Write a C program that reads A as 100 decimal (0x64) and calculate B so that A

(how do you express A B in C program?).

B = 120

Enhance your program so that it can read A as 120 decimal and calculate B so that A
100.

B =

6. (10%) Example 7-4 on page 255 of the book showed a C program that
sends values of -4 to +4 (excluding 0) to Port B (using an array).
(a) Write a C program that sends values of -20 to +20 excluding 0 to Port
B (there are 40 such values from -20, -19, .., -1, +1, =2,.. , until +20).
You can use array, but are not limited to use an array.

(b)Write a C program that sends values of -100 to +100 excluding 0 to


Port B

7. (11%) Talk about your dream PIC project(s) such as using PIC to digitize your home
(connecting the appliances for example), to improve your car or the communication of
your car with say highway patrol, etc. You may talk about your plan on how to do that
6

using some of your current lab assignments or using something you plan to do in the
future.
Based on our most recent lab of constructing a circuit that acts as traffic light controllers I
was amazed on the capabilities of the PIC programmer. I think the PIC will be a great
tool to use to program Christmas lights. Every year people constantly purchase more and
more Christmas lights sometimes even replacing their old ones. It would be awesome if
we could synchronize these lights to work with music. Lights could turn on and off at
different ranges of frequencies the music delivers. Also we can control the color choices
of the LEDs that will turn on. Creating a pre-programmed device with these capabilities
is sure to do well in the market. Im sure if I studied more about the PIC all these options
will be workable.

S-ar putea să vă placă și