* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Interrupts
Survey
Document related concepts
Embedded system wikipedia , lookup
Electrical ballast wikipedia , lookup
Current source wikipedia , lookup
Electrical substation wikipedia , lookup
Alternating current wikipedia , lookup
Resistive opto-isolator wikipedia , lookup
Integrating ADC wikipedia , lookup
Voltage regulator wikipedia , lookup
Stray voltage wikipedia , lookup
Voltage optimisation wikipedia , lookup
Power MOSFET wikipedia , lookup
Mains electricity wikipedia , lookup
Opto-isolator wikipedia , lookup
Schmitt trigger wikipedia , lookup
Buck converter wikipedia , lookup
Transcript
Embedded Systems (Microcontrollers) Lab LAB #5: INTERRUPTS <Student 1 Name> <Student 2 Name> <Student 1 ID> <Student 2 ID> Start Date: September 24, 2013 Due date: September 7, 2013 I. Learning Objectives Gain experience elaborating applications using interrupts and exceptions in C language and the STM32F3 microcontroller. II. Introduction You won’t be able to find a microcontroller without interrupt capability. It is an essential attribute of any modern microcontroller or processor. Interrupts may seem confusing and tricky at first glance, but soon you will find out that normal MCU operation is impossible without interrupts. Interrupts can be easily compared to real life events. Look around – all your activities are full of them. For instance you are reading this text and find it interesting so you are all in it. But suddenly you cell phone rings. What do you do? You remember last line you’ve read and answer the phone. Once phone conversation is over you get back to your reading as nothing happened. Well, this is only one example to give you a clue of what interrupts are. An interrupt is a signal (generally called an "interrupt request") to the CPU to immediately begin executing different code; code that is written to respond to the cause of the interrupt. "Immediately" can be as soon as the end of the current instruction, in the best case. The time between the generation of the interrupt request and the entry into the ISR is called the "interrupt latency," and faster (lower latency) is always better. The CPU will remember the location of the next instruction it was going to execute by storing that instruction address in a register or memory location, and will then jump directly to the code designated by the programmer for that particular interrupt. In addition to saving the address of the next instruction, it will often also save the CPU status register and disable further interrupts. It will also, in most cases, automatically clear the interrupt request that triggered the ISR entry, so that a single interrupt request does not result in the ISR being entered multiple times. Occasionally, depending on interrupt type and microcontroller design, it will require user code in the ISR to explicitly clear the interrupt request. It is very important to check the datasheet for a given interrupt source to see if the ISR code must clear the interrupt request, or the uC will just keep re-interrupting on the one interrupt request event forever. A microcontroller CPU will be designed to respond to a number of different interrupt sources (perhaps 10 to 100 sources, typically), and each source can have specific user-written code which executes when that interrupt triggers. The code that executes for an interrupt is called the "interrupt service routine" or ISR. Interrupts are always off or disabled when coming out of RESET. In fact, they are doubly disabled. Each individual interrupt source is disabled, and also the global CPU interrupt flag is disabled. For an interrupt to happen (to have its ISR run), both the individual interrupt must be enabled, and the CPU global interrupts must LAB #5 – INTERRUPTS 1/6 Embedded Systems (Microcontrollers) Lab be enabled. And finally, of course, the interrupt condition itself must occur (e.g. a pin being driven low, or a timer rolling over, or whatever the particular interrupt is triggered by.) It is up to the user program to select which interrupts to enable, and at what points in the program execution to enable them (and once enabled, an interrupt can be disabled again, then re-enabled, as often as needed). The user program will also have an ISR (very similar to a function) for each interrupt that will be enabled, and each of these ISRs will be mapped to its corresponding interrupt source. It is crucial that any interrupt that is enabled, ever, has a valid ISR mapped to it. III. PRELAB Be sure to have completed these prelab questions before starting the lab. They will help you to perform the tasks in this lab and hopefully give you more time to experiment. 1. In computing there are traditionally two ways for a microprocessor to listen to other devices and communicate. These two methods are commonly called 'polling' and 'interrupts.' A large amount of information about these two methods exists. Please describe what each of them is and a few examples where you would choose one over the other. 2. What is a time-critical system? 3. What is the function of the PRIGROUP and VECTKEYSTAT/VECTKEY fields of SCB_AIRCR? What will be the result if PRIGROUP is equal to 4 and SCB->SHP[11] is equal to 0x80? 4. What is the function of bits 2, 1 and 0 of STK_CTRL? 5. What is the function of registers SYSCFG_EXTICRx? 6. What is the function of registers EXTI_SWIERx? 7. What is the difference between an interrupt and an event? 8. Explain the function of bits 15, [6:4], and 0 of COMP2_CSR. 9. How do we enable EXTI1 interrupt in the NVIC registers? 10. The STM32 microcontroller uses 'interrupt vectors' to run code when an interrupt is triggered. What is an interrupt vector? List the memory locations for the following vectors in the STM32F3 microcontroller: EXTI0 and DMA1_Channel1. 11. In the STM32 microcontroller like with many others, there are several different ways of triggering interrupts. Below is a sample signal being input onto one of the external interrupt pins. Indicate where the interrupt would trigger on this waveform if the interrupt was set up as: a) rising edge, b) falling Edge, c) level high, and d) level low. IV. LAB For all the exercises of this lab, consider the circuit shown in the figure below. LAB #5 – INTERRUPTS 2/6 Embedded Systems (Microcontrollers) Lab Part I: SYSTICK Timer Interrupt a) In the main function, configure the SYSTICK timer for generating an interrupt every millisecond and PA15 as output (LD6). b) Within the SYSTICK interrupt service routine (handler), just increment a global variable named ticks. c) Based on this variable, elaborate a function called delaybymsSTK that receives as argument the number of milliseconds to wait in a variable named j. Don’t stop or modify the variable ticks within this function. Just add j to ticks and store it in another variable called k. Then wait until k is equal to j. d) Finally, in the main function, create an infinite loop that toggles the status of LD6 and wait 500ms. Part II: EXTI0 Interrupt a) Connect two dip switches to PB1 and PB2. Display continuously on the LCD the number of seconds that have passed since the program was reset. Use a one-second delay using the function called delaybymsSTK. On the other hand, a rising edge on PB1 should add one to a variable called count. Use an interrupt for doing this. The value of count should constantly be shown also on the LCD. Download this program to the MCU and test it. b) Variation to the program above: now you should use two external interrupts. One interrupt (falling edge on PB1) to add one to the variable count, and the other interrupt on a falling edge of PB2 to subtract one to the variable count. Download this program to the MCU and test it. Part III: Digital Capacitance Meter As you know, capacitors are one of the most common passive electrical components used in all kinds of electronic circuits. Now you will build a digital capacitance meter using a STM32 microcontroller. Your device will be able to measure capacitance values from 1 nF to 100 μF. The technique is based on measuring the time elapsed when a capacitor is charged to a known voltage through a series resistor. Theory This capacitance meter is based on the principle of charging a capacitor through a series resistor. In a series RC circuit, as shown in the figure below, the voltage across the capacitor increases exponentially as it charges. Let’s assume that initially the capacitor was fully discharged. When Vin is applied across the RC circuit, the capacitor starts charging and consequently, the voltage (Vc) across it increases from 0 towards Vin in an exponential way, as shown in the right side of the figure. The equation provided in the figure describes how the voltage across the capacitor changes with time. If we know the time that is required for the capacitor to charge up to a known voltage then we can solve this equation for C, knowing the value of R. LAB #5 – INTERRUPTS 3/6 Embedded Systems (Microcontrollers) Lab The idea of measuring the time elapsed when the capacitor is charged from 0 to a known voltage can be implemented with any microcontroller. Here, we are using the STM32F3 microcontroller, which has 7 built-in analog comparators. We are going to use the Analog Comparator 2 and the SYSTICK timer to determine the time required by the capacitor to charge from 0V to 0.5Vin. The positive and negative inputs of the Analog Comparator 2 are externally accessible through PA3 and PA2 pins of the MCU, respectively. As shown in the figure below, two 10K resistors create a voltage divider that sets the positive input (PA3) of the comparator to half of the voltage applied to PA4 pin. The negative input (PA2) of the comparator goes to the positive end of the capacitor. When a voltage is applied to the PA4 pin, the capacitor (Cx), initially fully discharged, is charged through a 10K resistor. When the PA4 pin is just set high (say around 3V), the output of the comparator is high as the positive input of the comparator is at higher voltage (about 1.5V) than the negative input, which is close to 0V as the capacitor is fully discharged. Now the capacitor starts charging through the series resistor (10K) and when the voltage across it exceeds half of the voltage at PA4 pin, the comparator output is flipped to low. The comparator interrupt flag (EXTI_PR1 bit 22) is set whenever there is a change in the output value of the comparator. The SYSTICK timer is used to compute the time elapsed between when PA4 is set high and the comparator output goes low. This is the time required by the capacitor to charge from 0V to half of the supply voltage. By knowing the value of the charging resistor (in this case it is 10K) and the charging time (using the SYSTICK Timer), we can now solve the capacitor equation, mentioned above, to compute C. Here’s the mathematics involved in the process. The complete circuit setup for this project is given below. The pins PA[4:2] go to the voltage divider and capacitor charging circuit described earlier. LAB #5 – INTERRUPTS 4/6 Embedded Systems (Microcontrollers) Lab After declaring the variables you will use like: temp, microseconds, elapsedTime, capacitance, flag and an array for storing the data to be sent to the LCD, and after configuring PA2 and PA3 as analog inputs, PA4 as output + PP, the SYSTICK timer for a periodic 1-microsecond interrupt, and COMP2 for using PA2 as the negative input, PA3 as the positive input, and for generating an interrupt each time PA3 voltage is higher than PA2 voltage, the sequence of operations in the main function is: 1. PA4 goes low for one second to discharge C. 2. PA4 goes high to start charging C. 3. A global Boolean variable named flag is cleared. 4. The current microseconds value is stored in a temporal variable. 5. The COMP2 interrupt is enabled. LAB #5 – INTERRUPTS 5/6 Embedded Systems (Microcontrollers) Lab 6. Using the COMP2 interrupt handler the comparator 2 state change is detected indicating the voltage from the voltage divider and the capacitor voltage are equal. 7. The COMP2 interrupt is disabled. 8. The elapsedTime is calculated subtracting the current microseconds value from the temporal time value from step 3. 9. The capacitance is calculated using the equations shown above. 10. The sprinf function is used to format the capacitance value in scientific notation. 11. The capacitance value is send to the LCD display. 12. A one-second delay is introduced here. 13. The cycle is repeated over and over again. Additionally the SYSTICK timer must be configured to generate a periodic interrupt every microsecond. A global variable named microseconds (or any other convenient name) is just incremented within the SYSTICK timer interrupt handler. Within the COMP2 interrupt handler a Boolean variable named flag is set to 1, indicating that the condition expressed in step 5 has been occurred. Additionally the COMP2 pending interrupt flag is cleared. The one-second delay is implemented creating a function using also the SYSTICK timer microsecond interrupt. Important note: the procedure indicated here is just a guide. You can use any approach you want to implement the digital capacitance meter. Nonetheless, you must use the SYSTICK timer and the COMP2 interrupts. Test your device with the following capacitors: 10nF, 100nF, 1uF, 10uF. What results do you get? What are the limits of your system? What can be done to extend the range? V. Suggestions and Feedback Time for completing prelab: ________________________________________________ Time for completing lab: ________________________________________________ Please explain difficulties you had and suggestions for improving this laboratory. Be specific, e.g. refer to paragraphs or figures in the write-up. Explain what experiments should be added, modified (how?), or dropped. Why? Please be critical but constructive. Note: This section is not optional. Final note: Your report must include a detailed explanation of the solutions you have found to the assignments, in particular the programs. All the questions must be answered. Important note about academic misconduct: All work is to be the original work of the individual, or in the case of lab experiments, the two lab group members. Depending on the severity, individuals performing plagiarism, cheating, and/or any other violation of the Student Code of Conduct may result in a zero for the assignment, may receive a grade of zero for the class, and may be referred to the ITESM CEM Ethics Committee for disciplinary action. LAB #5 – INTERRUPTS 6/6