►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

Conversation

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
Tue Aug 12 2014, 10:32 AM
saloni
sir I was needing literature survey of''ANDROID CONTROLLED SPY ROBOT USING MICROCONTROLLER"
ajay_bhargav
Mon Aug 11 2014, 12:30 PM
@nisargpatel and @NARAYANA please post questions in forum.

Downloads

Comments

hasmukhjain
Wed Aug 20 2014, 06:28 AM
rablo
Tue Aug 19 2014, 10:10 AM
faizhameed
Tue Aug 12 2014, 10:39 PM
ind3siszive
Sun Aug 10 2014, 01:05 PM
snaxxx68
Sat Aug 09 2014, 11:06 AM
[email protected]
Sat Aug 09 2014, 08:58 AM
rajiabdul
Mon Aug 04 2014, 01:31 PM
giash
Sat Aug 02 2014, 04:44 AM

Online

Guests: 29, Members: 0 ...

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

Members: 37496
Newest member: hasmukhjain
Forum Activity
jayachandra Avatar
Posted By jayachandra
Wed Aug 20 2014, 08:11 AM
thanq for reply @hasmukhjain,actually my applicati...
hasmukhjain Avatar
Posted By hasmukhjain
Wed Aug 20 2014, 06:40 AM
Hi Mr jayachandra,I feel ur if statement for switc...
nisargpatel Avatar
Posted By nisargpatel
Wed Aug 20 2014, 01:29 AM
see we are measuring electrical parameters by CT, ...
jayachandra Avatar
Posted By jayachandra
Tue Aug 19 2014, 08:22 AM
HELLOim facing problem with eeprom, i wrote a prog...
ExperimenterUK Avatar
Posted By ExperimenterUK
Sat Aug 16 2014, 04:11 PM
You will have to be more specific about what you a...
nisargpatel Avatar
Posted By nisargpatel
Sat Aug 16 2014, 06:03 AM
Anyone have idea related to AC/DC current measurem...
sairfan1 Avatar
Posted By sairfan1
Wed Aug 13 2014, 01:48 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Tue Aug 12 2014, 04:34 PM
Usually its better to have a LED driver circuit in...
ajay_bhargav Avatar
Posted By ajay_bhargav
Tue Aug 12 2014, 04:01 PM
There are so many apps available. Did you try a se...
sairfan1 Avatar
Posted By sairfan1
Tue Aug 12 2014, 03:59 PM
thanks, finally advise about brightness issue, I'm...