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

ADC->PWM Frequency problems

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



Joined: 10 Aug 2011
Posts: 3

View user's profile Send private message

ADC->PWM Frequency problems
PostPosted: Wed Aug 10, 2011 12:51 pm     Reply with quote

Hello guys.

I am using a 16F877A microcontroller to do a motor speed control.
The speed can be set by a trimpot.
I used a program that I found here.
Code:

#include <16F877A.h>
#device adc=8
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)

//================================
void main()
{
int8 value;

setup_ccp1(CCP_PWM);

// Set the PWM frequency for 244 Hz (with a 4 MHz crystal)
setup_timer_2(T2_DIV_BY_16, 255, 1);

setup_port_a(AN0);
setup_adc(ADC_CLOCK_DIV_8); // Divisor for 4 MHz crystal
set_adc_channel(0);
delay_us(20);

while(1)
  {
   value = read_adc();

   set_pwm1_duty(value);
  }

}


But if the motor is running at medium/low speed, it makes a lot of audible noise.
I tried to make a higher frequency changing
Code:
setup_timer_2(T2_DIV_BY_16, 255, 1);

but if i do this, the pwm stops to work correctly as before.

How can i have a frequency higher than 20khz?
Sorry for my english.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 10, 2011 1:12 pm     Reply with quote

That program runs the PWM at 244 Hz. If you change the prescaler
to T2_DIV_BY_1 (instead of divide by 16), and if you change the
crystal to 20 MHz, then you can get 19531 Hz. Is that good enough ?

This post has links to all PWM formulas:
http://www.ccsinfo.com/forum/viewtopic.php?t=45968&start=1

My guess is that you probably don't need 20 KHz to make the motor
noise go away. It will probably work OK with a much lower frequency.
You might have to experiment and find the frequency that gives
acceptably low noise and maximum torque. It might not be 20 KHz.
loboartico



Joined: 10 Aug 2011
Posts: 3

View user's profile Send private message

PostPosted: Wed Aug 10, 2011 2:10 pm     Reply with quote

Thanks for your reply.
Actually my circuit is workin with a 20Mhz oscillator and i changed the setup of the timer to 15, so i could get a higher frequency.
But since i did this, the ADC saturates earlier than before.
Before i could set 0V -> 5V at the trimpot and the PWM changed proporcionaly.
But now, the PWM changes only with my trimpot as 0V->1V more or less, so the PWM gets 5V when my trimpot is only 1V.
What is wrong with it?
Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Wed Aug 10, 2011 2:40 pm     Reply with quote

Of course it would.
You must _not_ change the timer divider, only the _prescaler_.

The maximum value accepted by the PWM, is (timer divider *4)+1 if using a 'long' integer to control it, or just the timer divider value +1, if using an 8bit integer.
Change T2_DIV_BY_16, to DIV_BY_1, and with your clock frequency, the output will go up to 19520Hz, and the ADC range will still be correct.
Read the data sheet, or do an online search for the relationship between dividers, and available PWM resolution.

Best Wishes
loboartico



Joined: 10 Aug 2011
Posts: 3

View user's profile Send private message

PostPosted: Wed Aug 10, 2011 8:35 pm     Reply with quote

Thank you so much for your help, PCM Programmer and Ttelmah.
I did what you said and my program is working perfectly now =)
I can't hear the noise anymore, and the PWM is working fine.
Thanks for all.
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