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

Kristianstad University DT131A: Embedded Systems Programming
Part I. C Programming
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)
#define NUMCARS 50 //This definition can be changed with change in number of cars //In the next few lines, A data structure template is created using typedef
typedef struct car {
char plateno[6]; char model[15]; int year;
float mileage; double price;
};
c = malloc(NUMCARS * sizeof(struct car));
b) Return the price of the 10th car (2p)
struct car tempcar; //temporary variable to copy data at current pointer location tempcar = *(c+10); //locate the 10th element in the structure and copy to tempcar now. double carprice = tempcar.price; //This returns the price for tempcar.
c) The total price of all cars owned by the company (2p)
double sum = 0; //variable to store the sum
for (int j=0; j #include
#define NUMCARS 50 //This definition can be changed with change in number of cars
//In the next few lines, A data structure template is created using typedef
typedef struct car { char plateno[6]; char model[15]; int year;
float mileage;
double price; };
int main() {
struct car *mycar;
int y = sizeof(struct car);
printf(“The struct occupies %d bytes to store the information about each car\n”, y);
}
//The malloc statement in the following line answers question (a)
mycar = malloc(NUMCARS * sizeof(struct car)); if (mycar == NULL)
printf(“Unable to allocate memory to car\n”); else
printf(“Memory allocated to car successfully at %p! \n”, c);
//After this you can call the code for reading data from file
//We assume that readcadata() returns a pointer, data for 50 cars is copied to mycar. mycar = readcardata();
//The next 3 lines return the price of the 10th car. (part b of the question)
struct car tempcar; //temporary variable to copy data at current pointer location
tempcar = *(mycar+10); //locate the 10th element in the structure and copy to tempcar now. double carprice = tempcar.price; //This returns the price for tempcar.
//The next for-loop sums the price of the 50 cars
double sum = 0; //variable to store the sum for (int j=0; j= 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)