Docsity

Microcontroller Programming and Interfacing: Final Exam Study Guide - Prof. John Peatman, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
MEMO TO: ECE 4175 students February 8, 2008
FROM: John Peatman
SUBJECT: First quiz on Wednesday, February 13 Open book, open notes.
th
Cody Planteen, Andrew Ray, and Bren Whitfield will hold a review session in room C241 (our
classroom) on Monday from 7:00-9:00 PM. Also, review the first quiz from last semester, available
from the piclab website. While the course is drastically different, at least you may get an idea of
the kinds of questions I ask.
Both the 9:00 class and the 10:00 class will be taking the same quiz. If you take the quiz at 9:00, do
not talk about the quiz or otherwise divulge information about the quiz as you leave. If you are in
the 10:00 class, do not press the 9:00 students with questions about the quiz. Thanks!
I plan to begin each quiz at 3 minutes after the hour (i.e., 9:03 and 10:03) and end at 3 minutes
before the hour (i.e., 9:57 and 10:57) unless someone comes to me no later than Monday to raise an
objection. OK?
1. Be sure to bring your source files for all of the projects. I will assume that you are familiar with the
kinds of things that arise in all of the projects up through Project Five. You will need to print out the
source files for each of your team, regardless of who has the folder that you turned in. Also bring the
qwik.lst file for Project Four.
2. Review David Bauer’s “Hints on doing well in ECE 4175” posted on the piclab website.
3. Read through Chapters 1, 2, 3, 4, 5, 6, 7, Sections 9.1 to 9.6, and Appendix A2.
4. I do plan to ask you questions that have to do with the debugging that you have done, or should have
done, in the lab.
5. You should be able to write C code to do the kinds of things you have had to write code for on the
projects.
6. I will not ask you to write assembly code. However, given the information in Appendix A2, you should
be able to determine the execution time of a short segment of assembly code that the C compiler has
generated into the qwik.lst file.
7. For Project One, you used the pushbutton in a rudimentary way, taking action as long as the pushbutton
was pressed. Subsequently, you took action in response to the leading edge of a pushbutton press.
8. All of our code examples use the brownout-reset functionality of Figure 4-3 assuming that the chip
powers up into our code, to help ensure reliable startup. When we use the chip via QwikBug, this
dealing with the brownout reset really doesn’t serve this function since QwikBug has already turned off
the brownout reset.
9. We have used the Sleep state of the CPU, waking up with either the watchdog timer’s timeout or with
Timer1's interrupt.
10. We have talked about the internal clock and how it can be controlled. Refer to Figure 2-3. We also
talked about the role of clock rate versus the average current draw to execute a fixed number of
instructions before returning to sleep for the remainder of the loop time.
11. We have also talked about the watchdog timer and its possible control with a Configuration option and
the control we have actually used with a SWDTEN bit in the WDTCON register. Refer to Figure 2-6.
12. We have discussed the debouncing of a mechanical switch. Refer to Figure 2-7.
13. You should understand the rationale for the device connections of Figure 3-2 for the parts we have used.
14. Know how to set a bit, clear a bit, toggle a bit, and test a bit, for both a bit in a SFR (Special Function
Register) and a flag bit defined in RAM (e.g., LCDFLAG).
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Microcontroller Programming and Interfacing: Final Exam Study Guide - Prof. John Peatman and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

MEMO TO: ECE 4175 students February 8, 2008

FROM: John Peatman

SUBJECT: First quiz on Wednesday, February 13 th Open book, open notes.

Cody Planteen, Andrew Ray, and Bren Whitfield will hold a review session in room C241 (our

classroom) on Monday from 7:00-9:00 PM. Also, review the first quiz from last semester, available

from the piclab website. While the course is drastically different, at least you may get an idea of

the kinds of questions I ask.

Both the 9:00 class and the 10:00 class will be taking the same quiz. If you take the quiz at 9:00, do

not talk about the quiz or otherwise divulge information about the quiz as you leave. If you are in

the 10:00 class, do not press the 9:00 students with questions about the quiz. Thanks!

I plan to begin each quiz at 3 minutes after the hour (i.e., 9:03 and 10:03) and end at 3 minutes

before the hour (i.e., 9:57 and 10:57) unless someone comes to me no later than Monday to raise an

objection. OK?

1. Be sure to bring your source files for all of the projects. I will assume that you are familiar with the

kinds of things that arise in all of the projects up through Project Five. You will need to print out the

source files for each of your team, regardless of who has the folder that you turned in. Also bring the

qwik.lst file for Project Four.

2. Review David Bauer’s “Hints on doing well in ECE 4175” posted on the piclab website.

