#include #include #include #include "srm_p18.h" #pragma DATA _CONFIG1L, _WDT_OFF_1L & _STVREN_OFF_1L & _XINST_OFF_1L & _DEBUG_OFF_1L; #pragma DATA _CONFIG1H, _CP0_OFF_1H; #pragma DATA _CONFIG2L, _FOSC_HS_2L; #pragma DATA _CONFIG2H, _WDTPS_1024_2H; #define PROCESSOR_FREQ 25000000 #pragma CLOCK_FREQ PROCESSOR_FREQ #define Bauds(bval, bmul) (PROCESSOR_FREQ/((bval)*(bmul)) - 1)&0xFF #define BAUD57600 Bauds(57600, 4) #define adc_n(chan) ((((chan)&0xF)<<2) + 0x1) #define thres(chan) (0xF0 + chan) typedef unsigned char uchar; volatile unsigned char count; volatile char Rcsta @RCSTA2; volatile char Txsta @TXSTA2; volatile char Baudcon @BAUDCON2; volatile char Spbrgh @SPBRGH2; volatile char Spbrg @SPBRG2; volatile char Txreg @TXREG2; volatile char Rcreg @RCREG2; volatile bit adc_go @ADCON0.GO_DONE; // ADC start bit bool CLEAR@LATC.2; // clear sample and hold bool LED@LATA.0; // activity indicator inline void do_adc(char adc_chan) { adcon0 = adc_n(adc_chan); adc_go = 1; while(adc_go); } char hex2char(char uc) { uc &= 15; uc += '0'; if(uc>'9') uc +=7; return uc; } void txchr(char ch) { while(!test_bit(Txsta, TRMT)); Txreg = ch; } void setserial() { Baudcon = 0; Baudcon.BRG16 = 1; Spbrgh = 0; Spbrg = BAUD57600; Txsta = 0; Txsta.BRGH = 1; Txsta.TXEN = 1; Rcsta = 0; Rcsta.SPEN = 1; Rcreg = 0; } void setports() { lata = 0; // disable reference voltage source !!! latb = 0; latc = 0b00100000; // enable TX latd = 0; late = 0; latf = 0; latg = 0; lath = 0; latj = 0; trisa = 0b11101110; // RefON, LED(Activity) trisb = 0b11111111; trisc = 0b11000011; // SDO, ResDAT, ResCLK, CapCLR trisd = 0b11111111; trise = 0b11111111; trisf = 0b11111111; trisg = 0b11110101; // LED(Calibration), TX trish = 0b11111111; trisj = 0b11111111; } void config() { rcon = 0; intcon = 0b11010000; // enable peripherial interrupt intcon2 = 0; intcon2.RBPU = 1; // disable weak pull_up adcon0 = 0b00000001; adcon1 = 0b00110000; // external Vref, all AN(0..15) as analog adcon2 = 0b10011010; // right justif, Tacq = 6Tad, Fosc/32 (Tad=1.28us) cvrcon = 0b11110000; // enable & mount Vthres to output, external Vref pie1 = 0; pie2 = 0; pie3 = 0; ipr1 = 0; ipr2 = 0; ipr3 = 0; } void main(void) { setserial(); setports(); config(); count = 0; while(1) { wdt_reset(); // endless loop LED = count.7; } } void interrupt(void) { if(!intcon.INT0IF) return; do_adc(11); CLEAR = true; short channel; MAKESHORT(channel, adresl, adresh); channel -= count; count += 1; count &= 0xF; if(count==0) count = 3; // 13 values cvrcon = thres(count); if(30<=channel&&channel<=1007) { txchr(':'); txchr(hex2char(channel>>8)); txchr(hex2char(channel>>4)); txchr(hex2char(channel)); txchr('\r'); } intcon.INT0IF = 0; CLEAR = false; }