|
|
View previous topic :: View next topic |
Author |
Message |
sonicfire
Joined: 11 Feb 2007 Posts: 19 Location: Cedar Rapids
|
Strange PWM behavior |
Posted: Wed Mar 07, 2007 7:45 pm |
|
|
I was finally able to get my PWM to operate at 100KHz, but now I'm observing some strange behavior. Currently, I am using a potentiometer to control the input to the PIC 16F877's ADC port. I scale the ADC signal by dividing by 1024 and multiplying by what SHOULD be a value of 833.
I multiply by 833, because of information I got from the CCS compiler manual. Since I am operating on a 10MHz external oscillator, the period of my clock is 100uS. The maximum duty cycle I want for a 5V input from the ADC is 83.3%. Thus, the duty cycle I give to the PWM is equal to (period desired)/(1/clock) = (83.3uS)/(1/10MHz) = 833.
However, when I am observing the PWM output on the oscilloscope, the duty cycle starts over again at 0% when the output goes above about 63%. Does anyone know why it is doing this? I never had this problem before.
Code: | #include <16F877.h>
#device adc=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)
void main() {
INT16 value;
setup_ccp1(CCP_PWM); // Configure CCP1 as a PWM
setup_timer_2(T2_DIV_BY_1, 24, 1); //output at 100KHz
//Period = (1/clock)*4*t2div*(period+1)
setup_port_a(ALL_ANALOG);
setup_adc(adc_clock_internal);
set_adc_channel( 0 );
while( TRUE ) {
value=(read_adc()*833)/1024;
set_pwm1_duty(value); // This sets the time the pulse is
// high each cycle. We use the A/D
// input to make a easy demo.
// the high time will be:
// if value is LONG INT:
// value*(1/clock)*t2div
// if value is INT:
// value*4*(1/clock)*t2div
// for example a value of 30 and t2div
// of 1 the high time is 12us
// WARNING: A value too high or low will
// prevent the output from
// changing.
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 07, 2007 9:29 pm |
|
|
Quote: | value=(read_adc()*833)/1024; |
The problem is in the math. You need 32-bit math in the numerator
expression. CCS doesn't automatically do this. To force the compiler
to do it, you need to cast at least one of the operands in the numerator
to 32-bit. Remember this always, with CCS. |
|
|
sonicfire
Joined: 11 Feb 2007 Posts: 19 Location: Cedar Rapids
|
|
Posted: Thu Mar 08, 2007 4:52 pm |
|
|
The PWM output is perfect now. Thanks for the tip. |
|
|
|
|
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
|