Differences

This shows you the differences between two versions of the page.

Link to this comparison view

pm:lab:lab0xc0-3 [2020/03/30 01:07]
dumitru.tranca
pm:lab:lab0xc0-3 [2020/03/31 17:01] (current)
constantin.ghilinta [Generating PWM using ATmega Timers]
Line 3: Line 3:
 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?​500 |}}+{{ :​pm:​lab:​pwm_sin_simulation.png?​550 |}}
 === PWM on LEDs === === PWM on LEDs ===
  
Line 110: Line 110:
  
 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?​200 |}}+{{ :​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?​350 |}}+{{ :​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?​400 |}} +{{ :​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 235: 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 329: 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.
pm/lab/lab0xc0-3.1585519668.txt.gz · Last modified: 2020/03/30 01:07 by dumitru.tranca
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0