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.
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
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
Post a Comment