Issues with AD Conversion
Posted: 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, 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
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