\prog;XI2C8574.asm|I/O expander test ;JDN 280301 .Proc 16F84 RB0 = 5 ; bit on Status register that control Bank1 .Macro Bank0to1 Set Status:#RB0 .Endmacro .Macro Bank1to0 Clr Status:#RB0 .Endmacro \b;PortA bSCK = 0 bSDA = 1 DirA = 2'00011 ; Bits 0 and 1 as inputs PortI2C = 5 TrisI2C = PortI2C Ad8574 = 16'40 \b;Variables .Loc 16'0C ; begin of variables .Ins Xi2cV.asi .Ins Xi2cM.asi ; macros \b;Programme .Loc 0 ; program start Deb: Move #DirA,W Move W,TrisA Clr PortA ; important for SCK SDA Move #Ad8574,W Move W,AdSlave Move #Ad8574,W ; Declared as 16'40 to 16'4E, according to address inputs Move W,AdSlave Move #2'00001111,W Move W,Datai2c ; Data direction initialized Call WrSingleI2C Loop: Call RdSingleI2C ; Result in W and AdI2C Swap AdI2C,W Or #2'00001111,W Move W,DataI2C Call WrSingleI2C Call Wait ; space on the scope ; to ease synchronization Jump Loop WrSingleI2C: Move AdSlave,W Move W,ADi2c Start Call WriteI2C Move DataI2C,W Move W,ADi2c Call WriteI2C Stop Ret RdSingleI2C: Move AdSlave,W Or #00000001,W Move W,ADi2c Start Call WriteI2C Call ReadI2C Call GiveNAckR Stop Ret .Ins Xi2cR.asi \rout:Wait|1ms wait Wait: Clr W W$: Add #-1,W ; 4 us loop Skip,EQ ; repeated 256 times Jump W$ Ret .End