' Title: Digital dB Converter ' File Name: db60.bs2 ' Range: 60dB (3.0 decades) ' Resolution: 255 counts/60 dB = 4.25 counts/dB ' Hardware: Basic Stamp II + op amp + A/D + analog mux ' Four selectable op amp gains in 5.623:1 ratio ' Author: Allen Rushing, arushing@rochester.rr.com ' 429 Tara Lane ' Webster, NY 14580 ' Copyright(c) Allen Rushing (This work is unpublished.) LUT00 data @0,0,0,1,2,3,3,4,5,6,6,7,8,8,9,10,10,11,11,12,13 LUT01 data @20,13,14,14,15,15,16,16,17,17,18,18,19,19,20,20,21,21,22,22,23 LUT02 data @40,23,23,24,24,25,25,26,26,26,27,27,27,28,28,29,29,29,30,30,30 LUT03 data @60,31,31,32,32,32,33,33,33,34,34,34,34,35,35,35,36,36,36,37,37 LUT04 data @80,37,38,38,38,38,39,39,39,40,40,40,40,41,41,41,41,42,42,42,42 LUT05 data @100,43,43,43,43,44,44,44,44,45,45,45,45,46,46,46,46,47,47,47,47 LUT06 data @120,47,48,48,48,48,49,49,49,49,49,50,50,50,50,50,51,51,51,51,52 LUT07 data @140,52,52,52,52,53,53,53,53,53,53,54,54,54,54,54,55,55,55,55,55 LUT08 data @160,56,56,56,56,56,56,57,57,57,57,57,57,58,58,58,58,58,58,59,59 LUT09 data @180,59,59,59,59,60,60,60,60,60,60,61,61,61,61,61,61,61,62,62,62 LUT10 data @200,62,62,62,63,63,63,63,63,63,63,64 db var byte '0-255 scaled decibel conversion of the input delt var byte 'the db increment according to gain k var word 'counter ADC var inl 'A/D output byte, connected to BS2 P0(lsb) to P7(msb) code var outh 'code (0-3) to select the op amp gain dirl=%00000000 'low byte I/O pins (P0-7) receive A/D count as input dirh=%00000111 'P8-9 for gain code output to DG408; P10 for PWM output 'code outputs gain code to switch: 0,1,2,3 for 'nominal gains of 178, 31.6, 5.62, 1.00, respectively. 'Initialize gain code; start with smallest gain (code=3) code=3 ' k=0 again: 'main loop ' pause 500 'slow down to view debug; remove pause for PWM ' if k=10000 then timeout ' k=k+1 if ADC=255 then gmin_test 'A/D saturated? if ADC<45 then gmax_test 'A/D count too low? 'read scaled dB from the LUT; then add delt according to gain read ADC-45, db lookup code,[0,64,128,192],delt 'lookup the increment db=db+delt 'db byte (0-255) pwm 10,db,5 'DAC by pulse-width-modulated pin 10; 5 cycles debug ? code, ? ADC, ? db, CR 'show the gain code & scaled db 'enabling this debug slows down the update rate goto again 'loop endlessly gmin_test: 'is the gain minimum? if code=3 then range_hi 'input is too big code=code+1 'increment code to decrease gain goto again 'loop endlessly gmax_test: 'is the gain maximum? if code=0 then range_lo 'input is too small code=code-1 'decrement code to increase gain goto again: 'loop endlessly range_hi: 'error message: debug "input too big", CR 'input too big-saturated at min gain; db>255 goto again 'loop endlessly range_lo: 'error message: debug "input too small", CR 'input too small for the LUT; db < 0 goto again 'loop endlessly timeout: debug ? k end