AnsweredAssumed Answered

EOC interrupt for ADC

Question asked by capasso.ornella on Oct 22, 2012
Latest reply on Oct 24, 2012 by capasso.ornella
I want to use the interrupt to build me a vector containing eg 100 samples converted, I wrote the code but the processor stops ...
the code is as follows:
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"
#include <stdio.h>
#include <boh.h
#include <main.h>
#include <misc.h>
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC3_DR_ADDRESS     ((uint32_t)0x4001224C)
 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/******VARIABILI UTILIZZATE PER L'ACQUISIZIONE*********/
__IO uint16_t ADC3ConvertedValue[3] = {0,0,0};  //Vettore che contiene i valori convertiti dai tre canali
__IO uint32_t ADC3ConvertedVoltage[3];                  //Vettore sul quale effettuare le operazioni ??? DOVREBBE FUNZIONARE ANCHE CON uint16_t verificare!!!!!
__IO uint32_t Vin;                                                          //Variabile Vin
__IO uint32_t Vout;                                                         //Variabile Vout
__IO uint32_t Iin;                                                          //Variabile Iin
__IO uint32_t K_adc=3000;                                               //Costante di conversione dell'ADC
__IO uint32_t Input[100];
 
__IO uint32_t costante=1000;
 
 
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void ADC3_CH_0_1_2_DMA_Config(void);    //Funzione di configurazione ADC e DMA
//void Delay(__IO uint32_t nTime);          ////Funzione di configurazione Delay
void GPIO_Usart(void);
void RCC_Usart_Configuration(void);
void USART3_Configuration(void);
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
 int i=0;
     
    ADC3_CH_0_1_2_DMA_Config(); // ADC3 Configuration
  
  /* Start ADC3 Software Conversion */
  ADC_SoftwareStartConv(ADC3);
    /***********START USART************/
 RCC_Usart_Configuration();
  GPIO_Usart();
  USART3_Configuration();
 
     
     
 
  while (1)
  {
         
        Vin=ADC3ConvertedValue[0]*K_adc/0xFFF;      //recupero i valori dei segnali acquisiti per poi elaborarli
        Iin=ADC3ConvertedValue[1]*K_adc/0xFFF;      //K_adc è la costante di conversione che porta i valori dell'adc
        Vout=ADC3ConvertedValue[2]*K_adc/0xFFF;
         
 
         
        if(ADC_GetITStatus  ( ADC3,  ADC_IT_EOC )==SET ){
             
        Input[i]=Vin;
            printf("EOC"); 
            i=i+1;
}
            else{
                printf("NO EOC");  
            }   
  }
}
 
 
/*
  * @brief  ADC3 channel 0,1, 2 with DMA configuration
  * @param  None
  * @retval None
  */
/******************ADC and DMA***********************/
 
void ADC3_CH_0_1_2_DMA_Config(void)
{
  ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;
  GPIO_InitTypeDef      GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
 
   
  /* Enable ADC3, DMA2 and GPIO clocks ****************************************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE); //Abilito l' AHB1 peripheral clock per DMA2 e GPIOA.
 
 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); //Abilito l' APB2 peripheral clock per l'ADC3.
   
  /* DMA2 Stream0 channel2 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_2;  //Specifico il canale utilizzato per il flusso del (DMA canale 2)
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;  //Specifico l'indirizzo della periferica al quale associo il canale DMA
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue[0]; //Indirizzo di memoria nel quale il DMA porta i valori dei campioni acquisiti, è un vettore perchè sto acquisendo su tre canali
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //Specifico che il dato va da periferica a memoria
  DMA_InitStructure.DMA_BufferSize = 3; //dimensioni del buffer, 3 perchè ho tre canali
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //???????Specifica se il registro di indirizzo di periferica deve essere incrementato o meno. VERIFICARE SE UTILIZZANDO DUE PERIFERICHE NON BISOGNA CAMBIARE IN ENABLE
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;////Specifica se il registro di indirizzo di memoria deve essere incrementato o meno.
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //Specifico le dimensioni del dato proveniente dalla periferica
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;                   //Specifico le dimensioni del dato in memoria
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//???Specifica la modalità di funzionamento del flusso del DMAy
  DMA_InitStructure.DMA_Priority = DMA_Priority_High; //???Specifica la priorità
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;    //Abilità la modalità fifo  
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; //Specifica la soglia della memoria
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;  //Specifica la configurazione di trasferimento dati per il trasferimento di memoria. Si specifica la quantità di dati da trasferire in una singola operazione non interrompibile
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;  //Specifica la quantità di dati da trasferire in una singola transazione (da periferica)non interrompibile.
  
 DMA_Init(DMA2_Stream0, &DMA_InitStructure); //Inizializza
 DMA_Cmd(DMA2_Stream0, ENABLE);                         //Abilita
       
     
  /* Configure ADC3 Channel 7 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;              
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
  /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);
   
  /* ADC3 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 3; //Perchè effettuo tre e tre le conversioni dall'ADC3
  ADC_Init(ADC3, &ADC_InitStructure);
   
     
    /****************NVIC*************/
    ADC_ITConfig  ( ADC3,ADC_IT_EOC,ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;       // we want to configure the ADC interrupts
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the ADC interrupts
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;       // this sets the subpriority inside the group
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;          // the USART1 interrupts are globally enabled
    NVIC_Init(&NVIC_InitStructure);
     
    /*****************NVIC END****************/
     
  /* ADC3 regular channel configuration *************************************/
  
  ADC_RegularChannelConfig(ADC3, ADC_Channel_0, 1, ADC_SampleTime_3Cycles);
  ADC_RegularChannelConfig(ADC3, ADC_Channel_1, 2, ADC_SampleTime_3Cycles); 
  ADC_RegularChannelConfig(ADC3, ADC_Channel_2, 3, ADC_SampleTime_3Cycles); 
    
 /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);
   
  /* Enable ADC3 DMA */
  ADC_DMACmd(ADC3, ENABLE);
   
  /* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);
     
     
     
}
 
 
/*****************************************/
void RCC_Usart_Configuration(void)
{
  /* --------------------------- System Clocks Configuration -----------------*/
  /* USART3 clock enable */
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
   
  /* GPIOB clock enable */
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
   
   
void GPIO_Usart(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
   
  /*-------------------------- GPIO Configuration ----------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
   
  /* Connect USART pins to AF */
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3);   // USART3_TX
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);  // USART3_RX
}
   
 
   
void USART3_Configuration(void)
{
    USART_InitTypeDef USART_InitStructure;
   
  /* USARTx configuration ------------------------------------------------------*/
  /* USARTx configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 9600;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
   
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
   
  USART_Init(USART3, &USART_InitStructure);
   
  USART_Cmd(USART3, ENABLE);
}
/*****************************************/
 
 
 
 
 
#ifdef  USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

Outcomes