3. Read through Chapters 1, 2, 3, 4, 5, 6, 7, Sections 9.1 to 9.6, and Appendix A2.

4. I do plan to ask you questions that have to do with the debugging that you have done, or should have

done, in the lab.

5. You should be able to write C code to do the kinds of things you have had to write code for on the

projects.

6. I will not ask you to write assembly code. However, given the information in Appendix A2, you should

be able to determine the execution time of a short segment of assembly code that the C compiler has

generated into the qwik.lst file.

7. For Project One, you used the pushbutton in a rudimentary way, taking action as long as the pushbutton

was pressed. Subsequently, you took action in response to the leading edge of a pushbutton press.

8. All of our code examples use the brownout-reset functionality of Figure 4-3 assuming that the chip

powers up into our code, to help ensure reliable startup. When we use the chip via QwikBug, this

dealing with the brownout reset really doesn’t serve this function since QwikBug has already turned off

the brownout reset.

9. We have used the Sleep state of the CPU, waking up with either the watchdog timer’s timeout or with

Timer1's interrupt.

10. We have talked about the internal clock and how it can be controlled. Refer to Figure 2-3. We also

talked about the role of clock rate versus the average current draw to execute a fixed number of

instructions before returning to sleep for the remainder of the loop time.

11. We have also talked about the watchdog timer and its possible control with a Configuration option and

the control we have actually used with a SWDTEN bit in the WDTCON register. Refer to Figure 2-6.

12. We have discussed the debouncing of a mechanical switch. Refer to Figure 2-7.

13. You should understand the rationale for the device connections of Figure 3-2 for the parts we have used.

14. Know how to set a bit, clear a bit, toggle a bit, and test a bit, for both a bit in a SFR (Special Function

Register) and a flag bit defined in RAM (e.g., LCDFLAG).

15. Understand when and how to recast a variable from a char to an int, etc, in a mathematical expression.

16. The 4321 chip uses its Serial Peripheral Interface to send ASCII-coded characters to the starburst LCD.

You should understand the role of the 4321's RD5 pin in this process as well as how the SPI works for

these transfers. With our simple message protocol, we always send nine characters for an eight-character

display. Why?

17. For Project Two, you used each push of the pushbutton to double the step rate of the stepper.

18. For Project Three, you used a rate multiplier scheme to increment the step rate from 1 s/s up to 62 s/s.

You also measured how long the CPU remains awake during each loop time doing useful work.

19. For Project Four, you used high-priority interrupts to produce 1.6 ms tick times and then used these

interrupts to reuse the rate multiplier scheme of Project Three to produce step rates from 10 s/s up to 620

s/s in increments of 10 s/s.

20. For Project Five, you used the output of the pot to set the step rate, no longer using the rate multiplier

scheme. Now you translated the pot value into a step rate display and a number of counts of Timer

between interrupts - and thereby the interval between steps.

21. Timer0 necessarily requires us to use its buffer register, TMR0H, if we want to read out the content of

the timer, or write to it. What is the intent of the buffer register, in general? What is the effect of the

buffer register if we stop the counter while we read from or write to the counter?

22. Consider the TXascii macro called from within the Measure.c file. What is the effect of waiting for the

setting of the TRMT bit before exiting and going to sleep?

23. We have looked at a bunch of ways to convert a NUMBER of a given size into UNITS, TENS,

HUNDREDS, etc. ASCII characters. You should have a pretty good idea of what helps to minimize the

cycle count of an algorithm to do this.

24. On the projects, we have dealt with the LED, the pushbutton, the pot and its use with the ADC as both an

8-bit input and a 10-bit input device, the LCD, as well as individual output pins (e.g., RB0). None of

these interactions should hold any mysteries for you.

25. Timer1 and Timer3 offer an option not available for Timer0 of buffering the upper byte of the counter.

Whereas the circuitry for Timer0 forces the use of this buffering, we chose not to use either one with

Timer1 or Timer3. What went into this choice, given how we used Timer1 and Timer3? When this

buffering feature is used, what is it good for?

26. If the chip is asleep when the watchdog timer times out, the CPU just picks up with the execution of the

next instruction after the Sleep macro. If the chip is awake when the watchdog timer times out, the CPU

is reset and starts over from address 0x0000.

27. Timer1 and Timer3 generate interrupts whether or not the chip is asleep. The CPU awakens, vectors to

the respective interrupt service routine, executes it and then returns from the interrupt, back to the next

instruction in the code that was executing at the time of the interrupt, reenabling the interrupt for that

timer in the process.

