;-----------------------------------------------------------------------------------------------; ; Program: I2C Switch demo ; ; 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 program displays the status of 16 switches interfaced through the I2C expansion PCB on ; the PSIM. The STOP LED indicates no switches are closed. The RUN LED indicates one of the ; 16 switches is closed and the highest switch number-1 is displayed in binary in the output ; LEDs (e.g. switch number 7 is displayed as 6 in binary as "off" "on" "on" "off" ). ; ; Inputs: ; Start = n/a ; Stop = n/a ; In-1 = n/a ; In-2 = n/a ; In-3 = n/a ; In-4 = n/a ; ; Outputs: ; Out-1 = On=8 ; Out-2 = On=4 ; Out-3 = On=2 ; Out-4 = On=1 ; Aux = High during i2c input for time measurement with oscilloscope ; ; Run led = On if any switch is closed ; Stop led= On if all switches are open ; Both on indicates error ; ; Revision: 0.0 ; Date April 14, 2007 ; History: 0.0 initial release of program ; Written: April 14, 2007 ; ;-----------------------------------------------------------------------------------------------; ; 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=0/low, pressed=1/hi) ; ; P5 - stop button (no input/default=0/low, pressed=1/hi) ; ; P6 - I2C data ; ; P7 - I2C clock ; ; P8 - aux in/out (no input=hi) ; ; 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 ; ;-----------------------------------------------------------------------------------------------; ; ;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 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 run_led con p10 ;run led 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 in_j2 var word ;in-2 value in_j3 var word ;in-3 value in_j4 var word ;in-4 value out_j1 var word ;out-1 value out_j2 var word ;out-2 value out_j3 var word ;out-3 value out_j4 var word ;out-4 value ;misc declarations switch var word ;switch input last_switch var word ;last switch value index var word ;switch index ; ;-----------------------------------------------------------------------------------------------; ; Initialization ; ;-----------------------------------------------------------------------------------------------; ; ;initialize pins 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 low run_led ;reset run led low stop_led ;reset stop led ;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 gosub load_outputs ; ;-----------------------------------------------------------------------------------------------; ; Main Program ; ;-----------------------------------------------------------------------------------------------; ; let last_switch=0 ;initialize lasttemp to unlikely value i2cout i2c_data,i2c_clk,error,$0,[$48,$06,$ff];enable pullup resistors on switch inputs i2cout i2c_data,i2c_clk,error,$0,[$4a,$06,$ff] ;display switch status ;STOP LED indicates no switch closed ;RUN LED indicates switch closed and highest switch number displayed in binary in OUT1-OUT4 LEDs ;written by David J. Brown loop: pause 20 ;allow switch debounce time ;execution time to read all 16 switches is 2 mS high aux_j ;scope output to measure I2C input execution time i2cin i2c_data,i2c_clk,error,$48,$09,[switch.lowbyte ] ;get status of switches 1-8 i2cin i2c_data,i2c_clk,error,$4a,$09,[switch.highbyte] ;get status of switches 9-16 low aux_j ;each bit in temp now represents a switch position ; bit15 is switch 16 and bit0 is switch 1 ; 1 indicates a switch is open ; 0 indicates a switch is closed if switch=last_switch then loop ;compare with last state ; let last_switch=switch ;switch change so update history if switch=$ffff then ;check for all switches open let out_j1=0 ;turn off all output leds let out_j2=0 let out_j3=0 let out_j4=0 high stop_led ;stop indicates all switches open low run_led else ;shift each bit and display closed switch number in binary ;complete all switches so that the highest closed is last displayed for index=0 to 15 if switch.bit0=0 then ;check switch closed let out_j1.bit10=index.bit3 ;set 8 position to either 2048 or 0 let out_j2.bit10=index.bit2 ;set 4 position to either 2048 or 0 let out_j3.bit10=index.bit1 ;set 2 position to either 2048 or 0 let out_j4.bit10=index.bit0 ;set 1 position to either 2048 or 0 endif let switch=switch>>1 ;shift to next switch next high run_led ;run indicates one or more switches closed low stop_led endif gosub load_outputs ;display output leds goto loop ; error: high run_led ;indicate error high stop_led end ; ;-----------------------------------------------------------------------------------------------; ; Input and Output Subroutines ; ;-----------------------------------------------------------------------------------------------; ; ;sample in-1 to in-4 ;in_jx is input value (0 - 1023) ;150 uS execution time for PSIM get_inputs: adin pin_j1,in_j1 adin pin_j2,in_j2 adin pin_j3,in_j3 adin pin_j4,in_j4 return ; ;output out_jx values to dacs ;750 uS execution time for PSIM load_outputs: ;add dac address to out_jx values and shift 16 bits using mode 4 shiftout dac_sdata,dac_clk,4,[(out_j1|$c000)\16] pulsout dac_load,1 ;pulse loaddacs shiftout dac_sdata,dac_clk,4,[(out_j2|$8000)\16] pulsout dac_load,1 ;pulse loaddacs shiftout dac_sdata,dac_clk,4,[(out_j3|$4000)\16] pulsout dac_load,1 ;pulse loaddacs shiftout dac_sdata,dac_clk,4,[out_j4\16] pulsout dac_load,1 ;pulse loaddacs return ; ;-----------------------------------------------------------------------------------------------; ; End Of Program ; ;-----------------------------------------------------------------------------------------------;