View previous topic :: View next topic |
Author |
Message |
weg22
Joined: 08 Jul 2005 Posts: 91
|
one last question on PWM... |
Posted: Wed Aug 10, 2005 3:24 pm |
|
|
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
|
|
Posted: Wed Aug 10, 2005 3:33 pm |
|
|
Answer this question:
What happens when you divide an integer that's less than 4000000
by 4000000 ? What result do you get ? |
|
|
Ttelmah Guest
|
|
Posted: Wed Aug 10, 2005 3:55 pm |
|
|
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
|
|
Posted: Wed Aug 10, 2005 4:44 pm |
|
|
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! |
|
|
|