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 CCS Technical Support

one last question on PWM...

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



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

one last question on PWM...
PostPosted: Wed Aug 10, 2005 3:24 pm     Reply with quote

First off, I'd like to thank everyone for their help so far. I am inputting a PWM signal on pin B2 which has a pulse length of 2ms. I am using a 4MHz clock with a PIC16F87. Does anyone know why I am not seeing a high signal on pin A2?

Code:

main()
{
   long ch5=0;
   delay_ms(1500);

   while(1)
   {
      // timer1
      setup_timer_1(T1_INTERNAL);

      while(input(B2)) // if high, wait for pulse to go low
      delay_us(3); // account for fall time
      while(!input(B2)) // wait for pulse to go high (beginning)
      set_timer1(0);
      delay_us(3); // account for rise time
      while(input(B2)) // wait for pulse to end
      ch5 = (long)((get_timer1()*4)/4000000)*1000000; // [(# counts) * 4 * prescaler / (clk freq)] * 1e6 (convert to us)



      if(ch5>1500)
         output_high(A2);
      else
         output_low(A2);

   }
} // end of main
[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 10, 2005 3:33 pm     Reply with quote

Answer this question:

What happens when you divide an integer that's less than 4000000
by 4000000 ? What result do you get ?
Ttelmah
Guest







PostPosted: Wed Aug 10, 2005 3:55 pm     Reply with quote

As posted, the semi-colon is missing on the end of both 'while' statements. They will then repeatedly execute the next line of code, which is not what is wanted.
In general, use an int32, rather than a long. A long has a maximum value of 65535, and you are potentially overflowing this in the multiplication. If you use an int32, and do all the multiplications 'first', it avoids rounding errors becoming a problem.
However the 'sum' as given is unecessary... You are multiplying by 4, and by 1000000, and dividing by 4000000. Effectively a waste of a lot of processor time. The timer is already counting in uSec, so no 'conversion' is necessary.

Best Wishes
weg22



Joined: 08 Jul 2005
Posts: 91

View user's profile Send private message

PostPosted: Wed Aug 10, 2005 4:44 pm     Reply with quote

Ha! Yeah, I realized that on the drive home today as I was thinking about it. I originally kept it as (4/4000000)*1000000 to make it clear if I were to refer back to it in a few months - but, that's what comments are for :-)

Thanks!
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