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.

Li-Po Batteries Explained - Part 2

I explained about Li-ion batteries (the predecessor of Li-Po) in my earlier post ( Li-Po Batteries Explained - Part 1 ). Now let's see what these 'Lithium Polymers' are. Lithium-Polymer Batteries - The real ones Li-Po batteries - also known as Li-Poly, Pi-Pol, PLI and PLI - are a type of rechargeable batteries that uses same types of electrodes as the Li-ion batteries. The difference is with the electrolyte it uses. Instead of the liquid solvent used in Li-ions, Li-Po uses a polymer electrolyte. In the original design, which dates back to the 1970's, the electrolyte is a dry, plastic-like, thin film. It doesn't conduct electricity, but allows the exchange of ions between the electrodes. Theoretically, with this design, batteries could be made very thin (less than 1 millimeter) and to a variety of form factors. And because of the electrolyte being dry, the manufacturing process can be simplified and the safety of the battery will be higher because of no risk in