Kristianstad University DT131A: Embedded Systems Programming
Examiner: Dawit Mengistu Note:
• Standard calculators are allowed. NO other help materials are allowed;
• You can write your answers in English or/and Swedish
Date: 2018.04.25
• Write your answers clearly. No points will be given for unreadable answers
• The answer to each question should start on a new page.
• Pay special attention to underlined and bold face text to understand the key points and/or focus of the questions properly.
• Use the information in the attached pages as references or data sheet wherever applicable.
• Assume the ATmega328P or similar microcontroller wherever applicable.
• This examination has two parts.
o Part1 is about C programming.
o Part 2 is about Embedded Systems
• A minimum of 25 points total and, at least 10 points in each part is required to pass this examination.
Q1. General Questions
(7 points)
Part I. C Programming
a) In your opinion, what are the main differences between the Java and C programming languages? What are the advantages of the C language over Java, for Embedded Systems developers? (2p)
b) In C programming, one of the uses of directives is conditional compilation. Explain what is meant by conditional compilation;
Using an example C code of your own, show how conditional compilation is applied in C. (3p)
c) What is the purpose of a function prototype? Using an example code, show how a function prototype is used in a C program. (2p)
Q2. Bitwise Operations (4 points)
Write the result of the following bitwise operations in hexadecimal. Assume the variables (a, b, c, d) are all of integer type.
The variables have the following values: a= 18, b=13, c= 2, d= 6.
Hint: convert the data to hexadecimal first.
It is necessary to show the steps of your computation details (not only the final results).
A. B. C.
(a << c) | ( a >> 2) | (b & ~b) (a + (b << 3)) << (d &c)
(~(~b | b) & (a << c))
Q3. Data Structures (7 points)
Write a C program that stores the following information about the vehicles belonging to a car dealer company in a data structure (struct).
- Plate No. (6 characters)
- Model (up to 15 characters)
- Y ear (integer value)
- Mileage (Kilometers driven, rounded to 1 decimal place) - Price (Value of the car, rounded to 2 decimal places)
You can expect that the company has a maximum of 50 cars. Your program should:
a) Allocate the necessary storage memory for the data (3p)
b) Return the price of the 10th car (2p)
c) The total price of all cars owned by the company (2p)
You may assume that the information about the cars is read for you from a disk file using a function called readcardata( ) and stored in the memory you allocated. This function is already written for you (so, you don’t need to worry about it).
Q4. Debugging C Programs (7 points)
a) Consider the 8-line C code given below. What should the statement in line No. 4 be so that the displayed output is x = 26 (when the printf statement in line 7 is executed, x=26 is printed on the screen.)
Line No.
1 2 3
4
Statement
#define A 5 #define B 3 #defineCA+B
??
5
6
7 8}
int x = C * D; void main ( ) {
printf(“x = %d\n”, x);
b) Study the following program code carefully.
(3p)
int a, b;
int *x;
int i= 0;
a = 5;
b= 10;
x = &a;
while ( a > 0){
*(x + i) = 20 – i; a–;
b++;
} i++;
printf(“a = %d, \tb = %d”, a, b);
i) How many times will the while-loop be executed?
ii) Write the exact text you expect to be printed on the screen by the printf statement in the above code. (4p)
Part II. Embedded Systems
(Refer to the attached data sheets wherever needed)
Q5. General Concepts (5 points)
a) Explain the difference between Harvard and Von Neumann memory architectures.
Compare the fetch-execute cycle in both architectures to show their differences. (2p)
b) Explain the main advantages of dynamic memory (DRAM) compared to the static memory
(SRAM). (1.5p)
c) Using examples, explain the differences between one-address and two-address machines with regards to CPU architectures. (1.5p)
Q6. I/O Module of Microcontrollers (6 points)
A typical I/O module of a computer can be described using the following diagram:
a) As you can see in the diagram, an I/O module has three types of registers: data, status, control.
Demonstrate how these registers are used in the ADC of the Atmega328 microcontroller. What are the functions of each register type? You may use a C code or a pseudocode to explain your answer. If you use a C code, you should write proper comments to make your point clear and understandable. (3p)
b) Explain the concept of an interrupt and how it can be used in I/O.
Use the ADC case above as an example. It is not necessary to write a complete application code. However, your code should show how interrupts can be implemented on the microcontroller. (3p)
Q7. Assembly Programming (6 points)
You are required to write an assembly program code for a weather station device. This device is to be based on the ATMega328 microcontroller. Your code displays the total amount of rainfall in a month, say March 2018. The total rainfall is the sum of the daily rainfall readings. Assume that:
– The daily rainfall data is already stored in consecutive memory as integer values.
– The starting memory address of the rainfall data is 0x1000 (e.g. March 1’s rainfall).
– The microcontroller has an 8-bit processor with 16MHZ speed
You should write an assembly program code that:
– –
a) b)
c)
Note:
Q8.
Calculates the sum of the monthly rainfall data which is already stored in memory. Shows the calculated sum as binary on LEDs connected to one of the digital I/O ports.
Write an assembler code with proper comments to do the required task. (3p)
How much time does it take to complete the execution of the above program? Justify your answer by showing the necessary calculations. (2p)
How much flash memory is required to store your compiled code? Justify your answer by showing the necessary calculations. (1p)
• Use a flow chart or relevant diagram in order to make your solution understandable. • Showallnecessarycalculationsyouhavemade.
Microcontroller I/O Programming in C (8 points)
A factory wants to use a microcontroller to manufacture an alarm system used at Seniors Care Home. The system uses LED lamp lighting sequences and a PANIC button for signaling purposes as follows:
– When it is switched on (POWER ON), the GREEN and BLUE lamps blink for 3 seconds each one after the other.
– When the patient presses the PANIC button, only the RED lamp blinks for 2 seconds, then goes off for 1 seconds, and this sequence is repeated until the nurse arrives and takes care of the emergency situation.
Write a C program for this application. Use the following assumptions:
– You shall use the ATMega328P processor and assume it has a speed of 10MHZ – You shall use Timer0 to control the lighting interval.
– You shall handle the PANIC situation appropriately.
Use a flow chart or relevant diagram in order to make your solution understandable.
Write proper comments to describe your code.
Note: The number of points you earn depends on clarity and accuracy of your solution.
1. Interrupt Address table
2. Timer Initialization Information
• TCNT0: Timer/Counter an 8-bit register, stores the current value of Timer0.
• TCCR0: Timer/Counter Control Registers, configures the operations of Timer0.
• OCRxA, OCRxB: Output Compare Registers store the preset values for output compare.
• TIMSK enables timer interrupts
• TIFR monitors status of timer interrupts. •
TCR0 (Timer 0)
CS2:0 (clock select prescaler)
000 None
001 clkI/0
010 clkI/0/8
011 clkI/0/64
100 clkI/0/8clkI/0/256
101 clkI/0/8clkI/0/1024
110 External clock on T0 (falling edge trigger) 111 External clock on T1 (rising edge trigger)
Waveform generation mode (WGM)
00 Normal
10 PWM, Phase Correct 01 CTC
11 Fast PWM
• TIMSK Timer 0 uses the Timer/Counter 0 Output CompareMatch Interrupt Enable (OCIE0) bit and the Timer/Counter 0 Overflow Interrupt Enable (TOIE0) bit.
OCIE0 (=1): Compare Match interrupt is enabled if the I-bit in the Status Register is set (=1) TOIE0 (=1): Overflow interrupt is enabled if the I-bit in the Status Register is set (=1).
• TIFR Timer 0 uses
– the OCF0 bit position, (sets for an output compare match).
– the TOV0 bit position, (sets when Timer/Counter 0 Overflows).
3. ADC Initialization Information
Input Channel Selections
0000 ADC0 0001 ADC1 0010 ADC2 0011 ADC3 0100 ADC4 0101 ADC5 0110 ADC6 0111 ADC7
ADC Prescaler Selections
ADPS2 ADPS1 ADPS0 Division Factor
0002 0012 0104 0118
1 0 0 16 1 0 1 32 1 1 0 64 1 1 1 128
4. ATMega168/328 Instruction Set
ATMega168/328 Instruction Set (cont’d)