#include "SPI.h"
#include <Adafruit_GFX_AS.h> // http://rcl-radio.ru/wp-content/uploads/2020/06/Adafruit_GFX.zip
#include <Adafruit_ILI9341_STM.h>
#include <STM32ADC.h>
#include <HardwareTimer.h>
STM32ADC myADC(ADC1);
uint8 pin = PA1;
volatile static bool dma1_ch1_Active;
#define maxSamples 640
uint16_t buffer[maxSamples];
#define TFT_CS PB1
#define TFT_DC PB10
#define TFT_RST PB11
Adafruit_ILI9341_STM tft = Adafruit_ILI9341_STM(TFT_CS, TFT_DC, TFT_RST); // Mosi - PA7, SCK - PA5
byte data[640],data_old[640];
int i,x,y,i2;
long h0,h1;
long times;
void setup() {
Serial.begin(115200);
pinMode(pin, INPUT_ANALOG);
///// SETUP ALL ////////////////////////////////////////
tft.begin();tft.setRotation(1);tft.fillScreen(ILI9341_BLACK);
Timer3.pause();
Timer3.setPrescaleFactor(1);
Timer3.setOverflow(4);
Timer3.setMasterModeTrGo(TIMER_CR2_MMS_UPDATE);
Timer3.refresh(); // обнулить таймер
Timer3.resume(); // запускаем таймер
myADC.setSampleRate(ADC_SMPR_1_5);
myADC.setPins(&pin, 1);
}
void loop() {
DMA();
if(dma1_ch1_Active == 0){
for(int x=0; x<maxSamples; x++){
// Serial.println(buffer[x]);
data[x]=map(buffer[x],0,4095,0,200);
}
}
////// TFT //////////////////////////////////////////
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE); tft.setTextSize(1);
tft.println("RCL-RADIO.RU");
// tft.fillRect(0,0,150,40,ILI9341_BLACK);
setka(); i=0;
while(i<319){i++;
tft.drawLine(i, 230-data_old[i],i, 230-data_old[i+1], ILI9341_BLACK);
tft.drawLine(i, 230-data[i],i, 230-data[i+1], ILI9341_RED);}i=0;
while(i<639){i++;data_old[i]=data[i];}i=0;
}
void DMA(){
dma1_ch1_Active = 1;
myADC.setDMA(buffer, maxSamples, (DMA_MINC_MODE | DMA_TRNS_CMPLT), DMA1_CH1_Event);
myADC.setTrigger(ADC_EXT_EV_TIM3_TRGO);
myADC.startConversion();
while (dma1_ch1_Active >= 1){};
dma_disable(DMA1, DMA_CH1);
}
void setka(){
for(y=40;y<240;y=y+40){
for(x=10;x<320;x=x+10){
tft.drawPixel(x, y, ILI9341_WHITE);}}
for(x=40;x<320;x=x+40){
for(y=40;y<240;y=y+10){
tft.drawPixel(x, y, ILI9341_WHITE);}}}
static void DMA1_CH1_Event() {
dma1_ch1_Active = 0;
}