[SOLVED] converted 16-bit-ADC value on LCD

Status
Not open for further replies.

Max.Otto11

Newbie level 6
Joined
Oct 13, 2014
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
180
Hi all,
i am working with PsoC 5LP. I want to display 16-bit ADC value on LCD in Volts format. ADC input range is 0.0 to Vref so the LCD will start from (0,1, 0,2, 0,3...1.024V). Or may be in another understandable format i tried but i don't understand the converted value.
pls find the code which i tried.

Code:
int main()
{
	
    uint16 ADC_Ans;
	//uint8 ADC_Ans8;
	
	Sys_Init(); // System Initializing
	CY_SET_REG8(CYREG_MLOGIC_DEBUG, CY_GET_REG8(CYREG_MLOGIC_DEBUG) | 0x40);
	
	ADC_DelSig_Start();
	ADC_DelSig_StartConvert();
	
    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
    while(1)
    {
		LCD_ClearDisplay() ;
		LCD_PrintString("ADC Value:"); 
		CyDelay(1500); // Delay in ms 
		
        ADC_DelSig_IsEndConversion(ADC_DelSig_WAIT_FOR_RESULT); 
		ADC_Ans = ADC_DelSig_GetResult16();
		//ADC_DelSig_StopConvert();				
			
		//ADC_Ans8 = ADC_Ans >> 8;
		LCD_Position(0,10);
		LCD_PrintString("0x"); 
				
		LCD_Position(0,12);
		LCD_PrintHexUint8(ADC_Ans);
		//LCD_PrintInt8(ADC_Ans);  
		CyDelay(1500); // Delay in ms 
		
		
		
		/* Place your application code here. */
    }
}
 

Hi,

You haven't given much information. Is the code you posted the only code you have tried? I'm not sure where exactly this code has come from, but from the comments its looks like example code that wasn't written to do what you are asking.

What happens when you run this code? It looks to me like it will display a hexadecimal number on the screen, something like 0x0CD4, and that number will change depending on what voltage is supplied to the ADC.

I'm not familiar with the system you are using, but to me the code to read the ADC looks OK. You just need to convert the number the ADC gives you into a voltage value. This is just, ADC_Ans/step_size * Vref, however, you're going to have to be careful with types.

Is there a function to print a floating point number directly, something like LCD_PrintFloat()?.
 
Try this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
int main()
{
    
    uint16 ADC_Ans;
    uint16 ADC_Ans;
    
    Sys_Init(); // System Initializing
    CY_SET_REG8(CYREG_MLOGIC_DEBUG, CY_GET_REG8(CYREG_MLOGIC_DEBUG) | 0x40);
    
    ADC_DelSig_Start();
    ADC_DelSig_StartConvert();
    
    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
    while(1)
    {
        LCD_ClearDisplay() ;
        LCD_Position(1u, 0u);
            LCD_PrintString("ADC Value:"); 
        CyDelay(1500); // Delay in ms 
        
            ADC_DelSig_IsEndConversion(ADC_DelSig_WAIT_FOR_RESULT); 
        ADC_Ans = ADC_DelSig_GetResult16();
                    
        LCD_Position(1u, 12u);
            LCD_PrintNumber(ADC_Ans);
            
        CyDelay(1500); // Delay in ms 
        
        
        
        /* Place your application code here. */
    }
}



Is there a function to print a floating point number directly, something like LCD_PrintFloat()?.

I guess printNumber() will do it. I am not sure. Try and see.
 
Hi for that purpose there is another function under the Hex function

Code C - [expand]
1
LCD_PrintInt8(ADC_Ans);


which is commented.

So using that will be enough for integer, using sprintf we can make it work for directly float.

Try this code with stdio.h

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
 
int main()
{
    
    uint16 ADC_Ans;
    float ans;
    char string[20];
    //uint8 ADC_Ans8;
    
    Sys_Init(); // System Initializing
    CY_SET_REG8(CYREG_MLOGIC_DEBUG, CY_GET_REG8(CYREG_MLOGIC_DEBUG) | 0x40);
    
    ADC_DelSig_Start();
    ADC_DelSig_StartConvert();
    
    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
    while(1)
    {
        LCD_ClearDisplay() ;
        LCD_PrintString("ADC Value:"); 
        CyDelay(1500); // Delay in ms 
        
        ADC_DelSig_IsEndConversion(ADC_DelSig_WAIT_FOR_RESULT); 
        ADC_Ans = ADC_DelSig_GetResult16();
 
        ans = ADC_Ans * 5.0 / 65536;
 
        LCD_Position(0,0);
        sprintf(&string[0],"%f", ans)        
        LCD_PrintString(&string[0]); 
 
        CyDelay(1500); // Delay in ms         
 
    }
}


So this program will print in float.
 

I guess

Code C - [expand]
1
LCD_PrintInt8(ADC_Ans);

prints 8 bit integer but he is doing a 16 bit ADC read, So, I guess he has to use LCD_PrintNumber().
 

yeah thats ok, may be you can try with int16 or 32 in the end of that function for integer. but the program in #5 will print in float, we have adjust the Vref and type casting only.
 
I guess if in my code ADC_Ans is made a float or double type then it will print float value.
 



Hi.. Thanks for reply. Here is the example code for 8bit ADC. I want to Display on LCD either Hex form or Analog Voltage value.
Code:
#include <device.h>