E C E 41 7 5 Q U IZ 2-13 -08 2 o f 2

  1. C onsider the code below, taken from the qwik.lst file for a modified version of the T 3.c tem plate pro gram. [25% ] // For high priority interrupts: #pragma code high_vector=0x void interrupt_at_high_vector(void) 3 { 2 0008 EF24 GOTO HiPriISR _asm GOTO HiPriISR _endasm 000A F . . 2 0048 CFDA MOVFF FSR2H,PREINC1 void HiPriISR() 004A FFE 1 004C 52E6 MOVF POSTINC1,F { 004E 90B1 BCF T3CON,0 T3CONbits.TMR3ON = 0; 1 0050 5007 MOVF STEPCNTL,W TMR3L = STEPCNT; 1 0052 6EB2 MOVWF TMR3L 1 0054 5008 MOVF STEPCNTH,W TMR3H = STEPCNT/256; 1 0056 6EB3 MOVWF TMR3H 0058 80B1 BSF T3CON,0 T3CONbits.TMR3ON = 1; 005A 92A1 BCF PIR2,1 PIR2bits.TMR3IF = 0; 005C 8283 BSF PORTD,1 PORTDbits.RD1 = 1; 005E 9283 BCF PORTD,1 PORTDbits.RD1 = 0; 0060 7081 BTG PORTB,0 PORTBbits.RB0 ^= 1; 0062 52E5 MOVF POSTDEC1,F } 0064 CFE5 MOVFF POSTDEC1,FSR2H 0066 FFDA 0068 0011 RETFIE FAST

C onsider the possib le consequences of not stopping the counting of T imer3 and also of reloading (rather than adding into) T M R 3 H :T M R 3 L. Assum e that it takes a maxim um of three cycles of the C PU after T imer3 rolls over and before the CP U has fetched the instruction at the high-priority interrupt vector address 0 x0008 and is ready to execute it. a. C ross out the instruction above that stops the co unting of T im er3 and the instruction that starts the co unting of T imer3 again. b. H ow m any C P U clock cycles can occur after T im er3 rolls over and b efore the ab ove co de will have reloaded T M R 3 H :T M R 3 L with the content of S T E P C N T H :S T E P C N T L? In answering this, to the left of each line above that is executed until T M R 3 H :T M R 3 L has been reloaded, write the number of cycles executed for that line. Cycles = 12 c. H ow m any microseco nds will pass after T im er3 rolls over until T im er3 is incremented again by the 32.768 kH z watch crystal? Microseconds = 30. d. G iven this, will reloading T imer3 rather than stopping T imer3 and adding into it always work? E xplain your answer b riefly. Yes; we are assured that Timer3 will not be reloading at the same time that it is counting.

  1. T he T 2.c tem plate program of C hap ter 5 uses the pushbutton to increment a number displayed o n the LC D , one increment per p ress o f the pushbutton. For this to work correctly, the maximum contact [10 % ] bo unce time of the pushbutton must satisfy what requirement? B e specific. It must be less than the loop time, which is 16 ms for T2.c.
  2. A ssume that in one of your projects you want to display the following message on the LC D display each time you detect a new condition has been fulfilled and before displaying the new data.

[15% ] NEW DATA

Show the code you would use to do this, making use of the L o a d L C D S T R IN G macro. LoadLCDSTRING("NEW DATA "); Display();

MEMO TO: ECE 4175 students March 14, 2008

FROM: John Peatman

SUBJECT: Second quiz on Wednesday, March 26 th Open book, open notes.

Cody Planteen, Andrew Ray, and Bren Whitfield will hold a review session in room C241 (our

classroom) on Monday from 7:00-9:00 PM. Also, review the quizzes and final exam from last

semester, available from the piclab website.

Both the 9:00 class and the 10:00 class will be taking the same quiz. If you take the quiz at 9:00, do

not talk about the quiz or otherwise divulge information about the quiz as you leave. If you are in

the 10:00 class, do not press the 9:00 students with questions about the quiz. Thanks!

I plan to begin each quiz at 3 minutes after the hour (i.e., 9:03 and 10:03) and end at 3 minutes

before the hour (i.e., 9:57 and 10:57) unless someone comes to me no later than M onday to raise an

objection. OK?

1. Be sure to bring your source files for all of the projects. I will assume that you are familiar with the kinds

of things that arise in all of the projects up through Project Ten. You will need to print out the source

files for each of your team, regardless of who has the folder that you turned in.

2. Read through the remainder of the book, except for Chapter 14 on EEPROM and Chapter 16 on the LCD

and the appendices A1, A3, and A4. Be sure to bring the book in either Lulu or Tech-printed form.

3. I will not ask you to write assembly code. However, given the information in Appendix A2, you should

be able to determine the execution time of a short segment of assembly code that the C compiler has

generated into the qwik.lst file. You should also understand the functioning of short assembly code

