PDA

View Full Version : Pulsin alternative for 2khz pwm



retepsnikrep
- 3rd January 2014, 11:56
I have a 12F683 (8mhz internal osc) measuring the pulse width of a 2khz pwm signal coming in on the GP3/MCLR pin.

I'm currently using pulsin (5us period) and get a resolution of 100 units over the 500us width of the PWM signal.

I want to improve that to 256 units or better resolution by using timer1 and interrupts or similiar to capture the pulse length on the pin.

The problem i have is what osc frequency will give me a reliable 256 units over 500us pwm pulse.

Should I set the osc freuency to try and get as near to 256 as possible or just use as much accuracy as timer1 will provide at 8mhz, then manipulate the result to give my reqd 256 unit range.

I can't easily change pin or I might have tried the Timer1 special capture mode.

The CCP1 pin is also in use by another part of the software.

HenrikOlsson
- 3rd January 2014, 12:44
Hi,
Generally speaking, use as much resolution as you can and then manipulate your results after capturing. The reason for this is that you say you want 256 units or better. If you use as much resolution as you can then it's easy to tweak the result to whatever is needed later on - instead of changing the frequency again. The obvious drawback is that it needs calculations done to it aftwerwards which, depending on the application, may or may not be an issue.

If you want 500us divided into a byte variable, ie 0-255, then each bit is "worth" 1.953125us which equals a frequency of 512kHz so clocking TMR1 with 512kHz will give a TMR1 value of 255 for 500us.

TMR1 is 16bits wide, clocking it with Fosc/4 (using your 8MHz oscillator) and a prescaler of 1:1 means it overflows after 65536/2000000=32.768ms so it's MORE than enough for your 500us period. It'll give you a resolution of 1000 units since each timer tick is 0.5us.

Again, the drawback here would be that if you really need 256 units then it'll be a "tricky" calculation since 255 doesn't divide evenly into 1000 but I think TimerValue**16712 should get you close.

Too bad you can't use capture mode, it would have been ideal. Now you're going to have to use IOC since that's what's available on GP3 and then account for the interrupt latency when you grab the timer value.

Only my $0.02 of course.

/Henrik.

Acetronics2
- 3rd January 2014, 14:41
Hi,

Thinking closer to it ...

IF you intend to use internal osc ... no real need for over resolution as your timebase precision will be out of the needed precision ...

so ... whole project to clean up !!! :rolleyes:

too bad, ehhhh ????

Alain

retepsnikrep
- 5th January 2014, 19:19
Thanks for the ideas.

One of the reasons I am looking to get the incoming 2khz PWM pulse into a 0-255 variable is that my device is passing through the pwm and using the HPWM output to mimic the input.

So the input pwm is being measured, manipulated by my program if reqd and squirted out using hpwm which uses a byte variable for the duty.

I'm trying to keep the maths simple and improve on the resolution that the pulsin command provides.