////////////////////////////////////////////////////////////////////
//TITULO: CONTROL DE 8 SERVOMOTORES PARA HITEC //////
//AUTOR: illusionista_86 //////
//FECHA: 20 - 04 - 11 //////
//DESCRIPCION: POR MEDIO DEL PROGRAMA CONTROL DE 8 SERVOS.EXE //////
// CONTROLARAS POR MEDIO DE BARRAS QUE TE INDICARA//////
// EL GRADO Y EL SERVO A CONTROLAR. //////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
#include<16F628A.H>
#include <string.h>
#fuses HS,NOWDT,NOLVP,MCLR,NOPROTECT
//HS: CRISTAL DE HIGH SPEED 20MHZ
//NOWDT: NO SE UTILIZA EL PERRO GUARDIAN
//NOLVP: SE DESACTIVA ESTA OPCION DE PROGRAMACION
//MCLR:ACTIVAMOS EL MASTER CLEAN
//NOPROTECT: NO SE PROTEGE EL CODIGO()
#use delay(clock=20000000) //CRISTAL DE 20MHZ
#use rs232(uart1,baud=9600,parity=N,bits=8,stop=1) //PIN_B1->RX,PIN_B2->TX
//#use rs232(uart1,baud=9600)
#use fast_io(A) //RAPIDA INTERVENCION DEL PUERTO A
#use fast_io(B) // '' '' PUERTO B
#priority timer1,rda //Es mas primordial que se atienda la interrupcion
//en el timer1
//CONSTANTES DE ENCENDIDO
int MODO=10;
unsigned int baudrate_config=7;
//VARIABLES A CONTROLAR
unsigned long tick; //E
unsigned int servo=0; //ES EL NUMERO DEL SERVO A CONTRLAR MAXIMO CONTAR DE 0 A 7
short FLAG=0; //ACTUALIZADOR
unsigned int NSERVO=0; //NUMERO DE SERVO SELECCIONADO
unsigned int i;
int ini=0;
unsigned int pos2[]={0,0,0,0,0,0,0,0,0,0};
//VARIABLE DE CONTROL DE PULSO DE ALTO Y BAJO PARA LOS 8 SERVOS
//COLOCADO COMO UN ARREGLO
unsigned long T_ON[]={0,0,0,0,0,0,0,0},T_OFF=0;
float ms_ton[]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
float ms_toff=0;
void config(void){
output_a(0x00);
output_b(0x00);
set_tris_a(0xA0);
set_tris_b(0x02);
//output_bit(PIN_B3,1);
setup_comparator(NC_NC_NC_NC); //Comparadores APAGADO
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //Tick de 0.0002ms
set_timer1(tick);
disable_interrupts(INT_TIMER1);
disable_interrupts(INT_RDA);
disable_interrupts(GLOBAL);
}
void calc_tiempos(int grados[]){
ms_toff=20.0;
//((2.5-0.5)/(180.0-0.0))
for (int i=0;i<8;i++){
ms_ton[i]=0.01111*(float)grados[i]+0.5;
ms_toff=ms_toff-ms_ton[i];
T_ON[i]=65536-(unsigned long)((ms_ton[i]/1000.0)*5000000.0); // 62556
}
T_OFF=65536-(unsigned long)((ms_toff/1000.0)*5000000.0); // 28516
}
#INT_RDA
void Interrupcion_X_recepcion(void){
//disable_interrupts(INT_TIMER1);//DESACTIVA EL TIMER 1
if(kbhit()){ //HAY INFORMACION A CAPTAR
if (MODO==0){
//enable_interrupts(INT_TIMER1);
}else if(MODO==1){
}else if(MODO==2){
output_toggle(PIN_B3);
}else if (MODO==3){
}else if (MODO==4){
}
}
FLAG=1; //ESTE PARAMETRO TE INDICA QUE SE A EFECTUADO UN CAMBIO EN UN DE LOS
//SERVOMOTORES
//PARA QUE SIGA DONDE SE QUEDO...
}
void config_baud_uart(unsigned int baudrate){
switch (baudrate)
{
case 0: set_uart_speed(1200);
break;
case 1: set_uart_speed(2400);
break;
case 2: set_uart_speed(4800);
break;
case 3: set_uart_speed(9600);
break;
case 4: set_uart_speed(19200);
break;
case 5: set_uart_speed(38400);
break;
case 6: set_uart_speed(57600);
break;
case 7: set_uart_speed(115200);
break;
}
}
void set_servos(int s1,int s2, int s3, int s4, int s5, int s6, int s7, int s8){
output_bit(PIN_A0,s1);output_bit(PIN_A1,s2);output_bit(PIN_A2,s3);output_bit(PIN_A3,s4);
output_bit(PIN_B4,s5);output_bit(PIN_B5,s6);output_bit(PIN_B6,s7);output_bit(PIN_B7,s8);
set_timer1(T_ON[servo]);
}
#INT_TIMER1
void Interrupcion_timer1(void){
if (servo==0){
set_servos(1,0,0,0,0,0,0,0);
servo=1;
}else if(servo==1){
set_servos(0,1,0,0,0,0,0,0);
servo=2;
}else if(servo==2){
set_servos(0,0,1,0,0,0,0,0);
servo=3;
}else if(servo==3){
set_servos(0,0,0,1,0,0,0,0);
servo=4;
}else if(servo==4){
set_servos(0,0,0,0,1,0,0,0);
servo=5;
}else if(servo==5){
set_servos(0,0,0,0,0,1,0,0);
servo=6;
}else if(servo==6){
set_servos(0,0,0,0,0,0,1,0);
servo=7;
}else if(servo==7){
set_servos(0,0,0,0,0,0,0,1);
servo=8;
}else if(servo==8){
output_bit(PIN_A0,0);output_bit(PIN_A1,0);output_bit(PIN_A2,0);output_bit(PIN_A3,0);
output_bit(PIN_B4,0);output_bit(PIN_B5,0);output_bit(PIN_B6,0);output_bit(PIN_B7,0);
set_timer1(T_OFF);
} //ESTABLECE EL TIMER PARA MANTENER EL PUSLO ALTO
//O BAJO PARA EL PIN QUE CORRESPONDA
}
void main(void){
config();
output_bit(PIN_B3,1);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
while(TRUE){
delay_ms(50);
if (FLAG==1){
if (MODO==1){
output_bit(PIN_B3,1);
for(int i=0;i<=7;i++){
}
calc_tiempos(pos2);
if (ini==0){
enable_interrupts(INT_TIMER1);
ini=1;
}
}else if (MODO==2){
output_toggle(PIN_B0);
printf("%u\n",pos2
[NSERVO
-1]); calc_tiempos(pos2);
}else if (MODO==3){
printf("%u\n",pos2
[NSERVO
-1]);
}else if (MODO==4){
printf("%u\n",baudrate_config
); config_baud_uart(baudrate_config);
}
FLAG=0;MODO=10;
}
}
}