Issues with AD Conversion

What are you developing?
henrosan
Posts: 6
Joined: Fri Jun 25, 2021 6:24 pm

Issues with AD Conversion

Post by henrosan »

Hello guys, I'm newbie with arduino IDE and stm32f103c8, and I'm having this issue with ad conversion.
Well, I'm using Roger's STM32duino (https://github.com/rogerclarkmelbourne/Arduino_STM32), and this ILI9341 library (https://github.com/iwalpola/Adafruit_ILI9341_8bit_STM), Ok.

But at certain point in my code, I'm trying to do the conversion and put the reading on the display, I've changed some pins, but when I try to use PB0 or PB1 as AD Input pins, it doesn't work, the AD always reads other pin (that obviously I don't know what pin is), and doesn't read neighter PB0 nor PB1.
that's my code, as you can see, I'm using some roger's lib function. I need some help.

--------------------------------------------------------------------------------------------------------------------------------
#include <Arduino.h>
#include <Adafruit_GFX.h>
#include <gfxfont.h> // Need this library because the fonts are inside it
#include <ili9341.h> // Display Library
#include <Fonts/FreeMonoBoldOblique12pt7b.h>
#include <Fonts/FreeSansBold18pt7b.h>
#include <Fonts/Org_01.h>

Adafruit_TFTLCD_8bit_STM32 tft; // tft is an alias to use the functions on TFTLCD library
STM32ADC myADC(ADC2); // An alias for STM32ADC library // ADC

#define rotation 3 //

int sensor = 0; // SENSOR READ VARIABLE

// 4049 = 2^12 , THEN THIS CONVERSION HAS 12 BITS
// MAYBE THIS PART OF THE CODE CAN BE CHANGED, BECAUSE THE CONVERSION CAN BE LESS PRECISE
float conversion = (float(sensor) / 4096) * 3.3; // TURNS ANALOG READ INTO VOLTS

//Channels to be acquired.
// I FOUND OUT THAT PINS FOR ADC NEED TO HAVE A DIFFERENTE DECLARATION
// PB0 = 28
// PB1 = 29
uint8_t analogPin[] = {28}; // Declara PB0 para a leitura analógica
const int maxSamples = 1; // Number of channels
uint16_t dataPoints[maxSamples]; // Array for the ADC data

int i = 0; // For the delay



void setup(void) {

tft.begin(0x9341); // Initialize the Display
tft.setRotation(rotation);
// ADC setup start
//calibrate ADC
myADC.calibrate(); // Calibrate the ADC
pinMode(analogPin[1], INPUT_ANALOG); // PUT THE ANALOG PIN AS ANALOG INPUT

myADC.setSampleRate(ADC_SMPR_7_5);//set the Sample Rate
myADC.setScanMode(); //set the ADC in Scan mode.
myADC.setPins(analogPin, 1); //set how many and which pins to convert.

myADC.setContinuous(); //set the ADC in continuous mode.

//set the DMA transfer for the ADC.
//in this case we want to increment the memory side and run it in circular mode
//By doing this, we can read the last value sampled from the channels by reading the dataPoints array
myADC.setDMA(dataPoints, 1, (DMA_MINC_MODE | DMA_CIRC_MODE), NULL);

//start the conversion.
//because the ADC is set as continuous mode and in circular fashion, this can be done
//on setup().
myADC.startConversion();

// THIS LOOP WILL GIVE 5 SECONDS OF NON OPERATION IN THIS CODE
for (int d = 0; d < 6; d++)
{
delay(1000); // Give some delay 1 second
}

}

void loop(void) { // START MAIN FUNCTION

// IN THIS PART OF THE CODE START THE ADC CONVERT
sensor = myADC.getData(); // READ THE PIN AND LOAD THE SENSOR VARIABLE
float adc_read = conversion; // CONVERT THE ANALOG READ INTO VOLTS
float pressure = (adc_read * 20) / 3.3; // CONVERT VOLTS IN PRESSURE

// HERE STARTS THE DISPLAY PART
tft.setCursor(90, 140); // THE POSITION OF NUMBERS ON THE SCREEN
tft.fillScreen(BLACK); // BACKGROUND COLOR
tft.setFont(&Org_01);
tft.setTextSize(8);
tft.setTextColor(WHITE);
tft.println(sensor);

// GIVE SOME DELAY (5 SECONDS)
for (i; i < 6; i++)
{ // INTT OF DELAY
delay(1000);

} // END OF DELAY

i = 0; // Turns i = 0 after de delay
} // END MAIN FUNCTION
stevestrong
Posts: 502
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 8
Location: Munich, Germany
Contact:

Re: Issues with AD Conversion

