# PIC18 PWM ouput sync

#### ccurtis

##### Well-Known Member
Hey guys, any idea why the PWM output from a PIC18 is not synchronized with a strobe output I generate on a port pin on the same device? The timer driving the PWM is clocked with the instruction clock (fosc/4). But if I put the two outputs up on my scope and sync on the strobe output, the edges of the PWM output wander all over the place. The PWM output is set for 50% duty cycle and fixed frequency. I'm using the PWM output as a clock signal for an external chip along with the strobe, and they need to be in sync.

Last edited:

#### Les Jones

##### Well-Known Member
There will be many instruction cycles for every cycle of the PWM waveform so syncing on the instruction cycle will trigger the scope at many different points on the PWM waveform. If you sync on the PWM waveform you will see the PWM waveform on one trace and a large number ( 2^10 = 1024 ) of instruction cycles on the other trace.

Les.

#### gophert

##### Well-Known Member
Hey guys, any idea why the PWM output from a PIC18 is not synchronized with a strobe output I generate on a port pin on the same device? The timer driving the PWM is clocked with the instruction clock (fosc/4). But if I put the two outputs up on my scope and sync on the strobe output, the edges of the PWM output wander all over the place. The PWM output is set for 50% duty cycle and fixed frequency. I'm using the PWM output as a clock signal for an external chip along the the strobe, and they need to be in sync.
I'm confused. If you have a PWM frequency of f(osc)/4 then you have PWM resolution of 1 bit. Or are you just expecting the PWM to start on every x rising edge of the f(osc)/4 clock? Or, what exactly are you upset about?

#### ccurtis

##### Well-Known Member
There will be many instruction cycles for every cycle of the PWM waveform so syncing on the instruction cycle will trigger the scope at many different points on the PWM waveform. If you sync on the PWM waveform you will see the PWM waveform on one trace and a large number ( 2^10 = 1024 ) of instruction cycles on the other trace.

Les.
Yeah, that makes sense. Since the PWM output and instruction to generate the strobe are developed from the same instruction clock, when I put those two up on the scope, shouldn't those signals be in sync too?

Last edited:

#### ccurtis

##### Well-Known Member
I'm confused. If you have a PWM frequency of f(osc)/4 then you have PWM resolution of 1 bit. Or are you just expecting the PWM to start on every x rising edge of the f(osc)/4 clock? Or, what exactly are you upset about?
The PWM output (square wave) runs continuously, after I set up its registers. When I trigger on either of the two signals (strobe or PWM clock), I expect the other to be stable with regard to the time between the edges of the two signals. I expect that due to program branches, and such, the strobe might not be periodic, but when it occurs is should occur at a time with respect to a PWM signal edge that is constant, no?

This is a PIC18F25K22 I'm working with.

#### Dan Soze

##### Member
There is a lot of variation in how the PWM function is implemented in the PIC18 family of controllers.

This results in the descriptions of how to setup these resources being more than a little confusing.

The clock source for the PWM period counter is most often derived from the system oscillator. For most PIC18F controllers this is FOSC/4, there are some now that can be clocked form FOSC or even another asynchronous clock generated within the controller.

The situation is more comprehensible with the older controllers like the PIC18F4520 or the PIC18F45K20. In these controllers the PWM period timer is the 8-bit TIMER2 and is only clocked from FOSC/4. The PWM period will always begin on the same edge of the instruction clock. Unless your application code uses a number of instruction cycles that is an exact multiple of the period counts there is no synchronization between PWM periods and instruction execution.

In general it is a bad idea to design an application that requires a GPIO event asserted from code to be synchronized to a hardware generated PWM pulse.

#### ccurtis

##### Well-Known Member
The clock source for the PWM period counter is most often derived from the system oscillator. For most PIC18F controllers this is FOSC/4, there are some now that can be clocked form FOSC or even another asynchronous clock generated within the controller.

