#include #include "io.h" /*========================================================================== * delay() *-------------------------------------------------------------------------- * Delay function. *========================================================================*/ void delay(){ int i, j; /*++++++*/ for(i=0; i<500; i++) for(j=0; j<10; j++) i = i + 0; return; } /*========================================================================== * hello() *-------------------------------------------------------------------------- * Just to say hello... *========================================================================*/ void hello( void ) { unsigned char loops; unsigned char counter; /*++++++++++++++++++*/ /* Just to show we're alive */ #if 0 for( counter = 0; counter < 0xff ; counter++){ P2 = 0xff ^ counter; delay(); } #else for( loops = 3; loops ; loops--){ for( counter = 1; counter; counter <<= 1){ P2 = 0xff ^ counter; delay(); } } #endif return; } /*========================================================================== * main() *-------------------------------------------------------------------------- * Delay function. *========================================================================*/ sbit READ = P3^2; /* Define these according to how you have connected the */ sbit WRITE = P3^3;/* RD, WR, and INTR pins */ sbit INTR = P3^4; sbit BEEP5 = P3^5; sbit BEEP6 = P3^6; sbit BEEP7 = P3^7; void main( void ) { unsigned char adVal; unsigned char counter1, counter2; unsigned minmaxVal = 0xff; unsigned maxVal; unsigned uptime = 0, downtime = 0, olddowntime = 0, direction = 0; unsigned i; /*+++++++++++++++*/ BEEP5 = 0; BEEP6 = 0; BEEP7 = 0; hello(); READ = 1; WRITE = 1; INTR = 1; for( counter1 = 0; ; counter1++){ for( counter2 = 0; ; counter2++){ /*================================================ * We determine the current signal state here. *==============================================*/ /* Make a low-to-high transition on the WR input */ WRITE=0; WRITE=1; /* wait until the INTR signal makes */ /* high-to-low transition indicating */ /* completion of conversion */ while( INTR == 1); /* Read the voltage value from the port */ READ = 0; adVal = P1; READ = 1; /*================================================ * We process the current signal state here. *==============================================*/ /* Seek the local maximum */ if( adVal > maxVal) maxVal = adVal; /* Determine what to do with the peak local reading: */ if( ! counter1 && ! counter2){ if( maxVal < minmaxVal) /* Currently not used. */ minmaxVal = maxVal; /* Display current local max */ P2 = 0xFF ^ maxVal; /* We do our sound level duration counting here */ if( maxVal >= 0x40 ){ if( direction == 0){ /* Switched intensity states? */ direction = 1; uptime = 1; } else ++uptime; } else{ if( direction == 1){ /* Switched intensity states? */ direction = 0; olddowntime = downtime; downtime = 1; } else ++downtime; } /* Do we have a irritating "dog"? */ if( direction == 0 && olddowntime > 20 && downtime == 3 /* Only fire at this point */ && uptime > 2 && uptime < 10 ){ BEEP5 = 1; BEEP6 = 1; BEEP7 = 1; for( i = 0; i < 20; i++){ /* Bad dog! */ delay(); } BEEP5 = 0; BEEP6 = 0; BEEP7 = 0; } /* We count on the roll over to "reset". */ maxVal = 0; } } } }