// servo_pwm_232
// Ejemplo con un servo FUTABA S3003
// Alimentación y pulsos a 5V
// Cuadro de Tiempos :
// Periodo 20 ms (Frecuencia 50 Hz)
// Ancho Pulso minimo 0.5 ms
// Ancho pulso medio 1.5 ms
// Ancho pulso maximo 2.5 ms
// TMR0 a 1:16 -> 1 RTCC cada 4.096 ms
// -> 1 Tick cada 0.096 / 256 = 0.016 ms
// -> 20 ms = (4 x RTCC completas) + (1 * RTCC - 30 ticks
// Ancho Pulso minimo 0.5 ms -> 31 ticks de TMR0
// Ancho pulso medio 1.5 ms -> 93 ticks de TMR0
// Ancho pulso maximo 2.5 ms -> 155 ticks de TMR0
#include <16f877a.h>
#fuses XT,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#use delay(clock=4000000)
#use standard_io(b)
#define PIN_SERVO1 PIN_d5
#define PIN_SERVO2 PIN_d6
int sensor1b=0;
int sensor2b=0;
const int AJUSTE_FINO_DE_RTCC = 30;
const int ticks_PULSO_MINIMO = 31;
const int ticks_PULSO_MEDIO = 93;
const int ticks_PULSO_MAXIMO = 155;
int i,h=0,a=0,b=5000;
int1 flagRTCC = 0;
int contRTCC = 0;
int1 flagSERVO1 = 0;
int tSERVO1 = ticks_PULSO_MEDIO;
int1 flagSERVO2 = 0;
int tSERVO2 = ticks_PULSO_MEDIO;
#int_RTCC
RTCC_isr(){
++contRTCC;
if(contRTCC==4){
set_TIMER0(AJUSTE_FINO_DE_RTCC);
}
if(contRTCC==5){
flagRTCC=1;
contRTCC=0x00;
}}
void main() {
int ValTIMER0;
setup_counters(RTCC_INTERNAL,RTCC_DIV_16);
enable_interrupts(int_rda);
enable_interrupts(global);
setup_adc(ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_TIMER0(0);
enable_interrupts(INT_RTCC);
do {
tservo1=85;
tservo2=85;
set_adc_channel( 0 );
sensor1b=read_adc();
set_adc_channel( 1 );
sensor2b=read_adc();
// DISPARO DEL PULSO PWM
if(flagRTCC==1){
flagRTCC=0;
output_high(PIN_SERVO1);
flagSERVO1=1;
output_high(PIN_SERVO2);
flagSERVO2=1;
}
// CONTROL DE ANCHO DEL PULSO PWM
if(flagSERVO1==1){
valTIMER0 = get_TIMER0();
if(valTIMER0>tSERVO1){
flagSERVO1=0;
output_low(PIN_SERVO1);
}
}
if(flagSERVO2==1){
valTIMER0 = get_TIMER0();
if(valTIMER0>tSERVO2){
flagSERVO2=0;
output_low(PIN_SERVO2);
}
}
}while (true);
}