The first of my problems is that the standard i2c library will not work for me at all. For example here's some code that talks to the ina220 chip via the i2c bus. It tells it which register to access and then reads 16-bits of data from this register (this is the current data that it is measuring).
Code: Select all
Device = 18F26K22 // Tell the compiler what chip we are using
Clock = 16
Config FOSC = INTIO67
Config MCLRE = INTMCLR
Dim reading As Word
Include "I2C.bas"
// Start Of Program...
OSCCON = %01110111 // Sets the internal oscillator for 16Mhz
I2C.Initialize()
// Main Loop
While True() // This creates an infinite loop
I2C.Start
I2C.WriteByte(%10000000) // point to the ina220 chip and put it in write mode (the LSB as a 0 means write)
I2C.WriteByte($04) // sets register pointer to the Current register
I2C.Restart
I2C.WriteByte(%10000001) // point to the ina220 chip and put it in read mode (the LSB as a 1 means read)
reading = I2C.ReadByte(I2C_ACKNOWLEDGE) // receive high byte (overwrites previous reading)
reading = reading << 8 // shift high byte to be high 8 bits
reading = reading Or I2C.ReadByte(I2C_NOT_ACKNOWLEDGE) // receive low byte as lower 8 bits
I2C.Stop // stop transmitting
Wend
Now having said that, If I modify the code to si2c instead of just i2c it actually works! Like this:
Code: Select all
Device = 18F26K22 // Tell the compiler what chip we are using
Clock = 16
Config FOSC = INTIO67
Config MCLRE = INTMCLR
Dim reading As Word
Include "SI2C.bas"
// Start Of Program...
OSCCON = %01110111 // Sets the internal oscillator for 16Mhz
SI2C.Initialize()
// Main Loop
While True() // This creates an infinite loop
SI2C.Start
SI2C.WriteByte(%10000000) // point to the ina220 chip and put it in write mode (the LSB as a 0 means write)
SI2C.WriteByte($04) // sets register pointer to the Current register
I2C.Restart
SI2C.WriteByte(%10000001) // point to the ina220 chip and put it in read mode (the LSB as a 1 means read)
reading = SI2C.ReadByte(I2C_ACKNOWLEDGE) // receive high byte (overwrites previous reading)
reading = reading << 8 // shift high byte to be high 8 bits
reading = reading Or SI2C.ReadByte(I2C_NOT_ACKNOWLEDGE) // receive low byte as lower 8 bits
I2C.Stop // stop transmitting
Wend
Does anyone have any thoughts / suggestions as to why the standard i2c.bas won't work and I guess more importantly, why my variable 'reading' does not get updated with data from the i2c read command - even though I can see that the i2c communication is working?
Thanks for any help guys.