The situation is more comprehensible with the older controllers like the PIC18F4520 or the PIC18F45K20. In these controllers the PWM period timer is the 8-bit TIMER2 and is only clocked from FOSC/4. The PWM period will always begin on the same edge of the instruction clock. Unless your application code uses a number of instruction cycles that is an exact multiple of the period counts there is no synchronization between PWM periods and instruction execution.

In general it is a bad idea to design an application that requires a GPIO event asserted from code to be synchronized to a hardware generated PWM pulse.
Thanks. I am using the Fosc/4 clock for the PWM (Timer 2), which I understand is also supposedly the clock that steps the program counter and thus to execute the instruction to generate the strobe signal. It's not so much that I want to generate the strobe on a particular cycle of the PWM output (square wave), but I do want the time between the edges to be constant ("synchronized"). So far, I don't understand why they are not locked,. I get that if I were to use the system clock (Fosc) to trigger Timer2, which is not possible for Timer 2, perhaps they would not be synchronized, although I don't really understand why that would be either, since I would think Fosc/4 is just a division of Fosc.

#### Pommie

##### Well-Known Member
Note that some instructions take multiple cycles (up to 3) and so can prevent perfect syncing.

Mike.

#### ccurtis

##### Well-Known Member
Note that some instructions take multiple cycles (up to 3) and so can prevent perfect syncing
So then, at worst, the PWM edge that follows the strobe changes from a rising edge to a falling edge, occasionally, which is okay by me. I just want the edges to be synced, whatever its direction is.

#### gophert

##### Well-Known Member
What are your pwm setup parameters?

Also, the code you are using for a pass-through of the own frequency.

#### ccurtis

##### Well-Known Member
For PWM setup:

ccptmrs0 = %00001000 'Assign Timer 4 to CCP2 PWM (pin 12)
pr4=80 '198 KHz 'Timer 4 load value; CCP2 PWM freq= Fosc/((PR4+1)*4* timer prescale), Fosc is 64 MHz
ccpr2L= %00110000 : ccp2con = %00101100 'CCP2 is PWM mode, 50% DC
t4con=%00000100 'Timer 4, postscaler=1, prescale=1, timer on

PWM generates 198 KHz square wave. Checks okay.

Strobe is just a write to a GPIO port pin in my code when needed. If I sync a scope on the strobe and look at PWM signal, the PWM edges eventually take on every offset delay value from the strobe edge possible, so it's not a matter of edges slipping from one increment to another.

Earlier, I mistakenly said Timer 2, but it's Timer 4. The PWM is CCP 2 driven by Timer 4. Timer 4 is driven by the instruction clock, Fosc/4.

I am using the PICs internal clock at 16 Mhz, and the internal 4X PLL to give Fosc (system clock) of 64 MHz.

Thanks for looking.

#### Pommie

##### Well-Known Member
How do you time the write to the port pin? What do you check to decide now is the time?

Why can't you trigger the scope on the PWM signal and use the x shift to view the next one?

Mike.

#### dougy83

##### Well-Known Member
If you want to sync to the PWM, you can use an overflow interrupt on Timer4, that you only enable when you want the strobe. For example, the following code sends a better synchronised strobe for a single cycle of the PWM. There will be a few instruction cycles of jitter, which should be barely noticable, compared with the PWM frequency.
C++:
void __interrupt timer4overflow()
{
timer4IF = 0;
if (!strobePin)
{
strobePin = 1;
}
else
{
strobePin = 0;
timer4overflowInterruptEnable = false;
}
}

void mainloop()
{
while(1)
{
// ...

if (I_want_to_send_a_strobe_pulse)
{
timer4IF = 0;
timer4overflowInterruptEnable = true;
I_want_to_send_a_strobe_pulse = false;
}
}
}
An alternative is to have another timer OC send the strobe. This will have no jitter w.r.t. time between the clock and strobe signal outputs.

#### ccurtis

##### Well-Known Member
I may have to do something like an interrupt, but that's messy. I really want to understand why signals that should be sync'ed are not sync'ed. I look at the datasheet for the PIC, It has diagrams in it, and I expect the diagrams to be correct. The diagrams show the PWM timer is clocked by the same clock that steps through the instructions. If that is true, the offset between the strobe and the next PWM edge should be a constant. But, the reality is, it is not constant.

