# 25 Hz Pwm micro

Status
Not open for further replies.

#### BobS

##### New Member
Is there a micro that can do a 25 Hz PWM in the background, without any software support other than to set it up?

#### birdman0_o

##### Active Member
That is rather slow, you would normally need to do it in software. If you used a 'slow' Clock on the microcontroller it can be done with hardware PWM.

#### Pommie

##### Well-Known Member
You could do it with interrupts so it appears to require no software support.

Mike.

#### BobS

##### New Member
It is slow. The software will be busy timing input pulses. The input timing and output PWM can not interfere with each other. Back ground PWM is one solution, if there is a micro that can go that slow without running the clock so slow the micro is not good for anything else.

#### birdman0_o

##### Active Member
To get down to 25 Hz you would need a 0.4Mhz clock maximum. After a bit of searching the only crystal you can find under ~2 Mhz is a clock one at 32 Khz which is probably no good for you. Actually most PICs have a built in second frequency which is around 44khz. What kind of signals are you dealing with?

#### Pommie

##### Well-Known Member
If you tell us what you are trying to do we may be able to help. Had you said 50Hz I would have guessed it was to do with model servos but I've no idea what uses 25Hz.

Mike.

#### be80be

##### Well-Known Member
You could do it with interrupts so it appears to require no software support.

Mike.
I think I would go with mikes idea use interrupts it wouldn't be much deference then pwm It would run in back ground.

Last edited:

#### BobS

##### New Member
PWM interrupts can run in the back ground??
The time between every set of 80 Hz input pulses has to be measured at the same time the PWM out is operating. And some math to make the input to output conversion. The 25 Hz. PWM runs a very small solenoid. Have an analog solution for this task which uses three dozen+ parts, requires calibrating, can drift, etc. With only 3 I/O a small micro should work for this task, if a way could be found to do the inputs and output simultaneously. It appears either the input or output would have to run in the back ground. 8 bit resolution is adequate.

#### Pommie

##### Well-Known Member
Do you have a timing diagram? Without knowing the specifics of the signals it is hard to advise the best solution. Both of the things you mention may be able to be done completely in hardware but it really depends on the actual signals.

Mike.

#### Al1970

##### New Member
Sorry but you lost me on this. You say that you only want to time 80 Hz input and PWM at 50 Hz? I have programmed a PIC to send data to a serial port, count the pulses from two encode wheels who signal is 2,000 Hz, check the voltage on 3 pins and do PWM on two motor at 500 Hz. Is there something in your main program that is taking all your PIC power?

Al

#### BobS

##### New Member
This should be doable, the issue is not having the simultaneous and continuous I/O functions interfere with each other. They can not be performed serially.
The two 1 ms. input pulses are the same frequency and synchronized, with each one from a separate source. The time between the leading edge of these pulses modulates between 0 to 12.5 ms. (0-100%).
The output is 0-100% PWM at 25 Hz. ± 2.5Hz.
The input and output do not need to be synchronized, must be proportional, and have fast response. ie. no RC filters.
A digital hardware solution may work, but we have not determined a simple way to digitally convert the input to a proportional output. The input pulses can easily operate a flip flop, but from there it gets messy with clocks, ramps, etc.
A micro would be a nice solution if the software could be worked out.

#### Pommie

##### Well-Known Member
Are you saying you have two input pulses that have simultaneous rising edges, are from 1 to 12.5mS long and repeat every 12.5mS, so 100% is permanently on? Also, the output is a pulse from 1 to 40mS long repeated every 40mS? Again, 100% will be permanently on? And an error of 0.4% (8 bit precision) is permissible?

Mike.

#### Mike - K8LH

##### Well-Known Member
Hi BobS,

One method I developed several years ago for producing low frequency PWM signals using the PWM module involves breaking down the period into smaller PWM period "frames" and using ISR "helper" code to combine these "frames" into the much longer total period. This method has relatively low ISR "overhead" and produces a very high resolution jitter free PWM pulse with a 0% to 100% duty cycle.