sequences. I will avoid the cryptic stuff involving internal function calls like the math functions and the

variables that are introduced by the compiler to deal with them.

4. For Project Six, you decreased the INTOSC frequency from 4 MHz to 1 MHz while sending bytes to the

PC. What was this attempting to do and what was the result?

5. For Project Seven, you used the RPG to scroll an eight-character window over the sixteen-hex-character

silicon serial number.

6. For Project Eight, you used a rate-sensitive RPG scheme to change the stepping rate of the stepper motor.

7. For Project Nine, you compared different implementations of a linear equation, looking at the qwik.lst file

to discern how the c18 compiler is handling each implementation.

8. For Project Ten, you calibrated the HLVD feature of the PIC chip. You should go back and read the data

sheet on this feature and understand what it is for and how it works.

9. Chapter 12 deals with interrupts. You should understand how to deal with an interrupt source with either

a high or a low priority interrupt and how the structuring of the interrupt service routine can affect the

code generated by the C compiler. The use of a “critical region” of code is a topic that should have been

in Chapter 12. We discussed this last Monday in conjunction with the Timer1Check function where we

wanted to take action within microseconds after an event occurred, so we disabled interrupts to make sure

that code was not postponed momentarily by an intervening interrupt.

10. Chapter 15 deals with Dallas Semiconductor’s 1-wire interface, used by the silicon serial number part and

also by Dallas Semi’s iButtons. We discussed the implementation of open-drain outputs from the MCU

to communicate with these devices. We also discussed how a multiple-byte protocol that is specific to a

device like the DS2415 time chip can be used to read from it or write to it.

- OVER -

E C E 41 7 5 Q U IZ 3-26 -08 1 o f 2 H igh 113 SIX PR O B LEM S U pper quarter 87 O pen book, open notes M edian 68 Lower quarter 58 I added 10 points to everyone’s grade. Low 35

  1. C onsider the following assemb ly code generated into a qwik.lst file: [10% ] 014C 5039 MOVF RPG,W 014E E103 BNZ L 0150 0E0F MOVLW 0x0F 0152 6E39 MOVWF RPG 0154 D001 BRA L 0156 0639 L013 DECF RPG,F 0158 9084 L012 BCF PORTE, D escribe what this code sequence does to the R A M variable, R P G. If RPG is initially equal to zero, it is reloaded with 0x0F (i.e., 15). Otherwise it is decremented.

2. For P roject Six, you decreased the IN T O SC frequency from 4 M H z to 1 M H z while sending five bytes

[1 5% ] to the P C. a. If you had left IN T O SC = 4 M H z, ho w long wo uld it have taken to transmit the five bytes? Each byte, including its start and stop bits, takes 10/19200 seconds, or 10000000/19200 microseconds. This computes to 521 microseconds, or about 0.5 millisecond. Five bytes thus take about 2.5 ms. b. W ith IN T O SC = 1 M H z, ho w long does it take to transmit the five bytes? The same 2.5 ms. c. W hy does d ecreasing IN T O SC from 4 M H z to 1 M H z while send ing these five bytes to the P C decrease the average current draw on the coin cell? Because in either case there is a 2.5 ms time interval during which the chip draws a current determined by the clock rate. That current is about 1 mA for INTOSC = 4 MHz and about 0.5 mA for INTOSC = 1 MHz. (See Figure 2-4)

  1. W e have discussed how a critical region of co de must no t be perm itted to be interrupted. T hat is, while that section of co de is being executed , it must be allowed to pro ceed instruction by instruction with no [2 5% ] intervening interrupt perm itted to break the lock-step of the instruction sequence. a. For an application that makes use of no interrupt sources other than the single interrupt source, IN T 2 (the interrupt input from the R P G ), this can b e achieved by inserting the following line of C code before the critical region of code. G iven that every interrupt has both a local enable control bit and a glob al enable contro l bit, show the code that will locally disable this single interrupt source. INTCON3bits.INT2IE = 0; b. Show the line of C code to locally reenable IN T 2 interrupts at the completion of the critical region of code. INTCON3bits.INT2IE = 1; c. If an R PG is incremented during this tim e that IN T 2 interrupts are mom entarily disabled, will the C PU acco unt for that increment of the R P G , or is it lost forever? The CPU will account for that increment of the RPG when INT2 interrupts are reenabled. d. For a different application that makes use of IN T 2 interrupts and also T imer3 overflow interrupts, show the line that will globally disable all possib le interrupts by clearing a single contro l bit. INTCONbits.GIEH = 0; e. Finally, show the line of C code that globally reenables all interrupts at the com pletion of the critical region of code. INTCONbits.GIEH = 1; f. If a T im er3 overflow occurs during the tim e that all interrupts are globally disabled, will the C PU account for this overflow of T imer3 , or is it lost forever? U sing Figure 7-5, explain yo ur answer. The TMR3IF flag is set by the overflow. Either the clearing of the local interrupt enable bit, TMR3IE, or the clearing of the global interrupt enable bit, GIEH, will block TMR3IF from reaching the CPU. When GIEH is set again (along with TMR3IE already being set), the signal gets through to the CPU and produces the interrupt whose interrupt service routine will then account for the overflow.

