#include "main.h"
#define LED PIN_D7
#define RECORDS 1000 // length of buffer
#define NOISE 2 // noise treshold
int16 count, count_new;
int16 log[RECORDS];
int16 record;
#int_RDA
void RDA_isr(void) // Interrupt from received character
{
int16 n;
if(getc()=='r') // Send records
{
printf("%Lu,", record);
for (n=0; n<record; n++) printf("%Lu,", log[n]);
printf("0\n\r");
}
else
{ // Reset counters
set_timer0(0);
count = 0;
count_new = 0;
record = 0;
}
}
void main()
{
setup_adc_ports(AN0_TO_AN7|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_EXT_H_TO_L);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
output_low(LED);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
record = 0;
while(true)
{
set_timer0(0);
count = 0;
count_new = 0;
while(count_new == 0) // waiting for a new radiation event
{
count_new=get_timer0();
}
output_high(LED);
while(count != count_new) // waiting for the end of radiation event
{
count=count_new;
count_new=get_timer0();
delay_us(5);
}
if ((record<RECORDS)&&(count>NOISE)){ log[record++]=count;} // Record radiation event
output_low(LED);
}
}