Author Topic: getting 24 bits from adc and converting them into decimal ??  (Read 1501 times)

0 Members and 1 Guest are viewing this topic.

Offline basantabTopic starter

  • Contributor
  • Posts: 21
  • Country: fi
getting 24 bits from adc and converting them into decimal ??
« on: December 07, 2018, 11:51:19 am »
Hello
Can anyone check or suggest regarding conversion and saving 24 bit data from adc into stm32 eval board.

here is what i have tried.

data[24] is integer array of 24.
i read pin and write down to data array.

when i write down by reading pin(pin state i presume) then does every cell has only one zero and ones or is it so that in every cell of array i have several zeros and ones...

Basically in short can i write pin state in one digit (integer) in one cell ?.

Well idea is that i will get bit stream of 24 bits and with every clock i need to read bits (so 24 pulse of clock corresponds to 24 bit out).

and i wanted to store then as integer ones and zeros in 24 cells and convert as shown in code above

thankyou very much for reading all these..



just curious about what people think about it.

thanks
basanta
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 14172
  • Country: de
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #1 on: December 07, 2018, 12:06:49 pm »
The code looks a little odd. Normally one does not repeat the type of variable.
The obvious mistake is to have J=23 inside the loop.

The more usual way would be to directly put the bits together, when reading the data. So no extra array. So something like
 data = (data >> 1) + 2^24*GPIO inside the reading loop.

The actual conversion from binary to decimal would be with  ItoA() or similar. Chances are one would use floating points for scaling.
 
The following users thanked this post: basantab

Offline basantabTopic starter

  • Contributor
  • Posts: 21
  • Country: fi
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #2 on: December 07, 2018, 12:16:12 pm »
The code looks a little odd. Normally one does not repeat the type of variable.
The obvious mistake is to have J=23 inside the loop.

The more usual way would be to directly put the bits together, when reading the data. So no extra array. So something like
 data = (data >> 1) + 2^24*GPIO inside the reading loop.

The actual conversion from binary to decimal would be with  ItoA() or similar. Chances are one would use floating points for scaling.

thanks for reply
I need a bit more exercise in code though..
basically i am trying to read pin state in put them in cell and then later go one by one and convert them.
but any way what is that (data>>1) doing in that code ?

 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 3731
  • Country: lv
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #3 on: December 07, 2018, 12:41:52 pm »
Can anyone check or suggest regarding conversion and saving 24 bit data from adc into stm32 eval board.

here is what i have tried.

Use SPI peripheral instead - it will arrange bits into register (containing data) for you.
 
The following users thanked this post: basantab

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 14172
  • Country: de
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #4 on: December 07, 2018, 12:42:57 pm »
The   x >> N  operation in C is a bit shift of N steps to the right. So equivalent to   / 2^N.
So  data >>1 could also be written as data / 2 .
 
The following users thanked this post: basantab

Offline kulky64

  • Regular Contributor
  • *
  • Posts: 61
  • Country: sk
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #5 on: December 07, 2018, 02:12:45 pm »
You can't use 2^N expression in C language to raise 2 to the power of N. "^" operator does XOR operation in C and you don't want this.
 
The following users thanked this post: basantab

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • Country: it
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #6 on: December 07, 2018, 02:17:09 pm »
The   x >> N  operation in C is a bit shift of N steps to the right. So equivalent to   / 2^N.
So  data >>1 could also be written as data / 2 .
as long as data is of an unsigned type. The result of the operation is NOT guaranteed if the type is signed.

@basantab please never ever upload your code as an image again
if a forum has support for the code tags, such as this one, copy and paste it directly using the code tags. In other places (such as facebook support groups) which don't support visualization of code with the proper formatting, use a service such as pastebin to post your code and just post the link. no screenshots and god forbid no photos of the screen :)

example that uses the code tags:
Code: [Select]
string str = "This is just an example";

if you click on quote you should see on the editor how i did it :)
 
The following users thanked this post: basantab

Offline basantabTopic starter

  • Contributor
  • Posts: 21
  • Country: fi
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #7 on: December 07, 2018, 03:36:47 pm »
The   x >> N  operation in C is a bit shift of N steps to the right. So equivalent to   / 2^N.
So  data >>1 could also be written as data / 2 .
as long as data is of an unsigned type. The result of the operation is NOT guaranteed if the type is signed.

@basantab please never ever upload your code as an image again
if a forum has support for the code tags, such as this one, copy and paste it directly using the code tags. In other places (such as facebook support groups) which don't support visualization of code with the proper formatting, use a service such as pastebin to post your code and just post the link. no screenshots and god forbid no photos of the screen :)

example that uses the code tags:
Code: [Select]
string str = "This is just an example";

if you click on quote you should see on the editor how i did it :)


Thanks Jportici

Code: [Select]
I will never ever upload code in image again :-)
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2254
  • Country: ca
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #8 on: December 10, 2018, 04:08:49 pm »
When you create a loop, like:

for ( ; ; )
{
...
}

Then everything inside the { ... } gets executed each time the loop runs. Any local scope variables (or other objects) will be created and initialized when that line of code is executed, and will be destroyed (destructors called) when the } is encountered, because that local scope variable goes out of scope at that point. In other words, that variable is in scope only for a single iteration of the loop, not for all of them! This is extremely important. This means that:
- In the first loop, your variable j is initialized to 23 each time the loop executes. It is decremented to 22 at the bottom of the loop, then destroyed, and a new j is created and initialized to 23 for the next time.
- In the other loop, int decimal is created with local (to the loop) scope, and is not the same "decimal" that you declared outside of the loop! Furthermore, it is also a new unique 'decimal' each time through the loop.  If this line of code was not inside the brackets {...} then it would result in a compiler error: re-declaration of a variable. But since it is inside brackets, it is simply a new differently-scoped variable which is perfectly legal (but won't do what you want).

Both of the above mean that your code can't work.

Most of the time, you do not want or need to declare new variables inside of loops.
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: getting 24 bits from adc and converting them into decimal ??
« Reply #9 on: December 10, 2018, 04:34:55 pm »
I would write it like this:

Code: [Select]
// at start of your C file, to have uint32_t etc.:
#include <stdint.h>

// then in your function:
uint32_t data = 0;
for (int i = 0; i < 24; i++) {
  // clk high code
  // delay code
  // shift data bits left
  data <<= 1;
  // read next data bit, so SPI format is assumed in MSB format
  if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_10) == GPIO_PIN_SET) data |= 1;
  // clk low code
  // delay code
}

// this is for 2^24, if you have a signed number, needs to be changed
float voltage = (float) data;
voltage = voltage / 16777216.0f * 3.0f;

Note that you need to enable the GPIO clock as well and the pin directions:

https://stackoverflow.com/questions/29496072/stm32f401re-nucleo-led-and-buttons-example

And you probably need some kind of chip select for the ADC. Note that the STM32 series has a nice hardware SPI peripheral, which is faster and you can even use interrupts to do something else while the data is transferred. See for example here:

http://www.handsonembedded.com/stm32f103-spl-tutorial-5/
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf