0% found this document useful (0 votes)
4 views

How to Generate PWM in 8051 Microcontroller - The Engineering Projects

The document provides a tutorial on generating Pulse Width Modulation (PWM) signals using the 8051 Microcontroller, which lacks dedicated PWM pins. It explains the concept of PWM, its applications in controlling devices like DC motors and LED brightness, and includes detailed code and circuit design for implementation. The tutorial also covers the use of Timer0 interrupts to manually create PWM signals and offers simulation results to demonstrate the functionality.

Uploaded by

Peter Chang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

How to Generate PWM in 8051 Microcontroller - The Engineering Projects

The document provides a tutorial on generating Pulse Width Modulation (PWM) signals using the 8051 Microcontroller, which lacks dedicated PWM pins. It explains the concept of PWM, its applications in controlling devices like DC motors and LED brightness, and includes detailed code and circuit design for implementation. The tutorial also covers the use of Timer0 interrupts to manually create PWM signals and offers simulation results to demonstrate the functionality.

Uploaded by

Peter Chang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Search Pricing

Contribute Wishlist Cart Help

Login Signup

Home Blog 8051 Microcontroller Projects How to Generate PWM in 8051 Microcontroller

David watson
Electrical Engineer How to Generate PWM in 8051 Microcontroller
London , UK
In today's post, I am going to share How to generate PWM in 8051 Microcontroller. PWM is an abbreviation ....

8051 Basics Posted at: 01 - Feb - 2016 Category: 8051 Microcontroller Projects Author: syedzainnasir 9 Comments

8051 Microcontroller Intro

Simple LED Blinking

LCD Interfacing

Keypad Interfacing

Seven Segment Display

8051 Functions

PWM Signal Generation

Serial Communication

Timer Interrupt

8051 Projects Hello everyone, hope you all are fine and having fun with your lives. In today's
post, I am going to share How to generate PWM in 8051 Microcontroller. PWM is
Simple Calculator
an abbreviation of Pulse Width Modulation and is used in many engineering

Electronic Quiz Project projects. It is used in those engineering projects where you want an analog
output. For example, you want to control the speed of your DC motor then you
Interrupt based Digital need a PWM pulse. Using PWM signal you can move your motor at any speed
Clock
from 0 to its max speed. Similarly suppose you wanna dim your LED light, again
8051 Misc you are gonna use PWM pulse. So, in short, it has numerous uses. If you are
working on Arduino then you should read How to use Arduino PWM Pins.
8051 Projects

PWM, as the name suggests, is simply a pulse width modulation. We take a pulse and then we modulate its width and make it
ARDUINO
small or big. Another term important while studying PWM is named duty cycle. The duty cycle shows the duration for which the
Raspberry Pi PWM pulse remains HIGH. Now if the pulse remains high for 50% and LOW for 50% then we say that PWM pulse has a duty
cycle of 50%. Similarly, if the pulse is HIGH for 70% and Low for 30% then it has a duty cycle of 70%.
ESP32
Most of the microcontrollers have special pins assigned for PWM as in Arduino UNO it has 6 PWM pins on it. Similarly, PIC
Microcontrollers also have PWM pins but unfortunately, the 8051 Microcontroller doesn't have this luxury means there are no
special PWM pins available in 8051 Microcontroller. But PWM is necessary so we are going to manually generate the PWM
pulse using Timer0 interrupt. So, before reading this tutorial you must first read How to use Timer Interrupt in 8051
Microcontroller so that you understand the functioning of Timer Interrupt. Anyways, let's get started with the generation of PWM
in the 8051 Microcontroller.

How to Generate PWM in 8051 Microcontroller ???

You can download both the simulation and the programming code for PWM in 8051
Microcontroller by clicking the below button:
Download PWM Code & Simulation

First of all, design a simple circuit as shown in the below figure:

Now what we are gonna do is we are gonna generate a PWM pulse using timer0 interrupt

and then we are gonna send it to P2.0.

I have attached an oscilloscope on which we can easily monitor this PWM pulse and can

check whether it's correct or not.


Code In Keil Uvision 3

Now, copy the below code and paste it into your Keil uvision software. I have used Keil

uvision 3 for this code compiling.


#include<reg51.h>

// PWM_Pin

sbit PWM_Pin = P2^0; // Pin P2.0 is named as PWM_Pin

