|
|
View previous topic :: View next topic |
Author |
Message |
zonared
Joined: 24 Mar 2004 Posts: 4
|
PWM in 16F873A |
Posted: Tue Nov 16, 2004 4:26 am |
|
|
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 ,
Zonared. |
|
|
Ttelmah Guest
|
Re: PWM in 16F873A |
Posted: Tue Nov 16, 2004 4:47 am |
|
|
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 ,
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 |
|
|
|
|
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
|