cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC™ 4

newelectronic
New Contributor

Hello,

I have psoc 4100s plus. And I want to communicate with SHT20 temperature and humidity sensor and read the data. But I could not. I could not find the correct communication and reading addresses. Can you help me ?

I share the images of the products here.

Thank you.

Best regards,

5106e562-54de-4d27-9345-be98bfed47e3.jpg

f33b22bb-1c7f-4195-9ac1-5ebae43e858a.jpg

  

0 Likes
1 Solution
newelectronic1
New Contributor II

Hi,

Thanks a lot for your support. I arrange like same  your code and ıt works good work. Below code running.

 

 

#include "project.h"

 

#define I2C_SLAVE_ADDR (0x40u)

bool SHT20_READ_USER_REGISTER(void)
{
uint8 buffer_cmd[1],sht20_is_connented=0;

buffer_cmd[0]=0xE7; // SHT20_READ_REG

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer_cmd, 1,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == 1)
{
sht20_is_connented=1;
}
else
sht20_is_connented=0;
}
}

(void) I2CM_I2CMasterClearStatus();

return sht20_is_connented;
}

float SHT20_GET_TEMP(uint8_t cmd)
{
uint8_t buffer_cmd[1],buffer_data[2];
uint16_t sht20_temp;
float tem;
// buffer_cmd[0]=0XF3;
/* Initialize buffer with packet */

buffer_cmd[0] = cmd;

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer_cmd, 1,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == 1)
{
//PRINTF_RAW("sht20_write ok\r\n");
}
}
}

(void) I2CM_I2CMasterClearStatus();
CyDelay(85);

if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR,
buffer_data, 2,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/* If I2C read started without errors,
/ wait until master complete read transfer */
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus()))
{
/* Check packet structure */
if (I2CM_I2CMasterGetReadBufSize() == 2)
{
//PRINTF_RAW("sht20_read ok\r\n");
}
}
}

sht20_temp=(buffer_data[0]<<8)|buffer_data[1];
//sht20_temp &=~0x0003; // Bitsel değili 0xFFFC'dir.
sht20_temp = sht20_temp&0xFFFC;
tem=sht20_temp*175.72/65536-46.85;

if(tem>25)
{
Pin_Red_Write(~Pin_Red_Read());
CyDelay(500);
Pin_Green_Write(~Pin_Green_Read());
CyDelay(500);
}

(void) I2CM_I2CMasterClearStatus();

return (tem);
}

float SHT20_GET_HUM(uint8_t cmd)
{
uint8_t buffer_cmd[1],buffer_data[2];
uint16_t sht20_hum;
float hum;

/* Initialize buffer with packet */

buffer_cmd[0] = cmd;

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer_cmd, 1,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == 1)
{
//PRINTF_RAW("sht20_write ok\r\n");
}
}
}

(void) I2CM_I2CMasterClearStatus();
CyDelay(30);

if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR,
buffer_data, 2,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/* If I2C read started without errors,
/ wait until master complete read transfer */
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus()))
{
/* Check packet structure */
if (I2CM_I2CMasterGetReadBufSize() == 2)
{
PRINTF_RAW("sht20_read ok\r\n");
}
}
}

sht20_hum=(buffer_data[0]<<8)|buffer_data[1];
sht20_hum &=~0x0003; //sht20_temp_code = sht20_temp_code&0xFFFC;
hum=sht20_hum*125.0/65536-6;

(void) I2CM_I2CMasterClearStatus();

return (hum);
}

 

int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */

/* Place your initialization/startup code here (e.g. MyInst_Start()) */
I2CM_Start();
SHT20_READ_USER_REGISTER();
SHT20_GET_TEMP(0XF3);
SHT20_GET_HUM(0XF5);


for(;;)
{


}
}

/* [] END OF FILE */

 

View solution in original post

12 Replies
MotooTanaka
Esteemed Contributor

Hi,

