View previous topic :: View next topic |
Author |
Message |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
Portable Timer Functions |
Posted: Tue Aug 06, 2013 2:37 am |
|
|
Hello everyone,
I want take your opinion in something,
I want to design my own pwm library, but somehow i want all my libs to be generic.(it's a curse)
i guess the best method of pwm is using timer interrupt method.
so for example there will be 2 functions:
Code: | generate_square_wave(double desired frequency, int8 timer_module)
{
//hard code
}
modulate_wave(int8 module, int16 pins[32+1])
{
//harder code
} |
so my question is it possible to make the build_in Timer functions, and the ISR function portable so it can be used in function within a library and then called from the main file.
Thanks in advance,
z3ngew |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Aug 06, 2013 3:08 am |
|
|
Er.
The best way of of PWM, is to use the PWM hardware.....
'Double'. Why?.
CCS, except for DSPIC's, does not _have_ a 'double' type. All doubles are treated as singles. Even worse though, a 'double', wastes a lot of space, and time. Think integer. Everything to do with the timers is going to be integer.
If you just make your functions as an include, then they are just part of your main. If instead you are intending to link them, then it might well result in slight inefficiency, since it will almost certainly result in the code not being placed optimally for the interrupt handling.
To do PWM at any high frequency/resolution, use the hardware. That is what it is for.
Remember array accesses are inefficient. Typically takes a dozen+ instructions to just read/write a value into an array. Better to hard code the pin numbers using #DEFINES.
Best Wishes |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Tue Aug 06, 2013 3:21 am |
|
|
but the hardware pwm is not so useful when low frequencies is need (for example rc servo motor - 50hz)
Thanks,
z3ngew |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Tue Aug 06, 2013 3:45 am |
|
|
Look at a chip with the power control module PWM.... /64 as a prescaler, and 12bit PWM. So can give 50Hz output rates up to 52Mhz.....
Four channels.
Hundreds of times better than any software solution.
Best Wishes |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Aug 06, 2013 8:50 am |
|
|
for arbitrary frequency square wave, consider 16f1509 NCO feature.
with advanced programming , FM and PSK should be able to be done too,
BUT.....
for arbitrary frequency with full range,glitchless PWM, good luck./
timer ints are a lousy way to get there, IMHO, because any decently useful
frequency for driving say a buck converter, with a reasonable value inductor,
is going to crush your MAIN() available cpu cycles.
just my 2 cents |
|
|
|