Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 16:55 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : I2C vs I2C2 vs I2C3

Author Message
elk1984

Senior Member

Joined: 11/07/2020
Location: United Kingdom
Posts: 228
Posted: 04:23pm 06 Nov 2020
Copy link to clipboard 
Print this post

This may be a stupid question, but what was the reasoning behind using I2C2 and I2C3 as commands rather than extending the I2C command to accept a <port> parameter?  Wouldn't combining and changing the parameters on the I2C command free up two tokens for other BASIC keywords?
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 01:49am 08 Nov 2020
Copy link to clipboard 
Print this post

Like many things MMBasic, it just grew over time.

While testing the HT mouse versus my Arduino version, I needed a simple way the swap I2C ports so came up with this SUB
SUB I2Cx p%, I2Ccmd$
 SELECT CASE p%
   CASE 1
     EXECUTE "I2C "+I2Ccmd$
   CASE 2
     EXECUTE "I2C2 "+I2Ccmd$
   CASE 3
     EXECUTE "I2C3 "+I2Ccmd$
 END SELECT
END SUB


The full test program which has been tested on ports 1 and 2

 ' ht_mouse test
 CLS
 MODE 1
 mousePort = 2
 initmouse
 SPRITE LOAD "mouse.spr",1
 'pause 100
 DO
   readmouse
   TEXT 0,0,STR$(x,6,0," ")+STR$(y,6,0," ")+STR$(lb,2,0," ")+STR$(rb,2,0," ")+STR$(sb,2,0," ")+STR$(xs,4,0," ")+STR$(ys,4,0," ")+STR$(ss,6,0," ")
   SPRITE SHOW 1,x,y,1
   PAUSE 30
 LOOP UNTIL INKEY$ <>""
 SPRITE HIDE 1
 I2Cx mousePort, "CLOSE"
 '
SUB initmouse
 I2Cx mousePort, "OPEN 100,1000"
 I2Cx mousePort, "WRITE 41,0,3,20,INT(MM.HRES \ 256),INT(MM.HRES MOD 256)"
 I2Cx mousePort, "WRITE 41,0,3,20,INT(MM.HRES \ 256),INT(MM.HRES MOD 256)"
 I2Cx mousePort, "WRITE 41,0,3,22,INT(MM.VRES \ 256),INT(MM.VRES MOD 256)"
 I2Cx mousePort, "WRITE 41,0,3,24,INT(MM.HRES \ 512),INT((MM.HRES\2) MOD 256)"
 I2Cx mousePort, "WRITE 41,0,3,26,INT(MM.VRES \ 512),INT((MM.VRES\2) MOD 256)"
 I2Cx mousePort, "WRITE 41,0,2,28,10"
END SUB
 '
SUB readmouse
 DO
   I2Cx mousePort, "WRITE 41,1,1,0"
   I2Cx mousePort, "READ 41,0,10,s$"
   x=STR2BIN(UINT16,LEFT$(s$,2),big)
   y=STR2BIN(UINT16,MID$(s$,3,2),big)
   lb=ASC(MID$(s$,5,1))
   rb=ASC(MID$(s$,6,1))
   sb=ASC(MID$(s$,7,1))
   xs=STR2BIN(int8,MID$(s$,8,1))
   ys=STR2BIN(int8,MID$(s$,9,1))
   ss=ss+STR2BIN(int8,MID$(s$,10,1))
 LOOP UNTIL x>=0 AND x<MM.HRES AND y>=0 AND y< MM.VRES 'trap missreads
END SUB
 
SUB I2Cx p%, I2Ccmd$
 SELECT CASE p%
   CASE 1
     EXECUTE "I2C "+I2Ccmd$
   CASE 2
     EXECUTE "I2C2 "+I2Ccmd$
   CASE 3
     EXECUTE "I2C3 "+I2Ccmd$
 END SELECT
END SUB


Jim
VK7JH
MMedit
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025