#use standard_io(D)
#use standard_io(A)
#use standard_io(B)
#define Servomotor PIN_D0
#define In1 PIN_A0
#define In2 PIN_A1
#define LCD_RS PIN_B0
#define LCD_E PIN_B2 //Inicializo mi LCD
#define LCD_DB4 PIN_B4
#define LCD_DB5 PIN_B5
#define LCD_DB6 PIN_B6
#define LCD_DB7 PIN_B7
#include <lcd1.c>
const int Pi=31; // const = Los datos son de solo lectura, se puede colocar los datos en la memoria para ahorrar espacio "Pulsoinicial"
const int Pm=93; // "Pulsomedio"
const int Pu=155; // "Pulsoultimo"
const int C=30; // Por que 1seg equivalen a 30 RTCC(desbordamiento por timer) Prescale1:16
int Bt0 = 0, Bs= 0; //Bandera t0/s; t0 -> timer0 ; s-> servo
int Kt0 = 0; //Contador timer0
int servo = Pu;
char Bt;
void lcd ()
{
printf(lcd_putc,"\f Posicion servo \n Servo = %u",servo);
delay_ms(200);
}
void posicion_servo()
{
switch (Bt)
{
case 'D':
if(++servo > Pu)
{
servo = Pu;
}
break;
case 'I':
if(--servo < Pi)
{
servo = Pi;
}
break;
}
}
#int_Timer0
Timer0_pulso()
{
Kt0++;
if (Kt0 == 4)
{
set_timer0(C); //inicializo timer
}
if (Kt0 == 5)
{
Bt0 = 1;
Kt0 = 0;
}
}
void main()
{
int ValorT0;
lcd_init();
setup_timer_0(RTCC_INTERNAL | RTCC_DIV_16 );
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
lcd();
set_timer0(0);
set_tris_a(0xFF);
do
{
//Disparo el pulso para PWM
if(Bt0 ==1)
{
Bt0 =0;
output_high(Servomotor);
Bs = 1;
}
// Controlo eL ancho de pulso PWM
if(Bs=1)
{
ValorT0 = get_TIMER0(); // Asigno a ValorT0 el valor del timer0
if(ValorT0 > servo) // timer0 > 0.5ms
{
Bs=0;
output_low(Servomotor);
}
}
//Control con los pulsadores In1
if(In1 == 1)
{
Bt = 'D'; //Derecha
posicion_servo();
}
if(In2 == 1)
{
Bt = 'I'; //Izquierda
posicion_servo();
}
}while(true);
}