' 'The code relating to MXD2125 is 'taken from example code by F1OAT (frible@teaser.fr) Nov 2006. Symbol pin_x = 7 ; pin #5 <= MXD2125 X output Symbol pin_y = 0 ; pin #3 <= MXD2125 Y output ' CORDIC section variables definition Symbol x = w2 Symbol y = w3 Symbol ang = w4 Symbol ang_l = b8 Symbol ang_h = b9 Symbol dx = w5 Symbol dy = w6 Symbol i = b0 Symbol d = b1 Symbol neg = b2 Symbol tmp = b3 'The code relating to I2C or DS1803 is 'taken from example code by Peter H. Anderson, Elmore, VT, Aug, '04. ' Define IO Terminals Symbol SCL = 0 Symbol SDAIn = Pin1 Symbol SDAOut = 1 Symbol DevAdr = B0 Symbol PotNum = B1 Symbol PotSetting0 = B2 Symbol PotSetting1 = B3 ' Used in I2C Routines Symbol OByte = B4 Symbol IByte = B4 Symbol Ack = B5 Symbol N = B6 Symbol MSBit = B7 setfreq m8 ' increases the sample rate START: p_cordic: ; CORDIC section to guage the XY values (i.e. angle) x = 0 dx = 0 y = 0 dy = 0 ; Cumulate several pulses measurement to enhance accuracy for i=1 to 16 pulsin pin_x,1,ang ; high level pulse duration x = x + ang pulsin pin_x,0,ang ; low level pulse duration dx = dx + ang pulsin pin_y,1,ang ; high level pulse duration y = y + ang pulsin pin_y,0,ang ; low level pulse duration dy = dy + ang next i neg = 0 ; Put vector in proper 90° quadrant if x >= dx then qx1 x = dx - x ang = 2048 ; 180° neg = 1 - neg goto qx2 qx1: x = x - dx ang = 4096 ; 360° qx2: if y >= dy then qy1 y = dy - y neg = 1 - neg goto qy2 qy1: y = y - dy qy2: d = 1 ; CORDIC iterations for i=0 to 9 if y <= 32767 then pos y = 65535 - y neg = 1-neg pos: dx = x / d dy = y / d x = x + dy y = y - dx lookup i, (512,302,159,81,40,20,10,5,2,1), dx ; ATAN table for ang_premult=1024/90 if neg=1 then sub ang = ang + dx goto nextstep sub: ang = ang - dx nextstep: d = d*2 next i 'convert the angle to a value which can be used by the I2C routine. 'In this case it coverts it to numbers close to 0-255 for 0-90 degrees 'which makes it suitable to write this direct to the DS1803 ang = ang - 1965 ang = ang * 2 ang = ang / 10 Top: DevAdr = 0 PotNum = 0 PotSetting0 = ang 'write angle to Pot0. 'PotSetting0 = 255 GoSub DS1803WritePot ' PotNum = 1 'Un-comment this code if you wish to use Pot1. 'PotSetting1 = 64 'Gosub DS1803WritePot 'Gosub DS1803ReadPots 'SerTxD (#PotSetting0, " ", #PotSetting1, 13, 10) 'debug PotSetting0 goto start DS1803WritePot: Branch PotNum, (WritePot0, WritePot1) WritePot0: Gosub I2CStart OByte = 2 * DevAdr + $50 Gosub I2COutByte OByte = $A9 GoSub I2COutByte OByte = PotSetting0 GoSub I2COutByte Gosub I2CStop Goto DS1803WritePotDone WritePot1: Gosub I2CStart OByte = 2 * DevAdr + $50 Gosub I2COutByte OByte = $AA GoSub I2COutByte OByte = PotSetting1 GoSub I2COutByte Gosub I2CStop Goto DS1803WritePotDone DS1803WritePotDone: Return DS1803ReadPots: Gosub I2CStart OByte = 2 * DevAdr + $51 Gosub I2COutByte Ack = 1 ' master is to ACK after receipt of byte Gosub I2CInByte PotSetting0 = IByte Ack = 0 ' no ACK as this is the last byte prior to the stop Gosub I2CInByte PotSetting1 = IByte Return I2CStart: High SDAOut High SCL Low SDAOut ' bring SDA low while SCL is high Low SCL Return I2CStop: High SCL High SDAOut ' bring SDA high while SCL is high Return I2COutByte: For N = 1 to 8 ' for eight bits MSBit = OByte / 128 ' beginning with the MSB If MSBit = 1 Then I2COutByte_1 ' most sig bit Low SDAOut GoTo I2COutByte_2 I2COutByte_1: High SDAOut I2COutByte_2: High SCL Low SCL OByte = OByte * 2 ' shift byte such that next bit is in most sig bit position Next High SDAOut ' null clock pulse to allow for slave to acknowledge High SCL Low SCL Low SDAOut Return I2CInByte: ' receives a byte, most sig byte first. ' result returned in I_BYTE High SDAOut For N=1 to 8 ' for eight bits High SCL IByte = IByte * 2 + SDAIn Low SCL Next Branch Ack, (NoAck, YesAck) NoAck: High SDAOut ' high Z on 9th clock pulse Goto I2CInByte1 YesAck: Low SDAOut ' logic zero on 9th clock pulse I2CInByte1: High SCL Low SCL Low SDAOut Return