;-----------------------------------------------------------------------------------------------; ; Program: CVS Shepard Generator ; ; Developed by David J. Brown ; ; Copyright (c) April 13, 2004 David J. Brown ; ; Email: davebr@earthlink.net ; ; Web site: http://modularsynthesis.com ; ;-----------------------------------------------------------------------------------------------; ; LICENSE AGREEMENT: ; ; This program is free software. You can redistribute it and/or modify it. ; ; ; ; This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, ; ; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ; ;-----------------------------------------------------------------------------------------------; ; ; Description: ; This is a code template for the PSIM or CVS which includes: ; hardware initialization ; interrupt MIDI in and out ; TTS256/speakjet communication support (DJB-PSIM configuration) ; TTS256/speakjet hexadecimal phrase support (DJB-PSIM configuration) ; 1 mS timer interrupts support ; analog output support ; analog input support including averaged (polled and interrupt) or with hysteresis ; rising edge detect support (for pulse width > 1 mS) ; Aux input rising edge interrupt support ; Hex to decimal conversion for fixed column width LCD display ; LCD display of all eight input or output voltages to 1 decimal point (e.g. 8.6) ; ; Inputs: ; Start = ; Stop = ; In-1 = Frequency control ; In-2 = ; In-3 = ; In-4 = ; In-5 = ; In-6 = ; In-7 = ; In-8 = ; Aux = ; Midi-In = ; ; Outputs: ; Out-1 = Ramp ; Out-2 = Ramp 90 degree shift ; Out-3 = Ramp 180 degree shift ; Out-4 = Ramp 270 degree shift ; Out-5 = Triangle ; Out-6 = Triangle 90 degree shift ; Out-7 = Triangle 180 degree shift ; Out-8 = Triangle 270 degree shift ; Aux = ; Midi-Out= ; Speakjet= ; ; Display = displays program name ; Run led = toggles @ 0.5 S (timer interrupts enabled) ; Stop led= ; ; Revision: 0.1S2 ; Date Sept 23, 2010 ; History: 0.1S2 changed serial for Studio 2 ; 0.1S changed i2cin, i2cout and shiftout for Studio ; 0.1 changed running, send_all_off, hex2ascii, display_in, ; and display_out to eliminiate push/pop, added pin ; modifications for IDE8.0.1.7 ; 0.0 initial release of program ; Written: Nov 21, 2007 ; ;---------PSIM OR CVS CONDITIONAL COMPILE OPTION------------------------------------------------; ; ; ; COMMENT OUT NEXT LINE FOR PSIM CONFIGURATION ; cvs con 1 ;define for CVS module ; ; ;comment for PSIM module ; ;-----------------------------------------------------------------------------------------------; ;---------ENABLE AUX_IN RISING EDGE INTERRUPT CONDITIONAL COMPILE OPTION------------------------; ; ; ; COMMENT OUT NEXT LINE TO DISABLE AUX INPUT RISING EDGE INTERRUPTS ; ;aux_int_en con 1 ;define to enable aux_in rising edge interrupts ; ; ;comment to disable aux_in rising edge interrupts ; ;-----------------------------------------------------------------------------------------------; ;---------ENABLE TIMER INTERRUPT CONDITIONAL COMPILE OPTION-------------------------------------; ; Notes: run led will blink to verify timer operation ; ; ; ; COMMENT OUT NEXT LINE TO DISABLE TIMER INTERRUPTS ; ;timer_int_en con 1 ;define to enable timer interrupts ; ; ;comment to disable timer interrupts ; ; ; ; ;------ENABLE AVERAGED ANALOG-IN IN TIMER INTERRUPT CONDITIONAL COMPILE OPTION------------; ; ; ; Note: requires timer_int_en to be defined ; ; ; ; ; ; ; ; COMMENT OUT NEXT LINE TO DISABLE ANALOG-IN ON TIMER INTERRUPTS/ENABLE POLLED ANALOG-IN ; ; ;input_int_en con 1 ;define to enable input interrupt mode ; ; ; ; ;comment to set polled input mode ; ; ; ;-----------------------------------------------------------------------------------------; ; ; ; ; ;------ENABLE RISING EDGE DETECT IN TIMER INTERRUPT CONDITIONAL COMPILE OPTION------------; ; ; ; Note: requires timer_int_en to be defined ; ; ; ; pulse must be > 1 mS and > 8 mS period (7 timer clocks low / 1 timer clock high) ; ; ; ; ; ; ; ; COMMENT OUT NEXT LINE TO DISABLE RISING EDGE DETECT IN TIMER INTERRUPTS ; ; ;edge_int_en con 1 ;define to enable rising edge detect mode ; ; ; ; ;comment to disable rising edge detect mode ; ; ; ; UNCOMMENT ONE OF NEXT THREE LINES TO SELECT INPUT FOR RISING EDGE DETECT ; ; ;edge_in var in4 ;use start jack for rising edge detect input ; ; ;edge_in var in5 ;use stop jack for rising edge detect input ; ; ;edge_in var in8 ;use aux jack for rising edge detect input ; ; ; ;-----------------------------------------------------------------------------------------; ; ; ; ;-----------------------------------------------------------------------------------------------; ; Program: PSIM-CVS Template ; Developed by David J. Brown ; Revision: 1.0 ; Date: November 11, 2007 ; History: 1.1 added subroutines for programmed characters ; 1.0 added CVS configuration and midi processor code, ; simplified hex2ascii and changed from byte to word, ; added get_inputs_hy, rewrote display_in and display_out ; 0.8 removed conditionals for MIDI interrupts, deleted ; polled routines check_midi and get_rcx_bfr, deleted ; obsolete debug_led and hex_console routines, optimized ; midi command subroutines ; 0.7 converted speakjet to i2c protocol, modified speakjet ; phrase definitions and changed hex_speakjet ; 0.66 added edge_flg to detect rising edges in timer isr, ; added aux_cnt for aux_in rising edge interrupts, added ; display command summary ; 0.65 added display_in to display four input voltages, ; added display_out to display four output voltages, ; programmed voltage characters, updated sample code, ; added GPL ; 0.64 added hex2ascii for conversion with leading 0's, ; default display message, removed serial hex_led routine ; 0.63 removed 'high midi_sdata' command which limited ; TxD (P15) to V(ol)=1.5 volts ; 0.62 added hex_lcd routine, changed debug_data to ; data_hex, changed debug_speakjet to hex_speakjet, ; debug_led to hex_led, and debug_console to hex_console, ; added start message ; 0.61 change dirs command for MIDI out pin, changed ; load_outputs to 'or' instead of 'add' ; 0.6 changed midi drivers to use hserial interrupt ; mechanism ; 0.51 changed midi-out during timer interrupt to use ; timerA on 512 uS ; 0.5 added midi-out during timer interrupt, added ; put_tx_bfr, modified send_midi routine, added ; conditional midi-out compile option, added send_cc ; 0.44 streamlined debug_speakjet routine, changed ; gra divisor from 2000 to 2001 for accurate time ; 0.43 corrected send_speakjet pin label error, changed ; display_led to debug_led, changed send_console to ; debug_console, added debug_speakjet routine, changed ; led_data and con_data to debug_data ; 0.42 renamed start_b to start_j, renamed stop_b to ; stop_j, added push/pop to display_led, changed ; comment delimiter to semicolon, added send_console ; routine, re-aliased input pins, sends midi all notes ; off during initialization, added midi program change ; 0.41 upper case conversion to lower, variables and ; labels renamed ; 0.4 added averaged input input driver (polled or ; interrupt mode), display_led driver (for debug), ; speakjet driver, renamed program to djb template ; 0.3 added program header information, modified ; pin initialization ; 0.2 initialize aux output low and speakjet pins ; 0.1 added DAC & led initialization, 1 mS timestamp, ; stop led blinks when midi data received, run ; led toggles @ 0.5 S for health indication ; 0.0 initial release of midi input & output program ; Written: April 13, 2004 ; ;-----------------------------------------------------------------------------------------------; ; BasicMicro AtomPro pin definitions ; ;-----------------------------------------------------------------------------------------------; ; P0 - analog in-1 ; ; P1 - analog in-2 ; ; P2 - analog in-3 ; ; P3 - analog in-4 ; ; P4 - start button (no input/default=low, pressed=hi) ; ; P5 - stop button (no input/default=low, pressed=hi) ; ; P6 - I2C data ; ; P7 - I2C clock ; ; P8 - aux in/out (PSIM: no input=hi, CVS: no input=low) ; ; P9 - stop led (low=off, hi=on) ; ; P10 - run led (low=off hi=on) ; ; P11 - DAC loaddacs ; ; P12 - DAC serial data ; ; P13 - DAC clock ; ; P14 - MIDI-in ; ; P15 - MIDI-out ; ; P16 - analog in-5 (AtomPro28) ; ; P17 - analog in-6 (AtomPro28) ; ; P18 - analog in-7 (AtomPro28) ; ; P19 - analog in-8 (AtomPro28) ; ;-----------------------------------------------------------------------------------------------; ; DJB-LCD commands ; ;-----------------------------------------------------------------------------------------------; ; $08 - (backspace) moves the active display position one backwards ; ; $09 - (tab) moves the active display position one forward ; ; $0a - (line feed) clears the display & sets active display position to beginning of line 1 ; ; $0b - (vertical tab) sets the active display position to beginning of line 2 ; ; $0c - (form feed) clears the display & sets active display position to beginning of line 2 ; ; $0d - (carriage return) Sets the active display position to beginning of line 1 ; ; $0e - (shift out) selects the lower characters set for display ; ; $0f - (shift in) selects the upper characters set for display ; ; $10 - program character0 row7, row6 ... row0 (bottom) ; ; $11 - program character1 row7, row6 ... row0 (bottom) ; ; $12 - program character2 row7, row6 ... row0 (bottom) ; ; $13 - program character3 row7, row6 ... row0 (bottom) ; ; $14 - program character4 row7, row6 ... row0 (bottom) ; ; $15 - program character5 row7, row6 ... row0 (bottom) ; ; $16 - program character6 row7, row6 ... row0 (bottom) ; ; $17 - program character7 row7, row6 ... row0 (bottom) ; ; $18 - sets the display mode to overwrite; new characters overwrite previous ; ; $19 - sets the display mode to scroll; display scrolls for new characters ; ; ; ; DJB-CVS LCD commands: ; ; $1a - sets absolute position data ; ; $1b - sets relative position data ; ; $1c - n/a ; ; $1d - n/a ; ; $1e - n/a ; ; $1f - writes new default message character 1, character 2 ... character 32 ; ; ; ; DJB-PSIM LCD commands: ; ; $1a - n/a ; ; $1b - n/a ; ; $1c - set Out1 data (0=no change, 1=reset to low, 2=set to hi, 3=toggle) ; ; $1d - set Out2 data (0=no change, 1=reset to low, 2=set to hi, 3=toggle) ; ; $1e - set Out3 data (0=no change, 1=reset to low, 2=set to hi, 3=toggle) ; ; $1f - set Out4 data (0=no change, 1=reset to low, 2=set to hi, 3=toggle) ; ;-----------------------------------------------------------------------------------------------; ; TTS-256 / SpeakJet commands ; ;-----------------------------------------------------------------------------------------------; ; enable TTS256 pass through: i2cout i2c_data,i2c_clk,$0,[($48<<1),"passthruon",$0d] ; ; terminate TTS256 pass through: i2cout i2c_data,i2c_clk,$0,[($48<<1),"X"] ; ; $00 - pause 0 mS ; ; $01 - pause 100 mS ; ; $02 - pause 200 mS ; ; $03 - pause 700 mS ; ; $04 - pause 30 mS ; ; $05 - pause 60 mS ; ; $06 - pause 90 mS ; ; $07 - play next sound fast ; ; $08 - play next sound slow ; ; $0e - play next sound high tone ; ; $0f - play next sound low tone ; ; $15 - speed ; ; $16 - pitch ; ; $17 - pitch ; ; $c8 to $d1 - robot sounds ; ; $d2 to $db - alarm sounds ; ; $dc to $e5 - beep sounds ; ; $e6 to $ef - biological sounds ; ; $f0 to $fb - DTMF sounds (0 1 2 3 4 5 6 7 8 9 * #) ; ; $fc - sonar ping ; ; $fd - pistol shot ; ; $fe - wow ; ;-----------------------------------------------------------------------------------------------; ; Non-volatile memory allocation map ; ;-----------------------------------------------------------------------------------------------; nv_step_max con 0 ;adr000 step_max nv_midi_offset con 1 ;adr001 midi_offset nv_gate1_lo con 2 ;adr002 gate1.byte0 nv_gate1_hi con 3 ;adr003 gate1.byte1 nv_gate2_lo con 4 ;adr004 gate2.byte0 nv_gate2_hi con 5 ;adr005 gate2.byte1 nv_patch con 6 ; 16 byte patch array ;nv_patch(0) con 6 ;adr006 step 1 patch ;nv_patch(1) con 7 ;adr007 step 2 patch ;nv_patch(2) con 8 ;adr008 step 3 patch ;nv_patch(3) con 9 ;adr009 step 4 patch ;nv_patch(4) con 10 ;adr010 step 5 patch ;nv_patch(5) con 11 ;adr011 step 6 patch ;nv_patch(6) con 12 ;adr012 step 7 patch ;nv_patch(7) con 13 ;adr013 step 8 patch ;nv_patch(8) con 14 ;adr014 step 9 patch ;nv_patch(9) con 15 ;adr015 step 10 patch ;nv_patch(10) con 16 ;adr016 step 11 patch ;nv_patch(11) con 17 ;adr017 step 12 patch ;nv_patch(12) con 18 ;adr018 step 13 patch ;nv_patch(13) con 19 ;adr019 step 14 patch ;nv_patch(14) con 20 ;adr020 step 15 patch ;nv_patch(15) con 21 ;adr021 step 16 patch nv_seq_mode con 22 ;adr022 seq_mode nv_unused con 23 ;adr023 first unused address ;-----------------------------------------------------------------------------------------------; ; ;pin declarations pin_j1 con p0 ;in-1 pin pin_j2 con p1 ;in-2 pin pin_j3 con p2 ;in-3 pin pin_j4 con p3 ;in-4 pin #ifdef cvs ;these pin definitions changed between IDE 8.0.1.0 and 8.0.1.7 ;uncomment the correct definitions for the version of IDE ;-----------------------------------------------------------------------------------------------; ;IDE 8.0.1.0 and previous ; ; pin_j5 con p16 ;in-5 pin ; ; pin_j6 con p17 ;in-6 pin ; ; pin_j7 con p18 ;in-7 pin ; ; pin_j8 con p19 ;in-8 pin ; ;-----------------------------------------------------------------------------------------------; ;IDE 8.0.1.7 ; pin_j5 con p18 ;in-5 pin ; pin_j6 con p19 ;in-6 pin ; pin_j7 con p17 ;in-7 pin ; pin_j8 con p16 ;in-8 pin ; ;-----------------------------------------------------------------------------------------------; #endif start_j var in4 ;start jack and switch stop_j var in5 ;stop jack and switch i2c_data con p6 ;i2c data pin i2c_clk con p7 ;i2c clock pin aux_j con p8 ;aux jack (use for output) aux_in var in8 ;aux jack (use for input) stop_led con p9 ;stop led stop_led_out var out9 ;stop led alias (for use with LET command) run_led con p10 ;run led run_led_out var out10 ;run led alias (for use with LET command) dac_load con p11 ;dac loaddacs pin dac_sdata con p12 ;dac serial data pin dac_clk con p13 ;dac clock pin ;i/o declarations in_j1 var word ;in-1 value: get_inputs, get_inputs_avg, avg1 in_j2 var word ;in-2 value: get_inputs, get_inputs_avg, avc2 in_j3 var word ;in-3 value: get_inputs, get_inputs_avg, avg3 in_j4 var word ;in-4 value: get_inputs, get_inputs_avg, avg4 #ifdef cvs in_j5 var word ;in-5 value: get_inputs, get_inputs_avg, avg1 in_j6 var word ;in-6 value: get_inputs, get_inputs_avg, avc2 in_j7 var word ;in-7 value: get_inputs, get_inputs_avg, avg3 in_j8 var word ;in-8 value: get_inputs, get_inputs_avg, avg4 #endif out_j1 var word ;out-1 value: load_outputs out_j2 var word ;out-2 value: load_outputs out_j3 var word ;out-3 value: load_outputs out_j4 var word ;out-4 value: load_outputs #ifdef cvs out_j5 var word ;out-5 value: load_outputs out_j6 var word ;out-6 value: load_outputs out_j7 var word ;out-7 value: load_outputs out_j8 var word ;out-8 value: load_outputs olat var byte ;control output latch: load_outputs ; bit7=dac2 select (hi=enable) ; bit6=dac1 select (hi=enable) ; bit5=n/a ; bit4=n/a ; bit3=n/a ; bit2=offset range (hi=5 volts, low=10 volts) ; bit1=offset voltage enable (hi=enable) ; bit0=control voltage enable (hi=enable) ;NOTE: input filter capacitors require settling time after changing voltage references settle con 7 ;special 7 mS settling time due to input filter response #endif ;midi variable declarations allup con $7b ;midi all notes-off command keyup con $80 ;midi note-off command keydn con $90 ;midi note-on command conctrl con $b0 ;midi continuous controller command pgmchg con $c0 ;midi program change command chnprs con $d0 ;midi channel pressure command pitchb con $e0 ;midi pitch bend command midi_chan var nib ;midi channel: send_note_on, send_note_off, send_cc, send_prog_change midi_note var byte ;midi note value: send_note_on, send_note_off midi_vel var byte ;midi velocity value: send_note_on, send_note_off midi_pgm var byte ;midi program change value: send_prog_change midi_cc var byte ;midi continuous controller number: send_cc trans_chan var byte ;midi in channel transpose (added to midi channel) trans_note var byte ;midi in note tranpose (added to note value) last_cmd var byte ;last midi command for running status midi_cmd var byte ;branch address for running status sys_cmd var byte ;branch address for system commands midi_data var byte ;variable: send_midi, get_rcx_bfr, send_note_on, send_note_off rcx_data_flg var bit ;received data flag (1=data, 0=no data): get_rcx_bfr ;timer declarations #ifdef timer_int_en time_count var long ;1 mS timer count value: tm_isr turn_off var long ;time value to turn off stop led: tm_isr #endif ;edge detect declarations #ifdef edge_int_en edge_state var byte ;eight samples of edge input: tm_isr edge_flg var byte ;edge flag (1=rising edge, 0=no rising edge): tm_isr #endif #ifdef aux_int_en aux_cnt var word ;aux interrupt count: aux_isr #endif ;input buffer declarations in_j1_ptr var byte ;in-1 buffer pointer: avgx, get_inputs_avg isr_cnt var nib ;timer pass counter: tm_isr (always define due to conditionals) #ifdef input_int_en in_j2_ptr var byte ;in-2 buffer pointer: avgx in_j3_ptr var byte ;in-3 buffer pointer: avgx in_j4_ptr var byte ;in-4 buffer pointer: avgx in_j5_ptr var byte ;in-5 buffer pointer: avgx in_j6_ptr var byte ;in-6 buffer pointer: avgx in_j7_ptr var byte ;in-7 buffer pointer: avgx in_j8_ptr var byte ;in-8 buffer pointer: avgx #endif in_j1_tmp var long ;in-1 temporary variable: avgx, get_inputs_avg in_j2_tmp var long ;in-2 temporary variable: avgx, get_inputs_avg in_j3_tmp var long ;in-3 temporary variable: avgx, get_inputs_avg in_j4_tmp var long ;in-4 temporary variable: avgx, get_inputs_avg #ifdef cvs in_j5_tmp var long ;in-5 temporary variable: avgx, get_inputs_avg in_j6_tmp var long ;in-6 temporary variable: avgx, get_inputs_avg in_j7_tmp var long ;in-7 temporary variable: avgx, get_inputs_avg in_j8_tmp var long ;in-8 temporary variable: avgx, get_inputs_avg #endif in_j1_bfr var long(2) ;in-1 last 4 samples buffer: avgx, get_inputs_avg in_j2_bfr var long(2) ;in-2 last 4 samples buffer: avgx, get_inputs_avg in_j3_bfr var long(2) ;in-3 last 4 samples buffer: avgx, get_inputs_avg in_j4_bfr var long(2) ;in-4 last 4 samples buffer: avgx, get_inputs_avg #ifdef cvs in_j5_bfr var long(2) ;in-5 last 4 samples buffer: avgx, get_inputs_avg in_j6_bfr var long(2) ;in-6 last 4 samples buffer: avgx, get_inputs_avg in_j7_bfr var long(2) ;in-7 last 4 samples buffer: avgx, get_inputs_avg in_j8_bfr var long(2) ;in-8 last 4 samples buffer: avgx, get_inputs_avg #endif #ifdef cvs hys con 1 ;CVS hysteresis +/- value #else hys con 3 ;PSIM hysteresis +/- value #endif in_j1_low var word ;in-1 low value: get_inputs_hy in_j1_high var word ;in-1 high value: get_inputs_hy in_j2_low var word ;in-2 low value: get_inputs_hy in_j2_high var word ;in-2 high value: get_inputs_hy in_j3_low var word ;in-3 low value: get_inputs_hy in_j3_high var word ;in-3 high value: get_inputs_hy in_j4_low var word ;in-4 low value: get_inputs_hy in_j4_high var word ;in-4 high value: get_inputs_hy #ifdef cvs in_j5_low var word ;in-5 low value: get_inputs_hy in_j5_high var word ;in-5 high value: get_inputs_hy in_j6_low var word ;in-6 low value: get_inputs_hy in_j6_high var word ;in-6 high value: get_inputs_hy in_j7_low var word ;in-7 low value: get_inputs_hy in_j7_high var word ;in-7 high value: get_inputs_hy in_j8_low var word ;in-8 low value: get_inputs_hy in_j8_high var word ;in-8 high value: get_inputs_hy #endif ;display declarations num_data var word ;data for ascii conversion: hex2ascii tths var byte ;ten thousands digit: hex2ascii thos var byte ;thousands digit: hex2ascii huns var byte ;hundreds digit: hex2ascii tens var byte ;tens digit: hex2ascii, display_in, display_out ones var byte ;ones digit: hex2ascii, display_in, display_out tmp_dig var word ;temporary digit: display_in, display_out #ifdef cvs ;external controls declarations ext_ctrl var byte ;external control flag (1=present, 0=not present): check_ext key_data var byte ;keypad data: key_keynum key_prev var byte ;previous keypad data: key_keynum key_temp var byte ;temp keypad: key_keynum key_entry var word ;0-999 keypad entry: get_keyentry #endif ;misc declarations tempb var byte ;temp byte tempb2 var byte ;temp byte tempw var word ;temp word index var word ;index counter temp1 var word ;temporary variable temp2 var word ;temporary variable temp3 var word ;temporary variable temp4 var word ;temporary variable spkj_data var byte ;data: send_speakjet phrase1 var byte ;temporary variable: hex_speakjet phrase2 var byte ;temporary variable: hex_speakjet data_hex var byte ;hex data: hex_speakjet fivevolts con 1920 ;5 volt output value #ifdef cvs gra_val con 2003 ;measured resonator divisor for 1 mS #else gra_val con 2005 ;measured resonator divisor for 1 mS #endif rtable var word(256) ;ramp table ttable var word(256) ;triangle table ; ;-----------------------------------------------------------------------------------------------; ; Tables ; ;-----------------------------------------------------------------------------------------------; ; phrase bytetable "0123456789ABCDEF" ;tts256 digit phrases for hex output ; ;-----------------------------------------------------------------------------------------------; ; Initialization ; ;-----------------------------------------------------------------------------------------------; ; ;initialize pins dires=%0000000000000000 ;configure pin direction (1=output, 0=input) ;note: my PSIM has an AtomPro28 ;inputs: in-7, in-6, in-5, in-4 dirs =%0001011000000000 ;configure pin direction (1=output, 0=input) ;note: setting midi-out, i2c_clock, i2c_data, and dac_load to outputs can glitch so initialize as inputs ;inputs: midi-in, aux, start, stop, in-4, in-3, in-2, in-1 ;initialize as inputs: midi-out, dac_clk, dac_load, ic2_data, ic2_clk ;outputs: dac_sdata, stop led, run led high dac_load ;set dac load high high dac_clk ;set dac clock high so chip select will not clock low run_led ;reset run led low stop_led ;reset stop led ;initialize control hardware #ifdef cvs let olat=$ff ;set olat to default i2cout i2c_data,i2c_clk,($20<<1),[$0a,olat];set outputs to olat i2cout i2c_data,i2c_clk,($20<<1),[$00,$00] ;then set iodir to output #endif ;initialize outputs let out_j1=0 ;set out-1 low let out_j2=0 ;set out-2 low let out_j3=0 ;set out-3 low let out_j4=0 ;set out-4 low #ifdef cvs let out_j5=0 ;set out-5 low let out_j6=0 ;set out-6 low let out_j7=0 ;set out-7 low let out_j8=0 ;set out-8 low #endif gosub load_outputs ;initialize input variables let in_j1_ptr=0 ;reset in-1 pointer #ifdef input_int_en let in_j2_ptr=0 ;reset in-2 pointer let in_j3_ptr=0 ;reset in-3 pointer let in_j4_ptr=0 ;reset in-4 pointer let isr_cnt=0 ;reset isr pass counter #endif let in_j1_bfr(0)=0 ;reset in-1 buffer data let in_j1_bfr(1)=0 let in_j2_bfr(0)=0 ;reset in-2 buffer data let in_j2_bfr(1)=0 let in_j3_bfr(0)=0 ;reset in-3 buffer data let in_j3_bfr(1)=0 let in_j4_bfr(0)=0 ;reset in-4 buffer data let in_j4_bfr(1)=0 #ifdef cvs let in_j5_bfr(0)=0 ;reset in-5 buffer data let in_j5_bfr(1)=0 let in_j6_bfr(0)=0 ;reset in-6 buffer data let in_j6_bfr(1)=0 let in_j7_bfr(0)=0 ;reset in-7 buffer data let in_j7_bfr(1)=0 let in_j8_bfr(0)=0 ;reset in-8 buffer data let in_j8_bfr(1)=0 #endif let in_j1_low=0 ;initialize in-1 hysteresis values let in_j1_high=2*hys let in_j2_low=0 ;initialize in-2 hysteresis values let in_j2_high=2*hys let in_j3_low=0 ;initialize in-3 hysteresis values let in_j3_high=2*hys let in_j4_low=0 ;initialize in-4 hysteresis values let in_j4_high=2*hys #ifdef cvs let in_j5_low=0 ;initialize in-5 hysteresis values let in_j5_high=2*hys let in_j6_low=0 ;initialize in-6 hysteresis values let in_j6_high=2*hys let in_j7_low=0 ;initialize in-7 hysteresis values let in_j7_high=2*hys let in_j8_low=0 ;initialize in-8 hysteresis values let in_j8_high=2*hys #endif ;initialize midi variables let midi_chan=0 ;initialize midi channel let midi_note=0 ;initialize midi note let midi_vel=64 ;initialize default velocity let midi_cmd=$0f ;initialize last midi command to invalid let trans_note=0 ;initialize transpose note to none let trans_chan=0 ;initialize transpose channel to none #ifdef cvs ;define external control variables let key_prev=255 ;set keypad to all keys up let ext_ctrl=0 ;default to external controls not present #endif #ifdef edge_int_en ;define edge interrupt variables let edge_state=0 ;reset current state let edge_flg=0 ;reset edge flag #endif ;enable aux_in rising edge interrupts #ifdef aux_int_en let aux_cnt=0 ;reset count let pmr1=pmr1|%00100000 ;set Port Mode Register to enable irq1 let iegr1=iegr1|%00000010 ;set irq1 to rising edge let irr1=irr1&%11111101 ;clear any pending interrupt oninterrupt irq1int,aux_isr enable irq1int ;enable rising edge interrupts #endif ;initialize timerW hardware for 1 mS #ifdef timer_int_en let tmrw=%10001000 ;set Timer Mode Register to enable count let tcrw=%10110000 ;set Timer Control Register ; clear on compare match A ; 16 MHz clock /8 prescalar S = 2 MHz timer clock let gra=gra_val ;divide 2 MHz for 1 mS interrupt let time_count=0 ;initialize real time count let turn_off=0 ;initialize turn off time oninterrupt timerwint_imiea, tm_isr enable timerwint_imiea ;enable timer interrupt #endif ;enable midi hardware sethserial1 h31200,h8databits,hnoparity,h1stopbits gosub send_all_off ;turn off any stuck notes ;display program name #ifdef cvs pause 50 ;allow display to initialize hserout [$f0,$7d,$0a,$18,"DJB-CVS Program ModularSynthesis",$f7] ;clear, overwrite, name #else pause 250 ;longer for asynchronous reset hserout [$f0,$7d,$0a,$18,"DJB-PSIMprogram ",$f7] ;clear, overwrite, name ; i2cout i2c_data,i2c_clk,($48<<1),["i am ready",$0d] ;announce #endif ; ;-----------------------------------------------------------------------------------------------; ; Main Program ; ;-----------------------------------------------------------------------------------------------; ; lp: ;determine frequency let adin pin_j1,in_j1 let temp1=((in_j1<<4)^$3fff)+1 ;set frequency range 16536 - 1 ; ;output one cycle for index=0 to 1023 step 16 ;2.67 volts let out_j1=1023-index ;saw let out_j5=2*(out_j1 max (1023-out_j1));triangle let out_j2=(255-index)&$03ff ;saw let out_j6=2*(out_j2 max (1023-out_j2));triangle let out_j3=(511-index)&$03ff ;saw let out_j7=2*(out_j3 max (1023-out_j3));triangle let out_j4=(767-index)&$03ff ;saw let out_j8=2*(out_j4 max (1023-out_j4));triangle gosub load_outputs ; pauseus temp1 next goto lp for index=0 to 4095 step 64 ;10.666 volts let out_j1=4095-index ;saw let out_j5=2*(out_j1 max (4095-out_j1));triangle let out_j2=(1023-index)&$0fff ;saw let out_j6=2*(out_j2 max (4095-out_j2));triangle let out_j3=(2047-index)&$0fff ;saw let out_j7=2*(out_j3 max (4095-out_j3));triangle let out_j4=(3071-index)&$0fff ;saw let out_j8=2*(out_j4 max (4095-out_j4));triangle gosub load_outputs ; pauseus temp1 next goto lp begin: ;create 256 long wave table of saw function for index=0 to 255 let rtable(index)=15*(255-index) ;scale from 256 to 3840 for 10+ volt output next ; ;create 256 long wave table of ramp function ; for alpha=0 to 255 ; let rtable(alpha)=15*alpha ;scale from 256 to 3840 for 10+ volt output ; next ;create 256 long wave table of triangle function for index=0 to 255 if index <128 then let ttable(index)=30*index ;scale 2x for each half cycle to 3840 for 10+ volt output else let ttable(index)=30*(255-index) endif next loop: ;determine frequency let adin pin_j1,in_j1 let temp1=((in_j1<<4)^$3fff)+1 ;set frequency range 16536 - 1 ; ;output one cycle for index=0 to 255 let out_j1=rtable(index) ;ramp let out_j5=ttable(index) ;triangle let out_j2=rtable((index+192)&$ff) ;ramp offset 90 degrees, wrap at 360 let out_j6=ttable((index+192)&$ff) ;triangle offset 90 degrees, wrap at 360 let out_j3=rtable((index+128)&$ff) ;ramp offset 180 degrees, wrap at 360 let out_j7=ttable((index+128)&$ff) ;triangle offset 180 degrees, wrap at 360 let out_j4=rtable((index+64)&$ff) ;ramp offset 270 degrees, wrap at 360 let out_j8=ttable((index+64)&$ff) ;triangle offset 270 degrees, wrap at 360 gosub load_outputs pauseus temp1 next goto loop ; ;-----------------------------------------------------------------------------------------------; ; MIDI Protocol Subroutines ; ;-----------------------------------------------------------------------------------------------; ; ;process midi in commands ;transposes note to midi note+trans_note ;transposes channel to midi channel+trans_chan ;written by David J. Brown proc_midi: hserin proc_none,0,[midi_data] if midi_data=$fe then proc_midi ;ignore active status high stop_led #ifdef timer_int_en disable timerwint_imiea let turn_off=time_count+20 ;set led time enable timerwint_imiea #endif if midi_data>$f7 then real_time ;check for real time if midi_data.bit7=0 then running ;check to see if running status let last_cmd=midi_data ;save command to expand running let midi_cmd=(midi_data>>4)&$07 ;strip midi channel for branch branch midi_cmd,[mkey_up,mkey_dn,key_prs,ctrl_chg,prgm_chg,chan_prs,ptch_bnd,sys_com] ; proc_none: return ; ;running status running: let tempb=midi_data ;save second byte let midi_data=(last_cmd&$f0)|((last_cmd+trans_chan)&$0f) ;expand to non-running and transpose midi channel hserout [midi_data] let midi_data=tempb ;restore second byte branch midi_cmd,[mkey_upr,mkey_dnr,key_prsr,ctrl_chgr,prgm_chgr,chan_prsr,ptch_bndr,proc_none] ; ;note off command mkey_up: let midi_data=keyup|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer mkey_upr: let midi_data=midi_data+trans_note ;transpose midi note hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;note on command mkey_dn: let midi_data=keydn|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer mkey_dnr: let midi_data=midi_data+trans_note ;transpose midi note hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;key pressure command key_prs: let midi_data=chnprs|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer key_prsr: hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;control change command ctrl_chg: let midi_data=conctrl|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer ctrl_chgr: hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;program change command prgm_chg: let midi_data=pgmchg|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer prgm_chgr: hserout [midi_data] return ; ;channel pressure command chan_prs: let midi_data=chnprs|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer chan_prsr: hserout [midi_data] return ; ;pitch bend command ptch_bnd: let midi_data=pitchb|((midi_data+trans_chan)&$0f) ;transpose midi channel hserout [midi_data] gosub get_next_byte ;get data from received buffer ptch_bndr: hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;system common command sys_com: let sys_cmd=midi_data&$07 ;get system common command branch sys_cmd,[sys_ex,proc_none,song_ptr,song_sel,proc_none,proc_none,tune_req,proc_none] ; ;system exclusive sys_ex: hserout [midi_data] sys_ex2: gosub get_next_byte ;get data from received buffer hserout [midi_data] ;send it if midi_data<>$f7 then sys_ex2 ;check for end of string return ; ;song pointer command song_ptr: hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;song select command song_sel: hserout [midi_data] gosub get_next_byte ;get data from received buffer hserout [midi_data] return ; ;tune_request command tune_req: hserout [midi_data] return ; ;real time commands real_time: let sys_cmd=midi_data&$07 ;strip high bits for branch branch sys_cmd,[time_clk,proc_none,start_cmd,cont_cmd,stop_cmd,proc_none,proc_none,reset_cmd] return ; ;timing clock command time_clk: hserout [midi_data] return ; ;start command start_cmd: hserout [midi_data] return ; ;start command cont_cmd: hserout [midi_data] return ; ;start command stop_cmd: hserout [midi_data] return ; ;start command reset_cmd: hserout [midi_data] return ; ;wait for data and check for real time data get_next_byte: hserin get_next_byte,0,[midi_data] if midi_data=$fe then get_next_byte ;ignore active status if midi_data>$f7 then ;check for real time gosub real_time goto get_next_byte endif return ; ;-----------------------------------------------------------------------------------------------; ; MIDI Subroutines ; ;-----------------------------------------------------------------------------------------------; ; ;send midi note-on command ;midi_chan is channel ;midi_note is note ;midi_vel is velocity send_note_on: hserout [keydn|midi_chan,midi_note,midi_vel] return ; ;send midi note-off command ;midi_chan is channel ;midi_note is note send_note_off: hserout [keyup|midi_chan,midi_note,0] return ; ;send midi all notes off command on all channels send_all_off: let tempb=midi_chan for midi_chan=0 to 15 ;send on all channels hserout [conctrl|midi_chan,allup,0] ;send all notes off next let midi_chan=tempb return ; ;send midi program change command ;midi_chan is channel ;midi_pgm is program change send_prog_change: hserout [pgmchg|midi_chan,midi_pgm] return ; ;send midi continuous controller command ;midi_cc is controller number ;midi_data is controller data send_cc: hserout [conctrl|midi_chan,midi_cc,midi_data] return ; ;get midi_data from buffer ;rcx_data_flg=1 if successful, rcx_data_flg=0 if no data in buffer get_rcx_bfr: let rcx_data_flg=0 ;set empty flag get_rcx_bfr1: hserin get_rcx_bfr2,0,[midi_data] if midi_data=$fe then get_rcx_bfr1 ;ignore active status let rcx_data_flg=1 ;set data returned flag get_rcx_bfr2: return ; ;send midi_data send_midi: hserout [midi_data] ;put data in transmit buffer return ; ;-----------------------------------------------------------------------------------------------; ; SpeakJet Subroutines ; ;-----------------------------------------------------------------------------------------------; ; ;send spkj_data via i2c ;retransmit on error for flow control send_speakjet: i2cout i2c_data,i2c_clk,send_speakjet,($48<<1),[spkj_data] ;send to TTS256 return ; ;send data_hex to speakjet ;pause 100 mS before first nibble and 60 mS before second nibble ;written by David J. Brown hex_speakjet: let phrase1=data_hex>>4 ;get high nibble let phrase1=phrase(phrase1) ;get first byte let phrase2=data_hex&$0f ;get bottom nibble let phrase2=phrase(phrase2) ;get first byte hex_error: i2cout i2c_data,i2c_clk,hex_error,($48<<1),[$01,phrase1,$05,phrase2,$0d] ;send to TTS256 return ; ;-----------------------------------------------------------------------------------------------; ; Display Subroutines ; ;-----------------------------------------------------------------------------------------------; ; ;program input voltage characters prog_in_chars: hserout [$f0,$7d,$10,$01,$1f,$09,$00,$11,$1f,$11,$f7] ;character0 "I1" hserout [$f0,$7d,$11,$0d,$15,$17,$00,$11,$1f,$11,$f7] ;character1 "I2" hserout [$f0,$7d,$12,$0e,$15,$15,$00,$11,$1f,$11,$f7] ;character2 "I3" hserout [$f0,$7d,$13,$1f,$04,$1c,$00,$11,$1f,$11,$f7] ;character3 "I4" #ifdef cvs hserout [$f0,$7d,$14,$12,$15,$1d,$00,$11,$1f,$11,$f7] ;character4 "I5" hserout [$f0,$7d,$15,$02,$15,$0e,$00,$11,$1f,$11,$f7] ;character5 "I6" hserout [$f0,$7d,$16,$18,$17,$10,$00,$11,$1f,$11,$f7] ;character6 "I7" hserout [$f0,$7d,$17,$0a,$15,$0a,$00,$11,$1f,$11,$f7] ;character7 "I8" #endif return ; ;program output voltage characters prog_out_chars: hserout [$f0,$7d,$10,$01,$1f,$09,$00,$0e,$11,$0e,$f7] ;character0 "O1" hserout [$f0,$7d,$11,$0d,$15,$17,$00,$0e,$11,$0e,$f7] ;character1 "O2" hserout [$f0,$7d,$12,$0e,$15,$15,$00,$0e,$11,$0e,$f7] ;character2 "O3" hserout [$f0,$7d,$13,$1f,$04,$1c,$00,$0e,$11,$0e,$f7] ;character3 "O4" #ifdef cvs hserout [$f0,$7d,$14,$12,$15,$1d,$00,$0e,$11,$0e,$f7] ;character4 "I5" hserout [$f0,$7d,$15,$02,$15,$0e,$00,$0e,$11,$0e,$f7] ;character5 "I6" hserout [$f0,$7d,$16,$18,$17,$10,$00,$0e,$11,$0e,$f7] ;character6 "I7" hserout [$f0,$7d,$17,$0a,$15,$0a,$00,$0e,$11,$0e,$f7] ;character7 "I8" #endif return ; ;convert word num_data to three ascii digits ;updates tths, thos, huns, tens, ones variables ;use to generate leading zeros for fixed column display ;written by David J. Brown hex2ascii: let tempw=num_data ;save data let tths=num_data/10000 ;get ten thousands let num_data=num_data//10000 ;get thousands remainder let tths=tths+$30 ;convert to ascii let thos=num_data/1000 ;get thousands let num_data=num_data//1000 ;get hundreds remainder let thos=thos+$30 ;convert to ascii let huns=num_data/100 ;get hundreds let num_data=num_data//100 ;get tens remainder let huns=huns+$30 ;convert to ascii let tens=num_data/10 ;get tens let num_data=num_data//10 ;get ones remainder let tens=tens+$30 ;convert to ascii let ones=num_data+$30 ;convert to ascii let num_data=tempw ;restore data return ; ;display input voltages in_jx ;range must be 0 - 1023 ;10 displays as "X" (e.g. X.0 volts) ;written by David J. Brown display_in: let tempb=ones let tempb2=tens ;(*33)/337 is divide by 10.23 to convert 1023 to 100 (10.0 volts) let tmp_dig=(in_j1*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$f0,$7d,$0d,$00,ones,".",tens] ;home1 and display in_j1 voltage let tmp_dig=(in_j2*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$01,ones,".",tens] ;display in_j2 voltage let tmp_dig=(in_j3*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$02,ones,".",tens] ;display in_j3 voltage let tmp_dig=(in_j4*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$03,ones,".",tens,$f7] ;display in_j4 voltage and terminate sysex #ifdef cvs let tmp_dig=(in_j5*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$f0,$7d,$0b,$04,ones,".",tens] ;home2 and display in_j5 voltage let tmp_dig=(in_j6*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$05,ones,".",tens] ;display in_j6 voltage let tmp_dig=(in_j7*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$06,ones,".",tens] ;display in_j7 voltage let tmp_dig=(in_j8*33)/337 ;scale 0 to 100 if tmp_dig=100 then ;check for 10.0 let ones=$58 ;set ones digit to X let tens=$30 ;set tenths digit to 0 else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$07,ones,".",tens,$f7] ;display in_j8 voltage and terminate sysex #endif let tens=tempb2 let ones=tempb return ; ;display output voltages out_jx ;range must be 0 - 4095 ;10 displays as "X" (e.g. X.3 volts) ;written by David J. Brown display_out: let tempb=ones let tempb2=tens ;(/4)*25)/240 is divide by 38.4 to convert 4095 to 106 (10.6 volts) let tmp_dig=((out_j1/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$f0,$7d,$0d,$00,ones,".",tens] ;home1 and display out_j1 voltage let tmp_dig=((out_j2/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$01,ones,".",tens] ;display out_j2 voltage let tmp_dig=((out_j3/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$02,ones,".",tens] ;display out_j2 voltage let tmp_dig=((out_j4/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$03,ones,".",tens,$f7] ;display out_j4 voltage and terminate sysex #ifdef cvs let tmp_dig=((out_j5/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$f0,$7d,$0b,$04,ones,".",tens] ;home2 and display out_j5 voltage let tmp_dig=((out_j6/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$05,ones,".",tens] ;display out_j6 voltage let tmp_dig=((out_j7/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$06,ones,".",tens] ;display out_j7 voltage let tmp_dig=((out_j8/4)*25)/240 ;scale 0 to 100 if tmp_dig>99 then ;check for 10.0 to 10.6 let ones=$58 ;set ones digit to X let tens=(tmp_dig-100)+$30 ;set tenths digit to remainder else let tmp_dig=bin2bcd tmp_dig ;convert to two digits let ones=(tmp_dig.nib1)+$30 ;get ones digit and convert to ascii let tens=(tmp_dig.nib0)+$30 ;get tenths digit and convert to ascii endif hserout [$07,ones,".",tens,$f7] ;display out_j8 voltage and terminate sysex #endif let tens=tempb2 let ones=tempb return ; ;-----------------------------------------------------------------------------------------------; ; Input and Output Subroutines ; ;-----------------------------------------------------------------------------------------------; ; #ifndef input_int_en ;sample in-1 to in-4 ;in_jx is input value (0 - 1023) ;150 uS execution time for PSIM ;300 uS execution time for CVS get_inputs: adin pin_j1,in_j1 adin pin_j2,in_j2 adin pin_j3,in_j3 adin pin_j4,in_j4 #endif #ifdef cvs adin pin_j5,in_j5 adin pin_j6,in_j6 adin pin_j7,in_j7 adin pin_j8,in_j8 #endif return ; ;sample and average in-1 to in-x ;in_jx is input value averaged over last four samples (0 - 1023) ;in_jx_bfr(0) & in_jx_bfr(1) are last four word samples ;600 uS execution time for PSIM ;1.2 mS execution time for CVS ;written by David J. Brown get_inputs_avg: adin pin_j1,in_j1_bfr.word1(in_j1_ptr) ;get in-1 into buffer let in_j1_tmp=in_j1_bfr(0)+in_j1_bfr(1) ;add two high words together and low words together let in_j1=(in_j1_tmp.word1+in_j1_tmp.word0)/4 adin pin_j2,in_j2_bfr.word1(in_j1_ptr) ;get in-2 into buffer let in_j2_tmp=in_j2_bfr(0)+in_j2_bfr(1) ;add two high words together and low words together let in_j2=(in_j2_tmp.word1+in_j2_tmp.word0)/4 adin pin_j3,in_j3_bfr.word1(in_j1_ptr) ;get in-3 into buffer let in_j3_tmp=in_j3_bfr(0)+in_j3_bfr(1) ;add two high words together and low words together let in_j3=(in_j3_tmp.word1+in_j3_tmp.word0)/4 adin pin_j4,in_j4_bfr.word1(in_j1_ptr) ;get in-4 into buffer let in_j4_tmp=in_j4_bfr(0)+in_j4_bfr(1) ;add two high words together and low words together let in_j4=(in_j4_tmp.word1+in_j4_tmp.word0)/4 #ifdef cvs adin pin_j5,in_j5_bfr.word1(in_j1_ptr) ;get in-5 into buffer let in_j5_tmp=in_j5_bfr(0)+in_j5_bfr(1) ;add two high words together and low words together let in_j5=(in_j5_tmp.word1+in_j5_tmp.word0)/4 adin pin_j6,in_j6_bfr.word1(in_j1_ptr) ;get in-6 into buffer let in_j6_tmp=in_j6_bfr(0)+in_j6_bfr(1) ;add two high words together and low words together let in_j6=(in_j6_tmp.word1+in_j6_tmp.word0)/4 adin pin_j7,in_j7_bfr.word1(in_j1_ptr) ;get in-7 into buffer let in_j7_tmp=in_j7_bfr(0)+in_j7_bfr(1) ;add two high words together and low words together let in_j7=(in_j7_tmp.word1+in_j7_tmp.word0)/4 adin pin_j8,in_j8_bfr.word1(in_j1_ptr) ;get in-8 into buffer let in_j8_tmp=in_j8_bfr(0)+in_j8_bfr(1) ;add two high words together and low words together let in_j8=(in_j8_tmp.word1+in_j8_tmp.word0)/4 #endif let in_j1_ptr=(in_j1_ptr+1)&$03 ;increment pointer and wrap at 3 return ; #ifndef input_int_en ;sample in-1 to in-x with hysteresis ;in_jx is input value (0 - 1023) ;550 uS execution time for PSIM ;1.1 mS execution time for CVS ;written by David J. Brown get_inputs_hy: adin pin_j1,in_j1 let in_j1=in_j1+hys ;offset for compare at zero if in_j1>in_j1_low then ;check lower range if in_j1in_j2_low then ;check lower range if in_j2in_j3_low then ;check lower range if in_j3in_j4_low then ;check lower range if in_j4in_j5_low then ;check lower range if in_j5in_j6_low then ;check lower range if in_j6in_j7_low then ;check lower range if in_j7in_j8_low then ;check lower range if in_j8242 then let key_data=255 ;no keys down elseif key_temp>221 let key_data=10 ;* key elseif key_temp>201 let key_data=7 ;7 key elseif key_temp>180 let key_data=4 ;4 key elseif key_temp>158 let key_data=1 ;1 key elseif key_temp>137 let key_data=0 ;0 key elseif key_temp>116 let key_data=8 ;8 key elseif key_temp>95 let key_data=5 ;5 key elseif key_temp>74 let key_data=2 ;2 key elseif key_temp>54 let key_data=11 ;# key elseif key_temp>33 let key_data=9 ;9 key elseif key_temp>10 let key_data=6 ;6 key else let key_data=3 ;3 key endif if key_data=key_prev then ;check if state change return ;two scans same so have key value endif let key_prev=key_data ;save state pause 20 ;let keys settle goto get_keynum ;scan again ; ;get keypad entry 0 - 999 ;displays digits at current character position ;* or # terminates ;written by David J. Brown get_keyentry gosub get_keynum if key_data<>255 then get_keyentry ;wait for keys up get_key1: gosub get_keynum if key_data=255 then get_keyentry ;loop until key entry if key_data>9 then get_keyentry ;must be 0 - 9 let key_entry=key_data ;save digit hserout [$f0,$7d,key_data+$30,$f7] ;display digit get_key2: gosub get_keynum if key_data<>255 then get_key2 ;wait for key up get_key3: gosub get_keynum if key_data=255 then get_key3 ;loop until key entry if key_data>9 then get_keydone ;done with entry hserout [$f0,$7d,key_data+$30,$f7] ;display digit let key_entry=(key_entry*10)+key_data ;shift and save digit get_key4: gosub get_keynum if key_data<>255 then get_key4 ;wait for key up get_key5: gosub get_keynum if key_data=255 then get_key5 ;loop until key entry if key_data>9 then get_keydone ;done with entry hserout [$f0,$7d,key_data+$30,$f7] ;display digit let key_entry=(key_entry*10)+key_data ;shift and save digit get_key6: gosub get_keynum if key_data<>255 then get_key6 ;wait for keys up get_keydone: return #endif ; #ifdef cvs ;set input normal voltage to 10 volts nmlv_10: let olat.bit0=1 ;set for input normal voltage to +10 i2cout i2c_data,i2c_clk,($20<<1),[$0a,olat] ;set olat pause settle ;let input filters settle return ; ;set input normal voltage to 0 nmlv_0: let olat.bit0=0 ;reset for input normal voltage to 0 i2cout i2c_data,i2c_clk,($20<<1),[$0a,olat] ;set olat pause settle ;let input filters settle return ; ;set input reference voltage to 10 volts refv_10: let olat.bit1=1 ;set to enable input reference voltage let olat.bit2=0 ;reset for input reference voltage to +10 i2cout i2c_data,i2c_clk,($20<<1),[$0a,olat] ;set olat pause settle ;let input filters settle return ; ;set input reference voltage to 5 volts refv_5: let olat.bit1=1 ;set to enable input reference voltage let olat.bit2=1 ;set for input reference voltage to +5 i2cout i2c_data,i2c_clk,($20<<1),[$0a,olat] ;set olat pause settle ;let input filters settle return ; ;set input reference voltage to 0 refv_0: let olat.bit1=0 ;reset for input reference voltage to 0 i2cout i2c_data,i2c_clk,($20<<1),[$0a,olat] ;set olat pause settle ;let input filters settle return #endif ; ;-----------------------------------------------------------------------------------------------; ; Interrupt Service Routines ; ;-----------------------------------------------------------------------------------------------; ; #ifdef aux_int_en ;interrupt service routine for aux input rising edge aux_isr: let aux_cnt=aux_cnt+1 ;count number of aux interrupts resume ;ignore overflow #endif ; #ifdef timer_int_en ;interrupt service routine for timer ;increments time_count value ;turn off stop led at turn_off value ;toggle run led at 512 mS intervals ;written by David J. Brown tm_isr: toggle aux_j let time_count=time_count+1 ;increment real time count if time_count=turn_off then ;value to turn off stop led low stop_led endif let run_led_out=time_count.bit9 ;toggle run led at 512 mS intervals #endif #ifdef edge_int_en ;check for trigger rising edge ;pulse width must be > 1 ms timer let edge_state=edge_state<<1 ;shift previous state left let edge_state.bit0=edge_in ;set low bit to current input state if edge_state=$01 then ;edge detect requires 7 previous lows and current high let edge_flg=1 ;set edge flag endif #endif #ifndef input_int_en resume ;resume if input polled mode #else ;sample in-1 - in-4 at specific intervals ;injx is input averaged over last 4 samples (0 - 1023) ;injx_bfr(0) & injx_bfr(1) are last 4 word samples ;written by David J. Brown let isr_cnt=isr_cnt+1 #endif #ifdef cvs ;change the following table to modify the scan rate for each CVS input ;175 uS execution time so maximum of one sample and average per interrupt ;currently set for: in-1 to 8 @ 8 mS branch isr_cnt, [avg1,avg2,avg3,avg4,avg5,avg6,avg7,avg8,avg1,avg2,avg3,avg4,avg5,avg6,avg7,avg8] #else ;change the following table to modify the scan rate for each PSIM input ;175 uS execution time so maximum of one sample and average per interrupt ;currently set for: in-1 @ 2 mS, in-2 @ 4 mS, in-3 @ 8 mS, in-4 @ 8 mS branch isr_cnt, [avg1,avg2,avg1,avg3,avg1,avg2,avg1,avg4,avg1,avg2,avg1,avg3,avg1,avg2,avg1,avg4] #endif ;sample in-1 avg1: #ifdef input_int_en adin pin_j1,in_j1_bfr.word1(in_j1_ptr) ;get in-1 into buffer let in_j1_tmp=in_j1_bfr(0)+in_j1_bfr(1) ;add two high words together and low words together let in_j1=(in_j1_tmp.word1+in_j1_tmp.word0)/4 let in_j1_ptr=(in_j1_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-2 avg2: #ifdef input_int_en adin pin_j2,in_j2_bfr.word1(in_j2_ptr) ;get in-2 into buffer let in_j2_tmp=in_j2_bfr(0)+in_j2_bfr(1) ;add two high words together and low words together let in_j2=(in_j2_tmp.word1+in_j2_tmp.word0)/4 let in_j2_ptr=(in_j2_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-3 avg3: #ifdef input_int_en adin pin_j3,in_j3_bfr.word1(in_j3_ptr) ;get in-3 into buffer let in_j3_tmp=in_j3_bfr(0)+in_j3_bfr(1) ;add two high words together and low words together let in_j3=(in_j3_tmp.word1+in_j3_tmp.word0)/4 let in_j3_ptr=(in_j3_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-4 avg4: #ifdef input_int_en adin pin_j4,in_j4_bfr.word1(in_j4_ptr) ;get in-4 into buffer let in_j4_tmp=in_j4_bfr(0)+in_j4_bfr(1) ;add two high words together and low words together let in_j4=(in_j4_tmp.word1+in_j4_tmp.word0)/4 let in_j4_ptr=(in_j4_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-5 avg5: #ifdef input_int_en adin pin_j5,in_j5_bfr.word1(in_j5_ptr) ;get in-5 into buffer let in_j5_tmp=in_j5_bfr(0)+in_j5_bfr(1) ;add two high words together and low words together let in_j5=(in_j5_tmp.word1+in_j5_tmp.word0)/4 let in_j5_ptr=(in_j5_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-6 avg6: #ifdef input_int_en adin pin_j6,in_j2_bfr.word1(in_j6_ptr) ;get in-6 into buffer let in_j6_tmp=in_j6_bfr(0)+in_j6_bfr(1) ;add two high words together and low words together let in_j6=(in_j2_tmp.word1+in_j6_tmp.word0)/4 let in_j6_ptr=(in_j6_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-7 avg7: #ifdef input_int_en adin pin_j7,in_j7_bfr.word1(in_j7_ptr) ;get in-7 into buffer let in_j7_tmp=in_j7_bfr(0)+in_j7_bfr(1) ;add two high words together and low words together let in_j7=(in_j7_tmp.word1+in_j7_tmp.word0)/4 let in_j7_ptr=(in_j7_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ;sample in-8 avg8: #ifdef input_int_en adin pin_j8,in_j8_bfr.word1(in_j8_ptr) ;get in-8 into buffer let in_j8_tmp=in_j8_bfr(0)+in_j8_bfr(1) ;add two high words together and low words together let in_j8=(in_j8_tmp.word1+in_j8_tmp.word0)/4 let in_j8_ptr=(in_j8_ptr+1)&$03 ;increment pointer and wrap at 3 resume #endif ; ;-----------------------------------------------------------------------------------------------; ; End Of Program ; ;-----------------------------------------------------------------------------------------------;