/* LCD specific */
#define ROW_0       0  /* LCD row 0     */
#define COLUMN_0    0  /* LCD column 0  */
#define COLUMN_9    9  /* LCD column 9  */
#define COLUMN_10  14  /* LCD column 10 */
#define COLUMN_11  15  /* LCD column 11 */
/* For clearing Tens and Hundreds place */
#define CLEAR_TENS_HUNDREDS "    "
/* For clearing Hundreds place */
#define CLEAR_HUNDREDS      "   "

void UpdateDisplay(uint16 voltageRawCount);


/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*   The main function initializes both the ADC and LCD, starts and waits for an 
*   ADC conversion, then it displays the raw counts to the LCD.
*
* Parameters:
*  void
*
* Return:
*  void
*
*******************************************************************************/
int main()
{
    uint16 voltageRawCount;
    CY_SET_REG8(CYREG_MLOGIC_DEBUG, CY_GET_REG8(CYREG_MLOGIC_DEBUG) | 0x40);
	/* Configure and power up ADC */
    ADC_DelSig_1_Start();  
	
	/* Initialize and clear the LCD */
    LCD_Char_1_Start(); 

	/* Move the cursor to Row 0 Column 0 */
    LCD_Char_1_Position(ROW_0,COLUMN_0); 
    
    /* Print Label for the pot voltage raw count */
    LCD_Char_1_PrintString("Count: 0x");

    /* Force ADC to initiate a conversion */
	ADC_DelSig_1_StartConvert(); 

    while(1)
    {
        /* Wait for end of conversion */
        ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT); 
		
		/* Get converted result */
        voltageRawCount = ADC_DelSig_1_GetResult16(); 

        /* Set range limit */
        if (voltageRawCount > 0x7FFF)
        {
            voltageRawCount = 0;
        }
        else 
        {
            /* Continue on */
        }
        /* Print result on LCD */
		CyDelay(1500); 
		UpdateDisplay(voltageRawCount); 
    }
}

/*******************************************************************************
* Function Name: UpdateDisplay
********************************************************************************
*
* Summary:
*   Print voltage raw count result to the LCD.  Clears some characters if
*   necessary.
*
* Parameters:
*   voltageRawCount: The voltage raw counts being received from the ADC 
*
* Return:
*   void
*
*******************************************************************************/
void UpdateDisplay (uint16 voltageRawCount)
{
    /* Move the cursor to Row 0, Column 9 */
    LCD_Char_1_Position(ROW_0,COLUMN_9); 
	
	/* Print the result */
	LCD_Char_1_PrintHexUint16(voltageRawCount); 
	
    //LCD_Char_1_PrintNumber(voltageRawCount); 
    
    if (voltageRawCount < 10)
    {
        /* Move the cursor to Row 0, Column 10 */
        LCD_Char_1_Position(ROW_0,COLUMN_10); 
		
		/* Clear last characters */
        LCD_Char_1_PrintString(CLEAR_TENS_HUNDREDS);
    }
    else if (voltageRawCount < 100)
    {
        /* Move the cursor to Row 0, Column 11 */
        LCD_Char_1_Position(ROW_0,COLUMN_11);
		
		/* Clear last characters */
        LCD_Char_1_PrintString(CLEAR_HUNDREDS); 
    }
    else
    {
        /* Continue on */
    }
}


/* [] END OF FILE */

- - - Updated - - -

Thanks for ur Suggestion. I tried it but it's not working.
Can u pls suggest me other one.?

 

Thanks but it'S not worked. LCD Display nothing.
Can u pls suggest me other one?

 

Thank for your Suggestion . But the main Problem i face is of Type-casting i use uint16 for ADC_ans, Step_size = 65536 & Vref = 5.0v.

For LCD Display i have PrintDecUint16, PrintHexUint16, PrintHexUint8, PrintInt16, PrintInt32, PrintInt8, PrintNumber, PrintString.

Pls suggest me.



 

Declare ADC_Ans as float or double and cast adc value to it. If ADC_Ans is double type and ADC_Ans16 is uint16 type then


Code C - [expand]
1
2
3
4
5
6
ADC_Ans16 = ADC_DelSig_GetResult16();
ADC_Ans = (float)ADC_Ans16;
 
//or
 
ADC_Ans = (float)ADC_DelSig_GetResult16();



Then use


Code C - [expand]
1
PrintNumber()



Is the LCD print library functions provided are for HD44780 compatible LCDs ? If yes, then your LCD should be HD44780 compatible type like JHD162 A.

I guess PSoC pins work at 3.3V. I am not Sure. Does this causing a problem ? LCD might need 5V sigmals.
 

Hallo all,

I have 1.024v for 16-bit ADC with Single Sample Input Range to VSSA.

Code:
 uint16 ADC_Ans; 

   ADC_Ans = ADC_DelSig_GetResult16 (); 

   LCD_PrintDecUint16 (ADC_Ans);
This Programm works, also same as Example Programm.

I gave Input 0.5V. The LCD shows 34044 or 34048th Is this
Answer Correct? Because my project manager says that the LCD should 48000,
49000 or 50000 shows


Please help me for this.
 

What is your Vref- and Vref+? What is the VDD of PSoC ? What is the input voltage you are measuring ? What is the range of input you want to measure ? What value are you getting for a certain adc input voltage ?
 

Thanks you all for all suggestions & ideas..
My program works perfect as i Need.
Thanks

My Programm Working guys..:thumbsup:
 

It is shortly to change my input. Now I have the input range of VSSA To VDDA. So 0.0V to 3.3V. For Input 3.3V my LCD shows 0004. I think it automatically takes 4.9V. I have selected in ADC design input range "VSSA to VDDA". But should I write in the program for a maximum input 3.3. But what .. ??

regards
Max
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…