//********************************************************************************
/* ** Programa para generar una Interrupcion cada 4,096ms usando el TIMER0 ** */
/* ** Con Interrupcion de Baja Prioridad ** */
/* ** 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 - 31 ticks) ** */
/* ** Ancho Pulso minimo 0.5 ms -> 37 ticks de TMR0 ** */
/* ** Ancho pulso medio 1.5 ms -> 99 ticks de TMR0 ** */
/* ** Ancho pulso maximo 2.5 ms -> 161 ticks de TMR0 ** */
//********************************************************************************
/* ** Archivo con definicion de registros y bits del microcontrolador elegido ** */
#include <p18f4550.h>
/* ** Include para el USART ** */
#include <usart.h>
/* ** Include para Timer0 ** */
#include <timers.h>
/* ** ** */
#include <stdio.h>
/* ** Configuracion de los Fuses del microcontrolador ** */
#pragma config FOSC = XT_XT,FCMEN = OFF,IESO = OFF,CPUDIV = OSC1_PLL2
#pragma config PWRT = ON,BOR = OFF,BORV =0,WDT = OFF
#pragma config WDTPS = 32768,CCP2MX = OFF,PBADEN = OFF,LPT1OSC = OFF
#pragma config MCLRE = OFF,STVREN = OFF,LVP = OFF,ICPRT = OFF
#pragma config XINST = OFF,DEBUG = OFF,CP0 = OFF,CP1 = OFF
#pragma config CP2 = OFF,CP3 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF,WRT3 = OFF
#pragma config WRTC = OFF,WRTB = OFF,WRTD = OFF,EBTR0 = OFF
#pragma config EBTR1 = OFF,EBTR2 = OFF,EBTR3 = OFF,EBTRB = OFF
/* ** Directivas de Preprocesador ** */
#define SERVO1 LATBbits.LATB0
/* ** Declaracion de Constantes ** */
const int AJUSTE_FINO = 31;
const int ticks_PULSO_MEDIO = 99;
const int ticks_PULSO_MINIMO = 37;
const int ticks_PULSO_MAXIMO = 161;
/* ** Declaracion de Funciones a utilizar ** */
void ISRBajaPrioridad(void);
void Ajuste_del_Servo(void);
void eco_servos (void);
/* ** Variables de uso General ** */
volatile int Cont = 0;
int FlagRTCC=0;
int FlagSERVO1=0;
int ValorTIMER0;
int PosSERVO1=99;
char Keypress=0x00;
/* ** Seccion de codigo a partir de la direccion 0x0018 ** */
#pragma code BajaPrioridad = 0x18
void VectorPrioridadBaja(void)
{
_asm goto ISRBajaPrioridad _endasm
}
#pragma code // Cerramos seccion.-
/* ** Rutina de Interrupcion ** */
#pragma interrupt ISRBajaPrioridad
void ISRBajaPrioridad(void)
{
if(INTCONbits.TMR0IF==1){
Cont++;
if(Cont==4){
WriteTimer0(AJUSTE_FINO);
}
if(Cont==5){
FlagRTCC=1;
Cont=0;
}
INTCONbits.TMR0IF=0;
}
}
/* ** Main ** */
void main(void)
{
ADCON1 = 0X0F;
TRISB = 0X00;
PORTB = 0X00;
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT &
USART_CONT_RX & USART_BRGH_HIGH,25);
OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_16); // Config.Timer0.-
INTCON2bits.TMR0IP = 0; // Prioridad baja.-
RCONbits.IPEN = 1; // Habilitamos nivel de Prioridad.-
INTCON = 0xE0; // Habilitamos todas las Interrupt de baja prioridad.-
// Permite todas las interrupt de alta Prioridad.-
// Habilitamos Interrupt por overflow .-
WriteTimer0(0); // Inicializamos el Timer0.-
do{
/* ** Disparo del Pulso ** */
if(FlagRTCC==1){
FlagRTCC=0;
SERVO1=1;
FlagSERVO1=1;
}
/* ** Control del ancho del Pulso ** */
if(FlagSERVO1==1){
ValorTIMER0 = ReadTimer0();
if(ValorTIMER0>PosSERVO1){
FlagSERVO1=0;
SERVO1=0;
}
}
/* ** Control del Servo con PC ** */
if(Keypress!=0x00)
{
Ajuste_del_Servo();
Keypress=0x00;
}
} while (1);
}
void Ajuste_del_Servo(void)
{
switch(Keypress)
{
case '1' : PosSERVO1=ticks_PULSO_MINIMO;
break;
case '2' : PosSERVO1=ticks_PULSO_MEDIO;
break;
case '3' : PosSERVO1=ticks_PULSO_MAXIMO;
break;
case '+' : if(++PosSERVO1>ticks_PULSO_MAXIMO)
{
PosSERVO1=ticks_PULSO_MAXIMO;
}
break;
case '-' : if(--PosSERVO1<ticks_PULSO_MINIMO)
{
PosSERVO1=ticks_PULSO_MINIMO;
}
break;
/* ** Posicion del Servo ** */
case 'r' : eco_servos();
break;
}
}
void eco_servos(void)
{
printf("S=%u\r\n",PosSERVO1);
}