#include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#use delay(clock=20000000)
unsigned int8 posicao[4] = {3, 6, 12, 9}; // {1, 2, 4, 8};
//3=0011 6=0110 12=1100 9=1001
signed int8 passo=0;
int1 dar_passo=0;
signed int8 sentido=-1;
unsigned int8 ref_passo=10,contador=0;
unsigned int16 ref_mcc=127;
//================== MOTOR PASSO =================================//
#int_RTCC
void RTCC_isr(void)
{
if(++contador > ref_passo){
passo = passo + sentido;
if(passo>3)passo=0;
if(passo<0)passo=3;
OUTPUT_D(posicao[passo]);
contador = 0;
}
}
#int_TIMER1
void TIMER1_isr(void)
{
//== Motor CC ======//
set_adc_channel(0);
delay_us( 50 );
ref_mcc = read_adc();
if(ref_mcc<1) ref_mcc =1;
if(ref_mcc>254) ref_mcc =254;
set_pwm1_duty(4*ref_mcc);
//== Motor Passo ======//
set_adc_channel(1);
delay_us( 50 );
ref_passo = read_adc();
if(ref_passo<1) ref_passo =1;
if(ref_passo>254) ref_passo =254;
}
//================== Interrupção externa B0 =====================//
#int_EXT
void EXT_isr(void)
{
sentido *= (-1);
}
//================== SERVOMOTOR =================================//
void Rotation0() //0 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
output_high(PIN_C7);
delay_us(800); // pulse of 800us
output_low(PIN_C7);
delay_us(19200);
}
}
void Rotation45() //0 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
output_high(PIN_C7);
delay_us(1150); // pulse of 800us // 1400 - 180o
output_low(PIN_C7); // x 45o
delay_us(18850);
}
}
void Rotation90() //90 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{ // 180o 0o
output_high(PIN_C7); // 2200 - 800 = 1400 90o = 700
delay_us(1500); // pulse of 1500us
output_low(PIN_C7);
delay_us(18500);
}
}
void Rotation180() //180 Degree
{
unsigned int i;
for(i=0;i<50;i++)
{
output_high(PIN_C7);
delay_us(2200); // pulse of 2200us
output_low(PIN_C7);
delay_us(17800);
}
}
void main()
{
setup_adc_ports(AN0_AN1_AN2_AN3_AN4);
setup_adc(ADC_CLOCK_DIV_16);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DIV_BY_16,255,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(512);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_EXT);
ext_int_edge( L_TO_H );
enable_interrupts(INT_RTCC);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
set_adc_channel(1);
delay_us( 50 );
while(true){
Rotation0(); // 0 graus
delay_ms(4000);
Rotation90(); // 90 graus
delay_ms(4000);
Rotation180(); // 180 graus
delay_ms(4000);
Rotation45(); // 45 graus
delay_ms(6000);
}
}