I just ported my sample below to CY8CKIT-149, so the attached project should work with your board, too.

https://community.infineon.com/t5/Code-Examples/MCU-Tester-a-Swiss-Army-Knife-for-PSoC-CY8CKIT-044-v...

I connected

 I2C-SCL : P3[0]

I2C-SDA: P3[1]

GND: GND

VDD: VDDD (I recommend to use VDDD instead of VDDA)

Since I don't have a SHT20, I used a HDC-1000 for testing below.

After download my sample, build and run debugger.

At the first prompt "> ", type "i2c" and return.

Then prompt will change to "i2c> ", then type "scan" and return.

It will scan I2C bus and report the device found. (my case HDC1000 at 0x40)

For more detailed usage please refer to my post above.

001-TeraTerm-log.JPG

moto

newelectronic1
New Contributor II

Hi,

Thanks for feedback. I checked your example but couldn't adapt the below image SHT20 sensor data address  to your example.

 

Diana_0-1638795540192.png

 

MotooTanaka
Esteemed Contributor

Hi,

I thought that you wanted to know the I2C address of your sensor.

But from the diagram, we can read that it is 0x40.

So if you try my sample and use command "scan", 0x40 should be reported.

If nothing is reported, you may need to check the connection.

 

Now about reading the data, since I don't have that sensor I can not test it in my side, 

but I hope something like below may work

#include "project.h"

...

#define TIMEOUT_MSEC         300u
#define SHT20_I3C_ADDRESS   0x40 
uint8_t  i2c_slave_address = SHT20_I3C_ADDRESS ;
int      i2c_delay = TIMEOUT_MSEC ;

...

in_your_function
{
    int     i2c_result ;
    uint8_t cmd = 0x45 ; // command to write
    uint8_t data_msb, data_lsb, sum ;
    uint16_t data ;
    ....
    
    // the first line
    i2c_result = I2C_I2CMasterSendStart(i2c_slave_address, (uint32_t)0, TIMEOUT_MSEC) ; // 0 to write
    if (i2c_result != I2C_I2C_MSTR_NO_ERROR) {
        return i2c_result ;
    }
    i2c_result = I2C_I2CMasterWriteByte(cmd, TIMEOUT_MSEC) ;
    CyDelayUs(20) ;
    I2C_I2CMasterSendStop(TIMEOUT_MSEC) ;
    
    // the second line
    // wait for measurement
    CyDelay(40) ; // 40ms for the time being... specify something more reasonable
    
    i2c_result = I2C_I2CMasterSendStart(i2c_slave_address, (uint32_t)1, TIMEOUT_MSEC) ; // 1 to read
    // we should get NACK here
    I2C_I2CMasterSendStop(TIMEOUT_MSEC) ;   
    
    // the third line
    i2c_result = I2C_I2CMasterSendStart(i2c_slave_address, (uint32_t)1, TIMEOUT_MSEC) ; // 0 to read
    if (i2c_result != I2C_I2C_MSTR_NO_ERROR) {
        return i2c_result ;
    }    
    
    i2c_result = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA, &data_msb, TIMEOUT_MSEC) ; 
    if (i2c_result != I2C_I2C_MSTR_NO_ERROR) {
        return i2c_result ;
    }    
    i2c_result = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA, &data_lsb, TIMEOUT_MSEC) ;   
    if (i2c_result != I2C_I2C_MSTR_NO_ERROR) {
        return i2c_result ;
    }    
    
    // the fourth line
    i2c_result = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA, &sum,      TIMEOUT_MSEC) ;    
    if (i2c_result != I2C_I2C_MSTR_NO_ERROR) {
        return i2c_result ;
    }    
    
    I2C_I2CMasterSendStop(TIMEOUT_MSEC) ;       
    
...    

}

moto

newelectronic1
New Contributor II

Hi,

it goes directly to the output and doesn't work when I try the above code and debug it. 

if (i2c_result != I2C_I2C_MSTR_NO_ERROR)    ended after this code


