'**************************************************************** '* Name : UNTITLED.BAS * '* Author : Rob Seward * '* Notice : Copyright (c) 2004 Richard Hauck * '* : All Rights Reserved * '* Date : 3/4/2005 * '* Version : 1.0 * '* Notes : * '* : * '**************************************************************** ' PicBasic Pro program to display result of ' 10-bit A/D conversion through serial at 9600 baud ' ' Connect analog input to channel-0 (RA0) ' 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 inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted ADCvar VAR WORD ' Create variable to store result ADCvar2 VAR WORD average var word total var word sampleNum var word i var word ceiling var word cycleCount var word firstCycleVal var word arraySize con 10 array var byte[arraySize] index var byte arrayTotal var word arrayAve var byte picTx var portc.3 lightThresh con 550 occupied var bit 'variable is true if seat is occupied, tied to light sensor index = 0 cycleNum con 10 cycleCount = 0 sampleNum = 100 'bucket system variables numBuckets con 3 volThresh1 con 12 volThresh2 con 30 bucket var word[numBuckets] soundLevel var Byte TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result Pause 500 ' Wait .5 second 'init buckets for i = 0 to 2 step 1 bucket[i] = 0 next main: ADCIN 0, ADCvar ' Read channel 0 to adval i = 0 ceiling = 0 while i < sampleNum ADCIN 0, ADCvar ' Read channel 0 to adval ADCvar = ADCvar / 10 if ADCvar > ceiling then ceiling = ADCvar endif 'average = ADCvar + average i = i + 1 wend adcIN 1, ADCvar2 'spread ceiling values between 0 and 100 if ceiling < 50 then ceiling = 50 endif ceiling = ceiling - 50 ceiling = (ceiling * 20) ceiling = ceiling / 10 if cycleCOunt > cycleNum then cycleCount = 0 gosub decBuckets endif cycleCount = cycleCOunt + 1 gosub fillBuckets if adcvar2 > lightthresh then occupied = 1 else occupied = 0 endif serout2 PORTC.6, 16468, [DEC occupied, 13, 10] 'serout2 PORTC.6, 16468, ["Sound LVL: ",DEC soundLevel, 13, 10] serout2 picTx, non9600, ["P"] serout2 picTx, non9600, [soundLevel] serout2 picTx, non9600, [occupied] GoTo main ' Do it forever ' getArrayAve: arrayTotal = 0 for i = 0 to arraySize arrayTotal = arraytotal + array[i] next i arrayAve = arrayTotal / arraySize return fillBuckets: 'bucket strategy if ceiling < volThresh1 then bucket[0] = bucket[0] + 1 endif if ceiling > volThresh1 then bucket[1] = bucket[1] + 1 endif if ceiling > volthresh2 then bucket[2] = bucket[2] + 1 endif for i = 0 to numBuckets - 1 if bucket[i] > 20 then soundLevel = i endif next i return decBuckets: for i = 0 to numBuckets - 1 if bucket[i] > 0 then bucket[i] = BUCKEt[i] - 1 endif next i return