|
Forum Index : Microcontroller and PC projects : Cannot read Word variable using Webmite I2C
| Author | Message | ||||
| Johnfd Newbie Joined: 04/08/2023 Location: AustraliaPosts: 8 |
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: NetherlandsPosts: 5599 |
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 KingdomPosts: 8430 |
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: AustraliaPosts: 6426 |
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 |
||||
| Johnfd Newbie Joined: 04/08/2023 Location: AustraliaPosts: 8 |
Thank you gentlemen, much appreciated I shall try the suggestions. I'll report back. John |
||||
| Johnfd Newbie Joined: 04/08/2023 Location: AustraliaPosts: 8 |
All working as it should. Thanks very much for the replies, it had me baffled. Just a little different from my experience with the PICs but what an exceptional program from Geoff, Peter and Jim. This project is for one of our repeater sites. It will be monitoring solar charging volts and current and ditto for the load from the battery. Two INA226 boards. The web connection will save trips to the top of the hill. Kind regards, John |
||||
| lizby Guru Joined: 17/05/2016 Location: United StatesPosts: 3536 |
Please post your code when you are satisfied with it (or sooner if satisfaction seems a long way off). PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2916 |
FotS has code for the INA219 and a link to Jim's INA3221 INA219 |
||||
TassyJim![]() Guru Joined: 07/08/2011 Location: AustraliaPosts: 6426 |
Chart from VK7RTV for yesterday.(Using the INA3221) ![]() Green is peak solar output, blue is average solar. I also monitor the radio usage with a webmite. It is at least 18 months since I touched the webmite. Provided you don't push the speed too high, they are very reliable. Jim VK7JH MMedit |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |