News:

Let's find out together what makes a PIC Tick!

Main Menu

I2Cin and I2Cout question

Started by Yves, Oct 06, 2024, 08:08 AM

Previous topic - Next topic

Yves

I have a question about I2Cin and I2Cout for the MCP3422 a/d ship.
I2Cin SDA_Pin, SCL_Pin, Control, { Address }, [ Variable {, Variable...} ]
I2Cout SDA_Pin, SCL_Pin, Control, { Address }, [ OutputData ] as per manual

My MCP3422 address is Symbol cMCP3422_SlaveAddress = %1101001

I2COut PORTA.1,Control, cMCP3422_SlaveAddress, [ MCP34_bConfig ]
and
I2CIn  PORTA.0,Control, cMCP3422_SlaveAddress,0, [MCP34_wResult.Byte1,MCP34_wResult.Byte0,MCP34_bConfig ]
I have no problems with any of most of the entry but the Control byte.My question is what Control should be? is it required? I have to use these commands as Busin and Busout is use for something else in the circuit. Many thanks

Yves


Yves

Frizie

Try this snippet Yves:

;============ I²C ============

'ECLARE SCL_PIN        = PORTC.3        ;Serial Clock pin van I²C componenten met PORTC.3 verbinden
'ECLARE SDA_PIN        = PORTC.4        ;Serial Data  pin met I²C componenten met PORTC.4 verbinden
'ECLARE I2C_SLOW_BUS   = ON             ;Langzame snelheid gebruiken voor de I²C bus (anders kan het te snel gaan voor het PCF8583 RTC IC)
Declare I2C_BUS_SCL    = On             ;Geen pull-up op de print aanwezig voor de kloklijn (ook niet plaatsen!)

Symbol MCP3422_ADR  = 0b11010000     
Symbol MCP3422_SCL  = PORTC.3 ;Q: Serial Clock (for I2C)
Symbol MCP3422_SDA  = PORTC.4 ;IQ:Serial Data  (for I2C)

Dim ADC_Value      As Word  ;Contains a received value from the ADC - For 12, 14 and 16-bit resolution
'im ADC_Value      AS Long  ;Contains a received value from the ADC - For 18-bits resolution
Dim MCP3422_CFG    As Byte  ;MCP3422 configuration byte

DelayMS 250
Clear
Cls                          ;CLear Screen (display)


Do
'I2COUT  MCP3422_SDA, MCP3422_SCL, MCP3422_ADR, [0b10000000] ;For CH1, one shot, 12-bit, gain x1 (xnnxxxxx nn=channel. xxxnxxxx n=conversion mode. xxxxnnxx nn=resolution. xxxxxxnn nn=gain factor)
'I2COUT  MCP3422_SDA, MCP3422_SCL, MCP3422_ADR, [0b10000100] ;For CH1, one shot, 14-bit, gain x1 (xnnxxxxx nn=channel. xxxnxxxx n=conversion mode. xxxxnnxx nn=resolution. xxxxxxnn nn=gain factor)
  I2COut  MCP3422_SDA, MCP3422_SCL, MCP3422_ADR, [0b10001000] ;For CH1, one shot, 16-bit, gain x1 (xnnxxxxx nn=channel. xxxnxxxx n=conversion mode. xxxxnnxx nn=resolution. xxxxxxnn nn=gain factor)
'I2COUT  MCP3422_SDA, MCP3422_SCL, MCP3422_ADR, [0b10001100] ;For CH1, one shot, 18-bit, gain x1 (xnnxxxxx nn=channel. xxxnxxxx n=conversion mode. xxxxnnxx nn=resolution. xxxxxxnn nn=gain factor)
 
  Repeat             
    I2CIn  MCP3422_SDA, MCP3422_SCL, MCP3422_ADR, [ADC_Value, MCP3422_CFG]  ;Note: Declare 'ADC_Value' as LONG with 18-bit resolution
  Until MCP3422_CFG.7 = 0

  If ADC_Value >  32767 Then ADC_Value = 0  ;Option: Extra safety when input is used as single ended input - For 12, 14 and 16-bit resolution
