;-----------------------------------------------------------------------------------------------; ; Program: I2C ADC 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 reads all sixteen 8 bit inputs and loops them through to the outputs. ; Out-1 is the sum of inputs 1-4, Out-2 is the sum of inputs 5-8, Out-3 is the sum of inputs ; 9-12, and Out-4 is the sum of inputs 13-16. ; ; 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 = Sum of i2c inputs 1-4 ; Out-2 = Sum of i2c inputs 5-8 ; Out-3 = Sum of i2c inputs 9-12 ; Out-4 = Sum of i2c inputs 13-16 ; Aux = High during i2c input for time measurement with oscilloscope ; ; Run led = Program operational ; Stop led= 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 exp_in_1 var byte ;expansion in-1 value exp_in_2 var byte ;expansion in-2 value exp_in_3 var byte ;expansion in-3 value exp_in_4 var byte ;expansion in-4 value exp_in_5 var byte ;expansion in-5 value exp_in_6 var byte ;expansion in-6 value exp_in_7 var byte ;expansion in-7 value exp_in_8 var byte ;expansion in-8 value exp_in_9 var byte ;expansion in-9 value exp_in_10 var byte ;expansion in-10 value exp_in_11 var byte ;expansion in-11 value exp_in_12 var byte ;expansion in-12 value exp_in_13 var byte ;expansion in-13 value exp_in_14 var byte ;expansion in-14 value exp_in_15 var byte ;expansion in-15 value exp_in_16 var byte ;expansion in-16 value ; ;-----------------------------------------------------------------------------------------------; ; 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 ; ;-----------------------------------------------------------------------------------------------; ; high run_led ;indicate proper operation low stop_led loop: ;execution time to read all 16 channels is 7 mS ;execution time increases to 8.8 mS if oscillators are disabled to decrease power by 1 mA high aux_j ;scope output to measure I2C input execution time ;set control byte for analog output enable (run oscillator continuously), auto incr, and select ADC 0 ;first read will start conversion and next four reads will be inputs 1-4 i2cin i2c_data,i2c_clk,error,$98,$44,[exp_in_1,exp_in_1,exp_in_2,exp_in_3,exp_in_4] ; i2cout i2c_data,i2c_clk,error,$0,[$98,$07] ;optional disable oscillator to reduce power ; ;set control byte for inputs 5-8 and read results i2cin i2c_data,i2c_clk,error,$9a,$44,[exp_in_5,exp_in_5,exp_in_6,exp_in_7,exp_in_8] ; i2cout i2c_data,i2c_clk,error,$0,[$9a,$07] ;optional disable oscillator to reduce power ; ;set control byte for inputs 9-12 and read results i2cin i2c_data,i2c_clk,error,$9c,$44,[exp_in_9,exp_in_9,exp_in_10,exp_in_11,exp_in_12] ; i2cout i2c_data,i2c_clk,error,$0,[$9c,$07] ;optional disable oscillator to reduce power ; ;set control byte for inputs 13-16 and read results i2cin i2c_data,i2c_clk,error,$9e,$44,[exp_in_13,exp_in_13,exp_in_14,exp_in_15,exp_in_16] ; i2cout i2c_data,i2c_clk,error,$0,[$9e,$07] ;optional disable oscillator to reduce power low aux_j ; ;each variable now represents the sixteen 8 bit inputs ;loop sum of 4 inputs to each output let out_j1=((exp_in_1+exp_in_2+exp_in_3+exp_in_4)*15)/4 ;scale sum of 4 inputs to 10.0 volt output let out_j2=((exp_in_5+exp_in_6+exp_in_7+exp_in_8)*15)/4 ; by multiplying by 3.75 let out_j3=((exp_in_9+exp_in_10+exp_in_11+exp_in_12)*15)/4 let out_j4=((exp_in_13+exp_in_14+exp_in_15+exp_in_16)*15)/4 gosub load_outputs goto loop ; error: low run_led 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 ; ;-----------------------------------------------------------------------------------------------;