You are here

ATOI For PSOC 3 | Cypress Semiconductor

ATOI For PSOC 3

Summary: 27 Replies, Latest post by Bob Marlowe on 17 Apr 2013 11:17 AM PDT
Verified Answers: 0
Last post
Log in to post new comments.
pnielsen3's picture
User
132 posts

In Psoc 1 I was able to convert raw voltage data to actual voltage using ATOI base 10.

How should the program be setup for PSoc 3?

graa's picture
Cypress Employee
239 posts

In PSoC3 you can use the sprintf command. For example if you want to display an integer on an LCD.

int ADCResult;

char OutputString[7];

sprintf(OutputString, "%i", ADCResult);

LCD_Position(1,0);

LCD_PrintString(OutputString);

The sprintf function converts the integer into ASCII string and stores it in OutputString. LCD_PrintString can be used to print it to the LCD.

pnielsen3's picture
User
132 posts

Thank you for the fast response. I remember Psoc 1 did not have sprintf function or I could not find it.

Thank you

Philip Nielsen

pnielsen3's picture
User
132 posts

 

 

 

 

 

How would I convert:

LCD_Char_PrintInt8(HYT221_Read_Buff[0] & 0x3f);

LCD_Char_PrintInt8(HYT221_Read_Buff[0] & 0x3f);

Using: 

I need a decimal number instead of a Hexadecimal?

sprintf(Buffer, "%i", Temp);

pnielsen3's picture
User
132 posts

The second line should be

LCD_Char_PrintInt8(HYT221_Read_Buff[1]);

user_1377889's picture
User
10803 posts

uint8 * Buffer[17];

sprintf(Buffer,"%6d",(uint8)HYT221_Read_Buff[1]);

LCD_PrintString(Buffer);

 

Should do the trick. Do not forget to #include <stdio.h>

 

Bob

pnielsen3's picture
User
132 posts

Thanks for the fast response. How do I add the "

The LCD number is bouncing all over.

 

 

LCD_Char_PrintInt8(HYT221_Read_Buff[0] & 0x3f);"

pnielsen3's picture
User
132 posts

This is my code. The display section is a mess. I am trying to read the number with letters from a I2C sensor. Then convert the 4 charactures and convert to a 4 digit decimal. My Windows calculator will do this but my code is messed up during the conversion

 

/*******************************************************************************

 

* File Name: main.c

 

*

 

* Version: 1.0

 

*

 

* Description:

 

*

 

*

 

* Parameters used:

 

* I2CM Master

 

* Implementation Fixed function

 

* Data rate 100kbps

 

* SDA SCL pin config Open drain, drives low

 

* Pull-up resistors 2.67k each

 

*

 

*

 

*

 

********************************************************************************

 

* Copyright 2012, Cypress Semiconductor Corporation. All rights reserved.

 

* This software is owned by Cypress Semiconductor Corporation and is protected

 

* by and subject to worldwide patent and copyright laws and treaties.

 

* Therefore, you may use this software only as provided in the license agreement

 

* accompanying the software package from which you obtained this software.

 

* CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,

 

* WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,

 

* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

 

*******************************************************************************/

 

 

 

<device.h>

 

 

<stdio.h>

 

 

<stdarg.h>

 

 

<math.h>

 

 

HYT_MR 0

 

 

HYT_DR 1

 

 

I2CM_SLAVE_ADDRESS (0x28u)

 

 

HYT221_Read_Buff[4] = {0};

 

 

HYT221_Write_Buff[4] = {0};

 

 

HYT_MR_Initialization(void);

 

 

HYT_DR_Operation(void);

 

 

Display_Result(void);

 

 

OutputString[7];

 

char OutputString1[7];

 

 

main()

 

/* Start I2CMm */

 

/* The LCD will display Standare info */

 

"Hraw:");

 

"Traw:");

 

/* Enable global interrupts - required for I2CM */

 

for(;;)

 

/* Delay introduced for ease of reading LCD */

 

/*ms*/);

 

/* End forever loop */

 

/* End main */

 

 

HYT_DR_Operation(void)

 

uint8 status = 0;

 

uint8 i;

 

if(I2CM_MSTR_NO_ERROR == status) /* Check if transfer completed without errors */

 

for(i=0; i<4; i++)

 

if(i < 3)

 

else

 

else

 

"Bus Error");

 

"Bus Error");

 

/* Send Stop */

 

 

HYT_MR_Initialization(void)

 

uint8 status = 0;

 

if(I2CM_MSTR_NO_ERROR == status) /* Check if transfer completed without errors */

 

//Delay to debug - If routine execute to here, only stop need to archive MR operation

 

else

 

"Bus Error");

 

"Bus Error");

 

/* Send Stop */

 

 

Display_Result(void)

 

 

* Buffer[17];

 

"%6d",(uint8)HYT221_Read_Buff[1]);

 

// LCD_Char_PrintInt8(HYT221_Read_Buff[0] & 0x3f); //Mask HYT humidity the highest two status bits

 

// sprintf(OutputString, "%d", HYT221_Read_Buff[0] & 0x3f);

 

 

 

 

// LCD_Char_Position(1,6);

 

// LCD_Char_PrintString(OutputString1);

 

// LCD_Char_PrintString(HYT221_Read_Buff[0] & 0x3f); //Mask HYT humidity the highest two status bits

 

// LCD_Char_PrintInt8(HYT221_Read_Buff[1]);

 