E C E 41 7 5 Q U IZ 3-26 -08 2 o f 2

  1. Serial interfaces that reach from the M CU to more than one peripheral device have two problems to be considered here:

[20% ]! H o w d oes a sp ecific d evice kno w that it is the o ne b eing ad dressed?

! H ow are the beginning and the ending of multiple-byte messages marked?

a. For the serial peripheral interface, how are these two problems handled?

The MCU designates the specific device that is to receive and act upon the multiple-byte message by making that device’s chip enable pin active. Usually the chip enable pin is active low, so this means that the MCU pin that drives that chip enable pin has a zero written to it.

The MCU precedes the sending of the multiple-byte message with the activation of the specific device’s chip enable pin. It terminates the message by deactivating that device’s chip enable pin. Furthermore, because it is enabled, the SSPIF flag will be set after each received byte.

b. For the I C interface (a.k.a. the SM B us), how are these two problems handled?^2

The MCU indicates that a multiple-byte message is about to begin by transitioning the SDA and SCL lines through the I C^2 START condition. That is, the MCU first pulls the SDA line low and then the SCL line low (from their initial state of both being high). The end of the multiple-byte message is indicated when the MCU causes the SCL line to go high followed by the SDA line going high — a change that is called the I C^2 STOP condition.

Each device on the I2C bus has its own unique seven-bit address. Seven bits of the first byte of the multiple-byte message hold this seven-bit address. The addressed device responds to the rest of the message. All other devices on the bus ignore the rest of the message.

  1. C onsider a bipolar stepper m otor whose ro tor consists of just a single N orth pole and a single South po le. T he stator has two windings, designated as A and B. Full stepping is carried out by the [10 % ] following seq uence o f winding excitations, where A and its co m plem ent represent current in one d irectio n o r the o ther thro ugh wind ing A. __ __ __ __ ... A B A B A B A B ... a. W hat is the angle of rotation produced by each step? 90° b. H ow many steps per revolution does this sequence produce? 4

6. Suppo se that a transducer generates an output M ranging from 0 to 20 47. T he physical parameter N that

