Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
sagarp169
Level 1
Level 1
First question asked Welcome!

Hello, I am new to PSOC, and is currently working in CY8C4245AXI-483

I am trying to make a simple project to read and write the data in external eeprom using i2c, untill now i have aquired the result but the data i stored is in "HEX" whereas the data i received on my serial is in ascii

EX: I am sending (0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49)

and receiving "B C D E F G H I

Why do i not get HEX in output and how to get it ???

My code is given below

#include <project.h>
void main()
{
uint16 read_val[8]; //Array to read data from EEprom
uint8 array[8] = {0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49}; //Array to write data to EEPROM
uint8 i;
uint32 status; //variable to read error/status of I2C

I2C_Start();
UART_Start();

for(;;)
{

status = I2C_I2CMasterSendStart(0x50, I2C_I2C_WRITE_XFER_MODE); //Send start with slaveID 80u & write/read transfer mode

if(I2C_I2C_MSTR_NO_ERROR == status) //Check status
{
status = I2C_I2CMasterWriteByte(0x00); //address of eeprom on which data needs to be written

if(I2C_I2C_MSTR_NO_ERROR == status) //Check status
{ //if valid write data to external eeprom

for(i=0; i<8; i++)
{

status = I2C_I2CMasterWriteByte(array[i]);

if(I2C_I2C_MSTR_NO_ERROR != status) //Check status
{ //if success come out of loop

break;
}
}
}
else
{
UART_PutString("error"); //else print error
}
}
I2C_I2CMasterSendStop(); //Send stop

/***************Read that same data from external EEPROM**************************/
CyDelay(500u);
status = I2C_I2CMasterSendStart(80u, I2C_I2C_WRITE_XFER_MODE); //Again send start
if(I2C_I2C_MSTR_NO_ERROR == status) //Check status
{
status = I2C_I2CMasterWriteByte(0x00); //address of eeprom to read data on it
if(I2C_I2C_MSTR_NO_ERROR == status) //Check status
{
CyDelay(50u);
status = I2C_I2CMasterSendRestart(80u, I2C_I2C_READ_XFER_MODE); //Send restart
if(I2C_I2C_MSTR_NO_ERROR == status) //Check status if no error
{
for(i=0; i<8; i++)
{
if(i<7u)
{
read_val[i] = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA); //receive data bytes with ack


}
else
{
read_val[i] = I2C_I2CMasterReadByte(I2C_I2C_NAK_DATA); //receive last byte with nack

}
}
}
}
}
I2C_I2CMasterSendStop(); //Send stop

for(i=0; i<8; i++)
{

UART_PutChar(read_val[i],); //display data on uart
UART_PutString(" ");
CyDelay(500u);
}

CyDelay(3000u);
}
}

/* [] END OF FILE */

0 Likes
1 Solution
BiBi_1928986
Level 7
Level 7
First comment on blog 500 replies posted 250 replies posted

Hello.

You need to convert the HEX into ASCII-HEX before sending to UART.
I've written a routine to do this.  I'm sure there are lots of different ways to do this.

unsigned char hex_to_ascii[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46};
unsigned char temp1;

/*******************************************************************************
* Function Name: prthex()
********************************************************************************
*
* Summary:
*
* Print a hex byte as ASCII, followed by a space.
*
* Parameters:
* unsigned char
*
* Return:
* None.
*
*
*******************************************************************************/
void prthex(uint8 hex)
{temp1=(hex&0xf0)>>4;
temp1=hex_to_ascii[temp1];

UART_1_PutChar(temp1);

temp1=(hex&0x0f);
temp1=hex_to_ascii[temp1];

UART_1_PutChar(temp1);
UART_1_PutChar(0x20);}

View solution in original post

0 Likes
1 Reply
BiBi_1928986
Level 7
Level 7
First comment on blog 500 replies posted 250 replies posted

Hello.

You need to convert the HEX into ASCII-HEX before sending to UART.
I've written a routine to do this.  I'm sure there are lots of different ways to do this.

unsigned char hex_to_ascii[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,0x45,0x46};
unsigned char temp1;

/*******************************************************************************
* Function Name: prthex()
********************************************************************************
*
* Summary:
*
* Print a hex byte as ASCII, followed by a space.
*
* Parameters:
* unsigned char
*
* Return:
* None.
*
*
*******************************************************************************/
void prthex(uint8 hex)
{temp1=(hex&0xf0)>>4;
temp1=hex_to_ascii[temp1];

UART_1_PutChar(temp1);

temp1=(hex&0x0f);
temp1=hex_to_ascii[temp1];

UART_1_PutChar(temp1);
UART_1_PutChar(0x20);}

0 Likes