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 in 16F873A

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



Joined: 24 Mar 2004
Posts: 4

View user's profile Send private message MSN Messenger

PWM in 16F873A
PostPosted: Tue Nov 16, 2004 4:26 am     Reply with quote

I have been trying to setup a simple PWM for awhile now with this chip. It produces some bazaar results. Code is as follows:

set_tris_a(0b11101111);
set_tris_b(0b00000000);
set_tris_c(0b11011000);

setup_port_a(AN0);
setup_adc(adc_clock_internal);
set_adc_channel( 0 );

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 127, 4); // 10Mhz crystal

main()
{
ADInValue = read_adc();
printf("ADInValue=%03u%c",ADInValue,13);
set_pwm1_duty(ADInValue);
}

With this code the PWM output turns motor full on until count gets above ~250 and motor slows a little... What should I look for to fix this, code looks like the example file EX_PWM.C

Confused Confused ,
Zonared.
Ttelmah
Guest







Re: PWM in 16F873A
PostPosted: Tue Nov 16, 2004 4:47 am     Reply with quote

zonared wrote:
I have been trying to setup a simple PWM for awhile now with this chip. It produces some bazaar results. Code is as follows:

set_tris_a(0b11101111);
set_tris_b(0b00000000);set_tris_c(0b11011000);

setup_port_a(AN0);
setup_adc(adc_clock_internal);
set_adc_channel( 0 );

setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 127, 4); // 10Mhz crystal

main()
{
ADInValue = read_adc();
printf("ADInValue=%03u%c",ADInValue,13);
set_pwm1_duty(ADInValue);
}

With this code the PWM output turns motor full on until count gets above ~250 and motor slows a little... What should I look for to fix this, code looks like the example file EX_PWM.C

Confused Confused ,
Zonared.


Try executing the setup instructions!.....
All the 'setup' commands, need to actually be run. You have placed them outside the main, and they will never be called. Either use:
Code:

void init_ports(void) {
   set_tris_a(0b11101111);
   set_tris_b(0b00000000);
   set_tris_c(0b11011000);
     
   setup_port_a(AN0);
   setup_adc(adc_clock_internal);
   set_adc_channel( 0 );

   setup_ccp1(CCP_PWM);
   setup_timer_2(T2_DIV_BY_16, 127, 4);   // 10Mhz crystal
}

main()
{
   init_ports();
   ADInValue = read_adc();
   printf("ADInValue=%03u%c",ADInValue,13);
   set_pwm1_duty(ADInValue);
}

Or put the instructions inside your main.

You need to understand, that C will accept a declaration like this, assuming it to be a 'function prototype', and giving no errors, but the code is never run, unless it is put into the the actual body of the program somewhere...
Note also, using the 'Code' button, makes code put into postings, much easier to read.

Best Wishes
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