Pulse Width Modulation (PWM): AVR Code example
►AVR Timers as PWMMost of AVR controllers have onchip PWM channel which makes PWM usage much simple and more accurate. AVR timers or counters can be used in PWM mode without disturbing the basic timer function.
As in case of AT90S8515, Timer1 can be configured in PWM mode by setting PWM11 and PWM10 bits in TCCR1A register. Following modes are available for PWM:
The pre-scalar source for Timer/Counter1 can be selected with the help of clock select bits in TCCR1B register (more information please check datasheet at page 37).
Width of pulse is loaded in the timer output compare registers OCR1A (OCR1AH & OCR1AL) and OCR1B (OCR1BH & OCR1BL). Timer/Counter1 acts as an up/down counter, counting up from $0000 to TOP (see table below), where it turns and counts down again to zero before cycle is repeated. When the counter value matches the content of 10 least significant bits of OCR1A or OCR1B, the PD5 (OC1A)/OC1B pins are set or cleared according to the settings of COM1A1/COM1A0 or COM1B1/COM1B0 bits in Timer/Counter1 Control register (TCCR1A), see table below.
| PWM Resolution | Timer Top Value | Frequency |
|
|
|
| COM1X1 | COM1X0 | Effect on OCX1 |
|
|
|
|
Note: X = A or B►Assembly Code ExampleCODE:
;8-bit Non-Inverted PWM code example
.equ pulse_width = $40
;Pulse width can be changed from 0 to TOP
PWM_START:
ldi temp, pulse_width ;Load pulse width
out OCR1AL, temp ;OCR1A = Pulse width
clr temp
out OCR1AH, temp
ldi temp, $81 ;8-bit PWM Mode
out TCCR1A, temp ;Non Inverted
in temp, DDRD ;Make PortD.5 as o/p
ori temp, (1<<5)
out DDRD, temp
ldi temp, $1 ;Start PWM
out TCCR1B, temp
ret ;Return to main
;PWM will run in background automatically
►PWM Setup in CCODE:
//Global variables and definition
#define PULSE_WIDTH 0x40
void pwm_start(){
OCR1AL = PULSE_WIDTH; //Load Pulse width
OCR1AH = 0;
DDRD |= (1<<5); //PortD.5 as o/p
TCCR1A = 0x81; //8-bit, Non-Inverted PWM
TCCR1B = 1; //Start PWM
}
Most of AVRs have same set of registers for PWM setup. Just go through the datasheet once and configure registers. If you still feel any problem working on things, we are here to help. But please make use of
forum◄ Introduction to PWM | 8051 PWM Example Code | AVR PWM Example Code ►
Back to index