Post by stevestrong »

Why don't you use ADC1?
For pins please use PAx, PBy format.
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: Issues with AD Conversion

Post by mrburnette »

henrosan wrote: Fri Jun 25, 2021 6:45 pm Hello guys, I'm newbie with arduino IDE and stm32f103c8, and I'm having this issue with ad conversion.
Well, I'm using Roger's STM32duino (https://github.com/rogerclarkmelbourne/Arduino_STM32), and this ILI9341 library (https://github.com/iwalpola/Adafruit_ILI9341_8bit_STM), Ok.

But at certain point in my code, I'm trying to do the conversion and put the reading on the display, I've changed some pins,
...
Try the (simple) AD example for Roger's core:
https://github.com/rogerclarkmelbourne/ ... Serial.ino

It is very important to select the correct board type/options.
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Mini.html
henrosan
Posts: 6
Joined: Fri Jun 25, 2021 6:24 pm

Re: Issues with AD Conversion

Post by henrosan »

stevestrong wrote: Sat Jun 26, 2021 9:01 am Why don't you use ADC1?
For pins please use PAx, PBy format.
I've tried, but the result is the same, in this example I've forgot to put some libraries, but I'm trying to get the real reading, but it always get different values from 0 to 4096, even if I put the potenciometer in the max resistence, that's my trouble.
I've used the ADC example as support.

And I've other questions, because I don't know if the ADC control register is using Right or Left allignment and the internal reference voltage. I have some acknowledgment about this things, but I don't want change this thins in the main code, I'm afraid to get a worst things.
henrosan
Posts: 6
Joined: Fri Jun 25, 2021 6:24 pm

Re: Issues with AD Conversion

Post by henrosan »

mrburnette wrote: Sat Jun 26, 2021 12:31 pm
henrosan wrote: Fri Jun 25, 2021 6:45 pm Hello guys, I'm newbie with arduino IDE and stm32f103c8, and I'm having this issue with ad conversion.
Well, I'm using Roger's STM32duino (https://github.com/rogerclarkmelbourne/Arduino_STM32), and this ILI9341 library (https://github.com/iwalpola/Adafruit_ILI9341_8bit_STM), Ok.

But at certain point in my code, I'm trying to do the conversion and put the reading on the display, I've changed some pins,
...
Try the (simple) AD example for Roger's core:
https://github.com/rogerclarkmelbourne/ ... Serial.ino

It is very important to select the correct board type/options.
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Pill.html
https://stm32duinoforum.com/forum/wiki_ ... _Mini.html

I've tried, my code is based on the simple example, but I can't get the things right Hahaha. It's kind of scary and unconfortable
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Issues with AD Conversion

Post by ag123 »

use adc1. adc2 has less features and kind of use adc1 as 'master', i.e. for some things like dual interleaved samples, you need to start with adc1.
with adc1 use you have PA0 adc0, PA1 adc1, PA2 adc2, PA3, adc3, using more than that overlaps spi1 (i think there is alternate function gpios, but i'm not sure where they are).

notice there are 2 sets of things here, the gpio pin and the adc channels e.g. gpio PA0 , channel adc0, the channels are different from gpio pins and need to be setup and linked to gpio pins separately (the mapping is fixed in hardware, but nevertheless the pin config in software needs to be done).

oh and if you program stm32f103xx, you need to get familiar with the 'bible' RM0008.
u'd at least need to know how the hardware works, especially if you are going beyond simply analogRead(pin);

i've done something with adc 'long' back, forgotten most of it, but you can take a look at some of the codes
https://github.com/ag88/GirinoSTM32F103duino
the adc codes are mostly in CAdcMgr.cpp
https://github.com/ag88/GirinoSTM32F103 ... AdcMgr.cpp
henrosan
Posts: 6
Joined: Fri Jun 25, 2021 6:24 pm

Re: Issues with AD Conversion

Post by henrosan »

ag123 wrote: Sat Jun 26, 2021 2:50 pm use adc1. adc2 has less features and kind of use adc1 as 'master', i.e. for some things like dual interleaved samples, you need to start with adc1.
with adc1 use you have PA0 adc0, PA1 adc1, PA2 adc2, PA3, adc3, using more than that overlaps spi1 (i think there is alternate function gpios, but i'm not sure where they are).

notice there are 2 sets of things here, the gpio pin and the adc channels e.g. gpio PA0 , channel adc0, the channels are different from gpio pins and need to be setup and linked to gpio pins separately (the mapping is fixed in hardware, but nevertheless the pin config in software needs to be done).

oh and if you program stm32f103xx, you need to get familiar with the 'bible' RM0008.
u'd at least need to know how the hardware works, especially if you are going beyond simply analogRead(pin);

i've done something with adc 'long' back, forgotten most of it, but you can take a look at some of the codes
https://github.com/ag88/GirinoSTM32F103duino
the adc codes are mostly in CAdcMgr.cpp
https://github.com/ag88/GirinoSTM32F103 ... AdcMgr.cpp

I understand, what makes problem for me is I'm using also a TFT LCD ILI9341 and with this LCD i'm using 12 pins beyond the AD convert pin, furthermore those pins I'm already using is A0~A7 for DATA and B5~B9 for Control, and I didn't know that there are some dedicated pins on BluePill for Jtag and other things, But you gave me some ideas know, and put some light on my path, Thank you so much.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Issues with AD Conversion

Post by ag123 »

well, for a blue pill you would be squeezed for pins to run parallel interface. so one way is to get a spi based lcd, there is a spi one for ili9341.
if your lcd has spi inputs you would be in luck. the alternatives are shift registers like 74hc595 (quite cheap and abundant on aliexpress and the likes), there would be 2 problem with this approach.
one is speeds there would be some 'max' speeds, and this is 'custom', i.e. u'd need to invent a scheme to 'shift' all that bits. spi etc can be used as part of it.
of course another way is to get a bigger board, those that ends with RE, VE, ZE etc has lots of pins, of course prices are significantly higher for those boards. one of the 'fav' board used to be 'stm32f407ve black' type boards.
https://stm32-base.org/boards/STM32F407 ... -F4VE-V2.0
but for STM32F407VE it is 'bleeding edge' in libmaple, i.e. you are a core developer once you step into it, don't expect everything to be 'there'. many 'features' may not be 'there' yet, but it works mostly. of course for stmf4xx series there is the official core which runs ok on it.
oh and a lot of nucleo boards has 64 pins, that normally is a bit more than the blue pills. try to get a stm32f4xx one if you are going the nucleo route
and normally for nucleo boards, run the 'official' core on it, there is a 'compatible' list on the core repository.
https://github.com/stm32duino/Arduino_Core_STM32
oh and before you jump into those, check the pin counts and pinouts make sure you have enough for your needs e.g.
https://www.st.com/en/evaluation-tools/ ... 411re.html
https://os.mbed.com/platforms/ST-Nucleo-F446RE/
if you are near Bulgaria, you may like to check out olimex, they have quite a few stm32 boards, a nice thing about them is they do share
documentation about their boards and even provide things like user manual etc
(but note that they may be 'outside' the standard variants, the main thing is you may need to copy a variant and set the pll multipliers for the on board crystal frequency, normally that'd get started with a new board, for more details read the official core wiki on making a new variant)
https://www.olimex.com/Products/ARM/ST/
olimex also have a forum and maybe you can even post there if help is needed.
henrosan
Posts: 6
Joined: Fri Jun 25, 2021 6:24 pm

Re: Issues with AD Conversion

Post by henrosan »

Thank for your advertising, I'm a inexperient developer, but it will be good work with people like you all, you gave me some nice tips and advertising.
And I'm Brazilian, there aren't this kind of enterprises here, lot of taxes, etc., that turns things hard and mostly we need to import this kind of product and wait long time to get them.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Issues with AD Conversion

Post by ag123 »

well, some alternatives are like search aliexpress etc to see if you could find boards
i'm not sure what you would see from your end, but this is a query i made on aliexpress
https://www.aliexpress.com/wholesale?ca ... ment+board

then like above the original Nucleos from ST are worth considering as boards if you can find a local or nearby or lower cost source.
the thing is review pinouts and specs e.g. adc1 channels are normally on pa0 - pa7 (but check the specs), then spi1 uses pa3-pa7.
and for other pins you would need to check board pinouts (especially on the headers) and the chip specsheets so that you can use those pins for your lcd.
stm32f4xx is 'better' for 2 reason there is normally more sram (than stm32f103c8 blue pill 20k sram) they are typically faster than stm32f1xx as they tend to have the 'art' accelerator (on chip cache) and fpu. for starters the sram would help just in case you hit up the ceiling of sram usage.
it is easy to exhaust 20k sram on stm32f103c8, the speed is a plus some f4 run at 168 mhz and combined with the on-chip cache etc makes for a much faster firmware compared to stm32f103. note though that not all stm32f4 give you more hz. e.g. stm32f401 is rated for 84mhz. but the onchip cache and fpu still helps
a thing is all this 'extra' performance and sram and extra pins comes at extra cost, it can be a pretty steep premium.
and the recent chip shortage doesn't help, it just makes things more expensive.
Post Reply

Return to “Projects”