►Idea Behind Implementation

The basic idea behind PWM implementation on 8051 is using timers and switching port pin high/low at defined intervals. As we have discussed in the introduction of PWM that by changing the Ton time, we can vary the width of square wave keeping same time period of the square wave.

We will be using 8051 Timer0 in Mode 0. Values for high and low level will be loaded in such a way that total delay remains same. If for high level we load a value X in TH0 then for low level TH0 will be loaded with 255-X so that total remains as 255.

►Assembly Code Example

►Timer setup for PWM
	PWMPIN EQU P1.0		; PWM output pin
PWM_SETUP:
	MOV TMOD,#00H		; Timer0 in Mode 0
	MOV R7, #160		; Set pulse width control
	; The value loaded in R7 is value X as
	; discussed above.
	SETB EA		; Enable Interrupts
	SETB ET0		; Enable Timer 0 Interrupt
	SETB TR0		; Start Timer
	RET


►Interrupt Service Routine
TIMER_0_INTERRUPT:
	JB F0, HIGH_DONE	; If F0 flag is set then we just finished
				; the high section of the
LOW_DONE:			; cycle so Jump to HIGH_DONE
	SETB F0			; Make F0=1 to indicate start of high section
	SETB PWMPIN		; Make PWM output pin High
	MOV TH0, R7		; Load high byte of timer with R7
				; (pulse width control value)
	CLR TF0			; Clear the Timer 0 interrupt flag
	RETI			; Return from Interrupt to where
					; the program came from
HIGH_DONE:
	CLR F0			; Make F0=0 to indicate start of low section
	CLR PWMPIN		; Make PWM output pin low
	MOV A, #0FFH	; Move FFH (255) to A
	CLR C			; Clear C (the carry bit) so it does
					; not affect the subtraction
	SUBB A, R7		; Subtract R7 from A. A = 255 - R7.
	MOV TH0, A		; so the value loaded into TH0 + R7 = 255
	CLR TF0			; Clear the Timer 0 interrupt flag
	RETI			; Return from Interrupt to where
				; the program came from


In your main program you need to call this PWM_SETUP routine and your controller will have a PWM output. Timer Interrupt service routine will take care of PWM in the background. The width of PWM can be changed by changing the value of R7 register. In above example I am using 160, you can choose any value from 0 to 255. R7 = 0 will give you o/p 0V approx and R7 = 255 will give you 5V approx.

You can also make use of Timer1 if you want. And the output pin can be changed to whatever pin you want.
►C Code Example

►Timer setup for PWM in C
//Global variables and definition
#define PWMPIN P1_0

unsigned char pwm_width;
bit pwm_flag = 0;

void pwm_setup(){
	TMOD = 0;
	pwm_width = 160;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
}


►Interrupt Service Routine
void timer0() interrupt 1 {
	if(!pwm_flag) {	//Start of High level
		pwm_flag = 1;	//Set flag
		PWMPIN = 1;	//Set PWM o/p pin
		TH0 = pwm_width;	//Load timer
		TF0 = 0;		//Clear interrupt flag
		return;		//Return
	}
	else {	//Start of Low level
		pwm_flag = 0;	//Clear flag
		PWMPIN = 0;	//Clear PWM o/p pin
		TH0 = 255 - pwm_width;	//Load timer
		TF0 = 0;	//Clear Interrupt flag
		return;		//return
	}
}


Now your normal 8051 is capable of PWM output.



◄ Introduction to PWM  |  8051 PWM Example Code  |  AVR PWM Example Code ►

Back to index
Forum Activity
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...
NexusRevZ Avatar
Posted By NexusRevZ
Wed Jul 30 2014, 02:10 AM
Oh tahnk you so much for the reply. I will do imme...

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

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
PavanKh
Fri Jul 25 2014, 12:56 AM

Online

Guests: 46, Members: 0 ...

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

Members: 37487
Newest member: jayachandra