// LCD_Char_PrintString(" ");

 

// LCD_Char_Position(1,6);

 

// LCD_Char_PrintInt8(HYT221_Read_Buff[2]);

 

// LCD_Char_PrintInt8(HYT221_Read_Buff[3] & 0xfc); //Mask HYT temperature the lowest two status bits

 

// LCD_Char_PrintString(" ");

 

// sprintf(Buffer, "%i", Temp);

 

// int ADCResult;

 

// sprintf(OutputString1, "%i", HYT221_Read_Buff[0] & 0x3f);

 

// sprintf(OutputString, "%i", HYT221_Read_Buff[1]);

 

//sprintf(OutputString, "%i", HYT221_Read_Buff[1]);

 

//LCD_Char_Position(1,6);

 

//LCD_Char_PrintString(OutputString);

 

}

// LCD_Char_PrintInt8(HYT221_Read_Buff[1]); //Mask HYT humidity the highest two status bits

 

 

 

 

 

 

LCD_Char_PrintString(Buffer);

 

LCD_Char_Position(1,6);

 

 

sprintf(Buffer,

{

 

 

 

 

 

// char OutputString1[7];

 

 

 

uint8

}

 

 

 

/*******************************************************************************

 

* Function Name: Display_Result

 

********************************************************************************

 

*

 

* Summary:

 

* Update LCD with latest result

 

*

 

* Parameters:

 

* None.

 

*

 

* Return:

 

* None.

 

*

 

*******************************************************************************/

 

void

}

 

I2CM_MasterSendStop();

LCD_Char_Position(1,6);

 

LCD_Char_PrintString(

{

 

LCD_Char_Position(0,6);

 

LCD_Char_PrintString(

CyDelay(1);

 

}

 

 

{

 

 

 

 

status = I2CM_MasterSendStart(I2CM_SLAVE_ADDRESS, HYT_MR);

 

 

{

 

 

}

 

/*******************************************************************************

 

* Function Name: HYT_MR_Initialization

 

********************************************************************************

 

*

 

* Summary:

 

* Initializate HYT enable a measure cycle

 

*

 

* Parameters:

 

* None.

 

*

 

* Return:

 

* None.

 

*

 

*******************************************************************************/

 

void

}

 

I2CM_MasterSendStop();

LCD_Char_Position(1,6);

 

LCD_Char_PrintString(

{

 

LCD_Char_Position(0,6);

 

LCD_Char_PrintString(

{

 

HYT221_Read_Buff[i] = I2CM_MasterReadByte(I2CM_NAK_DATA);

 

}

 

}

 

 

{

 

HYT221_Read_Buff[i] = I2CM_MasterReadByte(I2CM_ACK_DATA);

 

 

 

}

 

 

{

 

 

 

 

 

status = I2CM_MasterSendStart(I2CM_SLAVE_ADDRESS, HYT_DR);

 

 

 

{

 

 

/*******************************************************************************

 

* Function Name: HYT_DR_Operation

 

********************************************************************************

 

*

 

* Summary:

 

* Read HYT measurement result and put into HYT221_Read_Buff

 

*

 

* Parameters:

 

* None.

 

*

 

* Return:

 

* None.

 

*

 

*******************************************************************************/

 

void

}

 

 

}

CyDelay(3000u

{

 

HYT_MR_Initialization();

 

HYT_DR_Operation();

 

Display_Result();

 

 

 

 

CyGlobalIntEnable;

 

 

 

LCD_Char_Position(1,0);

 

LCD_Char_PrintString(

LCD_Char_Start();

 

LCD_Char_ClearDisplay();

 

LCD_Char_Position(0,0);

 

LCD_Char_PrintString(

I2CM_EnableInt();

 

I2CM_Start();

 

 

{

 

 

/*******************************************************************************

 

* Function Name: main

 

********************************************************************************

 

*

 

* Summary:

 

* main function performs following functions:

 

*

 

* Parameters:

 

* None.

 

*

 

* Return:

 

* None.

 

*

 

*******************************************************************************/

 

void

 

char

/* The Display function define */

 

void

void

/* The HYT221 function define */

 

void

uint8

/* The I2CM buffer define */

 

uint8

/* The I2CM Slave address by default in a PSoC device is 8 */

 

#define

#define

//#include <stdio.h>

 

/* HYT221 command define */

 

#define

#include

#include

#include

#include

user_14586677's picture
User
7648 posts

Some example formatting scripts for use with sprintf(), attached.

 

Regards, Dana.

hli
user_78878863's picture
User
2759 posts

Do you want to convert a number to a string (for output), or do you want to convert the raw value you got from the ADC to a voltage value? For the latter one use the ADC_CountsTo_Volts() call (or _mVolts() or _uVolts()), which are available for both the DelSig and the SAR ADC.

pnielsen3's picture
User
132 posts

 I have a Humidity  sensor that I communicate with I2C bus. I am ready to tear up my code. I just want to read the value from the sensor and convert the value to a decimal value. The formula for the humidity is to take the decimal value * 100/16383.

 

The LCD display is currently displaying numbers and letters. I just want 4 numbers. fro 0 to 9.

 

 

I have taken the the value from the LCD display and have been able to convert to decimal with Windows Hex to decimal calculator. Should I just scrap my code and restart?

/*******************************************************************************

I have attached the entire file without any comments

Log in to post new comments.