CS计算机代考程序代写 Kristianstad University DT131A: Embedded Systems Programming

Kristianstad University DT131A: Embedded Systems Programming
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)

/*— Use the example code on the slides in Lecture 21-22 for actual implementation */
Data Register: used to transfer a single unit of data between the computer and the peripheral. E.g. the register pair called ADCH f and ADCL holds the digital equivalent of the analog sample converted by the ADC on the ATMega328 microcontroller. When conversion is complete, we will read the content of this register pair into to a variable
(e.g. int x;
x = ADSCH; //ignore the least significant bits) .
Control Register: used to initialize the operation mode of the I/O. E.g. the ADCSC register is used to set the input channel number, conversion rate, reference voltage, enable interrupt, etc., as given in the data sheet.
Status Register: is inspected by the programmer to know what is going on in the I/O unit: E.g. In the ADC, this is combined into a single register called status and control register (ADSC). One of the bits of this register is used for polling the ADC using a while loop, to check if conversion is complete.
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)
/* — Use the example code on the slides in Lecture 21-22 for actual implementation */
Instead of polling the conversion status bit with a while loop, the interrupt enable bit (bit3 or ADIE) of Control Register ADSCRA is set. When conversion is complete, the ADC interrupt service routine is executed automatically. We can place the code for processing the converted data in the ISR.
char *adcoutput; //char pointer used to store ADC output
int counter = 0; //used to track index of current storage location ISR(ADC_vect) {
*(adcoutput+counter) = ADCH;
Counter++; }

Q8. 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. We need to use the external interrupt on INT0 to handle the PANIC key press situation.
Int panicpressed =0; ISR(INT0_vect) {
If (panicpressed == 0) { Panicpressed = 1;
/* Reset the overflow counter */ /* Add code for blinking RED */
}
else {
panicpressed = 0;
/* add code for blinking Blue and Green depending on the number of overflows */ }
}
To create a delay of 3 seconds, we use timer0. We use the corresponding ISR for timer0 overflow. We then count the number of overflows that correspond to the desired delay (3sec). You need a global counter for this:
The timer counter0 is 8-bits: it overflows every 256 cycles (TCNT0 resets every 256 cycles).
Since the processor has 10MHZ the overflow duration corresponds to 256/10000000 seconds
However, with a pre-scaler value of 1024, the over flow duration can be
256/(10000000/1024) seconds = 0.0262sec 26.2msec.
For a delay of 1 seconds we need 1/0.0262 38.2 over flows. We can round this to a good figure (e.g. 40) by changing the TCNT0 register’s starting value as follows:
If 40 overflow = 1 sec, one overflow takes 1/40 = 0.025sec or 25msec. One way to achieve this, (to have an overflow every 25msec) is as follows:

We can achieve this if we can make TCNT0 to rest after only 244 clock cycles instead of the default 256.
This can be achieved by setting TCNT0 to 12 when an overflow occurs. Int gcount ;
The main method shall have the following structure:
Void main(){
DDRD |=(1 << PD7) |(1 << PD6) |(1 <= 12){ overflow_count= 0;
// handlerfor Timer1 overflowinterrupt
// incrementoverflowcount
// when about 3s has passed
// start new count

if(PORTD & 0x01) PORTD &= 0xfe; else
PORTD |= 0x01;
}
TCNT0 = 12; }
//if light is ON //set light OFF
// if it is OFF set it ON

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)