SHT_Click MikroBUS BDF API and I2C Communication Pins Problem

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
dnaci
Posts: 21
Joined: 09 Apr 2012 18:10

SHT_Click MikroBUS BDF API and I2C Communication Pins Problem

#1 Post by dnaci » 01 Nov 2023 20:55

Hello,
I'm trying to use the SHT Click library. My code below compiles without any problems. I am using the Pic18F46K22 processor. I want to read the SHT31 Sensor.

However, I see in the __ep_generic_I2C.mbas and __epp_generic_I2C.mbas library files that Port.C3 and Port.C4 pins are used by default for I2C communication. I want to use any two of the PortA or PortE pins as SOFTWARE I2C pins.

The reason why I cannot use the default PortC.3 and PortC.4 pins in the library is that there is a device performing SPI communication on these pins.How should I edit? Or is it possible to use a software I2C pin in the sht click library?

In the file __epv8_P18F45K22_I2C.mbas I see a special coding for PIC18F45K22. In this file, there is a subroutine for PORTD.0 and PORTD.1 in the _i2cInit_3 definition. Can similar software I2C communication be done for the PIC18F46K22 processor?

Thanks.

Davut

Code: Select all

program Click_SHT_PIC

include Click_SHT_types
include Click_SHT_config
sub procedure systemInit() 
    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_INT_PIN, _GPIO_INPUT) 
    mikrobus_gpioInit(_MIKROBUS1, _MIKROBUS_RST_PIN, _GPIO_OUTPUT) 
    mikrobus_i2cInit(_MIKROBUS1, @_SHT_I2C_CFG[0]) 
    mikrobus_logInit(_LOG_USBUART, 9600)
    Delay_ms(100) 
end sub

sub procedure applicationInit() 
    sht_i2cDriverInit(T_SHT_P(@_MIKROBUS1_GPIO), T_SHT_P(@_MIKROBUS1_I2C), SHT_I2C_ADDR0) 
    mikrobus_logWrite("System Initialized", _LOG_LINE) 
    Delay_ms(100) 
end sub

sub procedure applicationTask() 
dim
    temperature as float 
    humidity as float 
    text as char[15] 

    Delay_ms(1500) 
    temperature = sht_temp_ss() 
    FloatToStr(temperature, @text[0]) 
    mikrobus_logWrite("Temperature: ", _LOG_TEXT) 
    mikrobus_logWrite(@text[0], _LOG_TEXT) 
    mikrobus_logWrite("°C", _LOG_LINE) 
    Delay_ms(200) 
    humidity = sht_hum_ss() 
    FloatToStr(humidity, @text[0]) 
    mikrobus_logWrite("Humidity: ", _LOG_TEXT) 
    mikrobus_logWrite(@text[0], _LOG_TEXT) 
    mikrobus_logWrite("%", _LOG_LINE) 
    Delay_ms(200) 
end sub

main :

    systemInit() 
    applicationInit() 
    while (1) 
        applicationTask() 
    wend
end.
__ep_generic_I2C.mbas:

Code: Select all

module __ep_generic_I2C
include __t_PIC

#IFDEF ENABLE_I2C THEN

sub function _i2cInit_1 (dim cfg as ^const uint32_t) as T_mikrobus_ret
sub function _i2cInit_2 (dim cfg as ^const uint32_t) as T_mikrobus_ret

#ENDIF

implements

const _MIKROBUS_ERR_I2C          as byte = 4

' ------------------------------------------------------------ SYSTEM SPECIFIC '

#IFDEF ENABLE_I2C THEN

sub function _i2cInit_1 (dim cfg as ^const uint32_t) as T_mikrobus_ret   
dim arg1 as ^const uint32_t
    arg1 = (cfg + 0)
    ANSELC.B3 = 0
    ANSELC.B4 = 0    
    Result = 0
    select case (arg1^)
        case 100000 I2C1_Init( 100000 )
        case 400000 I2C1_Init( 400000 )
        case else Result = _MIKROBUS_ERR_I2C
    end select
