Max.Otto11
Newbie level 6
- Joined
- Oct 13, 2014
- Messages
- 13
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 180
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. */
}
}
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()?.
Code C - [expand] 1 LCD_PrintInt8(ADC_Ans);
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 } }
Code C - [expand] 1 LCD_PrintInt8(ADC_Ans);
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()?.
#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 */
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. */ } }
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.
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()?.
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();
Code C - [expand] 1 PrintNumber()
uint16 ADC_Ans;
ADC_Ans = ADC_DelSig_GetResult16 ();
LCD_PrintDecUint16 (ADC_Ans);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?