'IF ADC_Value > 131071 THEN ADC_Value = 0  ;Option: Extra safety when input is used as single ended input - For 18 bit resolution

  Print At 1, 1, Dec5 ADC_Value            ;For 12, 14 and 16-bit resolution (= WORD)
'PRINT AT 1, 1, DEC8 ADC_Value            ;For 18 bit resolution            (= LONG)
  Print At 2, 1, Bin8 MCP3422_CFG
 
  DelayMS 250
Loop
Ohm sweet Ohm | www.picbasic.nl

Yves

Thank you Frizie,
I saw you are addressing only Channel 1 how do you get channel 2?

Yves
Yves

Yves

#3
My original question was what the use of Control in command

I2Cin SDA_Pin, SCL_Pin, Control, { Address }, [ Variable {, Variable...} ]
I2Cout SDA_Pin, SCL_Pin, Control, { Address }, [ OutputData ]

It is not about the code for the MCP3422. I cant use Busin and Busout
I like to apply it to the MCP3422 driver written by Less
Yves
Yves

Frizie

Quote from: Yves on Oct 06, 2024, 10:31 AMThank you Frizie,
I saw you are addressing only Channel 1 how do you get channel 2?

Yves

See the REM line I put behind I2Cout:
[0b10001100]
;For CH1, one shot, 18-bit, gain x1 (xnnxxxxx nn=channel. xxxnxxxx n=conversion mode. xxxxnnxx nn=resolution. xxxxxxnn nn=gain factor)

Then look at the configuration register in the datasheet of the MCP3422.
Then you see that for CH2 bit 6-5 must be set to 01.
So it becomes: [0b10101100].

(Control is not required)
Ohm sweet Ohm | www.picbasic.nl

tumbleweed

QuoteMy original question was what the use of Control in command

I2Cout SDA_Pin, SCL_Pin, Control, { Address }, [ OutputData ]


Control is the first byte sent, which is pretty much always the device address byte.

The optional { Address } parameter might be a bit misleading. Its use depends on the device you're talking to... for most devices it would be the register you want to address.
In the case of the MCP3422 it's not used... the second byte is the configuration byte, so the command format would be:

I2COut PORTA.1, cMCP3422_SlaveAddress, [ MCP34_bConfig ]



Yves

Many thanks Tumbleweed,

That is all I wanted to know and your explanation was very useful.

Kind regards,

Yves
Yves

Stephen Moss

Quote from: Yves on Oct 06, 2024, 08:08 AMMy MCP3422 address is Symbol cMCP3422_SlaveAddress = %1101001
You only have 7 bits here, if the compiler does not automatically see them as left justified or shift them left 1, then the LSB will be overwritten as a 1 or 0 depending whether the command is I2Cin or I2Cout and the address will be wrong.

Personally, I create the address with the full 8 bits (7 Address + R/W bit), like this...
'PCF8591P ADC/DAC Control
Symbol Read_Control = $91   'PCF8591P Addressing & Read_Control value (1001, 000 (Address), 1 (Read))
Symbol Write_Control = $90  'PCF8591P Addressing & Write_Control value(1001, 000 (Address), 0 (Write))
Symbol Mode_Control = $40   'Enable PCF8591P Analogue output and set ADC channel to 0 [bits 1:0]
to be sure the Left/Right justification is correct and it seems to work, although I could have replace Read_Control & Write_Control with Address = $90 and let the command manipulate the LSB (it does anyway but just sets it to its pre-existing Read_Control/Write_Control value) I think seeing the full 8 bits makes it easier to compare to the data sheet and thus spot an error.

david

I use the same approach and define the 8 bit address for read and write.
Symbol ADC_W= %11010000    'Device address and write
Symbol ADC_R= %11010001    'Device address and read


'Ch1     
        HBusOut ADC_W,[136]      '136 for Ch1 one shot, 16 bit, gain 1
        Repeat
        HBusIn ADC_R,[ADC1, Setup]      'returns value of ADC as Word
        Until Setup.7=0
'Ch2         
        HBusOut ADC_W,[168]      '168 for Ch2 one shot, 16 bit, gain 1
        Repeat
        HBusIn ADC_R,[ADC2, Setup]   
        Until Setup.7=0   

David