To give more details on why I need them to be syned, I have a switched cap filter chip that is clocked by the PWM output. The output of the filter chip has clock noise feedthrough on its output that is only present for a short time during the clock edges, that the PIC ADC will see, now and then, unless I clock the filter chip before sampling the output with the ADC. As it stands, the clock noise runs through the whole range of sampling time because the PWM (clock) is not synced to the sampling period (nor to the strobe output).

#### Pommie

##### Well-Known Member
Why do you think the signals should be synced? One is hardware and the other is software. You also might try answering some of the questions above.

Mike.

#### dougy83

##### Well-Known Member
I may have to do something like an interrupt, but that's messy. I really want to understand why signals that should be sync'ed are not sync'ed. I look at the datasheet for the PIC, It has diagrams in it, and I expect the diagrams to be correct. The diagrams show the PWM timer is clocked by the same clock that steps through the instructions. If that is true, the offset between the strobe and the next PWM edge should be a constant. But, the reality is, it is not constant.
The diagrams are likely correct.
* How do you decide when to set the strobe?
* How do you decide when to start sampling?
You can't set the strobe at some random time and expect it to be in sync with the PWM signal.

Post some code showing how you're setting the strobe and sampling, please.

#### ccurtis

##### Well-Known Member
"You can't set the strobe at some random time and expect it to be in sync with the PWM signal."

If that is true, then the diagrams are not correct and my question is answered. A PWM and instruction set (program counter) clocked by the same source must be in sync. I can't set the strobe at a random time. It is is set when the instruction clock steps to the line in code to set the strobe, and then sets its. Like the PWM generator, It can't be set at any other time, not dictated by the edges of the instruction clock.

Last edited:

#### dougy83

##### Well-Known Member
If that is true, then the diagrams are not correct and my question is answered. A PWM and instruction set (program counter) clocked by the same source must be in sync. I can't set the strobe at a random time. It is is set when the instruction clock steps to the line in code to set the strobe, and then sets its. Like the PWM generator, It can't be set at any other time, not dictated by the edges of the instruction clock.
No.

Your PWM cycle repeats every 81 instruction cycles. If you set the strobe on the 15th instruction cycle, then it's going to occur ~15 instruction cycles after the PWM. If next time you set the strobe on the 140th instruction cycle, it's going to be ~60 instruction cycles past the PWM.

Post some code describing setting the strobe pin, and/or ADC sampling.

#### ccurtis

##### Well-Known Member
No.

Your PWM cycle repeats every 81 instruction cycles. If you set the strobe on the 15th instruction cycle, then it's going to occur ~15 instruction cycles after the PWM. If next time you set the strobe on the 140th instruction cycle, it's going to be ~60 instruction cycles past the PWM.

Post some code describing setting the strobe pin, and/or ADC sampling.
I totally agree with that, and that's not a problem. Regardless of how many instruction cycles it takes to produce a PWM cycle or to produce a strobe, the edges of both the PWM output and the Strobe occur on transitions of the instruction clock, and only on transitions of the instruction clock, if the documentation is correct. Showing specific code is not relevant to the bigger picture about synchronization. So, if I sync the scope on the strobe, an edge of the PWM output might be one or more instruction clock cycles away from the strobe, or even a variable number of instruction clock cycles, but never a period time that is not a multiple of the instruction clock, right?

#### ccurtis

##### Well-Known Member
Durr, hold everything. They ARE synchronized. It's just my scope setup. The instruction clock is so much faster than the PWM signal that with the time scale I was using, it just appeared that that they are not synchronized because of the edges being so close together!

Thanks guys for being ears and voices to help me think and talk through this. That helps a lot.

Looking back I also misstated the problem and what I was expecting, and threw some folk off. Sorry about that. Sometimes my fingers don't type what the mind is really thinking. I should have said the MINIMUM amount of time between the strobe and PWM edges should be constant.

Last edited: