'=============================================================================== ' ' V E R S A T I L E E N G I N E S O U N D U N I T ' ' PROGRAM 4 OF 4 - SOUND GENERATION (STEAM) ' '=============================================================================== ' VERSION 2.0 14th October 2008 © A.F.Bond 2008 ' ' (ONLY FOR USE IN CONJUNCTION WITH v2.0 SPEED DEMAND MODULE) '=============================================================================== ' ' This module uses one of the PIC's ADCs to read a speed demand voltage and ' then uses that value to create a variable frequency output signal. The output ' signal comprises repeated cycles of a period of pseudo white noise (to ' simulate the hiss of steam) followed by a period of silence. The repetition ' rate of this cycle is organised to be proportional to the speed demand voltage. ' ' To give the engine a characteristic 'beat', a slightly different pitch of ' pseudo-random noise is assigned to each of the cylinders before looping round ' to repeat the sequence. ' ' A jumper link is read at program launch and its state determines if the ' cylinder selection mode is entered. If so, then the program succesively plays ' a few seconds of fixed speed sound from the 1,2,3,4,5 or 6 cylinder engine ' variants. Removing the link during one of these periods locks the chip into ' that mode. The cylinder count value is then stored in the PICAXE's EEPROM ie ' non-volatile memory, so that when the link is in the 'engine run' position the ' cylinder count set by the user can be read at power up. ' ' NOTE: In the description below be aware that PIN1, PIN2 etc are virtual ' pins and not to be confused with the device's phyical pins, which in PIC ' parlance are refered to as 'legs' ' '=============================================================================== 'VARIABLE DEFINITIONS symbol demand=b0 'speed demand input, read from ADC on "pin4" (leg 3) symbol pawse=w4 'length of pause between chuffs symbol chuff=b2 'length of chuff symbol cyl=b4 'number of cylinders in engine 'note numbering begins at 0 as the lookup tables 'have a zero'th element. The length of the tables 'restricts the maximum cylinder count to six 'CONSTANT DEFINITIONS symbol neutral_LED=0 'neutral LED driven from "pin0" (leg 7) high=ON 'experiment with these values to affect character of the engine 'values must lie in range 128 to 255, but less than 250 unlikey to be used symbol p1=255 'pitch of pseudo random sound for cylinder 1 symbol p2=253 '. . . . . . . . . . . . . . . . . . . . . 2 symbol p3=254 '. . . . . . . . . . . . . . . . . . . . . 3 symbol p4=252 '. . . . . . . . . . . . . . . . . . . . . 4 symbol p5=254 '. . . . . . . . . . . . . . . . . . . . . 5 symbol p6=253 '. . . . . . . . . . . . . . . . . . . . . 6 'CHIP PROGRAMMING DIRECTIVE (only executed when program is loaded into chip) 'Pre-load non-zero value into EEPROM storage location 0 (cylinder count) data 0, (2) 'set default cylinder count to 3 '========================= PROGRAM STARTS HERE ================================ 'INITIALISATION setfreq m8 'set 8Mhz operation to yield 5uSec resolution pause 1000 'allow time for speed input voltage to settle init: if pin3=0 then 'check jumper link 'CYLINDER SELECT MODE high neutral_LED 'turn on LED sound 1,(120,60) 'warning beep for cyl=0 to 5 'step through all cylinder values pause 2000 '1 sec pause between variants (8Mhz doubles values) lookup cyl,(350,240,180,144,140,135),pawse 'establish speed for each demo chuff=pawse/12 'duration of steam sound for b7=0 to 7 'run for eight engine cycles for b5= 0 to cyl 'for each cylinder in turn lookup b5,(p1,p2,p3,p4,p5,p6),b6 'get steam pitch for cylinder sound 1,(b6,chuff) 'make the chuff pause pawse 'silence until next chuff next b5 next b7 if pin3=1 then 'has user selected the current cylinder variant? write 0,cyl 'YES - store value in non-volatile memory sound 1,(120,10) 'acknowledge with a beep goto main 'and jump out of programming mode end if 'NO - try next cylinder count next cyl goto init 'no selection made, loop round again else 'ENGINE RUN MODE read 0, cyl 'get cylinder count previously stored in EEPROM end if 'MAIN LOOP main: readadc 4,demand 'check speed demand status 'process speed demand value pawse=demand*120 max 35000 '120 sets the throttle sensitivity (ie revs range) 'the pawse calculation is limited to 35000 such 'that the next calculation limits the top speed 'to 5000 pawse=40000-pawse '40000 is slowest speed chuff=pawse/1000 'set chuff to fixed proportion of engine cycle 'approx 50% if demand>10 then 'check speed demand 'RUN low 0 'switch off neutral (ie stopped) LED for b5= 0 to cyl 'loop the required number of cylinders lookup b5,(p1,p2,p3,p4,p5,p6),b6 'get steam pitch for cylinder sound 1,(b6,chuff) 'make the chuff pulsout 2, pawse 'silence until next chuff next b5 else 'STOP toggle neutral_LED 'flicker neutral LED to signify stopped pause 80 'sets rate of flicker end if goto main '=============================================================================== ' VERSION HISTORY '=============================================================================== 'v1.0 1st October 2008 'first release 'v2.0 14th October 2008 'Logarithmic lookup table function moved into the speed demand module, which 'allowed a longer lookup table to be used without code execution time problems 'as the speed demand code only has to run at the transmitter frame rate rather 'than the engine revolution rate. The resulting throttle response of the 'overall system is thereby much smoother. ' 'The speed demand processing has been reworked to give single numeric values '(for user experimentation) to set the slowest speed and the revs range plus a 'top speed limiter which operates if the revs range multiplier is set too high. ' 'The timing for main engine loop now uses 'pulsout' (to an unused output) to 'create the inter-cylinder delays rather than the 'pause' statement. Much 'larger values for an equivalent delay result, meaning that the speed resolution '(ie size of the steps between succesive values) is much improved. ' '===============================================================================