is being transduced is related to M by the equation [20% ] N = 4.321M + 342 a. R eexpress this relationship to avoid floating-po int math as well as division, so as to m inimize co m putation time while maintaining 1 p art in 20 48 resolution. Multiply 4.321M by X/X, where X is a power of 256 so that the division by X can be achieved by moving the numerator to the right by one byte position for 256 or two byte positions for 256x256. To maintain 1 part in 2048 resolution, X = 256 is not sufficient whereas X = 256x256 = 65536 is. So N = (65536/65536) x 4.321 x M + or N = 283181M/65536 + b. E xpress the computation in C. Y ou can assum e that M has been defined as a four-byte “long” variable. N = ((283181 * M)>>16) +342; If the C compiler implements this with bit shifts rather than byte moves, use the following instead. N = (((283181 * M) >> 8) >> 8) +342; Michael Pribble and John Hamilton had this answer. Many students used N = ((8849 * M) >> 11; to get the 1 part in 2048 resolution. However this doesn’t minimize the computation.

EC E 4175A Final 4-29-08 Page 1 of 3 H igh 116 SEV EN PR O B LEM S U pper quarter 95 O pen book, open notes M edian 79 Lower quarter 68 Low 51

  1. [ ] I have completed the C IO S co urse evaluation. [5% ] [ ] I have not completed the C IO S co urse evaluation.
  2. T he Q wikL ow board that we have used in p revious sem esters includ ed a three-digit seven-segm ent num eric LC D that had to be updated every ten milliseconds. T he refreshing function might have emp loyed the [15% ] following macro to write the seven-segment coding for each digit out to three serially-connected 74H C shift registers via the Serial P erip heral Interface:

    d e f i n e S h i f t ( o u t ) S S P I F = 0 ; S S P B U F = o u t ; w h i l e (! S S P I F )

    C onsider the following code and assume that the SP I has been set up to clock the SC K line at the maxim um rate with a CP U clock rate of 1 M H z: S h i f t ( O U T H ) ; SUM / / L o a d t h r e e 7 4 H C 5 9 5 s h i f t r e g i s t e r s S h i f t ( O U T T ) ; SUM / / w i t h s e v e n - s e g m e n t c o d i n g o f S h i f t ( O U T U ) ; SUM / / h u n d r e d s , t e n s , a n d u n i t s d i g i t s P O R T C b i t s. R C 0 = 0 ; 1 / / T o g g l e t h e 7 4 H C 5 9 5 l a t c h c l o c k s P O R T C b i t s. R C 0 = 1 ; 1 a. A bo ve each instruction in the macro, list your estimate of the numb er of microseconds taken to execute that instruction. U se one of the qwik.lst files (handed out with this final exam, in class, or obtained for one of the design projects) to see how instructions equivalent to those above are executed. 1 2 8-11 SUM = 11-

d e f i n e S h i f t ( o u t ) S S P I F = 0 ; S S P B U F = o u t ; w h i l e (! S S P I F )

b. T o the right of each of the five lines above, list your estimate of the num ber o f micro seconds taken to execute that line. c. If the M C U draws 1.000 mA while executing instructions with a clock rate of 1 M H z, then determine the average current draw of the M C U contributed by this five-line sequence when it is executed every ten m illiseconds, assuming that the M C U sleeps when it is done doing useful work each loop tim e. Show all your work clearly. Iawake = 1000 :A x ((3xSUM)+2)/10000 = 0.1((3xSUM)+2):A. 5 :A

  1. C o nsid er the interrup t service ro utine listed belo w fo r d ealing with the m agnetic card read er with the M C U [15% ] co nnections discussed in class. /****** HiPriISR interrupt service routine ***********************************/ void HiPriISR() { INTCON3bits.INT1IF = 0; // Clear interrupt flag if (!PORTBbits.RB3) // Read in active-low data { // so RB3 == 0 represents bit = 1 1! MAG_BYTE |= 0b00100000; // 6th bit position; will be shifted into 5th if (!MAG_STATE) // This is the first 1 read from card { 3! MAG_CNT = 40; // Initialize counter of no more than 40 bytes MAG_STATE = 1; // Set flag to signal presence of data bytes MAG_BYTE = 0b00100000; // Initial value MAG_BITCNT = 5; } } if (MAG_STATE) // Handle data bytes { MAG_BYTE >>= 1; // Shift bits into position if (--MAG_BITCNT == 0) // Act further only for a completed digit { 2! MAG_BITCNT = 5; // Reset for next digit MAG_BYTE |= 0x30; // Convert to ASCII if (MAG_BYTE == '?') // End sentinal? { MAG_STATE = 2; // Done with data TXascii(MAG_BYTE); TXascii('\r'); // Carriage return, line feed TXascii('\n'); } else if (MAG_STATE == 2) // LRC character after end sentinal? { MAG_STATE = 0; } else // A data byte { if (MAG_CNT) // Send no more than 40 characters { TXascii(MAG_BYTE); --MAG_CNT; // Decrement scale of 40 counter MAG_BYTE = 0; // Reset byte for next data byte } } } } }

E C E 4175A Final 4-29-08 P age 2 of 3

a. M ark with 1 ˇ the place in the above code where you would insert a line of pro gram code that will

toggle the least-significant bit of a char variable, P A R IT Y F L A G , if bit 3 of P O R T B is read as a zero. Show that line of program code here: PARITYFLAG ^= 0x01;

b. M ark with 2 ˇ the place in the above code where you would turn on a LE D by setting bit 4 of P O R T D

(leaving the other b its of P O R T D unchanged) if P A R IT Y F L A G equals zero during the interrupt call when the fifth of each group of five bits occurs. Show the code here: if (!PARITYFLAG) { PORTDbits.RD4 = 1; }

c. M ark with 3 ˇ the place in the above code where you would turn off the LED by clearing bit 4 of

P O R T D (leaving the other bits of P O R T D unchanged) when the swipe of a new card is begun, as detected by the first time bit 3 of P O R T B is read as a zero. Show the code here: PORTDbits.RD4 = 0;

  1. A couple of groups in working on their design project had a part that expected bytes to be sent and received over the SP I interface least-significant bit first whereas the M C U ’s SP I module sends and receives bytes [20 % ] mo st-significant bit first. T o handle this, it is convenient to have a R ev er se function that will reverse the bits of a char global variable called IO. For this problem, you are to write the code for the R ev er se function using the following algo rithm that em ploys another char global variable, T E M P. T o carry out this reversal, execute the following procedure seven times using a “for” loop: C opy the least-significant bit of IO into the least-significant bit of T E M P. Shift IO right (one p lace) Shift T E M P left one place W hen this has been carried out, copy T E M P into IO to com plete the function. void Reverse() { for (i=0; i<7; I++) { TEMP |= (IO & 0x01); IO >>= 1; TEMP <<= 1; } TEMP |= (IO & 0x01); IO = TEMP;
  2. Consider the attached page showing a 1/2-multiplexed LCD display that might be used to display a two-

digit, 7-segment-coded decimal number directly from a microcontroller. T he two 100 kS resistors used in

[25 % ] the drive for each of the two C O M i lines provide a way to drive the line to 3 V (the M C U supply voltage), 1.5 V , or 0 V. T he backplane waveform s for C O M 1 and C O M 2 are shown in the middle of the page. B elow these two waveforms are shown the four frontplane waveforms to be applied to SEG 1. T he bottom waveform is intended to turn on bo th pixel 11 and pixel 12, the leftmo st two pixels illustrated. T he other three turn one, or the other, or b oth o f these sam e pixels off. a. T he LC D itself draws essentially no current. W hat is the current draw on the coin cell due to the C O M 1 resistor circuit when its voltage is 1.5 V?

I = 3 V / 0. 2 M S = 1 5 :A

b. C an you drive C O M 1 to +3V with no current draw (due to this drive) at all? E xplain your answer. Y e s ; s e t R C 0 = R C 1 = 1 c. C an you drive C O M 1 to 0V with no current draw at all? E xplain your answer. Y e s ; s e t R C 0 = R C 1 = 0 d. A s the C O M 1 and C O M 2 waveforms are continuously driven with a 50 H z frame frequency, determine the average current draw o n the coin cell due to the generation of these waveforms (ignoring the M C U ’s current draw)? Show your work clearly.

C O M 1 c u r r e n t = ½ x 1 5 :A = 7. 5 :A

C O M 2 c u r r e n t = ½ x 1 5 :A = 7. 5 :A

C O M 1 + C O M 2 c u r r e n t = 1 5 :A

e. W hat is the interval needed between interrupts so that the corresponding interrupt service routine can be used to generate these wavefo rm s? 5 m s f. Showing all work, determine the R M S voltage between the frontplane and the backplane for pixel 12 when both p ixel 11 and pixel 12 are off. R M S = /(( (1.5) 2 + (0) 2 + (1.5) 2 + (0) 2 )/ 4) = 1.06V

EC E 4175B Final 4-30-08 Page 1 of 3 H igh 99 FO U R PR O B LEM S U pper quarter 94 O pen book, open notes M edian 83 Lower quarter 68 Low 51

  1. [ ] I have completed the C IO S co urse evaluation. [5% ] [ ] I have not completed the C IO S co urse evaluation.
  2. T wo models of 16x2 character LCD displays are available with a serial interface. M odel A uses a 19200 baud U AR T interface (similar to that used by the Q wik& Low board for its connection to a P C ). M o del B [15 % ] uses an SP I interface (similar to that used by the Q wik& Lo w board for its co nnection to the starburst LC D ). Fo r this prob lem you are to consider how long it takes to upd ate the LC D with a five-character string co nsisting o f a “curso r-p ositio n co ntro l character” fo llo wed by fo ur d isp layab le characters. Igno re the tim e taken for anything o ther than the five serial transfers. a. H ow long do es it take the M C U on the Q wik& Lo w board to send a five-character string via its 19200 baud U A R T interface to M odel A? E xplain your answer. With a baud rate of 19,200, each character takes 10 bit times, where a bit time = 1000000/19200 = 52.08:s.So, a character takes 10 x 52.08= 520.8:s or about 0.5 ms. Five characters take 5 x 0.5ms = 2.5 ms b. H ow long does it take the M C U on the Q wik& Low bo ard to send a five-character string via its SP I interface to M odel B? E xplain your answer.

W ith a CP U clock rate of 1 M H z, the SPI interface transfers each byte in about 10:s. Five characters take

5 x 10 :s = 50 :s

  1. T he magnetic card reader discussed in class includes an LRC (longtitudinal redundancy check) character for the data bytes read from the card. For this problem, assume that a char variable called L R C has been [15% ] defined. a. W hen a magnetic card is scanned and after the first active-low “1" bit has been detected, sho w the code to initialize L R C to zero and to turn off the LED on the Q wik& Low board. LRC = 0; PORTDbits.RD4 = 0; b. Each time a group of five bits has been shifted into a char variable called M A G _ B Y T E , the L R C variable is to be upd ated by exclusive-O R ing M A G _ B Y T E into it. Sho w the co de to do this. LRC ^= MAG_BYTE; c. After the “end sentinal”as been received and detected, the next five bits that are shifted into M A G _ B Y T E are again to be exclusive-O Red into L R C. W hen this has been done, the lower five bits of L R C should equal zero if no LRC error has been detected. Show the code to make this check of the lower five bits of LR C and to turn on the LE D on the Q wik& Lo w board if there is an LR C error. Y ou may assum e that the upper three bits of L R C are zero. if (LRC) { PORTDbits.RD4 = 1; }
  2. O n the back of the review sheet for the final exam, I included a circuit showing the SP I connection between a P IC microcontroller and an LC D character display that employs the stand ard H itachi display co ntroller [65 % ] interface. Fo r this prob lem you are to consider rep lacing this circuitry with nothing but
    • another P IC microcontroller to serve as a display controller
    • the LC D character display with its H itachi display controller interface T he only connections to these two parts from the Q wik& Low bo ard are
      • an active-low interrupt input, IN T 0
      • SC K input
      • SD I input
      • V D D (po wer)
      • G N D U pon powering up , the P IC display contro ller initializes the LC D character d isplay with an eight-bit interface. T he pins required of the P IC display controller are
      • the five listed above
      • the eight d ata b its co nnecting it to the LC D
      • an output to the LC D ’s RS pin
      • an output to the LC D ’s E pin

E C E 4175B Final 4-30-08 P age 2 of 3

  1. (Continued) T he features required o f the P IC display co ntroller in addition to its SP I interface are - internal clock - interrupt input - sleep mode T he PIC16F690 is a $1.66 20-pin microcontroller that will serve this role. H owever for this problem, you will examine the code necessary assuming that the chip used is the PIC18LF4321. a. W hen an interrupt occurs via a falling edge on the R B 0 /IN T 0 pin, the chip wakes up and gets ready to receive characters from the SP I interface. As each o f up to 18 characters is received , it is put into a line buffer d efined with the following glo bal variable array, L B U F F E R , and an ind ex variable into it, L B IN D E X : unsigned char LB U FFE R [18 ]; // Line buffer unsigned char LB IN D EX ; // A value between 0 and 17 Show the C coding for a function called R e a d I n to B u f fe r that waits for each character to be received, reads it into the next position of L B U F F E R , and then waits up on the recep tio n o f the next b yte. Y o u can assum e that upo n entering R e a d I n to B u f fe r , the value of L B IN D E X is zero. Y ou are to exit from R e a d I n to B u f fe r after you have received and written the “null terminator” value of 0x00 into L B U F F E R. void ReadIntoBuffer() { CHAR = SSPBUF; // Clear buffer PIR1bits.SSPIF = 0; // and flag bit initially DONE = 0; while (!DONE) { while (!PIR1bits.SSPIF); // Wait for byte to be received PIR1bits.SSPIF = 0; // Clear flag LBUFFER[LBINDEX++] = SSPBUF; // and read byte into buffer if (!SSPBUF) { DONE = 1; // Exit from while loop } } } b. N ow write a W r ite T o L C D function that will write each o f the characters in L B U F F E R to the LC D , stopping before writing the null term inator. Assum e that P O R T D is co nnected to the LC D ’s 8-bit data inp ut and that R B 4 and R B 5 are connected to the LC D ’s RS and E pins, resp ectively. T he first character to be sent fro m the buffer is to be treated as a “cursor p osition code” and written to the display with R S = 0. T he subsequent characters are to be treated as displayable A SC II-co ded characters and written to the display with R S= 1. T o write each character after the 8-bit data and the R S p in are in place, drive E high and then low again. After writing to the display, use the D e la y macro (having the same definition as we have used all semester) to insert a delay of 40 micro seconds before writing the next character to the display. void WriteToLCD() { PORTBbits.RB4 = 0; // Clear RS for cursor position code LBINDEX = 0; while (LBUFFER[LBINDEX] // Continue until null character { PORTD = LBUFFER[LBINDEX++]; PORTBbits.RB5 = 1; // Clock display PORTBbits.RB5 = 0; PORTBbits.RB4 = 1; // Set RS for subsequent displayable chars Delay(4); // Wait for 40us } } c. O n the chance that data has not been sent to this P IC + LC D com bination in the correct form at, you are to rewrite R e a d I n to B u f fe r to hand le the following cases: 1 An om itted cursor-po sitioning code can be detected as a value less than 0x80. If the first character received is less than 0x80, then write 0x80 to L B U F F E R [0 ] and write the received character to L B U F F E R [1 ]. T his will result in the display of the received characters beginning in the upper left-hand corner o f the display. 2 If fewer than eighteen characters are received and no null terminator is received , tim e out after one millisecond (measured from the entry into the R e a d I n to B u f fe r function), using T imer0 to monitor this time. B efore returning, insert a null term inator at the end of the received characters so that W r ite T o L C D will function correctly with the characters that have been received.