#include <12F675.h> #device ADC=10 #use delay(internal=4MHz) #FUSES NOWDT //No Watch Dog Timer #FUSES NOPUT //Power Up Timer #FUSES BROWNOUT //No brownout reset #FUSES NOMCLR //Master Clear pin used for I/O //#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES PROTECT //Code protected from reads #define PCBV1 //#define LCDRS232 #ifdef LCDRS232 #define xmitPort PIN_A5 #define rcvPort PIN_A5 #use rs232(baud=600, parity=N, xmit=xmitPort, rcv=rcvPort, bits=8 ) #endif #ifdef PCBV1 #define IO_PROBE PIN_A2 #define IO_BUZZER PIN_A1 #define IO_POWER PIN_A4 #define vProbeADC IO_PROBE #define vProbeCnt 8 #define vProbeDiv 2 //16 #define vProbeAOPOffset 0 #endif #define timer1Set100ms 15123 #define timer1Set50ms 40329 #define buzerOff output_low( IO_BUZZER ) ; #define buzerOn output_high( IO_BUZZER ) ; #define powerOff output_LOW( IO_POWER ) ; #define powerOn output_high( IO_POWER ) ; //#use fast_io(c) int16 vProbe = 0 ; int8 beepWait = 0 ; int8 timerSeconde = 10 ; int1 beepChange = true ; int1 continuityOk = false ; int1 continuityChanged = false ; int16 shutdownTimeout = 3000 ; // --------------------------0R, 3R, 5R, 10R, > const int16 thresholds[6] = { 47, 83, 170, 200, 480 } ; // { 64, 105, 213, 480, 500 } ; int8 threshold, thresholdOld ; #ifdef LCDRS232 #include #endif //#INT_RTCC #INT_TIMER1 void TIMER1_isr(void) { if ( threshold < 3 ) { buzerOn continuityOk = true ; } else { buzerOff continuityOk = false ; } if ( continuityChanged ) { continuityChanged = false ; timerSeconde = 10 ; beepWait = threshold ; } if ( timerSeconde > 0 ) timerSeconde-- ; if ( timerSeconde == 0 ) { beepWait = threshold + 1 ; timerSeconde = 10 ; } beepChange = !beepChange ; if ( beepChange ) { if ( beepWait > 0 ) { beepWait -- ; buzerOff } } else { if ( continuityOk ) buzerOn } if ( threshold == 0 ) buzerOn if ( --shutdownTimeout == 0 ) powerOff set_timer1( timer1Set100ms ); } void delay_msx( int16 d ) { delay_ms( d ) ; //restart_wdt() ; } void buzzN( int8 n, int8 d, int16 p ) { while( n-- ) { //restart_wdt(); output_high( IO_BUZZER ) ; delay_msx( d ) ; output_low( IO_BUZZER ) ; delay_msx( p ) ; } //restart_wdt() ; } void buzz() { //while( true ) { buzzN( 5, 10, 20 ) ; //delay_ms( 2000 ); output_low( IO_BUZZER ) ; //sleep(); //} } int16 getAdc( int8 adc, int8 cnt, int8 div, int8 offset) { int16 adcRead = 0 ; set_adc_channel( adc ); delay_us(10); while ( cnt--) { adcRead += Read_ADC() ; } adcRead /= div ; if ( adcRead > offset ) adcRead -= offset ; else adcRead = 0 ; return ( adcRead ) ; } int8 getThreshold(int16 inData ) { int8 trh = 0 ; for ( trh = 0; trh< 4; trh++ ) { if ( inData <= thresholds[ trh ] ) { break ; } } return trh ; } // ------------------------------------------------------------------------------- // --------------- MAIN ---------------------------------------------------------- // ------------------------------------------------------------------------------- void main(){ powerOn buzerOff //! int8 rc = restart_cause() ; //! //! buzzN( (rc+1), 20, 400 ) ; //! if ( rc != NORMAL_POWER_UP ) buzz() ; port_a_pullups(0b000000) ; //setup_wdt(WDT_2304MS); //~2,3 s reset //restart_wdt() ; //sleep(); delay_msx( 100 ) ; #ifdef LCDRS232 startLcdRs232() ; #endif //setup_adc_ports(sAN0|sAN1|sAN2|sAN3); setup_adc_ports( sAN2 | VSS_VDD ); setup_adc( ADC_CLOCK_INTERNAL ); //set_adc_channel( 3 ); //setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //524 ms overflow setup_timer_1(T1_INTERNAL|T1_DIV_BY_2); //131 ms overflow enable_interrupts(INT_TIMER1); enable_interrupts ( GLOBAL ); //delay_msx( 500 ) ; while ( true ) { //restart_wdt() ; vProbe = getAdc( vProbeADC, vProbeCnt, vProbeDiv, vProbeAOPOffset ) ; threshold = getThreshold( vProbe ) ; //continuityChanged = false ; if ( threshold != thresholdOld ) continuityChanged = true ; thresholdOld = threshold ; #ifdef LCDRS232 // ---------------------------------------------------------------------------------- // ------------------------------- LCD DEBUG ---------------------------------------- // ---------------------------------------------------------------------------------- DI1 //printf( LCDCLS ) ; LCDL(1,0) ; DI1 //printf( "U%Lu I%Lu cg%Lu " , VIn, IOut, cg ) ; printf( "U%Lu ", vProbe ) ; LCDL(2,0) ; DI1 printf( "T%u " ,threshold ) ; EI1 #endif if ( threshold == 0 ) buzerOn } }