I try the below code but doesn't work.

#include "main.h"

#define I2C_SLAVE_ADDR 0X40 //SHT20_ADDRESS
#define SHT20_Write_Add 0x80
#define SHT20_Read_Add 0x81
#define SHT20_Measurement_RH_HM 0XE5 // Trigger RH measurement hold master
#define SHT20_Measurement_T_HM 0XE3 // Trigger T measurement hold master
#define SHT20_Measurement_RH_NHM 0XF5 // Trigger RH measurement no hold master
#define SHT20_Measurement_T_NHM 0XF3 // Trigger T measurement no hold master
#define SHT20_READ_REG 0XE7
#define SHT20_WRITE_REG 0XE6
#define SHT20_SOFT_RESET 0XFE
const int16_t POLYNOMIAL = 0x131;
uint8 buffer[BUFFER_SIZE];


/* Buffer and packet size */
#define BUFFER_SIZE (3u)
#define PACKET_SIZE (BUFFER_SIZE)

/* Packet positions */
#define PACKET_SOP_POS (0u)
#define PACKET_CMD_POS (1u)
#define PACKET_STS_POS (PACKET_CMD_POS)
#define PACKET_EOP_POS (2u)

/* Start and end of packet markers */
#define PACKET_SOP (0x80u)
#define PACKET_EOP (0x64u)

/* Command statuses */
#define STS_CMD_DONE (0x00u)
#define STS_CMD_FAIL (0xFFu)

/* Transfer statuses */
#define TRANSFER_CMPLT (0x00u)
#define TRANSFER_ERROR (0xFFu)

/* List of supported commands */
#define CMD_SET_OFF (0u)
#define CMD_SET_RED (1u)
#define CMD_SET_GREEN (2u)
#define CMD_SET_BLUE (3u)

/* Delay between commands in milliseconds */
#define CMD_TO_CMD_DELAY (500u)

uint32 WriteCommandPacket(uint8 cmd)
{
uint8 buffer[BUFFER_SIZE];
uint32 status = TRANSFER_ERROR;

/* Initialize buffer with packet */
buffer[PACKET_SOP_POS] = PACKET_SOP;
buffer[PACKET_CMD_POS] = cmd;
buffer[PACKET_EOP_POS] = PACKET_EOP;

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer, PACKET_SIZE,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == BUFFER_SIZE)
{
status = TRANSFER_CMPLT;
}
}
}

return (status);
}


uint32 ReadStatusPacket(void)
{
uint8 buffer[BUFFER_SIZE];
uint32 status = TRANSFER_ERROR;

(void) I2CM_I2CMasterClearStatus();

if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR,
buffer, PACKET_SIZE,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/* If I2C read started without errors,
/ wait until master complete read transfer */
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus()))
{
/* Check packet structure */
if ((I2CM_I2CMasterGetReadBufSize() == BUFFER_SIZE) &&
(PACKET_SOP == buffer[PACKET_SOP_POS]) &&
(PACKET_EOP == buffer[PACKET_EOP_POS]))
{
/* Check packet status */
if (STS_CMD_DONE == buffer[PACKET_STS_POS])
{
status = TRANSFER_CMPLT;
}
}
}
}

return (status);
}

char SHT2x_CheckCrc(char data[], char nbrOfBytes, char checksum)
{

char crc = 0;
char bit = 0;
char byteCtr = 0;

//calculates 8-Bit checksum with given polynomial
for(byteCtr = 0; byteCtr < nbrOfBytes; ++byteCtr)
{
crc ^= (data[byteCtr]);
for ( bit = 8; bit > 0; --bit)
{
if (crc & 0x80) crc = (crc << 1) ^ POLYNOMIAL;
else crc = (crc << 1);
}
}

if(crc != checksum)
return 1;
else
return 0;

}

