#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES XT //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#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=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
unsigned int32 sumatoria;
unsigned int contador=0, bandera =0;
int16 ADC;
float media;
#int_RTCC
void RTCC_isr(void)
{
bandera=1;
set_timer0(61);
}
#int_AD
void AD_isr(void)
{
contador++;
ADC=read_adc(adc_read_only); //lee el resultado de la conversion
sumatoria=sumatoria+ADC;
read_adc(ADC_START_ONLY); //linicia la conversión solamente
}
void main()
{
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_64);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_timer0(61);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_AD);
enable_interrupts(GLOBAL);
set_adc_channel(0); //the next read_adc call will read channel 0
delay_us(10); //a small delay is required after setting the channel
read_adc(ADC_START_ONLY);
while(TRUE){
if(bandera==1){
media=sumatoria /((float) contador+1);
printf("sumatoria = %LU muestras = %U media= %.0f \n\r"sumatoria,contador,media);
contador =0;
sumatoria =0;
bandera =0;
}
}
}