4 Timers ADC with filter and SSD1306Oled Support for beginners

What are you developing?
Post Reply
ats3788
Posts: 4
Joined: Fri Jun 05, 2020 10:14 am

4 Timers ADC with filter and SSD1306Oled Support for beginners

Post by ats3788 »

I hope it's useful

Code: Select all

/*
STM Sample - SSD1306 and filtering ADC Samples 
https://github.com/jeroendoggen/Arduino-signal-filtering-library
https://github.com/rogerclarkmelbourne/Arduino_STM32/blob/master/STM32F1/libraries/Adafruit_SSD1306/Adafruit_SSD1306_STM32.h
Chance the Start Logo on the SSD1306
https://www.instructables.com/id/Change-Adafruit-Library-OLED-Logo/
*/

#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306_STM32.h>
#include <medianFilter.h>




#define OLED_RESET -1
Adafruit_SSD1306 display(OLED_RESET);


#define TOGGLE(x) digitalWrite(x, digitalRead(x) ? LOW : HIGH)

#define Tim1Delay  1000000
#define Tim2Delay  500000

#define YStart 30


#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
  B00000001, B11000000,
  B00000001, B11000000,
  B00000011, B11100000,
  B11110011, B11100000,
  B11111110, B11111000,
  B01111110, B11111111,
  B00110011, B10011111,
  B00011111, B11111100,
  B00001101, B01110000,
  B00011011, B10100000,
  B00111111, B11100000,
  B00111111, B11110000,
  B01111100, B11110000,
  B01110000, B01110000,
  B00000000, B00110000 };

#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif


#define TOGGLE(x) digitalWrite(x, digitalRead(x) ? LOW : HIGH)

bool     bTriggerGetADC = false;
uint32_t Counter1 = 0;
uint32_t Counter2 = 0;
uint32_t Counter3 = 0;
uint32_t Counter4 = 0;

uint16_t AnalogVal1 = 0;
uint16_t AnalogVal2 = 0;

char BufferOld1[20] = {"0.00"};
char BufferOld2[20] = {"0.00"}; 


// We'll use timers 3 and 4
USBSerial usb;
medianFilter Filter;

void PrintText (const char Text[], uint16_t x, uint16_t y, uint16_t Color );
void PrintValue(char TextNew[], char TextOld[], uint16_t x, uint16_t y, uint16_t Color);
void SetupSSD1306();
void SetupPorts();
void  __Timer1();
void SetupTimer();
void ADCProcess();



void setup()   {                
  usb.begin(115200);
  
  for (int i = 0; i < 100; i++)
  {

delay(100);
usb.print('*');
usb.print(i);

  }
usb.println("");
SetupSSD1306();
SetupPorts();
SetupTimer();


}



void loop() {

if (bTriggerGetADC)
{
bTriggerGetADC = false; 
ADCProcess();


}
delay(250);
}


void SetupSSD1306()
{

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  // init done
  // Show image buffer on the display hardware.
  // Since the buffer is intialized with an Adafruit splashscreen
  // internally, this will display the splashscreen.
  delay(1000);

  // Clear the buffer.
display.display();
delay(2000);
display.clearDisplay();
display.display();
display.setTextSize(1);

PrintText("Spannung 1: ",2, YStart, WHITE);
PrintText("Spannung 2: ",2, YStart + 20, WHITE);
PrintText("V ", 110, YStart , WHITE);
PrintText("V ", 110, YStart + 20, WHITE);

display.display();

}
void SetupPorts()
{
pinMode(PC14,OUTPUT);
pinMode(PC15,OUTPUT);
pinMode(LED_BUILTIN,OUTPUT);
pinMode(PA0, INPUT);
pinMode(PA1, INPUT);

digitalWrite(LED_BUILTIN,LOW);
digitalWrite(PC14,LOW);
digitalWrite(PC15,LOW);
}

void __Timer1(void) 
{
    TOGGLE(LED_BUILTIN);
   Counter1++;
   bTriggerGetADC = true;
}

