# code to convert ADC value to hex?

#### HappyC4mper

Joined Oct 13, 2017
71
Hey guys, im a bit stuck atm in trying to convert my ADC data to hex values so that it can show up on terminal (ive already made a code for terminal).
I've found this code and added it to my code but I don't really understand how the code works or how it converts. If someone could somehow explain it to me or if there's a simpler way of doing it, that would great!
Thanks
[send_usart is me sending it to terminal. ]

Code:
void ADC_Hex_USART (float readadc)
{
unsigned  char Index_Counter =0;  // not sure what this does
char Hex_decimal[16];
printf(Hex_decimal, "Volts = %.3f", Voltage_Value);
while(Hex_decimal[Index_Counter] != '\0')    //not sure what !='\0' does as well.
{
send_usart(Hex_decimal[Index_Counter]);
Index_Counter++;
}

#### MrChips

Joined Oct 2, 2009
23,576
If you already have printf( ) in C there is nothing else you need.
Simply use:

printf("Hex value = %4X\n", value);

#### HappyC4mper

Joined Oct 13, 2017
71
If you already have printf( ) in C there is nothing else you need.
Simply use:

printf("Hex value = %4X\n", value);
so there's no need for Index_counter?
Code:
 unsigned  char Index_Counter =0;

#### AlbertHall

Joined Jun 4, 2014
11,318
Lines 7 and 9 use index_counter to loop through all the characters and send them to the usart. The '/0' bit is looking for the nul character which trerminates C strings to know when to stop sending characters.

so there's no need for Index_counter?
Code:
 unsigned  char Index_Counter =0;
Not if you can persuade printf to send the message to usart.

#### HappyC4mper

Joined Oct 13, 2017
71
Lines 7 and 9 use index_counter to loop through all the characters and send them to the usart. The '/0' bit is looking for the nul character which trerminates C strings to know when to stop sending characters.

Not if you can persuade printf to send the message to usart.
turns out, my orginial code does not work with printf.
I therefore wrote this, not sure if it's right, but I don't know how to send it to my usart (send_usart()
Code:
void Read_usart()
{
unsigned short Hex;
Hex    = read_adc();    // Get ADC result and store in HEX
printf("\n\r");        // Send out a space
printf("Hex value = %4X\n", Hex);

#### MrChips

Joined Oct 2, 2009
23,576
We had this question recently on how to redirect the stdio.h output. You need in supply your own putc( ) rountine.
I will have to dig this up.

#### MrChips

Joined Oct 2, 2009
23,576
What MCU are you using and what programming platform (IDE or toolset)?

#### HappyC4mper

Joined Oct 13, 2017
71
What MCU are you using and what programming platform (IDE or toolset)?
Using a nucleo-f429ZI and using KEIL to code

#### HappyC4mper

Joined Oct 13, 2017
71
Code:
unsigned short read_adc(void)
{
return ADC1->DR;                                    //return converted value
}

#### HappyC4mper

Joined Oct 13, 2017
71
If not, we can help you further.

I forgot to mention, that i copy and pasted this printf function into my main.c file so i was able to use printf in my other functions.
Code:
/////////////////////////PRINTF///////////////////////////////
///////////////////RETARGETTING PRINTF///////////////////////
#pragma import(__use_no_semihosting)
struct __FILE { int handle; };
FILE __stdout; // used for output – printf()
FILE __stdin; // used for input – getchar()
void _sys_exit(int x)
{ x = x; }
int fputc(int ch, FILE *f){
putLCD((uint8_t) ch);
return ch;
}
This code was given for me to use, but no explanation was given. However, I'm still not too sure how to get my ADC data to print into usart terminal (putty)

#### MrChips

Joined Oct 2, 2009
23,576
I forgot to mention, that i copy and pasted this printf function into my main.c file so i was able to use printf in my other functions.
When you say "i was able to use printf in my other functions"
where did the output appear?

Show us an example of the printf( ) statement that worked for you.

#### HappyC4mper

Joined Oct 13, 2017
71
When you say "i was able to use printf in my other functions"
where did the output appear?

Show us an example of the printf( ) statement that worked for you.
I was converting ADC to voltage
using this function
Code:
void realVoltage ()

{

cmdLCD(LCD_LINE1);
float voltage = (ADC_DATA*(3.3/4096));   //conversion adc to voltage conversion
printf( "Volts = %.3f", voltage);        //printing to lcd
That was reading to an LCD. Which has a max voltage of 3.3, using a potometer to change the voltage. So line 1 which is the code above displays my voltage. And line 2 displays a progression bar (which will be my next step in putting to terminal.)

#### MrChips

Joined Oct 2, 2009
23,576
If I understand this correctly, your working program puts out the data to a 2x16 character LCD.
Now, you want to see the hex data on a PC screen via puTTY.

You will need a USB-to-UART bridge such as a CP2102.
Then I can show you how to proceed. This is also covered in the AAC thread I posted in post #10.

#### HappyC4mper

Joined Oct 13, 2017
71
If I understand this correctly, your working program puts out the data to a 2x16 character LCD.
Now, you want to see the hex data on a PC screen via puTTY.

You will need a USB-to-UART bridge such as a CP2102.
Then I can show you how to proceed. This is also covered in the AAC thread I posted in post #10.
You are correct!
Do i need to convert my values to Hex for it to show up on puTTy? Since all I'm trying to do is to get the values and my progression bar to show up on puTTY.

#### MrChips

Joined Oct 2, 2009
23,576
You are correct!
Do i need to convert my values to Hex for it to show up on puTTy? Since all I'm trying to do is to get the values and my progression bar to show up on puTTY.
No. I was going to ask "why do you want to show the data in hex?".
You can replicate your LCD presentation or you can display the data in any format you choose, within reason.

#### HappyC4mper

Joined Oct 13, 2017
71
No. I was going to ask "why do you want to show the data in hex?".
You can replicate your LCD presentation or you can display the data in any format you choose, within reason.
No wonder, as i've seen other people using sprintf and they had to convert to hex which confused me a bit. Which is why I tried to replicate.
But yes, what I'm trying to do is replicate what I'm getting from my LCD , to show up on puTTY.
would want this-
Code:
void realVoltage ()
{
cmdLCD(LCD_LINE1);
float voltage = (ADC_DATA*(3.3/4096));   //conversion adc to voltage conversion
printf( "Volts = %.3f", voltage);        //printing to lcd
and this to show up-
Code:
void CustomChar (float q)   //analogue signal. (q is adcvalue)
{
float divider = 255;    //3.3V represented by 16 character spaces (4096/16)
int sum = q/divider;

cmdLCD(LCD_LINE2);
for(int i = 0; i< 16 ; i++)
if(i<=sum)
{
putLCD(0xFF);
}
else putLCD(' ');
}

#### MrChips

Joined Oct 2, 2009
23,576
Next question.
What advantage is it to you to be able to replicate the same information on a PC screen if your LCD is working properly?

#### HappyC4mper

Joined Oct 13, 2017
71
Next question.
What advantage is it to you to be able to replicate the same information on a PC screen if your LCD is working properly?
I need to be able to get this working so I could get my DAC to generate square/triangle and sine waves by pressing a switch. It's just a project I'm trying to do.

#### MrChips

Joined Oct 2, 2009
23,576
I need to be able to get this working so I could get my DAC to generate square/triangle and sine waves by pressing a switch. It's just a project I'm trying to do.
That doesn't answer my question.
Where is the DAC?
Where is the switch?