PWM fundamental
1.
Introduction: PWM stands for Pulse Width
Modulation. In ATMEGA 32 we get this type of output from Pin numbers 4 and 21.
It is important to control effective output voltage level. This type of output
is helpful in producing variable luminance of LEDs, speed of dc motors etc.
2.
Principle: It is a pulse train at equal
time intervals of which on time duration is controlled. Effective voltage in
PWM may be calculated by multiplying the pulse magnitude to the duty cycle
(i.e. percentage ratio of on time to the total time period). For example 5 volt
magnitude PWM with duty cycle 45% will produce the effective voltage 5X0.45
= 2.25 volts.
3.
Modes: A reference voltage is compared
with a sawtooth wave. Pulses produced with the difference between them are
divided into three modes of operation. It is inverted mode if pulse has
trailing edge as the waveform exceeds the reference voltage. The opposite is
the non-inverted mode. And under toggle modes of operation the
pulse toggles between two levels once the waveform exceeds the reference level.
4.
BOTTOM
- TOP – MAX: Realization using timer sets the initial value of the timer say
BOTTOM. Then timer starts counting to the maximum count say MAX. for 8 bit
timers TIMER0 and TIMER 2 MAX=255 and for TIMER1 MAX=65536. Under CTC mode
timer may be cleared by compare its value below the MAX called the TOP.
5.
PWM
modes with Timers: Three modes of operations. Fast PWM, Phase Correct PWM, Frequency
and Phase Correct PWM. In Fast PWM the timer counts from the BOTTOM to the TOP.
In between the pulse starts when the sawtooth wave crosses beyond the reference
level. Pulse ends with timer value match to the TOP. In Phase Correct PWM
instead of sawtooth reference voltage is compared to a triangular wave. Thus
PWM pulse produced here has equal interval of the center of the pulse i.e.
phase of the pulse is corrected whereas in the last case pulse ends at equal
interval with unequal distance between center of the pulse due change in
reference voltage and it took less time i.e. faster PWM. In Frequency and Phase
Correct PWM the sawtooth waveform has variable TOP (<=MAX) thus it basically
represents a Fast PWM with variable frequency.
6.
Expt-1
1.
Title: Generate a 50 Hz PWM signal
having 45% duty cycle.
2.
Components
required:
Sl. No.
|
Category
|
Item name/value
|
Quantity
|
Circuit diagram
|
1
|
IC
|
ATMEGA32
|
1
|
|
2
|
7805
|
1
|
||
3
|
Resistor
|
10K
|
1
|
|
4
|
330R
|
1
|
||
5
|
Capacitor
|
0.1uf
|
2
|
|
6
|
22pf
|
2
|
||
7
|
10uf, 24Volt
|
1
|
||
8
|
Crystal Oscillator
|
16MHz
|
1
|
|
9
|
LED
|
Green
|
1
|
|
10
|
Battery
|
9Volt
|
1
|
3.
Procedure
to code:
a. Assign
duty = 115; as duty
cycle = 45% of 255 = 114.75 = 115
b. Initialize timer in PWM mode
i.
Initialize
TCCR0 as per requirement, say as follows
ii.
Assign
PB3 as output port to make OC0 pin (pin PB3 for atmega32) as output pin
c.
Run
forever in finite loop
i.
Assign
OCR0 by duty
4.
Code:
#ifndef
F_CPU
#define
F_CPU 16000000L
#endif
#include
<avr/io.h>
#include
<util/delay.h>
void
pwm_init()
{
TCCR0 |=
(1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
DDRB |=
(1<<PB3);
}
void
main()
{
uint8_t
duty;
duty =
115;
pwm_init();
while(1)
{
OCR0 =
duty;
}
}
5.
Circuit:
6.
PWM
Simulation:
Expt-2
1.
Title: Program to change brightness of an LED.
2.
Components
required:
Sl. No.
|
Category
|
Item name/value
|
Quantity
|
Circuit diagram
|
1
|
IC
|
ATMEGA32
|
1
|
|
2
|
7805
|
1
|
||
3
|
Resistor
|
10K
|
1
|
|
4
|
330R
|
1
|
||
5
|
Capacitor
|
0.1uf
|
2
|
|
6
|
22pf
|
2
|
||
7
|
10uf, 24Volt
|
1
|
||
8
|
Crystal Oscillator
|
16MHz
|
1
|
|
9
|
LED
|
Green
|
1
|
|
10
|
Battery
|
9Volt
|
1
|
3.
Procedure
to code:
a. initialize timer0 in PWM mode
i.
initialize
timer0 in PWM mode
ii.
make sure
to make OC0 pin (pin PB3 for atmega32) as output pin
b. run forever
i.
increasing
brightness
ii.
set the
brightness as duty cycle
iii.
delay so as
to make the user "see" the change in brightness
iv.
decreasing
brightness
v.
set the
brightness as duty cycle
vi.
delay so as
to make the user "see" the change in brightness
vii.
repeat this
forever
4.
Code:
#ifndef
F_CPU
#define
F_CPU 16000000L
#endif
#include
<avr/io.h>
#include
<util/delay.h>
void
pwm_init()
{
TCCR0 |=
(1<<WGM00)|(1<<COM01)|(1<<WGM01)|(1<<CS00);
DDRB |=
(1<<PB3);
}
void main()
{
uint8_t
brightness;
pwm_init();
while(1)
{
for
(brightness = 0; brightness < 255; ++brightness)
{
OCR0 =
brightness;
_delay_ms(10);
}
for
(brightness = 255; brightness > 0; --brightness)
{
OCR0 =
brightness;
_delay_ms(10);
}
}
}
5. Circuit:
6. Simulation:
No comments:
Post a Comment