This shows you the differences between two versions of the page.
pm:lab:lab0xc0-3 [2020/03/29 23:15] dumitru.tranca |
pm:lab:lab0xc0-3 [2020/03/31 17:01] (current) constantin.ghilinta [Generating PWM using ATmega Timers] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Laboratorul 0xC3: PWM, Counters and External Interrupts ====== | + | ===== Laboratorul 0xC2: PWM, Counters and External Interrupts ====== |
This lab covers the topics of PWM, H-bridge drivers and reading encoders using external interrupts. | 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}}). | 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}}). | ||
- | Sketch is available here: | + | |
===== PWM (Pulse Width Modulation) ===== | ===== PWM (Pulse Width Modulation) ===== | ||
- | PWM (Pulse Width Modulation) or PDM (Pulse Duration Modulation) is a modul{{:pm:lab:lab3:lab2_0xc2.zip|}}ation technique used to reduce the average power delivered by an electrical signal to a load by chopping it into discrete parts. | + | 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. | 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. | ||
Line 33: | Line 33: | ||
<note important>All the voltages are measured with respect to GND, unless otherwise is explicitly noted.</note> | <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: | The output signal delivered to the load will look similar to the one showed in the next figure: | ||
- | {{ :pm:lab:lab3:pwm.png?500 |}} | + | {{ :pm:lab:lab3:pwm.png?550 |}} |
As we can observe, in one cycle (of duration T=T1+T2) the signal is switched between Vmax and Vmin. | As we can observe, in one cycle (of duration T=T1+T2) the signal is switched between Vmax and Vmin. | ||
Line 91: | Line 91: | ||
For example, the following signal is switched ON to 10V for 7.5ms and OFF (to 0V) for another 2.5ms (T=10ms, d=0.75). | For example, the following signal is switched ON to 10V for 7.5ms and OFF (to 0V) for another 2.5ms (T=10ms, d=0.75). | ||
- | {{ :pm:lab:lab3:pwm_unipolar.png?500 |}} | + | {{ :pm:lab:lab3:pwm_unipolar.png?550 |}} |
- | \begin{equstudation} | + | \begin{equation} |
V_{avg}=\frac{7.5ms}{10ms} \cdot 10=0.75 \cdot 10 = 7.5V | V_{avg}=\frac{7.5ms}{10ms} \cdot 10=0.75 \cdot 10 = 7.5V | ||
\end{equation} | \end{equation} | ||
Line 100: | Line 100: | ||
Example of PWM used to generate a sinusoidal waveform: | Example of PWM used to generate a sinusoidal waveform: | ||
- | {{ :pm:lab:pwm_sin_simulation.png?300 |}} | + | {{ :pm:lab:pwm_sin_simulation.png?550 |}} |
=== PWM on LEDs === | === PWM on LEDs === | ||
Line 107: | Line 107: | ||
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. | 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 |}} | + | {{ :pm:lab:lab3:r_limit.png?350 |}} |
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). | 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 |}} | + | {{ :pm:lab:lab3:ledrgb.png?230 |}} |
If we need to power a high power load or a higher power LED we can use a transistor to switch on and off the load. | If we need to power a high power load or a higher power LED we can use a transistor to switch on and off the load. | ||
R1 can be replaced with a constant current supply | R1 can be replaced with a constant current supply | ||
- | {{ :pm:lab:lab3:high_power_led.png?300 |}} | + | {{ :pm:lab:lab3:high_power_led.png?400 |}} |
=== Delivering PWM for DC brushed motors and other loads === | === Delivering PWM for DC brushed motors and other loads === | ||
Line 125: | Line 125: | ||
In the next schematic, L1 is our load that might have an inductive characteristics. In order to protect the transistor, we use the flyback diode D1. | In the next schematic, L1 is our load that might have an inductive characteristics. In order to protect the transistor, we use the flyback diode D1. | ||
The PWM will be generated by the microcontroller and will control the base of Q1. | The PWM will be generated by the microcontroller and will control the base of Q1. | ||
- | {{ :pm:lab:lab3:flyback_diode_example.png?300 |}} | + | {{ :pm:lab:lab3:flyback_diode_example.png?500 |}} |
== H Bridge principles and examples == | == H Bridge principles and examples == | ||
Line 176: | Line 176: | ||
* Wait For T2 | * Wait For T2 | ||
- | he issue here would be that we need to determine how much time does the code before this sequence or after it take to execute. | + | The issue here would be that we need to determine how much time does the code before this sequence or after it take to execute. |
Another solution would be to use timer interrupts and in the ISR to change the state of the pin. | Another solution would be to use timer interrupts and in the ISR to change the state of the pin. | ||
In this manner, we will at least have a precise timing, but we will generate many interrupts that might affect our code in some situations. | In this manner, we will at least have a precise timing, but we will generate many interrupts that might affect our code in some situations. | ||
Line 191: | Line 191: | ||
First, we need to identify the PWM capable pins. We can identify by looking for the alternative functionality OCnX | First, we need to identify the PWM capable pins. We can identify by looking for the alternative functionality OCnX | ||
- | {{ :pm:lab:lab3:atmega324_pwm_pins.png?300 |}} | + | {{ :pm:lab:lab3:atmega324_pwm_pins.png?450 |}} |
=== Fast PWM mode === | === Fast PWM mode === | ||
Line 198: | Line 198: | ||
//The counter counts from BOTTOM to TOP then restarts from BOTTOM. In non-inverting Compare Output mode, the Output Compare (OCnx) is cleared on the compare match between TCNTn and OCRnx, and set at BOTTOM. In inverting Compare Output mode output is set on compare match and cleared at BOTTOM. Due to the single-slope operation, the operating frequency of the fast PWM mode can be twice as high as the phase correct and phase and frequency correct PWM modes that use dual-slope operation. This high frequency makes the fast PWM mode well suited for power regulation, rectification, and DAC applications. High frequency allows physically small sized external components (coils, capacitors), hence reduces total system cost.// | //The counter counts from BOTTOM to TOP then restarts from BOTTOM. In non-inverting Compare Output mode, the Output Compare (OCnx) is cleared on the compare match between TCNTn and OCRnx, and set at BOTTOM. In inverting Compare Output mode output is set on compare match and cleared at BOTTOM. Due to the single-slope operation, the operating frequency of the fast PWM mode can be twice as high as the phase correct and phase and frequency correct PWM modes that use dual-slope operation. This high frequency makes the fast PWM mode well suited for power regulation, rectification, and DAC applications. High frequency allows physically small sized external components (coils, capacitors), hence reduces total system cost.// | ||
- | {{ :pm:lab:lab3:fast_pwm_timing.png |}} | + | {{ :pm:lab:lab3:fast_pwm_timing.png |}} (source {{:pm:doc8272.pdf|ATmega324 Datasheet}}) |
+ | |||
+ | From {{:pm:doc8272.pdf|ATmega324 Datasheet}} in Chapte 15-17 you can read more about timers. | ||
+ | Configuration bits are found in TCCRnA, TCCRnB and TCCRnC (configuration is dependent on the timer specs). | ||
Line 205: | Line 209: | ||
We can configure the timers in different ways, so that the timers count up to overflow, to ICRn or to OCRnA. | We can configure the timers in different ways, so that the timers count up to overflow, to ICRn or to OCRnA. | ||
+ | |||
+ | Configuring/selecting different modes can be done using the Waveform Generation Mode bits (WGM) | ||
+ | Let's see the WGM config bits for Timer 1. | ||
+ | {{ :pm:lab:wgm.png?700 |}} (source {{:pm:doc8272.pdf|ATmega324 Datasheet}})) | ||
+ | |||
+ | After we choose the WGM bits for 8-bit FAST PWM we need to choose a normal port operation: OCn set when timer rolls from TOP to BOTTOM and OCn is cleared on compare match with OCRn. | ||
+ | The selection of this functionality is done using the COM bits for Fast PWM mode. | ||
+ | |||
+ | {{ :pm:lab:com0_bits.png?600 |}} (source {{:pm:doc8272.pdf|ATmega324 Datasheet}}) | ||
+ | Last but not least, remember: <note>a timer, in order to do it's job, needs a clock source. Thus, we need to select the proper clock source.</note> | ||
+ | The Clock Source bits for timer 1 need to be set in TCCR1B. For other timers check the datasheet. | ||
+ | {{ :pm:lab:cs-bits_timer1.png?600 |}} (source {{:pm:doc8272.pdf|ATmega324 Datasheet}}) | ||
+ | |||
+ | <note important>The output frequency of a fast PWM pin is dependent on the prescaller and the clock frequency of the MCU. According to Section 17.7.3 | ||
+ | \begin{equation} | ||
+ | f_{OCnX}=\frac{f_{clk\_IO}}{2 \cdot N \cdot 256} | ||
+ | \end{equation} </note> | ||
+ | |||
Let now try a simple example with OC1A as output (PD5) set to fast PWM (8 bit) with a top at 0xFF. | Let now try a simple example with OC1A as output (PD5) set to fast PWM (8 bit) with a top at 0xFF. | ||
Line 213: | Line 235: | ||
TCCR1A |= _BV(WGM10); //Just WGM10 is in TCCR1A | TCCR1A |= _BV(WGM10); //Just WGM10 is in TCCR1A | ||
- | //WGM11, WGM12 and WGM13 will be set in TCCR1B | + | //WGM12 will be set in TCCR1B |
TCCR1B |= _BV(WGM12); | TCCR1B |= _BV(WGM12); | ||
Line 234: | Line 256: | ||
are preferred for motor control applications when using more than one PWM signals. | are preferred for motor control applications when using more than one PWM signals. | ||
- | {{ :pm:lab:lab3:phase_correct_pwm_timing.png?400 |}} | + | {{ :pm:lab:lab3:phase_correct_pwm_timing.png?500 |}} |
These two types of PWMs are relevant just when using PWMs on more than one channel and they help reducing interference, glitches, minimizing electromagnetic emissions (EMI) and for other complex systems and electronics. | These two types of PWMs are relevant just when using PWMs on more than one channel and they help reducing interference, glitches, minimizing electromagnetic emissions (EMI) and for other complex systems and electronics. | ||
Line 280: | Line 302: | ||
* On rising edge for signal A | * On rising edge for signal A | ||
* Verify the state of B. If it is LOW, it's one direction, if it's HIGH, the motor is rotating in the other way | * Verify the state of B. If it is LOW, it's one direction, if it's HIGH, the motor is rotating in the other way | ||
+ | |||
+ | Detecting a rising edge or a falling edge using interrupts can be configured in EICRA using the ISC bits (as described in the datasheet). | ||
+ | |||
+ | {{ :pm:lab:lab3:eicra_interrupts.png?500 |}} | ||
+ | {{ :pm:lab:lab3:iscn_bits.png?500 |}} | ||
+ | |||
+ | In the end, to enable the interrupts don't forget to set the bits for the interrupt you wish to activate in the EIMSK register. | ||
+ | {{ :pm:lab:eimsk.png?500 |}} | ||
+ | |||
+ | In the {{:pm:doc8272.pdf|Datasheet ATmega324}} you can find in Chapter 13 - External interrupts more details and descriptions. | ||
Testing the rising edge can be done through external interrupts or through continuous polling. | Testing the rising edge can be done through external interrupts or through continuous polling. | ||
Line 297: | Line 329: | ||
===== Tasks ===== | ===== Tasks ===== | ||
+ | Sketch is available here: {{:pm:lab:lab3:lab2_0xc2.zip|}} | ||
1. Compile the code sketch in the archive and run it. | 1. Compile the code sketch in the archive and run it. | ||
Line 315: | Line 348: | ||
4. At this moment we have motor that can spin in both directions and it has a little bit on inertia in the simulated model. | 4. At this moment we have motor that can spin in both directions and it has a little bit on inertia in the simulated model. | ||
- | Identify, using external interrupt INT0 on rising edge or using continuous polling (verifying in main loop), what is the direction of revolution and send the information to the virtual terminal. (2p) | + | Identify, using external interrupt INT0 ( or continuous polling on digital inputs) on rising edge or using continuous polling (verifying in main loop), what is the direction of revolution and send the information to the virtual terminal. (2p) |
+ | * Responsabili: [[cristi.tranca@gmail.com | Cristi Trancă]] | ||