View previous topic :: View next topic |
Author |
Message |
safwat
Joined: 08 Feb 2015 Posts: 20
|
Operating CCP1 and CCP2 of PIC 16F877A using C programming |
Posted: Sun Feb 08, 2015 2:56 am |
|
|
How can i operate both CCP1 and CCP2 of PIC 16F877A at the same time. I want to use both together. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Sun Feb 08, 2015 3:02 am |
|
|
Do search the forum. This has been answered literally thousands of times. For example (in the last couple of weeks):
<http://www.ccsinfo.com/forum/viewtopic.php?t=53474> |
|
|
safwat
Joined: 08 Feb 2015 Posts: 20
|
PWM phase shifting |
Posted: Mon Feb 16, 2015 12:16 pm |
|
|
a new topic
I want to generate two PWM signal using CCP1 and CCP2. The catch is CCP 2 signal should be phase shifted with CCP 1. If i generate 20khz PWM signal (for both CCP1 and CCP2) then the period is 50 microsecond. I want the CCP 2 signal lagging by 25 microsecond with CCP 1. A 50% duty cycle for both signal is okay.
If possible please provide me with a code. I am using PIC 16F877A. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Mon Feb 16, 2015 1:34 pm |
|
|
You can't.
You need to shift to one of the more modern PIC's that allows different timers for the PWM's. With these you can zero one timer, wait for the period required between the two PWM's, and then zero the other.
On the simpler PWM, you need to understand that the waveforms will always start when the timer jumps to zero. Since both PWM's use the same timer, the waveforms will always start at the same point.
The PWM is designed just to give easy modulation of the pulse width. Nothing else. |
|
|
safwat
Joined: 08 Feb 2015 Posts: 20
|
|
Posted: Mon Feb 16, 2015 1:55 pm |
|
|
is there any other way to generate this kind of signals using this microcontroller without using the CCP modules? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Mon Feb 16, 2015 2:57 pm |
|
|
CCP, is the easiest.
It depends though on frequency. Low frequency signals can be done using counters in a timer interrupt. There are examples here and in the code library generating servo control signals this way. However if the frequency is at all high, the chip will end up doing nothing else, and then running out of time....
There are literally dozens of later PIC's that offer multiple timers for the PWM's. You are using a very old chip, that can be replaced by newer chips that are cheaper, and do this. A chip like the 16F1784, has both two standard PWMs, and the PSC module, which has it's own timer, so two signals can be generated with a shift between them. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Mon Feb 16, 2015 3:27 pm |
|
|
Well, the 'old school' way is to 'bit bang' the PWM frames.
Now, this is possible, depending on the PWM frequency you need as well as 'what else' the PIC has to do.
CCS supplies a simple 'pattern generator' (ex_patg.c) in the examples folder. It's a easy example to understand though it doesn't have a lot of 'steps' or 'resolution'.
In your case you'd need to 'insert' the PWM code for channel 'B', inside the code for channel 'A', with a 50us delay
If you start with a 4MHz xtal, you'll get 1us for most PIC instructions.Delay_cycles(50) would give you a 50 us delay.
It is somewhat complicated but that's the way us old guys did it 25+ years ago.
You NEED to read and understand the PIC's instruction set as well as CCS C functions like output_high(), delay_us(),etc.
You need to decide on 'steps'( the number of on times as well as off times) for each channel,is 'B' always the same as 'A', just delayed 50us OR independent. That makes a big difference in the code.
As Mr. T points out, the newer PICs can do this in the 'background' far cheaper and easier than the obsolete 877 BUT yes, it can be done without the CCP.
Jay |
|
|
safwat
Joined: 08 Feb 2015 Posts: 20
|
|
Posted: Tue Feb 17, 2015 11:46 am |
|
|
I understand what you guys mean about taking an advance technology and throwing out the old one. Problem is my prof is little bit old school kind of guy. He does not want to change. He is giving me all sorts of assignments using pic 168F77A. Its getting too difficult for me.
Anyways, I am going to try using one CCP module to generate PWM and generate another square wave using port B (this signal will have the same period as the PWM). I will make a 25us delay (using delay_us(25)) on the square wave. Now my question is that a possible idea in practical situation? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Tue Feb 17, 2015 12:34 pm |
|
|
If the 2nd signal 'follows' the other, though delayed by 25us, simply use the output of the 1st(from the CCP output pin) as an input to another pin, test it, then set/clear the 2nd pin as needed.
Obviously there are delays but we don't know how fast a signal you need to generate.
Frankly, if he's 'old school', I'd just bitbang the whole project. It is simple, easy and takes maybe 3-4 hours ONCE you've defined the parameters of the project. Draw the required waveforms on paper. Get output #1 working, then add the delay and output #2.
As I posted, ex_patg.c should help you.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Tue Feb 17, 2015 12:57 pm |
|
|
One obvious option.
Feed the CCP1 output, into one of the interrupt on change inputs.
When this interrupts, read the input, and set an output to match it.
Given that you are talking about a 4MHz PIC16, you will get about a 30uSec delay.
Alternatively, just poll it.
It really does depend massively on what 'else' the chip has to do. If it only has to generate the two pulses, then just simple bit-banging could be written in a couple of seconds (Rather than a couple of hours!). What it couldn't do, is keep on generating the pulses at a steady rate, if the chip is doing other things. If you need to do this, then the problem gets harder. |
|
|
safwat
Joined: 08 Feb 2015 Posts: 20
|
|
Posted: Sat Feb 21, 2015 10:43 am |
|
|
does the PWM modules of PIC18F4431 have different timer? Is it possible with this PIC to generate two PWM with phase shift? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Sat Feb 21, 2015 1:15 pm |
|
|
There are multiple ways of doing this with the 4431.
First use one CCP, and for the second signal, the power control module. This module uses it's own timer so could be started at a different time.
Second just use the power control module only. One feature of this is 'programmable dead time', allowing a second output to change a programmed time after the first. Since you can also program the polarity of the outputs, you can use this to give a programmed shift. If you look at the waveforms in figure 18-18, and then imagine that you inverted one of these waveforms, you have the required time shifted waveforms. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Feb 21, 2015 5:55 pm |
|
|
Without specs for the application almost ANY PIC can do the job....no one here can say 'this' is the 'perfect' solution without knowing the details.
I just got some 12F1572 samples as it has 3 16bit PWMs and a UART, they might work great for your application IF we had more details...
Jay |
|
|
safwat
Joined: 08 Feb 2015 Posts: 20
|
|
Posted: Sun Feb 22, 2015 9:23 am |
|
|
Ttelmah and temtronic,
i want to generate a power PWM signal in pic18f4431. i am very much new to this PIC and power PWM.i want to generate a 20khz pwm (using a crystal 20Mhz) frequency with 50 percent duty in one of the module (as i learned there are four modules with 8 pins complementary). i have seen the C help but could not understand much of it so providing code will be helpful.
temtronic hopefully tomorrow i will be able to provide you with some details.
and guys sorry for asking so many question but i will keep asking until i understand everything. sorry again
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Sun Feb 22, 2015 10:19 am |
|
|
You have changed tack.
A little while ago, you wanted two PWM's running identical frequencies, with a phase shift between the signals. This is what we have been trying to answer, now you want to use the power PWM.
As always, start with the data sheet, and the CCS examples. EX_POWER_PWM.c shows setting up one PWM pair on the 2431, in complementary mode, and allowing you to set the frequency and duty cycle.
Sequence with using the PIC, is always a combination of:
1) Data sheet. Work out what the peripherl can actually do.
2) Examples.
3) Manual.
3) Header file for the PIC.
You need to use _all_. |
|
|
|