8051 PWM C Program with 50 percent duty cycle

PWM(pulse width modulation) generation guide using 8051.

8051 PWM

8051 PWM generation

What is PWM?

PWM(pulse width modulation) is a technique of generating pulses(square wave) having a specific on time and off time. Main use of PWM is to control power mostly in motor speed control. PWM is also used to perform DAC(digital to analogue) operation.

How to generate PWM in 8051?

To generate PWM in 8051 you have to enable PWM in 8051 microcontroller. PWMn = 1 enables the CEXn pin to be used as a pulse width modulation output.

You can use below program for 8051 PWM generation.


#include <at89c51xd2.h>
sbit CEX0 = 0x93;

void timer0()
{
TMOD=0x02;
TH0=0xFF;
TCON = 0x30;               // Start Timer0
}

void pwmset()
{
CMOD = 0x04;            // Clock from Timer0 Overflow
CCAPM0 = 0x42;      // PCA0 : Set to PWM Mode
CCON = 0x40;            // PCA0 : Output Enable &amp; Run PCA
}

void main()
{
timer0();
pwmset();
CCAP0H = 0x7F;      //set value between 0x00 to 0xFF 0x7F is for 50% duty cycle
while (1);
}

You can use above program to control speed of a motor. You have to add two buttons with functionality on increasing and decreasing CCAP0H value. By varying duty cycle of PWM you can control speed of  a motor.

Incoming search terms:

Amazon Auto Links: the template could not be found. Try reselecting the template in the unit option page.

by

Comments

  1. likes vs instagram says:

    Ӊi mmy lօved one! I wish to say that this ɑrticle is amaziոg, great written and cߋme with approximatelʏy all vital infos.
    I աould like to seee еxtra posts like this .

  2. matrix says:

    Hello, where to connect Motors??(pin number), do we use L293D driver??
    how to interface buttons, plss tell in detail.

    • Dear Matrix,

      You can use mosfet. Connect mosfet with PWM pin.

      For button you have to connect it with ground as described below (make sure button pin contains internal pullup or connect external pullup)

      — (uC pin) — (button) — ground(GND)

  3. matrix says:

    better send circuit diagram with explanations.

  4. Arul says:

    I’m getting these errors while executing the PWM based sine wave generation program in kEIL uVision 4.0
    error C231: ‘ET0’ : redefinition
    error C231: ‘EX0’ : redefinition

    what should I do to overcome these errors??
    Please help me

  5. Mehboob says:

    how to generate pwm signal using UART in 8051,, please send in c language,,,

  6. Mehboob says:

    can you send me source code of pwm using the UART in c language ???

Speak Your Mind

*