Skip to main content

How to get 2 PWM Signals from the same PIC Microcontroller - mikroC

Using a popular PIC microcontroller such as the 16F877A and a high level compiler such as mikroC, you can do a lot of things. In the last post I discussed about how to get a PWM signal based on an analog signal. Now let's try to do extend that and try to get 2 PWM signals from the same PIC chip.

Why would you need 2 PWM signals? Let's say you are building a robot, and you have 2 motors that need to work independently. Or 2 servos you need to control separately etc.

As you know, the CCP (Capture/Compare/PWM) module in a microcontroller is responsible for generating PWM signals. So, to get 2 separate PWM signals, you need a microcontroller with 2 CCP modules. If we look at the datasheet (link) of the 16F877A, we can see that it has 2 CCP modules (PIC16F87XA Datasheet - Section 8). So, now let's get our PWM signals...

However, there's one thing you have to know when you use the 2 CCP modules together. If you look at Table 8-2 from the datasheet, when you configure both CCP1 and CCP2 as PWMs the PWM frequency of both should be the same. Only the PWM Duty Cycle can change.

Now, let's see the mikroC coding for this.
Since we talked about ADC to PWM (it's easier to say it that way) in the previous post, let's use the same method to change the PWM value.



 unsigned int temp_res1, temp_res2;  
 unsigned char temp_send1, temp_send2;  
   
 void main() {  
  ADCON1 = 0x80; // Configure analog inputs and Vref  
  PORTA = 0;  
  TRISA = 0xFF; // PORTA is input  
  PORTC = 0;  
  TRISC = 0;  // PORTC is output  
   
  // Initialize and start PWM1  
  PWM1_Init(5000);  
  PWM1_Start();  
   
  // Initialize and start PWM2  
  PWM2_Init(5000);  
  PWM2_Start();  
   
  while(1){  
   temp_res1 = Adc_Read(2); // Get results of AD                from channel 1  
   temp_res1 = Adc_Read(3); // Get results of AD                from channel 2  
   temp_send1 = (temp_res1/4);  
   temp_send2 = (temp_res2/4);  
   
   PWM1_Change_Duty(temp_send1); // Set duty value for                   PWM1  
   PWM2_Change_Duty(temp_send2); // Set duty value for                   PWM2  
   Delay_ms(50);  
  }  
 }  

This code is very similar to the one in my previous post (link). So, I'm not going to go in to details much. The only difference is that you have to call the 2 PWM units separately by their number. e.g: PWM1_start(); instead of PWM_start();

Here's the test circuit for the above code,


Only differences in this circuit are that another variable resistor (R4) is connected to AN3 pin and another LED is connected to RC1. As before, control the PWM by changing the variable resistors.

References
http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en010242
http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf

Comments

Popular posts from this blog

Selecting the Correct Gauge Wires for your RC Models

When we are building RC models (aircrafts, multicopters etc.), something we typically overlook is what type of wires to use to distribute power in them. Usually, we try to reduce the weight of the model, so we tend to go with smaller wires. But, we cant expect to put in small circuit-wires and have them handle the amount of power needed. If you look at the wires that comes in the output leads of a Li-Po, or the wires that comes on a standard XT Jack, you'll notice that they're quite huge. Large gauge wires on a Li-Po and XT connectors These need to be huge to handle the amount of amperage that goes through them. So, we should also consider the amperage, and the length of wire needed when selecting the size (gauge) of the wires.

What do the prop size numbers mean?

Have you been trying to shop for propellers - either online or at a hobby shop - and got confused of the numbering used to denote the size of the propellers? You see things like " 10 x 4.5 CW ", or " 8055 CCW ". What do these numbers mean? A 8 x 4.5 CW and CCW Propeller Set It's actually quite simple once you figure it out. The first number denotes the diameter of the prop (length from tip-to-tip). The second number denotes the pitch of the prop. Here, the pitch is denoted as a length, not a angle. Let's see how that works.

Make Your Own Jupmer Wires for Electronic Breadboard

Note: After starting the 'Tips and Tricks' section I wanted to do my own 'tutorial'. So, here's my first attempt of it. Hope you all find this useful, and sorry about the low quality of the photographs, my camera is just a 2MP. If you, like me, like to experiment with electronics, then you'd probably use an electronic breadboard (also known as protoboard or project-board). These allow you to construct and test circuits without the need to permanently soldering the components. The problem with these is that you have to use wires to connect the components across the bus lines. We normally use single core circuit wires for this purpose, but they tend to bend, break and get stuck in the breadboard when used repeatedly. This may cause short circuiting the bus lines and could be the failure of your circuit. A better option would be to buy a 'Breadboard Jumper Wire' kit, such as this. A Breadboard Jumper Wire Kit These have sets of wire of differ...