void __Timer2(void) 
{
    TOGGLE(LED_BUILTIN);
   Counter2++;
}

void __Timer3(void) 
{
   TOGGLE(PC14);
   Counter3++;
}

void __Timer4(void) 
{
   TOGGLE(PC15);
   Counter4++;
}



void SetupTimer()
{
//Timer 1
    Timer1.pause();
    Timer1.setPeriod(500000); // in microseconds
    Timer1.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    Timer1.setCompare(TIMER_CH1, 1);  // Interrupt 1 count after each update
    Timer1.attachCompare1Interrupt(__Timer1);
    Timer1.refresh();
    Timer1.resume();

//Timer 2
    Timer2.pause();
    Timer2.setPeriod(1000000); // in microseconds
    Timer2.setChannel1Mode(TIMER_OUTPUT_COMPARE);
    Timer2.setCompare(TIMER_CH2, 2);  // Interrupt 1 count after each update
    Timer2.attachCompare1Interrupt(__Timer2);
    Timer2.refresh();
    Timer2.resume();

 //Timer 3
    Timer3.pause();
    Timer3.setPeriod(1500000); // in microseconds
    Timer3.setChannel3Mode(TIMER_OUTPUT_COMPARE);
    Timer3.setCompare(TIMER_CH3, 3);  // Interrupt 1 count after each update
    Timer3.attachCompare3Interrupt(__Timer3);
    Timer3.refresh();
    Timer3.resume();

//Timer 4
    Timer4.pause();
    Timer4.setPeriod(2000000); // in microseconds
    Timer4.setChannel4Mode(TIMER_OUTPUT_COMPARE);
    Timer4.setCompare(TIMER_CH4, 4);  // Interrupt 1 count after each update
    Timer4.attachCompare4Interrupt(__Timer4);
    Timer4.refresh();
    Timer4.resume();  
}


void PrintText (const char Text[], uint16_t x, uint16_t y, uint16_t Color )
{

display.setCursor(x ,y );
display.setTextColor(Color);
display.print(Text);
display.display();

}

void PrintValue(char TextNew[], char TextOld[], uint16_t x, uint16_t y, uint16_t Color )
{

display.setCursor(x ,y );
display.setTextColor(BLACK);
display.print(TextOld);
display.setCursor(x ,y );
display.setTextColor(Color);
display.print(TextNew);

display.display();

}



double GetVoltageinV(uint16_t Value)
{
double ReferenceV = 3.3;
uint16_t ADCDigits = 4096 -1;
double Result = Value  * ReferenceV / ADCDigits;
return (Result);

}


void ADCProcess()
{
char Puffer[8];

double Voltage1;
double Voltage2;

AnalogVal1 = analogRead(PA0);
AnalogVal1 = Filter.run(AnalogVal1);
delay(100);
AnalogVal2 = analogRead(PA1);
AnalogVal2 = Filter.run(AnalogVal2);

usb.print(AnalogVal1);
usb.print("\t\t\t");
usb.print(AnalogVal2);

Voltage1 = GetVoltageinV( AnalogVal1 );
dtostrf(Voltage1 , 3, 2, Puffer);
PrintValue(Puffer, BufferOld1, 75, YStart, WHITE );
strcpy(BufferOld1, Puffer);

Voltage2 = GetVoltageinV( AnalogVal2 );
dtostrf(Voltage2 , 3, 2, Puffer);
PrintValue(Puffer, BufferOld2, 75, YStart + 20, WHITE );
strcpy(BufferOld2, Puffer);

usb.print("\t\t\t");
usb.print(Voltage1);
usb.print("\t\t\t");
usb.print(Voltage2);

usb.print("\t\t\t");
usb.print(BufferOld1);
usb.print("\t\t\t");
usb.println(BufferOld2);
}

User avatar
fpiSTM
Posts: 664
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 25
Location: Le Mans
Contact:

Re: 4 Timers ADC with filter and SSD1306Oled Support for beginners

Post by fpiSTM »

Thanks for sharing.

Post Reply

Return to “Projects”