getting 24 bits from adc and converting them into decimal ?? - Page 1
Forum section: electronics / general

author: basantab
author_rank: Contributor
timestamp: December 07, 2018, 11:51:19 am
content: HelloCan anyone check or suggest regarding conversion and saving 24 bit data from adc into stm32 eval board.here is what i have tried.data 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 abovethankyou very much for reading all these..just curious about what people think about it.thanksbasanta

author: Kleinstein
author_rank: Super Contributor
timestamp: December 07, 2018, 12:06:49 pm
content: 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 likedata = (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.

author: basantab
author_rank: Contributor
timestamp: December 07, 2018, 12:16:12 pm
content: Quote from: Kleinstein on December 07, 2018, 12:06:49 pmthanks for replyI 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 ?

author: ogden
author_rank: Super Contributor
timestamp: December 07, 2018, 12:41:52 pm
content: Quote from: basantab on December 07, 2018, 11:51:19 amUse SPI peripheral instead - it will arrange bits into register (containing data) for you.

author: Kleinstein
author_rank: Super Contributor
timestamp: December 07, 2018, 12:42:57 pm
content: 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 .

author: kulky64
author_rank: Regular Contributor
timestamp: December 07, 2018, 02:12:45 pm
content: 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.

author: JPortici
author_rank: Super Contributor
timestamp: December 07, 2018, 02:17:09 pm
content: Quote from: Kleinstein on December 07, 2018, 12:42:57 pmas long as data is of anunsigned type. The result of the operation is NOT guaranteed if the type is signed.@basantab please never ever upload your code as an image againif 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 screenexample that uses the code tags:Code:string str = "This is just an example";if you click on quote you should see on the editor how i did it

author: basantab
author_rank: Contributor
timestamp: December 07, 2018, 03:36:47 pm
content: Quote from: JPortici on December 07, 2018, 02:17:09 pmThanks JporticiCode:I will never ever upload code in image again :-)

author: macboy
author_rank: Super Contributor
timestamp: December 10, 2018, 04:08:49 pm
content: 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, andwill 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 isin scopeonly 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.

author: FrankBuss
author_rank: Unknown
timestamp: December 10, 2018, 04:34:55 pm
content: I would write it like this:Code:// 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 leftdata <<= 1;// read next data bit, so SPI format is assumed in MSB formatif (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 changedfloat 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-exampleAnd 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/