►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
Forum Activity
a1s2d3 Avatar
Posted By a1s2d3
Fri Aug 01 2014, 05:44 PM
Peter_Parker Avatar
Posted By Peter_Parker
Fri Aug 01 2014, 09:37 AM
I linked the tx and rx pins,opened up the hyperter...
majoka Avatar
Posted By majoka
Fri Aug 01 2014, 07:25 AM
attached is the whole project with Proteus simulat...
pravin_35 Avatar
Posted By pravin_35
Thu Jul 31 2014, 01:49 AM
attach is the code for lcd may be need a little ch...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:45 PM
@ sermeteraythank you for sharing your experience ...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:38 PM
@ Peter_ParkerHC06 by default baud rate is 9600try...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:25 PM
attach is the code for lcd may be need a little ch...
majoka Avatar
Posted By majoka
Wed Jul 30 2014, 02:21 PM
@ pravin_35both compilers are almost sameif you ha...
pravin_35 Avatar
Posted By pravin_35
Wed Jul 30 2014, 07:38 AM
http://extremeelectronics.co.in/pic16f877a-tutoria...
NexusRevZ Avatar
Posted By NexusRevZ
Wed Jul 30 2014, 02:31 AM
Ok, I have edited the code, it's updated above wit...

Conversation

Thu Jul 31 2014, 04:32 PM
1
-1'
Thu Jul 31 2014, 04:32 PM
1
1
NexusRevZ
Tue Jul 29 2014, 09:32 PM
Anyone good in Asm lang?
ajay_bhargav
Mon Jul 28 2014, 12:34 AM
@ Anshu Raj sorry we do not take such requests.
Sun Jul 27 2014, 12:23 AM
Anshu Raj
sir plz send the automatic collage alram bells in 2 hrs bells ring. For microcontroller programme plz sir
ajay_bhargav
Thu Jul 24 2014, 01:06 AM
@R.ABHISHEK where? I dont see it. is it in forum?
Wed Jul 23 2014, 05:49 AM
R.ABHISHEK
sir,pls message on microcontroller 8051 assambly language new program
Wed Jul 23 2014, 05:47 AM
R.ABHISHEK
sir,pls message on 8051 microcontroller assambly language program new quastions
ajay_bhargav
Tue Jul 22 2014, 09:18 AM
@Pragya Android application is taken from google play. Only Author has its source with him. Application is only used in this project.
Tue Jul 22 2014, 04:58 AM
Pragya
plz send me 8051 code and android code for android based device controller via Bluetooth .Here in this project I am using Hc-06 Bluetooth module

Downloads

Comments

a1s2d3
Fri Aug 01 2014, 05:41 PM
jayachandra
Fri Aug 01 2014, 04:47 AM
risko
Thu Jul 31 2014, 03:53 PM
ismaelfogaca
Thu Jul 31 2014, 10:50 AM
NexusRevZ
Tue Jul 29 2014, 09:29 PM
kritz22
Mon Jul 28 2014, 10:33 AM
Abbasans786
Mon Jul 28 2014, 03:15 AM
Imran.pk
Fri Jul 25 2014, 07:50 PM

Online

Guests: 33, Members: 0 ...

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

Members: 37488
Newest member: a1s2d3