CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Portable Timer Functions

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
z3ngew



Joined: 20 Apr 2013
Posts: 50

View user's profile Send private message

Portable Timer Functions
PostPosted: Tue Aug 06, 2013 2:37 am     Reply with quote

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: 19369

View user's profile Send private message

PostPosted: Tue Aug 06, 2013 3:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Aug 06, 2013 3:21 am     Reply with quote

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: 19369

View user's profile Send private message

PostPosted: Tue Aug 06, 2013 3:45 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Aug 06, 2013 8:50 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group