Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:16 02 Jan 2026 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 : Cannot read Word variable using Webmite I2C

Author Message
Johnfd
Newbie

Joined: 04/08/2023
Location: Australia
Posts: 6
Posted: 08:25am 01 Jan 2026
Copy link to clipboard 
Print this post

G'day all,
I'm really having troubles reading the I2C with a INA226. Software Webmite V6.1.
The results for reading the word and current registers are only seeing the high byte.
I've set config and calibration registers OK.

To read a result register for voltage and current it is first necessary to write to the register to preparation for a read.
e.g.  
I2C WRITE &H40,&H00,&H01,&H02   'set up register $H02 ready for volts read
Next read the register
 I2C Read &H40,&H00,&H01,VoltOut
Using in volts float or integer same result-an integer 0 to 42 (highest voltage available 0-14V). Similar with Current register.  

I've tried
 I2C Read &H40,&H00,&H02,VoltOut  
Same result.

Similarly with the current register (I have config and calibration set).
So the issue is I can't load a word variable from  the INA226, only a single byte can be brought in.

I know I must be doing something stupid but I've nearly worn out the keys on the computer.

Here are my two routinge:
SUB GetINA226SetupLoad
 SETPIN 21,22,I2C     'set the pins for I2C
 I2C OPEN 100,200                   'speed in Hz, timeout in ms
 
' Get load volts
' register 01 shunt voltage, 02 is the bus voltage.
' register 04 is current register
 PAUSE 20     'give ana226 time
 I2C WRITE &H40,&H00,&H01,&H02      'set up register $H02 ready for read of volts
 I2C Read &H40,&H00,&H01,VoltOut    'read the volts
 voltout = voltout* 0.33333 + 0.005 'adjust the voltage and round
 Tempst$ = format$(VoltOut,"%.3f")  'get the volts in string form          
 voltsL$ = left$(TempSt$,4)         'ready for elsewhere in the program
 PAUSE 20
 
'now to read the load current
 I2C WRITE &H40,&H00,&H01,&H04      'set up ready for read of current
 I2C Read &H40,&H00,&H01,AmpOut     'get the current after calibration has been set  
 Tempst$ = format$(Ampout,"%.3f")   'format the current
 AmpsL$ = left$(TempSt$,4)
 print "Load: "voltsL$,"V ",Ampsl$,"A " 'Print volts/amps  on Tera Term
 I2C CLOSE     'close the connection
END SUB

I'd really appreciate help with this.
Thanks
John
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5563
Posted: 09:01am 01 Jan 2026
Copy link to clipboard 
Print this post

Hi John,

Reading and writing I2C is always byte oriented.
If you need to read a 16 bit value, you need to read 2x a byte register, and combine the 2 bytes to a single 16 bit.

I do not own a INA226, bit it should be done this way


DIM dta%(1)                        'to hold the 2 bytes

I2C WRITE &H40,&H00,&H01,&H02      'set up register $H02 ready for read of volts
I2C Read &H40,&H00,&H02,dta%()     'read 2 bytes

Voltout = dta%(1) + 256 * dta%(0)  'assume MSB in dta%(0)


Volhout
Edited 2026-01-01 19:10 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 8412
Posted: 09:21am 01 Jan 2026
Copy link to clipboard 
Print this post

I2C reads bytes. See the I2CREAD command and Appendix B of the manual:
I2CREAD addr, option, revlen, revbuf

If you want to receive a 16-bit value you will need to set MMBasic up.
addr - the address of the chip
option - usually 0 to relinquish the bus after use
revlen - the number of bytes expected, in this case 2
revbuf - a 1-dimensional array, Rdata() for example or a string

You can't receive into a simple numeric variable unless you have revlen set to 1 and are only receiving  a single byte. The array will return with the high byte in one element and the low byte in the other.
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6420
Posted: 07:08pm 01 Jan 2026
Copy link to clipboard 
Print this post

I don't have the INA226 but this is the relevant code I use for the INA3221

 
FUNCTION INA3221_read(reg AS INTEGER) AS INTEGER
 LOCAL registers$
 I2C2 WRITE ina3221addr,0,1,reg
 I2C2 READ ina3221addr,0,2,registers$
 INA3221_read = STR2BIN(INT16,registers$,BIG)
END FUNCTION
 
 
SUB INA3221_write reg AS INTEGER, value AS INTEGER
 LOCAL INTEGER hibyte, lobyte
 lobyte = value AND &hFF
 hibyte = (value >> 8) AND &hFF
 I2C2 WRITE ina3221addr,0,3,reg,hibyte,lobyte
END SUB
 


Change I2C2 to I2C as required.

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 2026