int main()
{
uint8 command = CMD_SET_RED;
uint32 status = TRANSFER_ERROR;


float temp_data=0;
char checksum = 0; //checksum
char data[2]; //data array for checksum verification
unsigned char addr = 0;
unsigned short tmp = 0;
float t = 0;

int readvalue0=0, readvalue1=0, readvalue=0, binary, base=1, dec, num =0;

CyGlobalIntEnable;

/* Disable LEDs */
LED_SUCCESS_Write(LED_SUCCESS_OFF);
LED_ERROR_Write(LED_ERROR_OFF);

/* Start the I2C Master */
I2CM_Start();

for(;;)
{
status = TRANSFER_ERROR;

/* Send packet with command to the slave */
if (TRANSFER_CMPLT == WriteCommandPacket(command))
{
/* Read response packet from the slave */
if (TRANSFER_CMPLT == ReadStatusPacket())
{
status = TRANSFER_CMPLT;

/* Next command to be written */
command++;
if (command > CMD_SET_BLUE)
{
command = CMD_SET_OFF;
}

while( buffer[0]!= 0)
{
dec = buffer[0] %10;
readvalue0 = readvalue0 +dec*base;
buffer[0] = buffer[0] /10;
base = base*2;

}
buffer[1] = buffer[1]<<8;
while(buffer[1]!= 0)
{
dec = buffer[1] %10;
readvalue1 = readvalue1 + dec*base;
buffer[1] = buffer[1] /10;
base = base*2;


}
readvalue = readvalue0+ readvalue1;

temp_data = (175.72 * readvalue)/(65536)-(46.85);

}
}

if(status == TRANSFER_CMPLT)
{
LED_SUCCESS_Write(LED_SUCCESS_ON);
LED_ERROR_Write(LED_ERROR_OFF);
}
else
{
LED_SUCCESS_Write(LED_SUCCESS_OFF);
LED_ERROR_Write(LED_ERROR_ON);
}

/* Delay between the commands */
CyDelay(CMD_TO_CMD_DELAY);


}
}

 

 

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

By any chance, would you teach me if my first sample mcu_tester could report the I2C address of the sensor?

 

> it goes directly to the output and doesn't work when I try the above code and debug it. 

> if (i2c_result != I2C_I2C_MSTR_NO_ERROR)    ended after this code

if you read the value of "i2c_result" then you can have some clue about the error.

in "I2CM_I2C.h" there are

#define I2CM_I2C_MSTAT_ERR_MASK         ((uint16) 0x3F0u) /* Mask for all errors                          */
#define I2CM_I2C_MSTAT_ERR_SHORT_XFER   ((uint16) 0x10u)  /* Master NAKed before end of packet            */
#define I2CM_I2C_MSTAT_ERR_ADDR_NAK     ((uint16) 0x20u)  /* Slave did not ACK                            */
#define I2CM_I2C_MSTAT_ERR_ARB_LOST     ((uint16) 0x40u)  /* Master lost arbitration during communication */
#define I2CM_I2C_MSTAT_ERR_ABORT_XFER   ((uint16) 0x80u)  /* The Slave was addressed before the Start gen */
#define I2CM_I2C_MSTAT_ERR_BUS_ERROR    ((uint16) 0x100u) /* The misplaced Start or Stop was occurred     */
#define I2CM_I2C_MSTAT_ERR_XFER         ((uint16) 0x200u) /* Error during transfer                        */

/* Master API returns */
#define I2CM_I2C_MSTR_NO_ERROR          (0x00u)  /* Function complete without error                       */
#define I2CM_I2C_MSTR_ERR_ARB_LOST      (0x01u)  /* Master lost arbitration: INTR_MASTER_I2C_ARB_LOST     */
#define I2CM_I2C_MSTR_ERR_LB_NAK        (0x02u)  /* Last Byte Naked: INTR_MASTER_I2C_NACK                 */
#define I2CM_I2C_MSTR_NOT_READY         (0x04u)  /* Master on the bus or Slave operation is in progress   */
#define I2CM_I2C_MSTR_BUS_BUSY          (0x08u)  /* Bus is busy, process not started                      */
#define I2CM_I2C_MSTR_ERR_ABORT_START   (0x10u)  /* Slave was addressed before master begin Start gen     */
#define I2CM_I2C_MSTR_ERR_BUS_ERR       (0x100u) /* Bus error has: INTR_MASTER_I2C_BUS_ERROR              */
#define I2CM_I2C_MSTR_ERR_TIMEOUT       (I2CM_I2C_MASTER_TIMEOUT) /* Operation timeout        */


