Position Control for a servomotor with Pic16f887

Control de posición servomotor con pic16f887, Position Control for a servomotor with Pic16f887

Position Control for a servomotor with Pic16f887

Objectives

  1. Learn to control a servormotor 
  2.  Generate a PWM signal with delay

Diagram in Proteus

Source Code in PIC C

#include <16F887.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPUT //No Power Up Timer
#FUSES NOMCLR //Master Clear pin used for I/O
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES NOFCMEN //Fail-safe clock monitor disabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 
#define SERVO PIN_C1
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=pin_c6, rcv=pin_c7,bits=8)

/*—————— Main Program ——————–*/
int16 pulso;
int16 bajo; // tiemp0 que esta en cero
int16 periodo; // periodo de la senal PWM
int i; // generar cliclo for para generar un tren de pulsa

void main(void)
{

while (true)
{

// m =1ms/180

pulso=2000 ; // 1ms -180
periodo=20000; // 20 ms
bajo=periodo-(pulso); // 20ms - pulso =19 ms

for (i = 0; i < 50; i++)
{

output_high(SERVO); // PIN_C1=1
delay_us(pulso); // retardo pulso
output_low (SERVO); // PIN_C1=0
delay_us(bajo); // retardo bajo
}

}

}

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *