ECE2049 -- PRACTICE EXAM #2 Clocks, Timers, and Digital I/O Study HW3, Class Notes, Davies Ch 2.6, 5.8, 8, 9.2-3, 9.7, MSP43F5529 User's Guide Ch 5, 17, 28 Work all problems with your note sheet first THEN look at solutions! 1. Answer the questions below completely. (25 pts) a. What are the default frequencies for ACLK, MCLK and SMCLK on the MSP430F5529 after power up? What is the purpose of these various clock signals (i.e. What are they used for)? b. How does the CPU know where to go on an interrupt? How does it find the correct ISR? c. Explain the operation of a Timer in up mode (ie. What happens to the timer count? When is an interrupt triggered?). d. True or False: The operation of peripherals like the Timers or ADC12 causes a big drain on CPU speed. Why or why not? e. True or False: Interrupt Service Routines (ISRs) should be kept short because the CPU will stop executing them after 255 instructions. ECE2049-E16 Practice Exam 2 Page 1 of 6
2. Answer the questions using the Timer A2 configuration code below. Assume that the MSP430F5529's system clocks are running in their default configuration. (25 pts) void runtimera2(void) TA2CTL = TASSEL_1 + MC_1 + ID_0; TA2CCR0 = 16383; TA2CCTL0 = CCIE; a) Explain exactly how TimerA2 is configured to run. Specifically what is the clock source, the clock divider, the resulting clock frequency, the counting mode, and the value of the maximum count? Is the counter's interrupt enabled? Explain the operation of the counter when the program is executing. b) Now disregard any of the settings above. Instead, assume TimerA2's interrupts are enabled and the clock source is set to ACLK and TA2CCR0 = 24000. What would be the time between interrupts if Continuous Mode were selected? What if Up-down Mode were selected? c) Set TimerA2's control registers to use SMCLK to count intervals 25 milliseconds. d) How long until a clock based on the timer from part (c) would be off by 0.025 sec? Is it fast or slow? ECE2049-E16 Practice Exam 2 Page 2 of 6
Note: This problem is longer than a standard exam problem. However, it is good practice and demonstrates all of the concepts you need for this exam! Give it a try! 4. You want to use an MSP430 to measure a person s reaction time. A simple way to do this is to have a person watch for an LED to turn on, and make them press a button as soon as they see it. You can compute the reaction time by measuring the time when the LED was turned on and when the button was pressed, and comparing the difference. In this problem, you will design a system to do this. (25 pts) a. Assume you have one LED and one button and want to connect them to your MSP430 board on P1.0 and P1.1, respectively (that is, connect the LED to P1.0 and the button to P1.1). Draw a schematic showing how you would connect these to your MSP430. You can connect them in any way you want, so long as it works. (Assume that you also have access to some resistors, should you choose to use them.) b. Based on your configuration, what logic level results when the button is pressed? What logic level turns on the LED? ECE2049-E16 Practice Exam 2 Page 3 of 6
c. Complete the following C function to configure the Digital I/O registers to use the button and LED you configured in part (a). void setupio(void) d. Complete the function buttonpressed() below such that it returns a 1 when your button from parts (a) and (b) is pressed and 0 when it is unpressed. // Returns 1 when button on P1.1 is pressed, 0 when unpressed char buttonpressed(void) (continued on the next page) ECE2049-E16 Practice Exam 2 Page 4 of 6
Now suppose you have written a main() function like the following to measure the reaction time. The example configures a timer that increments the variable timer at 10ms intervals. Example code for the rest of the program can be found on the next page. e. Complete the main() function below to use the timer value to measure the user s reaction time. To do this, you will need to add a few lines of code and fill in the computation for diff. You may declare additional variables as necessary. Hint: You do not need a lot of code for this. Keep it simple! // Global time count (increments every 10ms) volatile unsigned long timer = 0; #pragma vector=timer2_a0_vector interrupt void TimerA2_ISR(void) // Timer A2 ISR timer++; // Increment global time count void main(void) WDTCTL = WDTPW WDTHOLD; // Stop watchdog timer // Initialize peripherals setupio(); starttimera2(); enable_interrupt(); // Global interrupt enable while (1) // Forever loop randdelay(); // Wait for a random amount of time setled(1); // Turn on the LED while(readbutton()!= 1) // Do nothing until a button is pressed // Compute reaction time unsigned long diff = displaytimems(diff); // Display the reaction time setled(0); // Turn off LED // end while (1) ECE2049-E16 Practice Exam 2 Page 5 of 6
// Function Prototypes void swdelay(char numloops); void starttimera2(void); void randomdelay(void); void displaytimems(unsigned long curr_time); char buttonpressed(void); void setled(char value); void starttimera2(void) // Use ACLK, 16 bit, Up mode, Divide by 1 TA2CTL = TASSEL_1 MC_1 ID_0; TA2CCR0 = 327; // 327 + 1 = 328 ACLK ticks = ~1/100 seconds TA2CCTL0 = CCIE; // Enable TA2CCR0 interrupts char buttonpressed(void); void setled(char value) // void randdelay(void) // void displaytimems(unsigned long curr_time) //... // Other demo functions omitted... ECE2049-E16 Practice Exam 2 Page 6 of 6