// Function declarations

void cct_init(void);

void InitTimer0(void);

void InitPWM(void);

// Global variables

unsigned char PWM = 0; // It can have a value from 0 (0% duty cycle) to 255 (100% duty cycle)

unsigned int temp = 0; // Used inside Timer0 ISR

// PWM frequency selector

/* PWM_Freq_Num can have values in between 1 to 257 only

* When PWM_Freq_Num is equal to 1, then it means highest PWM frequency

* which is approximately 1000000/(1*255) = 3.9kHz

* When PWM_Freq_Num is equal to 257, then it means lowest PWM frequency

* which is approximately 1000000/(257*255) = 15Hz

* So, in general you can calculate PWM frequency by using the formula

* PWM Frequency = 1000000/(PWM_Freq_Num*255)

*/

#define PWM_Freq_Num 1 // Highest possible PWM Frequency

// Main Function

int main(void)

cct_init(); // Make all ports zero

InitPWM(); // Start PWM

PWM = 127; // Make 50% duty cycle of PWM

while(1) // Rest is done in Timer0 interrupt

{}

// Init CCT function

void cct_init(void)

P0 = 0x00;

P1 = 0x00;

P2 = 0x00;

P3 = 0x00;

// Timer0 initialize

void InitTimer0(void)

{
TMOD &= 0xF0; // Clear 4bit field for timer0

TMOD |= 0x01; // Set timer0 in mode 1 = 16bit mode

TH0 = 0x00; // First time value

TL0 = 0x00; // Set arbitrarily zero

ET0 = 1; // Enable Timer0 interrupts

EA = 1; // Global interrupt enable

TR0 = 1; // Start Timer 0

// PWM initialize

void InitPWM(void)

PWM = 0; // Initialize with 0% duty cycle

InitTimer0(); // Initialize timer0 to start generating interrupts

// PWM generation code is written inside the Timer0 ISR

// Timer0 ISR

void Timer0_ISR (void) interrupt 1

TR0 = 0; // Stop Timer 0

if(PWM_Pin) // if PWM_Pin is high

PWM_Pin = 0;

temp = (255-PWM)*PWM_Freq_Num;

TH0 = 0xFF - (temp>>8)&0xFF;

TL0 = 0xFF - temp&0xFF;

else // if PWM_Pin is low

PWM_Pin = 1;

temp = PWM*PWM_Freq_Num;

TH0 = 0xFF - (temp>>8)&0xFF;

TL0 = 0xFF - temp&0xFF;

TF0 = 0; // Clear the interrupt flag

TR0 = 1; // Start Timer 0

I have added the comments in the above codes so it won't be much difficult to understand.
If you have a problem then ask in the comments and I will resolve them.

Now in this code, I have used a PWM variable and I have given 127 to it as a starting

value.
PWM pulse varies from 0 to 255 as it's an 8-bit value so 127 is the mid-value which means

the duty cycle will be 50%.

You can change its value as you want it to be.

Proteus Simulation Result

So, now when you upload the hex file and run your simulation then you will get below

results:

Now you can check in the above figure that the duration of HIGH and LOW is the same

means the pulse is HIGH for 50% and LOW for the remaining 50% cycle.

Now let's change the PWM duty cycle to 85 which is 1/3 and it will generate a PWM pulse

of 33% duty cycle. Here's the result:


Now you can easily compare the above two figures and can get the difference. In the

above figure now the duty cycle has decreased as the HIGH timing of the pulse is now
reduced to 1/3 and pulse is LOW for 2/3 of the total time.
That's all, for today. That's how we can generate PWM in 8051 Microcontroller. Will meet you guys in the next tutorial. Till then
take care !!! :)

為每一位工程師所打造

Ad Tektronix

Tags: pwm in 8051, 8051 pwm, 8051 pwm code, pwm in 8051 microcontroller, 8051 and pwm,

555 Timer Projects Interrupt Based Digital Clock with 8051 Microcontroller

Previous Post Next Post


Author Image

-Website Author

Syed Zain Nasir


syedzainnasir
I am Syed Zain Nasir, the founder of The Engineering Projects (TEP). I am a programmer since 2009 before that I just search things, make
small projects and now I am sharing my knowledge through this platform. I also work as a freelancer and did many projects related to
programming and electrical circuitry. My Google Profile+