end sub

sub function _i2cInit_2 (dim cfg as ^const uint32_t) as T_mikrobus_ret   
dim arg1 as ^const uint32_t
    arg1 = (cfg + 0)
    ANSELC.B3 = 0
    ANSELC.B4 = 0   
    Result = 0 
    select case (arg1^)
        case 100000 I2C1_Init( 100000 )
        case 400000 I2C1_Init( 400000 )
        case else Result = _MIKROBUS_ERR_I2C
    end select
end sub

#ENDIF

end.

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: SHT_Click MikroBUS BDF API and I2C Communication Pins Problem

#2 Post by filip » 03 Nov 2023 11:46

Hi,

Basically, you want to use software I2C library in mikroSDK, by using the hardware I2C function which are diverted to other pins ?

Regards,
Filip.

dnaci
Posts: 21
Joined: 09 Apr 2012 18:10

Re: SHT_Click MikroBUS BDF API and I2C Communication Pins Problem

#3 Post by dnaci » 03 Nov 2023 13:14

Maybe Filip, I guess I chose the hard way. The attached microsdk library is installed in the ide. Do you have an example for mikrosdk sht_click?
Attachments
Mikroe-sdk.PNG
Mikroe-sdk.PNG (5.12 KiB) Viewed 346 times

dnaci
Posts: 21
Joined: 09 Apr 2012 18:10

Re: SHT_Click MikroBUS BDF API and I2C Communication Pins Problem

#4 Post by dnaci » 05 Nov 2023 13:20

In my work with Mikrosdk, although I followed the instructions correctly and updated the project path address, I could not fix the errors.
Anyway, I prepared a code that does not require a library. The sensor is read via PORTA.1 and PORTA.2 pins using software I2C.
The temperature value is stored in the tC variable and the humidity value is stored in the rhLin variable. These values can be printed on an LCD screen or sent to other devices. 3.3K pullup resistors can be used for data and clock ends.

Code: Select all

program SHT31_Example

' I2C sht31 pins
dim Soft_I2C_Scl as sbit at RA2_bit
      Soft_I2C_Sda as sbit at RA1_bit
      Soft_I2C_Scl_Direction as sbit at TRISA2_bit
      Soft_I2C_Sda_Direction as sbit at TRISA1_bit

Dim rhLin As Float         'hum.var.
Dim tC As Float            'temp.var.
Dim read_SHT As Byte[6] 'SHT31 Read var.

main:

  Soft_I2C_Init()
  Delay_ms(100)
   
  while TRUE
    Soft_I2C_Start()
    Soft_I2C_Write(0X88) ' SHT31 I2C write address
    Soft_I2C_Write(0X2C) ' MSB measurement
    Soft_I2C_Write(0X06) ' LSB high measurement, 0X0D Medium, 0x10 low measurement
    Soft_I2C_Stop()
   
    Delay_ms(25)

    Soft_I2C_Start()
    Soft_I2C_Write(0X89)  ' SHT31 I2C read address

    read_SHT[0] = Soft_I2C_Read(1)    ' temp MSB, high byte 1=ACK, 0=NACK
    read_SHT[1] = Soft_I2C_Read(1)    ' temp LSB, low byte
    read_SHT[2] = Soft_I2C_Read(1)    ' temperature CRC
    read_SHT[3] = Soft_I2C_Read(1)    ' hum  MSB high byte
    read_SHT[4] = Soft_I2C_Read(1)    ' hum  LSB low byte
    read_SHT[5] = Soft_I2C_Read(0)    ' humidity CRC
    Soft_I2C_Stop()

    rhLin = 100 * (read_SHT[3] * 256 + read_SHT[4]) / 65535 'calc humidity
    tC = read_SHT[0] * 256 + read_SHT[1]                            'calc temperature
    tC = -45 + (175 * tC) / 65535
   wend

end

Post Reply

Return to “mikroBasic PRO for PIC General”