PDA

View Full Version : Discussion PIC programming question


stegla
Dec 27, 2007, 09:38 AM
A question for you PIC programming experts..........

I'm using a pic12f683 o/p to drive a servo with the usual 1ms to 2ms pulse width and 20ms frame-rate.

I am taking the 6 most significant bits from an A to D convertor........ the AtoD result is used to access a 64way lookup table to give me a number that is inserted into Timer2 to give me my pulse width. Which all works fine.

Servo trim was an easy feature to implement just by adding or subtracting a number to the result.

BUT (here is the question)......... How can I implement variable rates?

For the servo to always centre in the same place the 1.5ms pulse uses a figure of 7Fh from the look-up table. The 7Fh centre figure must never change but I would like to compress or expand the numbers either side to give me variable rates?

Any thoughts?

Thanks
Steve


ps I have written a 2nd look-up table to give me roughly 70% rates and a switch to jump betwwen the two................but this is an inelegant solution.

Steve

AndyKunz
Dec 27, 2007, 02:53 PM
A table is an easy way to do it, but you probably want to do it mathematically at runtime.

If you know the formula you wish to implement and the range and resolution of each variable, it usually isn't difficult at all to implement a memory- and time-efficient implementation. You don't need to load math libraries.

The way I usually start is by making a spreadsheet with all the values I want, and then analyze it to derive an efficient algorithm. Make sure one of your columns is the ADC input, since this makes things easier to work with. You don't need fancy MathCAD or such - Excel or Open Office will give you all you need.

Andy

stegla
Dec 29, 2007, 04:18 AM
Thanks Andy

I've got something working but all it needs is fine-tuning.

Steve