why ccp set_pwm1_duty(250) does not work for servo.

Thread Starter

meshcurrent

Joined May 28, 2022
25
I am using PIC16F877a microcontroller. My aim is control the servo motor with pwm signal. I can create the pwm signal with RB0 pin with ccs c code. I have tried the ccp1 module to create pwm signal but I could'nt. Why does not work. And I have a question about these two ways to create pwm signals: What is diffrence between create pwm signals with digital outputs(etc: RB0) and ccp outputs.

Here is my codes
This is create pwm with RB0 pin output using timer0 it works properly to control servo.

#include <main.h>
#use delay(clock=20000000)
#fuses XT,NOWDT,NOPUT,NOLVP,NOCPD,NOPROTECT,NODEBUG,NOBROWNOUT,NOWRT

#use fast_io(b)

int sayac=0;

#int_timer0
void kesme(){
set_timer0(246);

if(sayac==40){
sayac=0;
}

if(sayac<3){
output_high(pin_b0);
}else{
output_low(pin_b0);
}
sayac++;
}


void main()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);

set_tris_b(0x00);

setup_timer_0(RTCC_INTERNAL | RTCC_DIV_256);
set_timer0(246);

enable_interrupts(INT_timer0);
enable_interrupts(GLOBAL);

while(true){
}
}

This code is to create pwm signal with ccp1. It does not work. Why? When I use the pwm signal with ccp1 the servo stops on the -90 degrees.
#include <main.h>
#use delay(clock=20000000)
#fuses XT,NOWDT,NOPUT,NOLVP,NOCPD,NOPROTECT,NODEBUG,NOBROWNOUT,NOWRT

#use fast_io(c)




void main()
{
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP2(CCP_OFF);

setup_timer_2(T2_DIV_BY_4,255,1);
setup_CCP1(CCP_PWM);

set_tris_c(0x00);


while(true){
set_pwm1_duty(18);
}
}

photo 470 about RB0 pwm output test
photo 471 about ccp1 set_pwm1_duty(18) pwm output test
photo 472 about ccp1 set_pwm1_duty(180) pwm output test
 

Attachments

MaxHeadRoom

Joined Jul 18, 2013
28,780
Define the servo you are using!
Is this an RC servo?
If so it completely different from a non-RC/positioning type version usually.
 
Top