►AVR Timers as PWM

Most 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:

PWM11PWM10Description
00PWM operation of Timer/Counter1 is disabled
01Timer/Counter1 in 8-bit PWM Mode
10Timer/Counter1 in 9-bit PWM Mode
11Timer/Counter1 in 10-bit PWM Mode

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 ResolutionTimer Top ValueFrequency
8-bit PWM$00FF (255)Ftck1/510
9-bit PWM$01FF (511)Ftck1/1022
10-bit PWM$03FF (1023)Ftck1/2046


COM1X1COM1X0Effect on OCX1
00Not Connected
01Not Connected
10Cleared on compare match, up-counting. Set on compare match down-counting (non-inverted PWM)
11Cleared on compare match, down-counting. Set on compare match up-counting (inverted PWM)
Note: X = A or B

►Assembly Code Example

;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 C
//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

Conversation

Fri Dec 19 2014, 11:31 AM
1
-1'
Fri Dec 19 2014, 11:30 AM
1
1
ajay_bhargav
Fri Dec 12 2014, 04:38 AM
@Abdul BAsit, Post in forum please. We will help you.
Wed Dec 10 2014, 06:41 PM
Abdul BAsit
I am working on smoke detector using AT89S52 and dont know how its wors..kindly send me circuit diagram and code for its hardware & software implementation
Tue Dec 09 2014, 11:30 AM
Irinel
Please send me the content of that cd-rom (embedded c - michael j pont. Thank you.
Tue Dec 09 2014, 02:45 AM
kmyo
please give me the operation of 12v trickle charger
Sun Dec 07 2014, 07:50 AM
hidawi
we need the circuit diagrom of the remote control of iduction motor by android appliction
PsySc0rpi0n
Tue Dec 02 2014, 10:30 AM
I have no idea... I'm new to micro controllers
Tue Dec 02 2014, 08:35 AM
samarnath dev
how can usb key bord interfasing to dot matrx led moving, by asm51 ?
Tue Dec 02 2014, 08:32 AM
samarnath dev
how can usb key bord interfasing to 8051 by asm code?

Downloads

Comments

naimish324
Wed Dec 17 2014, 04:48 AM
sathish177
Tue Dec 16 2014, 06:43 PM
djnokey
Sat Dec 13 2014, 12:12 PM
sai9113
Tue Dec 09 2014, 07:23 AM
alexlomol
Fri Dec 05 2014, 12:06 AM
chandujonty
Thu Dec 04 2014, 10:11 AM
fantasy1214
Mon Dec 01 2014, 01:45 PM
vumbelino
Sat Nov 29 2014, 04:43 PM

Online

Guests: 53, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 05:37 AM

Members: 37560
Newest member: naimish324
Forum Activity
sankalp_s Avatar
Posted By sankalp_s
Fri Dec 19 2014, 06:04 PM
This version works.Edit it to use either method. ...
muhibraza Avatar
Posted By muhibraza
Fri Dec 19 2014, 08:05 AM
software_developer Avatar
Posted By software_developer
Fri Dec 19 2014, 04:54 AM
Which files i have to use?Please give me .zip fold...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Dec 19 2014, 12:21 AM
This version works.Edit it to use either method. ...
sankalp_s Avatar
Posted By sankalp_s
Thu Dec 18 2014, 08:40 PM
SIR THIS CODE IS NOT WORKING.. ORG 000H ; starting...
sankalp_s Avatar
Posted By sankalp_s
Thu Dec 18 2014, 08:36 PM
i've tried this code but faced a syntax error on l...
sankalp_s Avatar
Posted By sankalp_s
Thu Dec 18 2014, 08:32 PM
SIR THIS CODE IS NOT WORKING.. ORG 000H ; starting...
ajay_bhargav Avatar
Posted By ajay_bhargav
Thu Dec 18 2014, 04:24 AM
Code is all ok.. just change EXT0 to EX0... By mis...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Dec 17 2014, 11:21 PM
i've tried this code but faced a syntax error on l...
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Dec 17 2014, 09:53 PM
Thank you sir for help! I did change the code sir ...