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

PWM from 25Hz

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



Joined: 31 Mar 2004
Posts: 23
Location: Switzerland

View user's profile Send private message

PWM from 25Hz
PostPosted: Mon May 10, 2004 3:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon May 10, 2004 7:17 am     Reply with quote

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

   while(1)
   {
       output_high(PWM_PIN);
       for( timecnt= dcon; timecnt > 0 ; timecnt--)
           delay_us(400);
       output_low(PWM_PIN);
       for( timecnt = dcoff; timecnt > 0; timecnt--)
           delay_us(400);     
   }


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.

- SteveS

oh yeah, your English is fine!
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