Mechatronics 3TA4
DAY CLASS Dr. Mark Lawford
DURATION OF EXAMINATION: 1 Hour 50 min
McMaster University Midterm Examination October 21, 2007
THIS EXAMINATION PAPER INCLUDES 10 PAGES AND 4 QUESTIONS. YOU ARE RE-SPONSIBLE FOR ENSURING THAT YOUR COPY OF THE PAPER IS COMPLETE. BRING ANY DISCREPANCY TO THE ATTENTION OF YOUR INVIGILATOR.
Special Instructions: The use of course notes, textbooks and any other written material is permitted during this exam. You may use the McMaster Standard Calculator. Answer all questions in the provided answer booklets unless directed to answer on this question sheet. Fill in your name and student number on each booklet you use. This paper must be returned with your answers.
1. Embedded Systems Considerations (25 marks)
a) (5 marks) The design of a new embedded system has Non-Recurring Engineering (NRE) costs of $100,000 and a unit cost of $20. How much will have to be added to the cost of each device to recover the NRE assuming the company sells
(i) 100 units
added cost = NRE / # units produced = $100,000 / 100 = $1,000
(ii) 10,000 unitsadded cost = NRE / # units produced = $100,000 / 10,000 = $10 (iii) 500,000 unitsadded cost = NRE / # units produced = $100,000 / 500,000 = $0.20 b) (3 marks) What are 3 examples of NRE costs?
Engineering time (salaries), Costs to access test facilities, Costs of equipment leased/pur-chased to perform design, prototype production costs, etc.
c) (3 mark) Why are many microcontrollers based upon the Harvard Architecture?
• Can simultaneously access data and code memory
• Peripheral can read/write data to memory while CPU is doing things that do not require memory access.
• You can develop your product a system with more memory and then pay only for how much program and data memory you need when you ship the product (e.g. develop on a Mega644P and then ship product with Mega164P.
d) (2 marks) Why do you have to be careful in choosing your AVR microcontroller when you know that you will ship a lot of units of the embedded system and there will be software upgrades to add new features?
• Buying too much additional memory will up the unit cost of the product and reduce profits - this is particularly important for high volume products.
e) (3 marks) Name 3 different peripherals commonly found built into a microcontroller and for each peripheral explain in a sentence or two what it does.
WDT : Watch Dog Timer
UART : Universal Asynchronous Receiver Transmitter - used to send and receive serial data
A/D : Analog to Digital converter - used to sample continuous data (e.g. analog sensor readings
f) (2 mark) Why do microcontrollers typically integrate these peripherals on chip when a general purpose Microprocessor does not?
Microcontrollers typically have to interface with sensors, actuators and other components of an embedded system so peripherials such as USARTs, PWM and A/D converts are built into a microcontroller to provide a single chip solution for an embedded system. This can speed up the system. In most applications a microprocessor will not need these peripherials.
g) (7 marks) Consider the following function that has been programmed by your Computer Science friend to compute the Fibonacci function for an embedded system you are building.
i n t f i b (i n t num)
{ switch(num) {
case 0 :
return( 0 ) ; break; case 1 :
return( 1 ) ; break; d ef au lt:
return( f i b (num − 1 ) + f i b (num − 2 ) ) ; break;
} }
Everything works fine values between 0 to 10, but for larger values, the AVR microcontroller you are using mysteriously crashes. Why do you think this is happening? If you only need to compute the Fibonacci function for values up to 12, what are some ways you can fix the problem?
Why do you think this is happening?
See Barnett Chapter 1 section1.8.3 Recursion andAVR Architecture slides - “Don’t Blow Your Stack!” for detailed discussion. Basic idea is that stack space is limited and recursive function calls eat up the stack space possibly causing a stack overflow.
If you only need to compute the Fibonacci function for values up to 12, what are some ways you can fix the problem?
• Increase the stack space in the compiler options (OK)
• Precompute the 12 values and just use a lookup table (probably best solution form processor load point of view)
2. Power, Interrupts & Interfacing (25 marks)
You have implemented a microcontroller design that uses 3.3V for the VCC of an ATmega16L as shown in Figure 1. The 3.3V is produced by a MAX6037 voltage regulator that takes a 5VDC battery input and produces the 3.3V reference voltage for the microcontroller. It is determined that the battery is getting dangerously low when its output falls from 5V to 4V. a) (10 marks) Use the microcontroller’s analog comparator and additional components to design a low power indicator circuit. You can assume the ATMega16L is an ATmega16 that can run off lower VCC. Draw your circuit on Figure 1 of this exam paper and give values of all components used on that figure.
b) (5 marks) How would you initialize the analog comparator for question 2a?
/∗e n a b l e a n a l o g comp , e n a b l e c om p ar at o r i n t e r r u p t , f a l l i n g e d g e∗/ ACSR=0x0A ;
c) (5 marks) Describe two different methods to get data more than 8 analog inputs into an ATmega324P.
(i) Use an analog mux or analog switches and some the mega324P’s digital I/O pins as selector line.
(ii) Use one or more external I2C or SPI A/D converters to get additional A/D lines that communicate their data using a serial protocol that requires only 2 or 3 digital pins.
3. Analog to Digital and Back Again (25 marks)
Suppose you are using an AVR mega324P with a 3.3V power supply. You want to sample some analog inputs, compute a control value and then generate an analog output.
a) (4 marks) For full scale 0-3.3V, what is Vresolution, the minimum change in input voltage that you can always detect, when
(i) 8-bits are used for the digital value (ii) 10-bits are used for the digital value In general for n-bit ADC
Vresolution =
Vf ullscale 2n−1 So for 8-bit
Vresolution = 3.3V
28−1 = 12.9mV and for 10-bit
Vresolution= 3.3V
210−1 = 3.23mV
b) (4 marks) For 0-3.3V full scale compute the resulting digital value for a 2.3V analog input when there are:
(i) 8-bits are used for the digital value In general for n-bit ADC
Vin Vf ullscale
= x
(2n−1) so
x= (2n−1) Vin Vf ullscale
Hence for 8-bit A/D, we haven = 8, Vin = 2.3 and Vf ullscale= 3.3 so
x= (28−1)2.3
3.3 = 177.7 so assuming rounding 178 and assuming flooring 177. (ii) 10-bits are used for the digital value
for 10-bit A/D, we have n= 8, Vin= 2.3 and Vf ullscale= 3.3 so
x= (210−1)2.3
3.3 = 713.0
No need to consider rounding or flooring. In this cases 713 is the answer.
c) (2 marks) How could you use a single pin of the AVR mega324P to generate an analog output?
PWM into a low pass filter if needed.
d) (10 marks) Write the code you would execute to convert an 8-bit digital value into mV (millivolts) to so that you could send the number to an LCD. If your computation produced 1234 mV for a digital input, how many of those digits were accurate (hint: recall your answer to question 3(a)i).
1 unsigned i n t p r e s e n t v o l t a g e ; 2
3 /∗ ADC i n t e r r u p t s e r v i c e r o u t i n e ∗/ 4 i n t e r r u p t [ ADC INT ] void a d c i s r (void) 5 {
6 unsigned i n t ADC DATA; 7
8 ADC DATA = ADCW; /∗g e t d a t a from ADC r e s u l t r e g i s t e r ∗/ 9 p r e s e n t v o l t a g e = ( (long) 3 3 0 0 ∗ (long)ADC DATA) / (long) 2 5 5 ; 10 }
NOTES:
i) You could use
unsigned char ADC DATA;
ADC DATA = ADCL; /∗g e t d a t a from ADC r e s u l t r e g i s t e r ∗/
for lines 6-8 above to save on SRAM usage since we are only doing 8-bit conversion, but if you went to more bits you’d have to be careful to change it to the original. ii) Naming something current voltage seems like a bad idea, hence present voltage
is used here.
For a digital value of 1234mV only the first two digits could be assumed to be accurate since the resolution for 8-bit A/D is only 12.9mV.
e) (5 marks) Suppose the microcontroller is running on a 8MHz clock.
(i) What value should you assign to the ADPS2-0 preselector bits to get 10-bit A/D conversion?
8 200
M Hz kHz =
8×106
200×103 = 40
so the smallest clock divisor ≥40 is 64. This corresponds to ADPS2-0 set to 110. (ii) What is the actual frequency of the clock supplied to the A/D converter as a result?
8 64
M Hz
= 8×10 6
64 = 125kHz
(iii) How many samples/second would you expect to achieve in this case?
A 200kHz clock supplied to the A/D results in a sampling rate of 15k Samples/Second (assuming 10 bit accuracy) so assuming sampling rateis proportional to the supplied clock 125 200 kHz kHz = x 15 kSP S kSP S so
x= 125
(200)(15) = 9.375kSP S
datasheet). You can use this to compute the number of SPS once you know your ADC clock frequency rather than just scaling as I did above.
4. Custom Single Purpose Processor Design (25 marks)
Consider the following blackbox (left) and desired functionality (right) for a simple custom single purpose processor to compute the Least Common Multiple (LCM)of x i and y i, returned as the outputd o (eg. for x i=6 and y i=9, the result is d o=18):
x_i y_i
go_i
d_o LCM
(Least Common Multiple)
1 i n t x , y , xs , y s ; 2 while ( 1 ) {
3 while ( ! g o i ) ;
4 x = x i ;
5 y = y i ;
6 x s = x ; 7 y s = y ;
8 while ( x s != y s ) { 9 i f ( x s < y s )
10 x s += x ;
11 e l s e
12 y s += y ;
13 }
14 d o = x s ;
15 }
a) (11 marks) Draw the state diagram for the processor, optimizing where possible for in-structions that can be executed independently.
b) (10 marks) Separate the state diagram into a controller and datapath.
6 states so we need 3 bits to encode.
d) (2 marks) Give two constructive comments on how to improve the course (other than replacing the instructor).