> I try the below code but doesn't work.

The sentence "doesn't work" doesn't give us any clue.

It would be nice to inform us, in which line, what kind error was detected.

Or if the code itself is running but the value(s) are different from what you are expecting, etc.

moto

 

0 Likes
newelectronic1
New Contributor II

hi,

By any chance, would you teach me if my first sample mcu_tester could report the I2C address of the sensor?

I debug and it stops in the get line function section.

-

newelectronic1_0-1638870424684.png

 

 

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

I debug and it stops in the get line function section.

So it was not working from the first place.

To use this sample, please use a serial terminal, like Tera Term, etc.

And connect to the COM port of the KitProg with 115200bps 8N1.

For the usage please refer to my post 

https://community.infineon.com/t5/Code-Examples/MCU-Tester-a-Swiss-Army-Knife-for-PSoC-CY8CKIT-044-v...

moto

0 Likes
newelectronic1
New Contributor II

hi, 

When I test your example, it gives output as in the picture, but when I add sensor addresses, I can't get any output.

newelectronic1_0-1638877020189.png

 

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

Today HiLetgo SHT20 modules, which I ordered for this thread arrived.

https://www.amazon.co.jp/gp/product/B083NTTRPC/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

And I tested with my MCU Tester program.

And as I expected, SHT20 was scanned as a device at 0x40.

002-TeraTerm-SHT20.JPG

FYI, my sample requires some VT100 Escape Sequence capabilities,

so please use Tera Term or some other terminal emulator which can act as VT100.

Meantime, in the Tera Term setup menu, please set New-line for Receive to "AUTO"

so that both "\n" and "\r\n" can be handled correctly.

003-TeraTerm-Menu-Terminal_setup.JPG

Since now I have a couple of SHT20 modules, I can look into the program "in my spare time"

Meantime, please try with "Tera Term" to make sure that your hardware is fine.

moto

 

0 Likes
MotooTanaka
Esteemed Contributor

Hi,

I played with my SHT20 sensor module a little more.

004-MCU_Tester_log.JPG

In the log

i2c> slave 0x40  // set the slave address to 0x40 (for SHT20)

i2c> readregs 0xE3 3 // read 3 bytes from 0xE3, which is Trigger T measurement

i2c> readregs 0xE5 3 // read 3 bytes from 0xE5, which is Trigger RH measurement

i2c> readreg 0xE7 // read register at 0xE7, which is user register

Everything seems to be working fine.

So I hope that you should be able to use the following functions from my i2c_api_utils.[ch]

i2c_set_slave_address(uint8_t newAddress) 

void i2c_readRegs(uint8_t addr, uint8_t *data, int len) 

uint8_t i2c_readReg(uint8_t addr) 

 

moto

 

0 Likes
newelectronic1
New Contributor II

Hi,

Thanks a lot for your support. I arrange like same  your code and ıt works good work. Below code running.

 

 

#include "project.h"

 

#define I2C_SLAVE_ADDR (0x40u)

bool SHT20_READ_USER_REGISTER(void)
{
uint8 buffer_cmd[1],sht20_is_connented=0;

buffer_cmd[0]=0xE7; // SHT20_READ_REG

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer_cmd, 1,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == 1)
{
sht20_is_connented=1;
}
else
sht20_is_connented=0;
}
}

(void) I2CM_I2CMasterClearStatus();

return sht20_is_connented;
}

