code to convert ADC value to hex?

Thread Starter

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];
    float Voltage_Value = readadc;
    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
30,706
If you already have printf( ) in C there is nothing else you need.
Simply use:

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

AlbertHall

Joined Jun 4, 2014
12,344
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.
 

Thread Starter

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
30,706
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.
 

Thread Starter

HappyC4mper

Joined Oct 13, 2017
71
My read_adc is already a function i made from a different headerfile, if you were wondering.
Code:
unsigned short read_adc(void)
{
    ADC1->CR2|=ADC_CR2_SWSTART;                //start ADC conversion
    while((ADC1->SR&ADC_SR_EOC)==0);    //wait for ADC conversion complete
    return ADC1->DR;                                    //return converted value
}
 

Thread Starter

HappyC4mper

Joined Oct 13, 2017
71
Read through this thread and see if you can find your answer.
If not, we can help you further.

https://forum.allaboutcircuits.com/...cleo-f401re-without-using-a-debug-kit.154483/

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
30,706
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.
 

Thread Starter

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 ()

{
    unsigned short ADC_DATA;
    ADC_DATA=read_adc();    //reads what ever value is and put into ADC DATA
  
    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
30,706
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.
 

Thread Starter

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
30,706
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.
 

Thread Starter

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 ()
{
    unsigned short ADC_DATA;
    ADC_DATA=read_adc();    //reads what ever value is and put into ADC DATA
    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
30,706
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?
 

Thread Starter

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.
 
Top