PWM pulse generation using PIC12F675 micro-controller

Published by MKDas on

To generate PWM(Pulse Width Modulation) both analog and digital circuits can be used. But using a micro-controller is much easier than other methods. To generate PWM, the micro-controller has a built-in PWM module. But if there is no such PWM module then how can you generate PWM? Yes, in this article we are going to learn how we can generate a PWM signal if there is no such module built-in. Let’s start out PWM pulse generation using PIC12F675.

Disclaimer: Electricity is always dangerous. Skill required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act. This website contents are also copyright protected. Anything copied and directly posted on your website and claiming it as yours is prohibited and nonsensical. The author published all the articles as open source to help you to make your project and learning purpose only. Learn and make one for yourself. If need any help feel free to ask the author. The author will be helpful to you. Thanks.

What is a PWM pulse?

PWM is a method of reducing the average power delivery by an electrical signal. You can read about PWM from wiki.

As you see, the Pulse duration is the main in PWM. Here, the duty cycle is measured as the (total on time)/(Total on time + Total off time). This duty cycle can be varied keeping the total time (ON+OFF) fixed. That means the frequency is fixed. If the duty cycle is 0% that means there is not on time, only off time. And duty is 100% means there is no off time, only on time. Besides, a 50% duty cycle means 50% on time and 50% off time.

How can we generate PWM puse using PIC12F675?

Usually, there is a built-in module in micro-controllers. But not all the micro-controllers have this feature. When there is such a module, you can not generate PWM using the built-in modules. But you can use some programming skills to generate a PWM signal.

Using timer interrupt, we can generate a PWM signal from any micro-controller. The method is very simple. First, use a timer interrupt to call interrupt say 100 times per second. Then, count each interrupt and set it zero at 100, besides creating a condition that until a duty (say 40 for 40%) the output is high, then the output is 0 until the counter is 0 again. So what will happen? Until 40 the pulse will be on and the rest 60 pulses will be off. So, we can get a 40% duty cycle at a frequency 1Hz. Now to increase this frequency simply increase timer interrupt speed i.e. reduce time.

Let’s do it in programming. but before that, we need a test circuit.

Diagram of our PWM pulse generation circuit:

PWM generation using PIC12F675

Programming:

/*******************************************************************************
Program for, PWM generation using PIC12F675
Program Written by_ Engr. Mithun K. Das; mithun060@gmail.com
MCU:PIC12F675; X-Tal: 4MHz(internal). Compiler: mikroC pro for PIC v7.6.0
Date: 30-03-2021; © labprojectsbd.com
*******************************************************************************/
#define  pwm_pin     GP0_bit
unsigned int scale=0,duty=0;

void InitTimer0()
{
  OPTION_REG     = 0x80;
  TMR0           = 200;
  INTCON         = 0xA0;
}


void Timer_interrupt() iv 0x0004 ics ICS_AUTO
{
  if (TMR0IF_bit)
  {
    TMR0IF_bit         = 0;
    TMR0         = 200; // change this value [0-255] to change the total frequency. 
    scale++;
    if(scale<duty)
    {
        pwm_pin = 1;
    }
    else
    {
        pwm_pin = 0;
    }
    if(scale>100)scale=0;
  }
}


void main()
{
  TRISIO = 0b00000000;//set I/O
  GPIO = 0x00;
  CMCON = 0x07;//comparator off
  ANSEL = 0b00000000;
  InitTimer0();
  while(1)
  {
           
         duty = 90;

  }//end of while(1)
}//end of void main

//End

Here, the duty can be set from 1 to 99. If you understand the concept and are smart to utilize this code, you can generate a range of PWM signals using this small micro-controller.

Result:

PWM output from PIC12F675
Duty = 10
PWM output from PIC12F675
Duty = 50
PWM output from PIC12F675
Duty = 90

Conclusion:

After reading this PWM pulse generation using PIC12F675 article, I hope you can now generate your own PWM signal using similar micro-controllers too. Only one this you must keep in mind that, as you are using timer interrupt at high speed, other work will be slow. You must select suitable timing for your project between the general work and timer interrupt speed.

I hope this project was helpful to you. If you make one for yourself, it will be a great pleasure for me. Anywhere you need help, let me know. Please share this project and subscribe to my blog. Thank you.


MKDas

I'm Mithun K. Das; B.Sc. in EEE from KUET, Bangladesh. Blog: https://labprojectsbd.com. "First, electronics was my passion, then it was my education, and finally, electronics is now my profession." I run my own electronics lab, M's Lab (https://mlabsbd.com). Where I work with the creation of new products from ideas to something in real life. Besides this is my personal blog where I write for hobbyists and newcomers in the electronics arena. I also have a YouTube channel where I publish other helpful videos, you can find the link inside the articles. I always try to keep it simple so that it becomes easy to understand. I hope these will help them to learn electronics and apply the knowledge in their real life.

2 Comments

Asimiyu · March 31, 2021 at 2:37 pm

Hummm. Good work sir. I always learn new…new… things from your blog. I am very happy to see this. You are BLESSED ALREADY. continue your good work. How I wish I have the money to donate to your company. I love anything knowledge!!!.

    Mithun K. Das · April 1, 2021 at 3:12 am

    Thanks. Subscribe if you did not yet for more interesting articles.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *