' * John and Rob, Toy Design ' * rob@faludi.com ' * this program reads a microphone signal and operates a relay based ' * upon sound amplitude ' Define ADCIN parameters: DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify TRISB = %00001000 ' Set PORTB to mostly output tx var portb.2 rx var portb.3 ' this port is output blinky var portb.4 relay var portb.1 serialSpeed con 16468 b var byte mikey var word 'our microphone amplitude var word 'overall amplitude of a/c microphone signal 'blink test for b=0 to 5 toggle blinky pause 300 next b main: adcin 0,mikey 'get mic value if mikey >= 512 then amplitude = mikey - 512 ' absolute high value of a/c signal else amplitude = 512 - mikey ' absolute low value of a/c signal endif if amplitude > 256 then high relay ' loud sounds turn on relay else low relay ' soft sounds turn off relay endif serout2 tx, serialSpeed,["Mic: ",dec mikey," Amplitude: ",dec amplitude," Relay: ",dec relay,13,10] goto main