Follow

Get Connected

Name *

Email *

Website

Add A Comment
Type a Comment Here David....

Post Comment

Comments on ‘’ How to Generate PWM in 8051 Microcontroller ‘’ ( 9 )


0

kg Says:
August 18, 2021 08:21 PM

Why PWM Frequency = 1000000/(PWM_Freq_Num*255). What is the reason for 1000000? I can see
the clock is 12 MHz not 1 MHz.

Reply 10 0

uzair Says:
February 23, 2019 12:55 PM

hello sir actually i wanted to produce pwm wave and i wanted to increse or decrease the duty cycle
based on the status of input pins , whether the switch is pressed on input pins or not like that , so how
can we do it???

Reply 10 0

stalini Says:
June 14, 2018 06:21 AM

sir,how to control the brightness of the led using pwm?

Reply 10 0

syedzainnasir1 Says:
June 14, 2018 02:52 PM

Simply Follow this tutorial and with your PWM Pin connect a 10k ohm resistor and then an
LED and make it GND on the other side.

Reply 10 0

stefan Says:
July 18, 2017 03:12 PM
I am doing a project on street light sensing system, i need the led switch on in a dim brightness at
night and become very bright when detected a vehicle, i haven't learn this before,please help me?

Reply 10 0

zionchow Says:
June 12, 2017 12:57 AM

hi, do you have 8051 assembly code?

Reply 10 0

parthipan Says:
March 18, 2017 08:49 PM

sir i have to varymy duty cycle according to ldr to get required output in led, so how can i get that type
of program which will change interms of ldr and how the code will modified then?

Reply 10 0

cristophertingcang Says:
September 14, 2016 12:55 AM

Hi Sir, If I am going to use this PWM program to control my DC motor, how would the circuit looks like
in Proteus. When I tried to connect the DC Motor to the same pin in simulation, the DC Motor just
rotate and rotate and the status of the microcontroller pin is gray (undefined). Can you please provide
me the schematic diagram. Thanks, Cris

Reply 10 0

cristophertingcang Says:
September 16, 2016 04:47 AM

Hi sir, I already got the answer of the above question. I used a driver (ULN2003) to drive my
DC Motor. In the code above which I show below, if(PWM_Pin) // if PWM_Pin is high {
PWM_Pin = 0; temp = (255-PWM)*PWM_Freq_Num; TH0 = 0xFF - (temp>>8)&0xFF; TL0 =
0xFF - temp&0xFF; } else // if PWM_Pin is low { PWM_Pin = 1; temp =
PWM*PWM_Freq_Num; TH0 = 0xFF - (temp>>8)&0xFF; TL0 = 0xFF - temp&0xFF; } Can you
explain this in detail? Thanks, Cris

Reply 10 0

Subscribe Now !!!

Learn Free Pro Tricks


Top PCB Design Service
RSS YT FB Pin

Receive Quality Tutorials Straight in your


Inbox by submitting your Email ID below.

Join Us !!!

Join Our Fb Groups !!!


ARDUINO PROTEUS
2K+ 3K+

THE ENGINEERING PROJECTS


“A platform for engineers & technical professionals to share their engineering
projects, solutions & experience with TEP Community & support open source.”

Newsletter
Get a weekly notification of great articels

Your email address

Recent Post
An Engineer's Guide to Intellectual Property Laws In 2023
01 - Aug - 2023

Raspberry Pi Pico Library for Proteus


01 - Aug - 2023

ESP32 Library for Proteus


31 - Jul - 2023

NodeMCU Library for Proteus


31 - Jul - 2023

Current Sensor Library for Proteus


30 - Jul - 2023

Introduction to FR4 PCB Fabrication - Complete Guide


29 - Jul - 2023

Popular Tutorials Series

Respberry pi
ESP32
Arduino
PIC Microcontroller
8051 Microcontroller
STM32
Asp.Net Core
MATLAB
LabVIEW
Proteus
PLC
Contact Us

Skype
theenggprojects

Email

[email protected]

[email protected]

Follow us
Follow us on social media

TERMS & CONDITIONS PRIVACY & POLICY DISCLAMIER CONTACT US


Copyright © 2020 TheEngineeringProjects.com. All rights reserved.

You might also like