►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

Mon Aug 25 2014, 07:12 AM
Vinod Soni
Hi Sir, How can i make this RFID with ATMega16/32 controller
pavanjain
Sat Aug 23 2014, 05:10 AM
Sir i want to download the hex file in P89v51rd2 microcontroller using max232 IC but we not load the program in IC
Tue Aug 19 2014, 04:51 PM
allen ndumu
please i will like to know if the problem of speed control of dc motor using keypad and l298 was if yes can i have the complete project send it to the address i really need to see it
Tue Aug 19 2014, 10:47 AM
Amit Bisht
sir i want to have some trending projects on which i could work related to ATmega 16 microcontroller
ExperimenterUK
Sat Aug 16 2014, 04:16 PM
@diligent . create a thread
Sat Aug 16 2014, 03:17 PM
diligent
in assembly langauage programming..
Sat Aug 16 2014, 03:14 PM
diligent
will anyone help me?
Thu Aug 14 2014, 10:24 PM
Amirudin Tone
i help, i need liberary IC 4047 in the proteus. thanks
ExperimenterUK
Thu Aug 14 2014, 03:59 PM
@NARAYANA are you getting an error ?
Tue Aug 12 2014, 11:47 PM
NARAYANA
i am not able to get reg please provide me registration link

Downloads

Comments

enisi
Wed Aug 27 2014, 06:08 AM
sadalakshmi
Wed Aug 27 2014, 12:39 AM
Paecake
Sun Aug 24 2014, 04:07 PM
pavanjain
Sat Aug 23 2014, 02:33 AM
samchovatiya
Thu Aug 21 2014, 02:30 PM
hasmukhjain
Wed Aug 20 2014, 06:28 AM
rablo
Tue Aug 19 2014, 10:10 AM
faizhameed
Tue Aug 12 2014, 10:39 PM

Online

Guests: 40, Members: 0 ...

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

Members: 37501
Newest member: enisi
Forum Activity
ExperimenterUK Avatar
Posted By ExperimenterUK
Wed Aug 27 2014, 04:45 PM
In your TX code I see where you set payload width ...
enisi Avatar
Posted By enisi
Wed Aug 27 2014, 07:05 AM
It can be more than one ansawer correctly
enisi Avatar
Posted By enisi
Wed Aug 27 2014, 06:57 AM
Port 0 is connected indicator 7 segments, so that ...
siddique Avatar
Posted By siddique
Wed Aug 27 2014, 01:48 AM
Pls check the Tx and Rx functions have made some c...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Aug 26 2014, 06:03 PM
I assume you have made the changes I suggested to ...
siddique Avatar
Posted By siddique
Tue Aug 26 2014, 03:20 AM
Hi,Tried your suggestions.when i put the device in...
ExperimenterUK Avatar
Posted By ExperimenterUK
Mon Aug 25 2014, 04:00 PM
This tutorial looks useful.http://www.diyembedded....
siddique Avatar
Posted By siddique
Mon Aug 25 2014, 03:30 AM
Hi,I'm currently working on the nRF24L01+ transcei...
gaurav7577 Avatar
Posted By gaurav7577
Mon Aug 25 2014, 12:38 AM
Code which is already loaded is running fine...but...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sun Aug 24 2014, 03:32 PM
thnks for rply..i hav attached error pic here...pl...