/********************************************************
*
* Definiciones para los PIC 16F873 16F874 16F876 16F877
* PIC 16F873A 16F874A 16F876A 16F877A
*
* (Estos ultimos tienen comparadores internos)
*
/*******************************************************/
// Definiciones para configurar el Timer0
#define T0_INTERNAL 0
#define EXT_L_TO_H 32
#define EXT_H_TO_L 48
#define T0_DIV_1 8
#define T0_DIV_2 0
#define T0_DIV_4 1
#define T0_DIV_8 2
#define T0_DIV_16 3
#define T0_DIV_32 4
#define T0_DIV_64 5
#define T0_DIV_128 6
#define T0_DIV_256 7
void set_timer0(unsigned char tiempo)
{
TMR0 = tiempo;
}
unsigned char get_timer0()
{
return TMR0;
}
#define setup_timer0(x) OPTION &= (0b11000000); OPTION += x
//Definiciones para configurar el Timer1
#define T1_DISABLE 0
#define T1_INTERNAL 133
#define T1_EXTERNAL 135
#define T1_EXTERNAL_SYNC 131
#define T1_CLK_OUT 8
#define T1_DIV_1 0
#define T1_DIV_2 16
#define T1_DIV_4 32
#define T1_DIV_8 48
void set_timer1(unsigned int tiempo)
{
TMR1L = (tiempo&0x00FF);
TMR1H = (tiempo/256);
}
unsigned int get_timer1()
{
return ((TMR1H*256)+TMR1L);
}
#define setup_timer1(x) T1CON = x
//Definiciones para configurar el Timer2
#define T2_DISABLED 0
#define T2_DIV_1 4
#define T2_DIV_4 5
#define T2_DIV_16 6
void set_timer2(unsigned char tiempo)
{
TMR2 = tiempo;
}
unsigned char get_timer2()
{
return TMR2;
}
#define setup_timer2(x,y,z) T2CON = (x | z*7); PR2 = y
//Definiciones para configurar el CCP
#define CCP_OFF 0
#define CCP_CAPTURE_FE 4
#define CCP_CAPTURE_RE 5
#define CCP_CAPTURE_DIV_4 6
#define CCP_CAPTURE_DIV_16 7
#define CCP_COMPARE_SET_ON_MATCH 8
#define CCP_COMPARE_CLR_ON_MATCH 9
#define CCP_COMPARE_INT 10
#define CCP_COMPARE_RESET_TIMER 11
#define CCP_PWM 12
#define setup_ccp1(x) CCP1CON = x
#define setup_ccp2(x) CCP2CON = x
unsigned int get_ccpr1()
{
return ((CCPR1H*256)+CCPR1L);
}
unsigned int get_ccpr2()
{
return ((CCPR2H*256)+CCPR2L);
}
void set_pwm1_duty (unsigned int duty)
{
CCPR1L = (duty & 0b0000001111111100)/4;
CCP1CON &= 0b00001111;
CCP1CON += (duty & 0b0000000000000011)*16;
}
void set_pwm2_duty (unsigned int duty)
{
CCPR2L = (duty & 0b0000001111111100)/4;
CCP2CON &= 0b00001111;
CCP2CON += (duty & 0b0000000000000011)*16;
}
#if defined(_16F873A)||defined(_16F874A)||defined(_16F876A)||\
defined(_16F877A)
#define NC_NC_NC_NC 7
#define A0_A3_A1_A2 2
#define A0_A3_A1_A2_OUT_ON_A4_A5 3
#define A0_A3_A1_A3_OUT_ON_A4_A5 5
#define A0_A3_A1_A3 4
#define A0_A3_NC_NC_OUT_ON_A4 1
#define A0_VREF_A1_VREF 6
#define A3_VREF_A2_VREF 14
#define CP1_INVERT 16
#define CP2_INVERT 32
#define setup_comparator(x) CMCON = x
#define VREF_DISABLE 0
#define VREF_A2 64
#define VREF_LOW 160
#define VREF_HIGH 128
#define setup_vref(x) CVRCON = x
#endif
//Definiciones para el conversor AD
#define ADC_START_ONLY 3
#define ADC_READ_ONLY 0
#define ADC_START_AND_READ 1
#define ADC_OFF 0
#define ADC_CLOCK_DIV_2 0
#define ADC_CLOCK_DIV_4 256
#define ADC_CLOCK_DIV_8 64
#define ADC_CLOCK_DIV_16 320
#define ADC_CLOCK_DIV_32 128
#define ADC_CLOCK_DIV_64 384
#define ADC_CLOCK_INTERNAL 192
#define ALL_ANALOG 0
#define AN0_AN1_AN2_VREF_AN4_AN5_AN6_AN7 1
#define AN0_AN1_AN2_AN3_AN4 2
#define AN0_AN1_AN2_VREF_AN4 3
#define AN0_AN1_AN3 4
#define AN0_AN1_VREF 5
#define NO_ANALOG 7
#define AN0_AN1_VREF_VREF_AN4_AN5_AN6_AN7 8
#define AN0_AN1_AN2_AN3_AN4_AN5 9
#define AN0_AN1_AN2_VREF_AN4_AN5 10
#define AN0_AN1_VREF_VREF_AN4_AN5 11
#define AN0_AN1_VREF_VREF_AN4 12
#define AN0_AN1_VREF_VREF 13
#define AN0 14
#define AN0_VREF_VREF 15
#define setup_adc(x) ADCON0 = x; ADCON1 += (x&0b0000000100000000)/4
#define setup_adc_ports(x) ADCON1 = (x+128)
void set_adc_channel (unsigned char canal)
{
ADCON0 &= 0b11000111;
ADCON0 += (canal*8);
ADCON0 |= 0b00000011;
}
unsigned int read_adc(unsigned char action)
{
//ADCON0 |= 0b00000011;
//DelayBigUs(20);
if ((action == 1)||(action == 3)) ADCON0 |= 0b00000100;
if (action == 1)
{
while (ADGO == 1);
ADCON0 &= 0b11111000;
ADIF = 0;
}
return (((ADRESH & 0x03)*256)+ADRESL);
}
//Definiciones para habilitar y deshabilitar interrupciones
#define GLOBAL 0x0BC0 //global y perifericas
#define INT_TMR0 0x0B20
#define INT_EXT 0x0B10
#define INT_RB 0x0B08
#define INT_PSP 0x8C80
#define INT_AD 0x8C40
#define INT_EEPROM 0x8D10
#define INT_BUSCOL 0x8D04
#define INT_CCP2 0x8D01
#define INT_RDA 0x8C20
#define INT_TBE 0x8C10
#define INT_SSP 0x8C08
#define INT_CCP1 0x8C04
#define INT_TMR2 0x8C02
#define INT_TMR1 0x8C01
#define INT_COMP 0x8D10
#define L_TO_H 1
#define H_TO_L 0
#define ext_int_edge(x) INTEDG = x
#define enable_interrupt(x) FSR = (x/256); INDF |= x
#define disable_interrupt(x) FSR = (x/256); INDF &= (x ^ 0xFF)