Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM Frequency Step Down

Status
Not open for further replies.

Dakta

New Member
Just wondering what an engineer would do in this situation: -

You want to control a linear actuator by modulating a duty cycle signal to a vacuum solenoid which tries to pull the actuator open or shut. This part of the design is rigid, i.e it can't be altered by the engineer.

However, by using the mentioned duty cycle signal, you can alter the position of the actuator more or less accurately, however you have a problem as the frequency output by the pic is way too high for the vacuum solenoid to respond. You really want to hit 8hz, but without clocking the pic stupidly low!

Is there any sort of logic chip or timer that can be used to generate a pwm at such a low frequency, using either a digital input for the value wanted, or would an engineer be bound to manually writing a routine in code to emulate his own duty cycle signal from a digital output on the pic?

thoughts?

cheers
Kris
 
Last edited:
Hi Kris,

You can still use the CCP module in PWM mode to develop an 8-Hz PWM period (without resorting to a ridiculously low oscillator frequency). You just need to use an interrupt driven software helper to link together a number of smaller PWM period "frames" which make up the much longer 8-Hz PWM period.

Here's an example software helper that links together 125 1-msec PWM "frames" to create your 8-Hz 125-msec PWM period with a jitter free output pulse with 15-bit pulse width resolution;

Regards, Mike

Code:
;******************************************************************
;
;  //  setup a 1000 usec PWM period with PR2 = 249 and prescaler
;  //  4:1 (4 MHz clock) for 125 PWM 'frames' per 125 msec (8-Hz)
;  //  period and 4 usec pulse width resolution
;
;  int dutycycle = 1000;        // 0..31250 (4-usec steps)
;  int width = 0;               // ISR work variable
;  int frame = 1;               // frame number 1..125
;
;  void ISR()
;  { PIR1bits.TMR2IF = 0;       // clear Timer 2 interrupt flag
;    frame--;                   //
;    if (frame == 0)            // if end of the 125 msec period
;    { frame = 125;             // reset for new 125 msec period
;      width = dutycycle;       // reset 'width' work vari
^^^ it seems listing was destroyed during or after Forum software upgrade ^^^ will fix later...
 
Last edited:
Thanks for the time and input, I think I grasp what you're getting at - even If I'm very puzzled by the code (not seasoned at C yet, my fault).

So the idea is we stitch together periods of 100% duty cycle every 125ms to artificually generate a pwm signal.

It's an idea to play with, though I'l admit the code takes some understanding - you wouldn't know if there's any hardware modules that could make life easier, or should I stick with it?

I'm trying to keep as many processing cycles as free as possible. I've even considered using aseperate controller to take an 8 bit input of the duty cycle on one port and generate the pwm on my main pic's behalf but I consider that wasteful and poor design.
 
Last edited:
So the idea is we stitch together periods of 100% duty cycle every 125ms to artificually generate a pwm signal.
You set up the PWM module to generate a 1000-usec PWM period and you stitch together 125 of these small 1000-usec PWM cycles to make up your 125-msec period. You generate 100% duty cycle PWM cycles until less than 1000 usecs of the pulse width remain and then you generate the last partial duty cycle value.
It's an idea to play with, though I'l admit the code takes some understanding - you wouldn't know if there's any hardware modules that could make life easier, or should I stick with it?
I can't think of an easier or more bullet proof method off hand. It's very low overhead using just a few instruction cycles every 1000-usec interrupt cycle. And since the PWM module double buffers the duty cycle value you place in the CCPR1L register you have up to almost 1000-usecs to process each interrupt so you can actually turn off interrupts for short periods if necessary or place the interrupt "helper" code at the low priority interrupt vector.
I'm trying to keep as many processing cycles as free as possible.
Again, this is a reasonably "low overhead" method but perhaps you're under some other constraints you haven't told us about?

Regards, Mike
 
Hi -

I 'think' I got it working, I did find it very confusing but we'l see. I'm using computer simulation at the minute so she's running very slow (taking about 5 seconds for each cycle) but the ratio of on-off time is working right, so might be time to get out the breadboard and see.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top