Joined: 31 Mar 2004 Posts: 23 Location: Switzerland
PWM from 25Hz
Posted: Mon May 10, 2004 3:23 am
hello everybody
I'm working with a 18F252 and CCS. I want to do a PWM signal from 25Hz. I've a XT from 4MHz, therefore I can not use the HW PWM and since I'm newbie in C, I need an exemple to do this in a program.
Thanks, Pablo.
excused my english
SteveS
Joined: 27 Oct 2003 Posts: 126
Posted: Mon May 10, 2004 7:17 am
So you want to do a SW PWM then? You want a 25Hz PWM signal = 40msec period. What resolution do you want? (How fine do you want to be able to set the PWM?) Say you want 0-100%. That's 400usec resolution.
A poor way to do it (but maybe good enough to get started) is:
Code:
int8 dcon, dcoff, timecnt;
dcon = (required on time) // the percentage duty cycle on time);
dcoff = 100-dcon; // percentage off time
So what's so bad? It doesn't let you do anything else and it's not real accurate (the output and loop statements take some time that I don't account for).
Much better would be to use a timer interrupt set at the smallest interval you want (400usec in the case above) and take care of the output and count changes there. There are many timer examples in CCS and on this forum.
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