This shows you the differences between two versions of the page.
pm:lab:lab0xc0-2 [2020/03/29 12:56] dumitru.tranca |
pm:lab:lab0xc0-2 [2020/03/29 13:04] (current) dumitru.tranca old revision restored (2020/03/27 17:14) |
||
---|---|---|---|
Line 11: | Line 11: | ||
/*<hidden>*/ | /*<hidden>*/ | ||
- | ====== Laboratorul 0xC2: PWM, Counters and External Interrupts ====== | + | ====== Laboratorul 0xC1: Interrupts and Timers. Build a 7-segment digital counter ====== |
- | This lab covers the topics of PWM, H-bridge drivers and reading encoders using external interrupts. | + | |
- | For more in-depth knowledge about the capabilities and the timing peripherals and how to program them you can find more information in the ATmega324 datasheet ({{:pm:doc8272.pdf|Datasheet ATmega324}}). | + | |
- | ===== PWM (Pulse Width Modulation) ===== | + | This lab covers the basics for working with **External Interrupts**, **Pin Change Interrupts** and **Timers** on the Atmega324. You will use timers and interrupts to create a digital counter and you will use buttons, LEDs and the good old 7-segment display to interact with your design in Proteus. You can check out the lab [[pm:lab:lab2|Laboratorul 2: Întreruperi, Timere]] and datasheet ({{:pm:doc8272.pdf|Datasheet ATmega324}}) for more information and references about timers and interrupts. |
- | + | ||
- | PWM (Pulse Width Modulation) or PDM (Pulse Duration Modulation) is a modulation technique used to reduce the average power delivered by an electrical signal to a load by chopping it into discrete parts. | + | |
- | The average value of a signal is dependent on the maximum and minimum amplitude and on the duration of the pulses of minimum and maximum amplitude. | + | |
- | + | ||
- | + | ||
- | + | ||
- | + | ||
- | ==== PWM - Electronics and average voltage ==== | + | |
- | In order to use PWM and prior to program our microcontroller to generate PWM signals, we first need to understand the basics of PWM and how to use it in different circuits. | + | |
- | + | ||
- | === Understanding PWM signals === | + | |
- | + | ||
- | The output signal (or the Pulse Width Modulated signal) will be generated by using electronic switches (such as Bipolar Transistors, MOSFET Transistors, IGBT) that connects a positive voltage power supply (e.g. a battery or a fixed voltage output) for a precise amount of time (T1) to the output, afterwards it connects a negative voltage (or the GND for unipolar PWM) the output from the power supply for another amount of time (T2), and than, the cycle repeats. | + | |
- | + | ||
- | In the following schematic, we can see that Q1 connects the output to the positive voltage supply (Vmax) and Q2 connects the ouput to the negative voltage supply (Vmin). Q1 and Q2 are controlled by a more complex circuit (transistor driving circuit), based on the signals given by the microcontroller (MCU). | + | |
- | The transistor driving circuit is sometimes necessary because of the different voltage levels of the MCU and the voltages of the power supplies. | + | |
- | + | ||
- | <note important>All the voltages are measured with respect to GND, unless otherwise is explicitly noted.</note> | + | |
- | The output signal delivered to the load will look similar to the one showed in the next figure: | + | |
- | + | ||
- | + | ||
- | As we can observe, in one cycle (of duration T=T1+T2) the signal is switched between Vmax and Vmin. | + | |
- | + | ||
- | + | ||
- | * Q1 is turned ON (and Q2 is OFF) for an interval of T1, and the output is forced to Vmax | + | |
- | * Q2 is turned ON (and Q1 is turned OFF) for an interval T2, and the output is forced to Vmin | + | |
- | * The cycle duration is T=T1+T2; | + | |
- | The average voltage of the signal (measured with respect to GND) can be calculated by computing the integral over time. | + | |
- | V_1 | + | |
- | + | ||
- | \begin{equation} | + | |
- | V_{avg}=\frac{1}{T}\int_{0}^{T} Vout(t) dt | + | |
- | \end{equation} | + | |
- | + | ||
- | Considering for one cycle, that from 0 to T1 the output is Vmax and from T1 to T (interval equal to T2) the output is Vmin than the average voltage will be computes as follows: | + | |
- | + | ||
- | \begin{equation} | + | |
- | V_{avg}=\frac{T1}{T}V_{max} + \frac{T2}{T} V_{min} | + | |
- | \end{equation} | + | |
- | + | ||
- | In electronics, the ratio between T1 (the duration of the positive pulse) and the period T is called the duty cycle. The duty cycles tells us how much time, compared to the total period, the positive pulse duration is. | + | |
- | + | ||
- | + | ||
- | \begin{equation} | + | |
- | d=\frac{T1}{T} | + | |
- | \end{equation} | + | |
- | + | ||
- | The duty cycle can be measured as a (percentage) % and it resembles the fraction of the positive pulse (T1) compared the total period (T). | + | |
- | + | ||
- | + | ||
- | \begin{equation} | + | |
- | d_{\%}=\frac{T1}{T}100 | + | |
- | \end{equation} | + | |
- | + | ||
- | Considering the definition of the duty cycle, the average voltage is: | + | |
- | + | ||
- | + | ||
- | \begin{equation} | + | |
- | V_{avg}=d\cdot V_{max} + (1-d)\cdot V_{min} | + | |
- | \end{equation} | + | |
- | + | ||
- | For the signal showed in the previous figure, we can observe that T1 is 7.5ms and T2 is 2.5ms (T=10ms), Vmax=10V, Vmin= -10V. | + | |
- | That leads us to $d=0.75 or d\%=75\%$ | + | |
- | + | ||
- | Thus, our output average voltage will be: | + | |
- | + | ||
- | + | ||
- | \begin{equation} | + | |
- | V_{avg}=0.75\cdot 10 + 0.25\cdot (-10)= 5V | + | |
- | \end{equation} | + | |
- | For an unipolar signal (the signal is switched between Vmax and 0 (GND) ) the average voltage will be computed as: | + | |
- | + | ||
- | \begin{equation} | + | |
- | V_{avg}=\frac{T1}{T}V_{max}=d\cdot V_{max} | + | |
- | \end{equation} | + | |
- | + | ||
- | <note tip>PWM is used in many applications where with a digital control different elements are controlled. For example, PWM can be used to control the speed of DC motors, to control the brightness of LEDs, to generate different colors using RGB LEDs (by controlling the intensity of each color), to generate analog signals (generating AC from DC), reducing the power and voltage from a power source, in MPTT solar battery chargers/controllers, in D-Class audio amplifiers, etc.</note> | + | |
- | + | ||
- | + | ||
- | === PWM on LEDs === | + | |
- | + | ||
- | Driving LEDs is done the same way as for simple digital outputs, as explained in our previous labs. | + | |
- | If the LED is a high power device (more than 25mW LED), we will need a controllable LED driver (usually based on a switchable current supply). | + | |
- | + | ||
- | The LED can be connected from the output of the MCU to GND with a resistor and it will light up when the MCU pin is driven HIGH. The resistor is used to limit the current through the LED. | + | |
- | {{ :pm:lab:lab3:r_limit.png?200 |}} | + | |
- | + | ||
- | The LED can be connected from the power supply to the MCU pin and it will light up when the MCU pin is driven LOW (the current will flow from Vcc to the MCU pin). | + | |
- | {{ :pm:lab:lab3:ledrgb.png?100 |}} | + | |
- | + | ||
- | + | ||
- | === Delivering PWM for DC brushed motors and other loads === | + | |
- | + | ||
- | == H Bridge basics == | + | |
- | + | ||
- | == H Bridge example == | + | |
- | + | ||
- | == H Bridge circuits == | + | |
- | + | ||
- | ==== Generating PWM using ATmega Timers ==== | + | |
- | + | ||
- | === Fast PWM mode === | + | |
- | + | ||
- | == Fixed Frequency == | + | |
- | == Programmable Frequency == | + | |
- | + | ||
- | === Phase Correct PWM mode === | + | |
- | + | ||
- | + | ||
- | ===== Reading quadrature encoders using external interrupts (INT0/INT1/INT2) ===== | + | |
- | + | ||
- | === Quadrature encoders - principles of operation === | + | |
- | + | ||
- | Encoders are electronic or electro-mechanic devices used to codify the relative movement (rotation) of a shaft. They generate two pulses (A and B) that are shifted 90 degrees apart that codify the speed of the movement and the direction of the movement. | + | |
- | * The rotation speed of the shaft will be deduced from the frequency of the pulses | + | |
- | * The number of revolutions will be calculated from the number of pulses | + | |
- | * The direction of rotation (clockwise or anticlockwise) will be deduced from the order of the pulses. | + | |
- | The encoders are usually characterized by the number of pulses/complete revolution and the type of circuitry for the output signals (PNP, NPN, PUSH-PULL stage). | + | |
- | + | ||
- | For example, let's say that we have an encoder with a Push-Pull stage of 5V (it can pull the output to 5V and GND and generate both states - HIGH and LOW) that generates a 4 pulses per complete revolution. | + | |
- | {{ :pm:lab:lab3:quadr_enc.png?400 |}} | + | |
- | + | ||
- | * We can observe that there are a number of 6 pulses, that means that the rotor has executed 1.5 revolutions (6/4) | + | |
- | * We can see that the period of signal A is 2s (from rising edge to the next rising edge), thus we can say that a complete revolution will be finished in 8 seconds. So, the speed of rotation is 7.5 (60 seconds /8 seconds per revolution) revolutions/minute | + | |
- | * We can observe that on a rising edge for signal A, signal B is on a LOW state, meaning that the rotation is in counter-clockwise direction | + | |
- | + | ||
- | === Example of using external interrupts to read quadratic encoders === | + | |
- | + | ||
- | + | ||
- | + | ||
- | ===== Setup ===== | + | |
- | + | ||
- | ===== Tasks ===== | + | |
+ | ===== Interrupts and Timers ===== | ||
+ | ==== Interrupts ==== | ||
+ | * Allow program to respond to **asynchronous** events when they occur, without busy-waiting. | ||
* The processor does an automatic procedure call (Interrupt Service Routine, **ISR**) when an interrupt event is detected. | * The processor does an automatic procedure call (Interrupt Service Routine, **ISR**) when an interrupt event is detected. | ||
* Defined as **interrupt vectors** which are memory locations for the ISRs. Each interrupt vector has an ISR which is basically a function that can be defined by the programmer. | * Defined as **interrupt vectors** which are memory locations for the ISRs. Each interrupt vector has an ISR which is basically a function that can be defined by the programmer. | ||
Line 325: | Line 193: | ||
+ | ===== Setup ===== | ||
{{ :pm:diverse:carantina-proteus.jpg?200 |}} | {{ :pm:diverse:carantina-proteus.jpg?200 |}} |