Here's what an ISR "helper" might look like for your 25-Hz (40-msec period) PWM output requirement;

Regards, Mike, K8LH

Code:
  //  setup a 1000-usec PWM period using PR2 = 249 and prescaler
//  4 (4 MHz) or 16 (16 MHz) for 4 usec pulse width resolution
//  and combine 40 of these 1000-usec "frames" for an overall
//  40-msec (25-Hz) PWM period

int dcy = 5000;              // 0..10000 (4-usec ticks)
int pulse = 0;               // ISR work variable
int frame = 1;               // ISR frame number, 1..40

void DutyCycle(char percent)
{ dcy = percent * 100;       // 0..100% -> 0..10000
}

void interrupt()
{ if(pir1.TMR2IF)            // if Timer 2 interrupt
{ pir1.TMR2IF = 0;         // clear Timer 2 interrupt flag
frame--;                 //
if(frame == 0)           // if end-of-period
{ frame = 40;            // reset for new 40 msec period
pulse = dcy;           // reset 'pulse' work variable
}
if(pulse > 250)          // if pulse > 1000-usecs
{ CCPR1L = 250;          // do a 100% duty cycle frame
pulse -= 250;          // subtract 1000-usecs
}
else                     // do a variable or 0% frame
{ CCPR1L = pulse;        // 0..1000 usecs
pulse = 0;             // force remaining frames to %0
}
}
}

Last edited:

#### Mike - K8LH

##### Well-Known Member
Hi again BobS,

If you can tolerate 1% resolution on those two 0-12.5-msec input signals then you could poll or sample the inputs at 125-usec intervals and simply count "on" intervals (0-100) and potentially use that value to set the PWM duty cycle.

Define "very fast" because while you might be able to update the PWM duty cycle value at your 80-Hz (12.5-msec) input frequency the PWM duty cycle is usually only updated at end-of-period (25-Hz).

I'm unclear on how you control one PWM output from two inputs? Or have I misunderstood and you're using two intputs and two PWM outputs?

Kind regards, Mike

Last edited:

#### BobS

##### New Member
Hi Mike and Mike,
The input pulses are always 1 ms long and both repeat at 80 Hz. They are relative to each other, with the data being the time between leading edges or the phase difference. One pulse stream could be considered a reference and the position of the other data. If they were going into a flip flop, one would be set and the other reset. At 50% one pulse lags the other by ~6.25 ms. At 0 and 100 %, the pulses would line up with each other. In effect the input pulses do not quite reach 1 and 100%.
The 25 Hz output should reach 100% on (continuously on) and 0% as off.
K8LH,
"very fast" = minimum delay. It would be nice if the output could be updated with every input period.
I'm not much of a programmer (elementary Basic), and have considered polling the inputs. Not sure how to handle the 1 ms. pulse. There would be several highs for the pulse then a string of lows between the pulses. The transition between high an low would have to be detected with counts accumulated from there.
If polling is used, wouldn’t it be possible to bit bang the output for every input poling cycle?? Any resultant jitter and variable granularity in the output would be ok, if it doesn't exceed ~1% of the output period.

Regards, Bob

#### Al1970

##### New Member
Yes, what you want to do can be done in software in a PIC. If you want to make this a paying project, let me know.

Al

#### Pommie

##### Well-Known Member
I don't have time to play at the moment but can probably have a play with this on Thursday. Have you a particular chip in mind? If you prefer basic then I would suggest Swordfish Basic with an 18F1320 as that is probably the simplest solution.

Mike.

#### BobS

##### New Member
Thanks for the interest.
For a couple reasons I'd like to see this in as small a micro as reasonable. One is there are only 3 I/O, the other is I have this penchant for making things small, not so small it is painful, but not scattered about either.
Why do you suggest the 18F1320? Does it have specific features that are necessary? Otherwise it has a lot of unneeded I/O, or is that just part if the program? How much would it take to do polling and bit banging the output in one loop?
A 10F--- and GCBasic anyone?? Is this any good?

Status
Not open for further replies.