float SHT20_GET_TEMP(uint8_t cmd)
{
uint8_t buffer_cmd[1],buffer_data[2];
uint16_t sht20_temp;
float tem;
// buffer_cmd[0]=0XF3;
/* Initialize buffer with packet */

buffer_cmd[0] = cmd;

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer_cmd, 1,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == 1)
{
//PRINTF_RAW("sht20_write ok\r\n");
}
}
}

(void) I2CM_I2CMasterClearStatus();
CyDelay(85);

if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR,
buffer_data, 2,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/* If I2C read started without errors,
/ wait until master complete read transfer */
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus()))
{
/* Check packet structure */
if (I2CM_I2CMasterGetReadBufSize() == 2)
{
//PRINTF_RAW("sht20_read ok\r\n");
}
}
}

sht20_temp=(buffer_data[0]<<8)|buffer_data[1];
//sht20_temp &=~0x0003; // Bitsel değili 0xFFFC'dir.
sht20_temp = sht20_temp&0xFFFC;
tem=sht20_temp*175.72/65536-46.85;

if(tem>25)
{
Pin_Red_Write(~Pin_Red_Read());
CyDelay(500);
Pin_Green_Write(~Pin_Green_Read());
CyDelay(500);
}

(void) I2CM_I2CMasterClearStatus();

return (tem);
}

float SHT20_GET_HUM(uint8_t cmd)
{
uint8_t buffer_cmd[1],buffer_data[2];
uint16_t sht20_hum;
float hum;

/* Initialize buffer with packet */

buffer_cmd[0] = cmd;

(void) I2CM_I2CMasterClearStatus();

/* Start I2C write and check status*/
if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterWriteBuf(I2C_SLAVE_ADDR,
buffer_cmd, 1,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/*If I2C write started without errors,
/ wait until I2C Master completes write transfer
*/
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_WR_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_ERR_XFER))
{
/* Check if all bytes was written */
if (I2CM_I2CMasterGetWriteBufSize() == 1)
{
//PRINTF_RAW("sht20_write ok\r\n");
}
}
}

(void) I2CM_I2CMasterClearStatus();
CyDelay(30);

if(I2CM_I2C_MSTR_NO_ERROR == I2CM_I2CMasterReadBuf(I2C_SLAVE_ADDR,
buffer_data, 2,
I2CM_I2C_MODE_COMPLETE_XFER))
{
/* If I2C read started without errors,
/ wait until master complete read transfer */
while (0u == (I2CM_I2CMasterStatus() & I2CM_I2C_MSTAT_RD_CMPLT))
{
/* Wait */
}

/* Display transfer status */
if (0u == (I2CM_I2C_MSTAT_ERR_XFER & I2CM_I2CMasterStatus()))
{
/* Check packet structure */
if (I2CM_I2CMasterGetReadBufSize() == 2)
{
PRINTF_RAW("sht20_read ok\r\n");
}
}
}

sht20_hum=(buffer_data[0]<<8)|buffer_data[1];
sht20_hum &=~0x0003; //sht20_temp_code = sht20_temp_code&0xFFFC;
hum=sht20_hum*125.0/65536-6;

(void) I2CM_I2CMasterClearStatus();

return (hum);
}

 

int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */

/* Place your initialization/startup code here (e.g. MyInst_Start()) */
I2CM_Start();
SHT20_READ_USER_REGISTER();
SHT20_GET_TEMP(0XF3);
SHT20_GET_HUM(0XF5);


for(;;)
{


}
}

/* [] END OF FILE */

 

View solution in original post

AlenAn14
Moderator
Moderator

Hi @newelectronic1 ,

Glad your issues is resolved and your code is running well :).
Also, thank you @MotooTanaka  for your contribution to the community, we appreciate the same.

Please do reach out to the community again on any queries or issues you may face on our products and we will be happy to help.

@newelectronic1 , You can mark your above response as the solution so we can close this thread.

Thanks & Regards
Alen