Plotting data from adc in matlab

code is below. How can I plot the dene[i] in code in matlab?
#include <stm32f4xx.h>
#include <stm32f4xx_conf.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_adc.h>
#include <stm32f4xx_gpio.h>
ADC_InitTypeDef ADC_init_structure; //Structure for adc confguration
GPIO_InitTypeDef alper; //Structure for analog input pin
int i;
static char dene1[2048];
static char dene2[2048];
static char dene3[2048];
static char dene4[2048];
static char dene5[1808];
float dum1;
int ms10Ticks; /* counts 1ms timeTicks */
/*----------------------------------------------------------------------------
SysTick_Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void) {
ms10Ticks++;
ADC_SoftwareStartConv(ADC1);//ADC1->CR2 |= (1<<30);
}
/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
GPIO_StructInit(&alper);
alper.GPIO_Pin = GPIO_Pin_5;//The channel 15 is connected to PC5
alper.GPIO_Mode = GPIO_Mode_AN; //The PC5 pin is configured in analog mode
alper.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down
GPIO_Init(GPIOC,&alper);//Affecting the port with the initialization structure configuration
//GPIOC->MODER |= (3<<10); // PC5--> ADC.15 input olarak ayarla
//GPIOC->PUPDR = 0x00000000; // pull up-down yok
ADC_DeInit();
ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
ADC_init_structure.ADC_ContinuousConvMode = DISABLE; //the conversion is continuous, the input data is converted once
ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
ADC_init_structure.ADC_NbrOfConversion = 1;
ADC_init_structure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
ADC_Init(ADC1,&ADC_init_structure);//Initialize ADC with the previous configuration
//Enable ADC conversion
ADC_Cmd(ADC1,ENABLE);
//Select the channel to be read from
ADC_RegularChannelConfig(ADC1,ADC_Channel_15,1,ADC_SampleTime_144Cycles);
SystemCoreClockUpdate(); /* Get Core Clock Frequency */
if (SysTick_Config(SystemCoreClock / 10000)) { /* SysTick 10 msec interrupts */
while (1); /* Capture error */
}
while(1) { /* Loop forever */
if(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)){//if((ADC1->SR & 0x00000002))
if(i<2048)
dene1[i]=(ADC_GetConversionValue(ADC1));
else if(i>2048 && i<4096)
dene2[i-2048]=(ADC_GetConversionValue(ADC1));
else if(i>4096 && i<6144)
dene3[i-4096]=(ADC_GetConversionValue(ADC1));
else if(i>6144 && i<8192)
dene4[i-6144]=(ADC_GetConversionValue(ADC1));
else
dene5[i-8192]=(ADC_GetConversionValue(ADC1));
i++;
if(i==10000){
i=0;
}
}
}
}

Answers (1)

Pragati
Pragati on 4 Jun 2026 at 13:55
The question you’re asking—how to plot ADC data in MATLAB—is primarily about getting sampled data into MATLAB and visualizing it. In your current setup, the ADC data is being generated on an STM32 and stored in arrays (dene1, dene2, etc.), but MATLAB has no direct visibility into that data yet.
Recommended practical approach
Step 1 — Get data into MATLAB
You still need to:
  • Send dene[i] from STM32 to MATLAB (e.g., via serial/USB)
  • Store it as a MATLAB vector
Step 2 — Basic plotting
Once data is in MATLAB:
plot(data);
xlabel('Sample Index');
ylabel('ADC Code');
title('ADC Output');
Step 3 — (Optional, but powerful) Use Mixed-Signal Blockset-style analysis
  • Compare with an ideal ADC model
  • Analyze frequency content (FFT)
  • Evaluate dynamic performance metrics
https://www.mathworks.com/help/msblks/data-converters.html?s_tid=CRUX_lftnav

Categories

Asked:

on 9 Aug 2020

Answered:

on 4 Jun 2026 at 13:55

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!