/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */  
	SystemInit();
       
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();
	
	/* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 2;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
	
	/* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
	
	/* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 2;
  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channel0 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_55Cycles5);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_55Cycles5);
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
     
  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  /* To achieve GPIO toggling maximum frequency, the following  sequence is mandatory. 
     You can monitor PD0 or PD2 on the scope to measure the output signal. 
     If you need to fine tune this frequency, you can add more GPIO set/reset 
     cycles to minimize more the infinite loop timing.
     This code needs to be compiled with high speed optimization option.  */
	/*Init LCD*/
	LCD_Init();
  while (1)
  {
		/* Start ADC1 Software Conversion */ 
		ADC_SoftwareStartConvCmd(ADC1, ENABLE);
		sprintf(LCD_Buffer,"ADC1=%d ",ADCConvertedValue[0]);
		LCD_GotoXY(0,0);
		LCD_PutStr(LCD_Buffer);
		sprintf(LCD_Buffer,"ADC2=%d ",ADCConvertedValue[1]);
		LCD_GotoXY(0,1);
		LCD_PutStr(LCD_Buffer);
		delay_ms(1000);
  }
}
Beispiel #2
0
void ADC_config(void) {
	DMA_InitTypeDef DMA_InitStructure_ADC;
	NVIC_InitTypeDef NVIC_InitStructure;
	GPIO_InitTypeDef GPIO_InitSt_C, GPIO_InitSt_D;
	ADC_CommonInitTypeDef  ADC_struct;
	ADC_InitTypeDef ADC_InitStructure;	
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	//CONFIGURE DMA FOR ADC
	DMA_InitStructure_ADC.DMA_Channel = DMA_Channel_0;
	DMA_InitStructure_ADC.DMA_PeripheralBaseAddr = ADC_DR_ADDRESS;
	DMA_InitStructure_ADC.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure_ADC.DMA_BufferSize = NUMBER_SAMPL_ADC;
	DMA_InitStructure_ADC.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure_ADC.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure_ADC.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure_ADC.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure_ADC.DMA_Mode = DMA_Mode_Normal;
	DMA_InitStructure_ADC.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure_ADC.DMA_FIFOMode = DMA_FIFOMode_Enable;
	DMA_InitStructure_ADC.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
	DMA_InitStructure_ADC.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream0, &DMA_InitStructure_ADC);
	//enable DMA interrupt
	DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
	NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);
	//CONFIGURE GPIO FOR ADC; 
	/*ADC Channel 10 -> PC0*/
  GPIO_InitSt_C.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitSt_C.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitSt_C.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOC, &GPIO_InitSt_C);
	//ADC COMMON INIT
	ADC_struct.ADC_Mode = ADC_Mode_Independent ;
	ADC_struct.ADC_Prescaler = ADC_Prescaler_Div8; //700kHz
	ADC_struct.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
	ADC_struct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
	ADC_CommonInit(&ADC_struct);
  //ADC CHANNEL INIT
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_3Cycles);
	ADC_DMARequestAfterLastTransferCmd(ADC1, DISABLE);
	// ENABLE ADC1 DMA
  ADC_DMACmd(ADC1, ENABLE);
	// ENABLE ADC1
  ADC_Cmd(ADC1, ENABLE);
	/*configure TIM3*/
	TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
  TIM_TimeBaseStructure.TIM_Period = 0;        
  TIM_TimeBaseStructure.TIM_Prescaler = 0;       
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;    
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);
	/* Input Trigger selection */
	TIM_ETRConfig(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
  TIM_SelectInputTrigger(TIM3, TIM_TS_ETRF);
  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);
	/*Trigger ADC -> PD2*/
	GPIO_InitSt_D.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitSt_D.GPIO_PuPd = GPIO_PuPd_DOWN;
  GPIO_InitSt_D.GPIO_Pin = GPIO_Pin_2;
  GPIO_Init(GPIOD, &GPIO_InitSt_D);
	GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_TIM3);
}
/*=====================================================================================================*/
void ADC_Config( void )
{
  DMA_InitTypeDef DMA_InitStruct;
  ADC_InitTypeDef ADC_InitStruct;
  ADC_CommonInitTypeDef ADC_CommonInitStruct;
  GPIO_InitTypeDef GPIO_InitStruct;

  /* ADC Clk Init *************************************************************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

  /* ADC_I PA4 */  /* ADC_V PA5 */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* ADC DMA Init *************************************************************/
  DMA_InitStruct.DMA_Channel = DMA_Channel_0;
  DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)ADC1_DR_ADDRESS;               // Peripheral address
  DMA_InitStruct.DMA_Memory0BaseAddr = (u32)&ADC_DMA_Buf;                     // Memory address
  DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory;                        // Peripheral to Memory
  DMA_InitStruct.DMA_BufferSize = ADC_Sample*ADC_Channel;                     // Memory Buffer Size
  DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;               // Peripheral address 遞增 Disable
  DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;                        // Memory address 遞增 Enable
  DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;    // Peripheral Data Size 16bit
  DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;            // Memory Data Size 16bit
  DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;                                // 循環模式 Enable
  DMA_InitStruct.DMA_Priority = DMA_Priority_High;                            // ADC DMA通道 高優先級
  DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Disable;                         // DMA FIFO Disable
  DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStruct);
  DMA_Cmd(DMA2_Stream0, ENABLE);

  /* ADC Common Init **********************************************************/
  ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;                       // 獨立模式
  ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div2;                    // 預分頻2
  ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;        // ADC DMA Mode Disable
  ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;   // 轉換延遲時間
  ADC_CommonInit(&ADC_CommonInitStruct);

  /* ADC Init *****************************************************************/
  ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;                         // 解析度 12bit
  ADC_InitStruct.ADC_ScanConvMode = ENABLE;                                   // 掃描模式
  ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;                             // 連續轉換模式
  ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;    // 外部觸發 Disable
  ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;                         // ADC數據右對齊
  ADC_InitStruct.ADC_NbrOfConversion = ADC_Channel;                           // 轉換ADC通道數目
  ADC_Init(ADC1, &ADC_InitStruct);

  /* ADC Regular Config *******************************************************/
  ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_3Cycles);  	// ADC_I
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_3Cycles);  	// ADC_V

  ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
  ADC_DMACmd(ADC1, ENABLE);
  ADC_ContinuousModeCmd(ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
  ADC_SoftwareStartConv(ADC1);
}
Beispiel #4
0
/**
  * @brief  ADC1 Channel Vbat configuration
  * @note   This function Configure the ADC peripheral
            1) Enable peripheral clocks
            2) DMA2_Stream0 channel 0 configuration
            3) Configure ADC1 Channel18 (VBAT)
  * @param  None
  * @retval None
  */
static void ADC_Config(void)
{
  ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  DMA_InitTypeDef       DMA_InitStructure;

  /* Enable peripheral clocks *************************************************/
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
  RCC_APB2PeriphClockCmd(ADCx_CLK, ENABLE);

  /* DMA2_Stream0 channel0 configuration **************************************/
  DMA_DeInit(DMA2_Stream0);
  DMA_InitStructure.DMA_Channel = DMA_CHANNELx;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA_STREAMx, &DMA_InitStructure);
  /* DMA2_Stream0 enable */
  DMA_Cmd(DMA_STREAMx, ENABLE);

  /* 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);

  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADCx, &ADC_InitStructure);

  /* Enable ADC1 DMA */
  ADC_DMACmd(ADCx, ENABLE);

  /* ADC1 regular channel18 (VBAT) configuration ******************************/
  ADC_RegularChannelConfig(ADCx, ADC_Channel_Vbat, 1, ADC_SampleTime_15Cycles);

  /* Enable VBAT channel */
  ADC_VBATCmd(ENABLE);

  /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE);

  /* Enable ADC1 **************************************************************/
  ADC_Cmd(ADCx, ENABLE);
}
Beispiel #5
0
void adcInit(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    ADC_InitTypeDef ADC_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    AQ_NOTICE("ADC init\n");

    memset((void *)&adcData, 0, sizeof(adcData));

    // energize mag's set/reset circuit
    adcData.magSetReset = digitalInit(GPIOE, GPIO_Pin_10, 1);

    // use auto-zero function of gyros
    adcData.rateAutoZero = digitalInit(GPIOE, GPIO_Pin_8, 0);

    // bring ACC's SELF TEST line low
    adcData.accST = digitalInit(GPIOE, GPIO_Pin_12, 0);

    // bring ACC's SCALE line low (ADXL3X5 requires this line be tied to GND or left floating)
    adcData.accScale = digitalInit(GPIOC, GPIO_Pin_15, 0);

    GPIO_StructInit(&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_ADC3, ENABLE);

    adcData.sample = ADC_SAMPLES - 1;

    // Use STM32F4's Triple Regular Simultaneous Mode capable of ~ 6M samples per second

    DMA_DeInit(ADC_DMA_STREAM);
    DMA_InitStructure.DMA_Channel = ADC_DMA_CHANNEL;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adcDMAData.adc123Raw1;
    DMA_InitStructure.DMA_PeripheralBaseAddr = ((uint32_t)0x40012308);
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = ADC_CHANNELS * 3 * 2;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init(ADC_DMA_STREAM, &DMA_InitStructure);

    DMA_ITConfig(ADC_DMA_STREAM, DMA_IT_HT | DMA_IT_TC, ENABLE);
    DMA_ClearITPendingBit(ADC_DMA_STREAM, ADC_DMA_FLAGS);

    DMA_Cmd(ADC_DMA_STREAM, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = ADC_DMA_IRQ;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // ADC Common Init
    ADC_CommonStructInit(&ADC_CommonInitStructure);
    ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_RegSimult;
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
    ADC_CommonInit(&ADC_CommonInitStructure);

    // ADC1 configuration
    ADC_StructInit(&ADC_InitStructure);
    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_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 16;
    ADC_Init(ADC1, &ADC_InitStructure);

    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGX, 1, ADC_SAMPLE_TIME);	// magX
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGX, 2, ADC_SAMPLE_TIME);	// magX
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGX, 3, ADC_SAMPLE_TIME);	// magX
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGX, 4, ADC_SAMPLE_TIME);	// magX

    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGY, 5, ADC_SAMPLE_TIME);	// magY
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGY, 6, ADC_SAMPLE_TIME);	// magY
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGY, 7, ADC_SAMPLE_TIME);	// magY
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGY, 8, ADC_SAMPLE_TIME);	// magY

    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGZ, 9, ADC_SAMPLE_TIME);	// magZ
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGZ, 10, ADC_SAMPLE_TIME);	// magZ
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGZ, 11, ADC_SAMPLE_TIME);	// magZ
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_MAGZ, 12, ADC_SAMPLE_TIME);	// magZ

    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_RATEX, 13, ADC_SAMPLE_TIME);	// rateX
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_RATEX, 14, ADC_SAMPLE_TIME);	// rateX
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_RATEX, 15, ADC_SAMPLE_TIME);	// rateX
    ADC_RegularChannelConfig(ADC1, ADC_CHANNEL_RATEX, 16, ADC_SAMPLE_TIME);	// rateX

    // Enable ADC1 DMA since ADC1 is the Master
    ADC_DMACmd(ADC1, ENABLE);

    // ADC2 configuration
    ADC_StructInit(&ADC_InitStructure);
    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_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 16;
    ADC_Init(ADC2, &ADC_InitStructure);

    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_RATEY, 1, ADC_SAMPLE_TIME);	// rateY
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_RATEY, 2, ADC_SAMPLE_TIME);	// rateY
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_RATEY, 3, ADC_SAMPLE_TIME);	// rateY
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_RATEY, 4, ADC_SAMPLE_TIME);	// rateY

    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCX, 5, ADC_SAMPLE_TIME);	// accX
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCX, 6, ADC_SAMPLE_TIME);	// accX
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCX, 7, ADC_SAMPLE_TIME);	// accX
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCX, 8, ADC_SAMPLE_TIME);	// accX

    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCY, 9, ADC_SAMPLE_TIME);	// accY
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCY, 10, ADC_SAMPLE_TIME);	// accY
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCY, 11, ADC_SAMPLE_TIME);	// accY
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCY, 12, ADC_SAMPLE_TIME);	// accY

    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCZ, 13, ADC_SAMPLE_TIME);	// accZ
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCZ, 14, ADC_SAMPLE_TIME);	// accZ
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCZ, 15, ADC_SAMPLE_TIME);	// accZ
    ADC_RegularChannelConfig(ADC2, ADC_CHANNEL_ACCZ, 16, ADC_SAMPLE_TIME);	// accZ

    // ADC3 configuration
    ADC_StructInit(&ADC_InitStructure);
    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_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 16;
    ADC_Init(ADC3, &ADC_InitStructure);

    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_RATEZ, 1, ADC_SAMPLE_TIME);	// rateZ
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_RATEZ, 2, ADC_SAMPLE_TIME);	// rateZ
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_RATEZ, 3, ADC_SAMPLE_TIME);	// rateZ
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_RATEZ, 4, ADC_SAMPLE_TIME);	// rateZ

    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_TEMP1, 5, ADC_SAMPLE_TIME);	// temp1
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_TEMP2, 6, ADC_SAMPLE_TIME);	// temp2

    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES1, 7, ADC_SAMPLE_TIME);	// pressure1
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES1, 8, ADC_SAMPLE_TIME);	// pressure1
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES1, 9, ADC_SAMPLE_TIME);	// pressure1
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES1, 10, ADC_SAMPLE_TIME);	// pressure1

    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_VIN, 11, ADC_SAMPLE_TIME);	// Vin
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_TEMP3, 12, ADC_SAMPLE_TIME);	// temp3

    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES2, 13, ADC_SAMPLE_TIME);	// pressure2
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES2, 14, ADC_SAMPLE_TIME);	// pressure2
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES2, 15, ADC_SAMPLE_TIME);	// pressure2
    ADC_RegularChannelConfig(ADC3, ADC_CHANNEL_PRES2, 16, ADC_SAMPLE_TIME);	// pressure2

    // Enable DMA request after last transfer (Multi-ADC mode)
    ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);

    // Enable
    ADC_Cmd(ADC1, ENABLE);
    ADC_Cmd(ADC2, ENABLE);
    ADC_Cmd(ADC3, ENABLE);

    adcData.adcFlag = CoCreateFlag(1, 0);
    adcTaskStack = aqStackInit(ADC_STACK_SIZE, "ADC");

    adcData.adcTask = CoCreateTask(adcTaskCode, (void *)0, ADC_PRIORITY, &adcTaskStack[ADC_STACK_SIZE-1], ADC_STACK_SIZE);

    // Start ADC1 Software Conversion
    ADC_SoftwareStartConv(ADC1);

    yield(100);

    // set initial temperatures
    adcData.temp1 = adcIDGVoltsToTemp(adcData.voltages[ADC_VOLTS_TEMP1]);
    adcData.temp2 = adcIDGVoltsToTemp(adcData.voltages[ADC_VOLTS_TEMP2]);
    adcData.temp3 = adcT1VoltsToTemp(adcData.voltages[ADC_VOLTS_TEMP3]);
    analogData.vIn = adcVsenseToVin(adcData.voltages[ADC_VOLTS_VIN]);

    adcCalibOffsets();
}
void ADC_Config( void )
{
  DMA_InitTypeDef DMA_InitStruct;
  ADC_InitTypeDef ADC_InitStruct;
  ADC_CommonInitTypeDef ADC_CommonInitStruct;
  GPIO_InitTypeDef GPIO_InitStruct;

  /* ADC Clk *******************************************************************/
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
  ADCx_CLK_ENABLE();
  ADCx_DMA_CLK_ENABLE();

  /* ADC Pin *******************************************************************/
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_InitStruct.GPIO_Pin  = ADCxP_PIN;
  GPIO_Init(ADCxP_GPIO_PORT, &GPIO_InitStruct);

  GPIO_InitStruct.GPIO_Pin  = ADCxN_PIN;
  GPIO_Init(ADCxN_GPIO_PORT, &GPIO_InitStruct);

  /* ADC DMA *******************************************************************/
  DMA_DeInit(ADCx_DMA_CHANNEL);
  DMA_InitStruct.DMA_PeripheralBaseAddr = ADCx_DR_ADDRESS;
  DMA_InitStruct.DMA_MemoryBaseAddr     = (uint32_t)ADC_DMA_ConvBuf;
  DMA_InitStruct.DMA_DIR                = DMA_DIR_PeripheralSRC;
  DMA_InitStruct.DMA_BufferSize         = ADC_BUF_CHENNAL * ADC_BUF_SIZE;
  DMA_InitStruct.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
  DMA_InitStruct.DMA_MemoryInc          = DMA_MemoryInc_Enable;
  DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStruct.DMA_MemoryDataSize     = DMA_MemoryDataSize_HalfWord;
  DMA_InitStruct.DMA_Mode               = DMA_Mode_Circular;
  DMA_InitStruct.DMA_Priority           = DMA_Priority_Medium;
  DMA_InitStruct.DMA_M2M                = DMA_M2M_Disable;
  DMA_Init(ADCx_DMA_CHANNEL, &DMA_InitStruct);
  DMA_Cmd(ADCx_DMA_CHANNEL, ENABLE);

  /* ADC Calibration ***********************************************************/
  ADC_VoltageRegulatorCmd(ADCx, ENABLE);
  delay_ms(10);

  ADC_SelectCalibrationMode(ADCx, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADCx);

  while(ADC_GetCalibrationStatus(ADCx) != RESET);
  calibrationValue = ADC_GetCalibrationValue(ADCx);

  /* ADC Common Init ***********************************************************/
  ADC_CommonInitStruct.ADC_Mode             = ADC_Mode_Interleave;             // ADC_Mode_Independent
  ADC_CommonInitStruct.ADC_Clock            = ADC_Clock_AsynClkMode;
  ADC_CommonInitStruct.ADC_DMAAccessMode    = ADC_DMAAccessMode_1;
  ADC_CommonInitStruct.ADC_DMAMode          = ADC_DMAMode_Circular;
  ADC_CommonInitStruct.ADC_TwoSamplingDelay = 0;
  ADC_CommonInit(ADCx, &ADC_CommonInitStruct);

  /* ADC Init *****************************************************************/
  ADC_InitStruct.ADC_ContinuousConvMode    = ADC_ContinuousConvMode_Enable;
  ADC_InitStruct.ADC_Resolution            = ADC_Resolution_12b; 
  ADC_InitStruct.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         
  ADC_InitStruct.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
  ADC_InitStruct.ADC_DataAlign             = ADC_DataAlign_Right;
  ADC_InitStruct.ADC_OverrunMode           = ADC_OverrunMode_Disable;   
  ADC_InitStruct.ADC_AutoInjMode           = ADC_AutoInjec_Disable;  
  ADC_InitStruct.ADC_NbrOfRegChannel       = ADC_BUF_SIZE;
  ADC_Init(ADCx, &ADC_InitStruct);

  /* ADC Regular Config *******************************************************/
  ADC_RegularChannelConfig(ADCx, ADCxP_CHANNEL, 1, ADC_SampleTime_601Cycles5);
  ADC_RegularChannelConfig(ADCx, ADCxN_CHANNEL, 2, ADC_SampleTime_601Cycles5);

  /* Enable & Start ***********************************************************/
  ADC_DMACmd(ADCx, ENABLE);
  ADC_Cmd(ADCx, ENABLE);
  while(!ADC_GetFlagStatus(ADCx, ADC_FLAG_RDY));
  DMA_Cmd(ADCx_DMA_CHANNEL, ENABLE);
  ADC_StartConversion(ADCx); 
}
int main(void)
{

/*  ADC1 Channels
    check the user manual for breakdown of all adc pins
    
    channel 5 = PA5 - free pin using this channel for adc
*/        
  /* Enable the HSI */
  RCC_HSICmd(ENABLE);
  /* Wait until HSI oscillator is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
  /* Enable the GPIOB Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  /* Enable ADC1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 
  /* Configure PB.12 in analog mode (ADC1 Channel0)  -*/
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; 
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);  
 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // green led
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // blue led
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* ADC1 Configuration -----------------------------------------------------*/
  /*
    This is set to ADC_Prescaler_Div1 by default and for a reason lost on me, does not work
    without these two lines the adc will not work
  */
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInit(&ADC_CommonInitStructure);

  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_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
  /* ADC1 regular channel0 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_16Cycles);
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Wait until the ADC1 is ready */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
  {
  }

  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConv(ADC1);

  static int led_state=0;

  while (1)
  {

  uint16_t adcValue = ADC_GetConversionValue(ADC1);
   if(adcValue > 2000){

    GPIO_WriteBit(GPIOB, GPIO_Pin_6, 1);
   }else{
    GPIO_WriteBit(GPIOB, GPIO_Pin_6, 0);

   }

    // port output - alternate between green and blue
    led_state = !led_state;
    GPIO_WriteBit(GPIOB, GPIO_Pin_7, led_state ? Bit_SET : Bit_RESET);
    delay(50000);

  }
}
Beispiel #8
0
void adc_configure(){
    ADC_InitTypeDef  ADC_init_structure; 
    GPIO_InitTypeDef GPIO_initStructre; 
    DMA_InitTypeDef  DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    // Clock configuration

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN,ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);

    // Analog pin configuration

    GPIO_initStructre.GPIO_Pin = GPIO_Pin_0;        // ADC Channel 10 is connected to PC0
    GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN;     
    GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; 
    GPIO_Init(GPIOC,&GPIO_initStructre);            

    // ADC structure configuration

    ADC_DeInit();
    ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Left;
    ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;
    ADC_init_structure.ADC_ContinuousConvMode = DISABLE; 
    ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC3;
    ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
    ADC_init_structure.ADC_NbrOfConversion = 1;
    ADC_init_structure.ADC_ScanConvMode = DISABLE;
    ADC_Init(ADCx,&ADC_init_structure);

    // Select the channel to be read from

    ADC_RegularChannelConfig(ADCx,ADC_Channel_10,1,ADC_SampleTime_144Cycles);

    /* DMA  configuration **************************************/

    DMA_DeInit(DMA_STREAMx);
    DMA_InitStructure.DMA_Channel = DMA_CHANNELx;  
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adc_buf;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = ADC_BUF_SZ;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init(DMA_STREAMx, &DMA_InitStructure);

    /* Enable DMA request after last transfer (Single-ADC mode) */

    ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE);

    /* Enable ADC1 DMA */

    ADC_DMACmd(ADCx, ENABLE);

    /* DMA2_Stream0 enable */

    DMA_Cmd(DMA_STREAMx, ENABLE);

    /* Enable DMA Half & Complete interrupts */

    DMA_ITConfig(DMA2_Stream0, DMA_IT_TC | DMA_IT_HT, ENABLE);

    /* Enable the DMA Stream IRQ Channel */

    NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);     

    // Enable ADC conversion

    ADC_Cmd(ADC1,ENABLE);
}
Beispiel #9
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */     
       
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();

  /* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_DualConvertedValueTab;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 16;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  /* Enable DMA1 Channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);

  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 2;
  ADC_Init(ADC1, &ADC_InitStructure);
  /* ADC1 regular channels configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);    
  ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_239Cycles5);
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);

  /* ADC2 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 2;
  ADC_Init(ADC2, &ADC_InitStructure);
  /* ADC2 regular channels configuration */ 
  ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_239Cycles5);
  ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 2, ADC_SampleTime_239Cycles5);
  /* Enable ADC2 external trigger conversion */
  ADC_ExternalTrigConvCmd(ADC2, ENABLE);

  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
  /* Enable Vrefint channel17 */
  ADC_TempSensorVrefintCmd(ENABLE);

  /* Enable ADC1 reset calibaration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));

  /* Enable ADC2 */
  ADC_Cmd(ADC2, ENABLE);

  /* Enable ADC2 reset calibaration register */   
  ADC_ResetCalibration(ADC2);
  /* Check the end of ADC2 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC2));

  /* Start ADC2 calibaration */
  ADC_StartCalibration(ADC2);
  /* Check the end of ADC2 calibration */
  while(ADC_GetCalibrationStatus(ADC2));

  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

  /* Test on DMA1 channel1 transfer complete flag */
  while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
  /* Clear DMA1 channel1 transfer complete flag */
  DMA_ClearFlag(DMA1_FLAG_TC1);

  while (1)
  {
  }
}
Beispiel #10
0
void adcInit()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStruct;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

#ifdef PLATFORM_REV_DEXMO_A_RIGHT

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

#endif

#ifdef PLATFORM_REV_DEXMO_A_LEFT

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_Init(GPIOC, &GPIO_InitStructure);

#endif

	ADC_DeInit(ADC1);

	ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
	ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
	ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
	ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	ADC_InitStruct.ADC_NbrOfChannel = 14;
	ADC_InitStruct.ADC_ScanConvMode = ENABLE;
	ADC_Init(ADC1, &ADC_InitStruct);

	ADC_TempSensorVrefintCmd(ENABLE);

/*
          |   |   |   |
          |4  |6  |8  |10
          |   |   |   |
  2 |    /-------------\
   1 \__| 3   5   7   9 nRF->
       0 \-------------/
 */

#ifdef PLATFORM_REV_DEXMO_A_RIGHT

	ADC_RegularChannelConfig(ADC1,  ADC_Channel_0,  1, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_2,  2, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_3,  3, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_4,  4, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_5,  5, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_6,  6, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_8,  7, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_9,  8, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_10,  9, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 10, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 11, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 12, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 13, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 14, ADC_SampleTime);

#endif

#ifdef PLATFORM_REV_DEXMO_A_LEFT

	ADC_RegularChannelConfig(ADC1,  ADC_Channel_0,  1, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_1,  2, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_2,  3, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_3,  4, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_4,  5, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_5,  6, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_6,  7, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1,  ADC_Channel_7,  8, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_10,  9, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 10, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 11, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 12, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 13, ADC_SampleTime);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 14, ADC_SampleTime);

#endif

	ADC_DMACmd(ADC1, ENABLE);
	ADC_Cmd(ADC1,ENABLE);

	ADC_ResetCalibration(ADC1);
	while(ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1));
	ADC_SoftwareStartConvCmd(ADC1, ENABLE);

	dmaInit();

	DEBUG_PRINT("init successfully\n");
}
Beispiel #11
0
/**
  * @brief  Configure the ADC1 channel18 using DMA channel1.
  * @param  None
  * @retval None
  */
void ADC_DMA_Config(void)
{
  /*------------------------ DMA1 configuration ------------------------------*/
  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  /* DMA1 channel1 configuration */
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  
  /* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);

  /*----------------- ADC1 configuration with DMA enabled --------------------*/
  /* Enable the HSI oscillator */
  RCC_HSICmd(ENABLE);

#if defined (USE_STM32L152_EVAL)
  /* Enable GPIOB clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  /* Configure PB.12 (ADC Channel18) in analog mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
#elif defined (USE_STM32L152D_EVAL)
  /* Enable GPIOF clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
  /* Configure PF.10 (ADC Channel31) in analog mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);  
#endif

  /* Check that HSI oscillator is ready */
  while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);

  /* Enable ADC1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  /* ADC1 configuration */
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

#if defined (USE_STM32L152_EVAL)
  /* ADC1 regular channel18 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_18, 1, ADC_SampleTime_4Cycles);
#elif defined (USE_STM32L152D_EVAL)
  /* ADC1 regular channel14 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_31, 1, ADC_SampleTime_4Cycles);
#endif

  /* Enable the request after last transfer for DMA Circular mode */
  ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
  
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* Wait until the ADC1 is ready */
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET)
  {
  }

  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConv(ADC1);
}
int main( void )
{
  //konfiguracija taktova
  RCC_ADCCLKConfig(RCC_PCLK2_Div2);//konfigurisanje takta za ADC 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//dovodjenje takta za DMA kontroler 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM1 | RCC_APB2Periph_ADC1, ENABLE);//dovodjenje takta portu A, B, C, tajmeru TIM1 i ADC-u
  
  //konfiguracija portova - analogni ulazi
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  //konfiguracija portova - bargraph tj. DIGIO konektor
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_5 | GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
   
  /* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//adresa izvorista za dma prenos - DATA REGISTER ADC-a
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_RegularConvertedValueTab;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 4;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  /* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 4;
  ADC_Init(ADC1, &ADC_InitStructure);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_1Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 2, ADC_SampleTime_1Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 3, ADC_SampleTime_1Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 4, ADC_SampleTime_1Cycles5);
  ADC_ExternalTrigConvCmd(ADC1, ENABLE);//omogucavanje externog triger moda
  ADC_DMACmd(ADC1, ENABLE);//omogucavanje DMA prenosa za ADC
  ADC_Cmd(ADC1, ENABLE);
  
  ADC_ResetCalibration(ADC1);//adc kalibracija
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
 
  //konfiguracija tajmera TIM1 koji radi u PWM modu, i svoj izlaz koristi za trigerovanje ADC-a
  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  TIM_TimeBaseInitStruct.TIM_Period = 150 - 1;
  TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
  TIM_TimeBaseInitStruct.TIM_ClockDivision = 0x0;
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct); 
  TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStruct.TIM_Pulse = 150 / 2;
  TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
  TIM_OC1Init(TIM1, &TIM_OCInitStruct);
  
  TIM_Cmd(TIM1, ENABLE);//dozovla rada tajmera tek kada se konfigurisu i DMA i ADC
  TIM_CtrlPWMOutputs(TIM1, ENABLE);//generisanje PWM izlaza za tajmer 1
  
  baterija_Acc_const=0.004032;
  servo_5V_const=0.004032;
  
  
  
  
    
  
  /* Inicijalizacija. */
  InitGPIO_Pin(GPIOB, GPIO_Pin_11, GPIO_Mode_IPU, GPIO_Speed_50MHz);
  UsartInit();
  
  /* Inicijalizacija glavnog tajmera. */
  initTimerServo();//zbog ovoga se baterija meri i dok je prekidac uvucen
  
  /* Glavna masina stanja. */
  while(1)
  {
    switch (state_robot)
    {
      /* Pocetno stanje u kome se inicijalizaciju sistemi, podesava preskaler, ukljucuje UV. */ 
      case 0:  // pocetno stanje, sve inicijalizujemo i krenemo napred
        
        if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11)) // ako je ocitano Vcc, tj. krene se sa izvrsavanjem, u suprotnom ostajemo u 
                                                       // istom stanju
        {
          /* Inicijalizacija glavnog tajmera. */
          initTimer90();
          /* Inicijalizacija tajmera za proveru pozicije robota. */
          SysTick_Config( SysTick_Config( SystemCoreClock / 1000 ) );
          /* Provera koja je stategije. */
          checkStrategy();
          /* Zadavanje komandi. */
          issueCommand( START_RUNNING, MOTION_DEVICE_ADDRESS, 30 );
          waitAck( START_RUNNING, MOTION_DEVICE_ADDRESS, 30 );
          issueCommand( PRESCALER, MOTION_DEVICE_ADDRESS, 1000 );
          waitAck( PRESCALER, MOTION_DEVICE_ADDRESS, 1000 );
          issueCommand( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
          waitAck( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
          issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 30 );
          waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 30 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 25 );            
            if ( FLAG_arriveOnDest ) break;
            sleep( 100 );
          }
          state_robot++;
          sleep(100);
        }
        break;
      
      /* Blago okretanje da bi se izbegla ivica na sredini terena. */  
      case 1:
        
        if( FLAG_strategyLeft )
        {
          issueCommand( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 20 );
          waitAck( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 20 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 25 );
            if ( FLAG_arriveOnDest ) break;
            sleep( 100 );
          }
        }
        else
        {
          issueCommand( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 20 );
          waitAck( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 20 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 25 );
            if ( FLAG_arriveOnDest ) break;
            sleep( 100 );
          }
        }
        
        state_robot++;
        sleep(100);
        break;
       
      /* Blago pomeranje napred, ka sredini terena. */
      case 2:
      {
        issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 50 );
        waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 50 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;          
        }
        state_robot++;
        sleep(100);
        break;       
      }
        
      /* Blaga rotacija da bi se poravnali opet. */  
      case 3:

        if( FLAG_strategyLeft )
        {
          issueCommand( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 25 );
          waitAck( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 25 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        else
        {
          issueCommand( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 25 );
          waitAck( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 25 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }          
        }
        state_robot++;
        sleep(100);
        break;

      /* Blago pomeranje napred da bi pomerili kocke u sredinu terena.  */
      case 4:
        
        issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 10 );
        waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 10 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break;
        
      /* Vracanje unazad. */
      case 5:
        
        issueCommand( PRESCALER, MOTION_DEVICE_ADDRESS, 500 );
        waitAck( PRESCALER, MOTION_DEVICE_ADDRESS, 500 );
        issueCommand( MOVE_BACKWARD, MOTION_DEVICE_ADDRESS, 50 );
        waitAck( MOVE_BACKWARD, MOTION_DEVICE_ADDRESS, 50 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break;  
        
      /* Okretanje ka prvoj kucici, onoj daljoj od ivice terana i gasenje senzora. */
      case 6:
        
        if( FLAG_strategyLeft )
        {
          issueCommand( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 175 );
          waitAck( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 175 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        else
        {
          issueCommand( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 175 );
          waitAck( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 175 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }          
        } 
        
        issueCommand( ULTRASOUND_OFF, MOTION_DEVICE_ADDRESS, 1 );
        waitAck( ULTRASOUND_OFF, MOTION_DEVICE_ADDRESS, 1 );
        
        state_robot++;
        sleep(100);
        break;
      
      /* Zatvaranje prvih vrata. */
      case 7:
        
        issueCommand( PRESCALER, MOTION_DEVICE_ADDRESS, 700 );
        waitAck( PRESCALER, MOTION_DEVICE_ADDRESS, 700 );
        issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 100 );
        waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 100 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break;        
        
      /* Vracanje unazad. */
      case 8:
        issueCommand( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
        waitAck( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
        issueCommand( PRESCALER, MOTION_DEVICE_ADDRESS, 500 );
        waitAck( PRESCALER, MOTION_DEVICE_ADDRESS, 500 );
        issueCommand( MOVE_BACKWARD, MOTION_DEVICE_ADDRESS, 60 );
        waitAck( MOVE_BACKWARD, MOTION_DEVICE_ADDRESS, 60 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        
        state_robot++;
        sleep(100);
        break;        
       
      /* Okretanje za 180 stepeni ka pocetnoj poziciji. */
      case 9:

        //issueCommand( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
        //waitAck( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
          
        if( FLAG_strategyLeft )
        {
          issueCommand( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 180 );
          waitAck( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 180 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        else
        {
          issueCommand( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 180 );
          waitAck( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 180 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }          
        } 
        
        state_robot++;
        sleep(100);
        break; 
      
      /* Odlazak naspram druge kucice. */
      case 10:

        issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 15 );
        waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 15 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break;             
       
      /* Okretanje ka kucici. */
      case 11:

        if( FLAG_strategyLeft )
        {
          issueCommand( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 175 );
          waitAck( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 175 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        else
        {
          issueCommand( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 175 );
          waitAck( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 175 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        
        issueCommand( ULTRASOUND_OFF, MOTION_DEVICE_ADDRESS, 1 );
        waitAck( ULTRASOUND_OFF, MOTION_DEVICE_ADDRESS, 1 );
        
        state_robot++;
        sleep(100);
        break;        
        
         
      /* Zatvaranje druge kucice. */
      case 12:
        
        issueCommand( ULTRASOUND_OFF, MOTION_DEVICE_ADDRESS, 1 );
        waitAck( ULTRASOUND_OFF, MOTION_DEVICE_ADDRESS, 1 );
        issueCommand( PRESCALER, MOTION_DEVICE_ADDRESS, 700 );
        waitAck( PRESCALER, MOTION_DEVICE_ADDRESS, 700 );
        issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 60 );
        waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 60 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break; 
        
      /* Vracanje unazad. */  
      case 13:
        
        issueCommand( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
        waitAck( ULTRASOUND_ON, MOTION_DEVICE_ADDRESS, 1 );
        issueCommand( PRESCALER, MOTION_DEVICE_ADDRESS, 500 );
        waitAck( PRESCALER, MOTION_DEVICE_ADDRESS, 500 );
        issueCommand( MOVE_BACKWARD, MOTION_DEVICE_ADDRESS, 60 );
        waitAck( MOVE_BACKWARD, MOTION_DEVICE_ADDRESS, 60 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break; 
      
      /* Okretanje ka centru naseg dela terena. */  
      case 14:
        
        if( FLAG_strategyLeft )
        {
          issueCommand( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 270 );
          waitAck( ROTATE_LEFT, MOTION_DEVICE_ADDRESS, 270 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        else
        {
          issueCommand( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 270 );
          waitAck( ROTATE_RIGHT, MOTION_DEVICE_ADDRESS, 270 );
          while( TRUE )
          {
            issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
            sleep( 100 );
            if ( FLAG_arriveOnDest ) break;
          }
        }
        state_robot++;
        sleep(100);
        break; 
        
      case 15:
      
        issueCommand( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 40 );
        waitAck( MOVE_FORWARD, MOTION_DEVICE_ADDRESS, 40 );
        while( TRUE )
        {
          issueCommand( CHECK_ARRIVE, MOTION_DEVICE_ADDRESS, 1 );
          sleep( 100 );
          if ( FLAG_arriveOnDest ) break;
        }
        state_robot++;
        sleep(100);
        break; 
        
      /* Podrazumevano stanje u kome se ne radi nista. */  
      default:
        break;
    }
  }
}
Beispiel #13
0
void initAdc(GPIO_TypeDef* GPIOx,uint16_t GPIO_Pin,__IO uint16_t* ConvertedValue){
	GPIO_InitTypeDef GPIO_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;
	/* Enable DMA1 clock */
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	/* Enable ADC1 and GPIOC clock */
	if (GPIOx == GPIOC){
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
	}
	if(GPIOx == GPIOA){
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
	}
	if(GPIOx == GPIOB){
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
	}
	/* Configure GPIO_Pin  as analog input -------------------------*/
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOx, &GPIO_InitStructure);
	/* DMA1 channel1 configuration ----------------------------------------------*/
	DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(ADC1->DR));
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
	/* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
	
	/* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
	
	/* ADC1 regular channel15 configuration */ 
	/*CAN CHHINH SUA CHANEL LAI CHO TUONG UNG VOI PIN DA CHON*/
  ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_55Cycles5);
	/* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
	/* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
	/* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
	/* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));
	/* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
	/* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
	/* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
Beispiel #14
0
void Sensors_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  ADC_InitTypeDef ADC_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;
  
  // following codes are call in previous functions
  //#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
  //  /* ADCCLK = PCLK2/2 */
  //  RCC_ADCCLKConfig(RCC_PCLK2_Div2); 
  //#else
  //  /* ADCCLK = PCLK2/4 */
  //  RCC_ADCCLKConfig(RCC_PCLK2_Div4); 
  //#endif
  /* Enable peripheral clocks ------------------------------------------------*/
  /* Enable DMA1 clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  
  /* Enable ADC1 and GPIOC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE);
  
  
  /* Configure PB0, PB1 (ADC Channel8, Channel9) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
  /* DMA1 channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 6;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  
  /* Enable DMA1 channel1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 2;
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /* ADC1 regular channelx configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_239Cycles5);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_239Cycles5);
  
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);
  
  /* Enable ADC1 reset calibration register */   
  ADC_ResetCalibration(ADC1);
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));
  
  /* Start ADC1 calibration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));
  
  /* Start ADC1 Software Conversion */ 
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);
  while(ADC_GetSoftwareStartConvStatus(ADC1));
  
  OW_CurrState = OneWire_Delay;
  OW_NextState = OneWire_Idle;
  OW_Delay = 3;
}
Beispiel #15
0
/**
  * @brief  Enter the MCU selected low power modes.
  * @param  lpmode: selected MCU low power modes. This parameter can be one of the
  *         following values:
  *              @arg STM32L_RUN: Run mode at 32MHz.
  *              @arg STM32L_RUN_1M: Run mode at 1MHz.
  *              @arg STM32L_RUN_LP: Low power Run mode at 32KHz.  
  *              @arg STM32L_SLEEP: Sleep mode at 16MHz.
  *              @arg STM32L_SLEEP_LP: Low power Sleep mode at 32KHz.
  *              @arg STM32L_STOP: Stop mode with or without RTC.
  *              @arg STM32L_STANDBY: Standby mode with or without RTC.
  * @param  RTCState: RTC peripheral state during low power modes. This parameter
  *         is valid only for STM32L_RUN_LP, STM32L_SLEEP_LP, STM32L_STOP and
  *         STM32L_STANDBY. This parameter can be one of the following values:
  *              @arg RTC_STATE_ON: RTC peripheral is ON during low power modes.
  *              @arg RTC_STATE_OFF: RTC peripheral is OFF during low power modes.
  * @param  CalibrationState: Bias Calibration mode selection state during low 
  *         power modes. 
  *         This parameter can be one of the following values:
  *              @arg BIAS_CALIB_OFF: Bias Calibration mode is selected during 
  *                   low power modes.
  *              @arg BIAS_CALIB_ON: Bias Calibration mode isn't selected during 
  *                   low power modes.
  * @retval None
*/
void IDD_Measurement(uint32_t lpmode, uint8_t RTCState, uint8_t CalibrationState)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  uint16_t mode = STM32L_MODE_LP, adcdata, i;

  /* Disable the Wakeup Interrupt */
  RTC_ITConfig(RTC_IT_WUT, DISABLE);

  /* Disable the JoyStick interrupts */
  Demo_IntExtOnOffConfig(DISABLE);

  /* Disable Leds toggling */
  Demo_LedShow(DISABLE);

  /* Save the RCC configuration */
  RCC_AHBENR   = RCC->AHBENR;
  RCC_APB2ENR  = RCC->APB2ENR;
  RCC_APB1ENR  = RCC->APB1ENR;

  /* Disable PVD */
  PWR_PVDCmd(DISABLE);

  /* Wait until JoyStick is pressed */
  while (Menu_ReadKey() != NOKEY)
  {}

  /* Save the GPIO pins current configuration then put all GPIO pins in Analog
     Input mode ...*/
  IDD_Measurement_SaveContext();

  /* Clear Wake Up flag */
  PWR_ClearFlag(PWR_FLAG_WU);

  RCC->AHBENR = 0x05;
  RCC->APB2ENR = 0x00;
  RCC->APB1ENR = 0x10000000;  /* PWR APB1 Clock enable */
  
  switch(lpmode)
  {
    /*=========================================================================*
     *                        RUN MODE 32MHz (HSE + PLL)                       *
     *========================================================================*/
    case STM32L_RUN:
    {  
      mode = STM32L_MODE_RUN;
      /* Needed delay to have a correct value on capacitor C25.
        Running NOP during waiting loop will decrease the current consumption. */
      for (i = 0;i < 0xFFFF; i++) 
      {
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
      }
    }
    break;

    /*=========================================================================*
     *                        RUN MODE MSI 1MHz                                *
     *========================================================================*/
    case STM32L_RUN_1M:
    {      
      mode = STM32L_MODE_RUN;
      
      /* Reconfigure the System Clock at MSI 1 MHz */
      SetHCLKToMSI_1MHz();
        
      /* Needed delay to have a correct value on capacitor C25.
        Running NOP during waiting loop will decrease the current consumption. */
      for (i = 0;i < 0x3FFF; i++) 
      {
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
      }
    }
    break;

    /*=========================================================================*
     *                        RUN LOW POWER MODE MSI 32KHz                     *
     *========================================================================*/    
    case STM32L_RUN_LP:
    {                
      if(!RTCState)
      {  
        RCC_LSEConfig(RCC_LSE_OFF);
      }
      else
      {
        if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET)
        {
          /* RTC Configuration ************************************************/
          /* Reset RTC Domain */
          RCC_RTCResetCmd(ENABLE);
          RCC_RTCResetCmd(DISABLE);
          
          /* Enable the LSE OSC */
          RCC_LSEConfig(RCC_LSE_ON);
          
          /* Wait till LSE is ready */
          while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
          {}
          
          /* Select the RTC Clock Source */
          RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
          
          /* Enable the RTC Clock */
          RCC_RTCCLKCmd(ENABLE);
          
          /* Wait for RTC APB registers synchronisation */
          RTC_WaitForSynchro();
        }        
      }
      /* Configure the System Clock at MSI 32 KHz */
      SetHCLKToMSI_64KHz(); 
      RCC_HCLKConfig(RCC_SYSCLK_Div2); 
      
      /* Clear IDD_CNT_EN pin */
      GPIO_ResetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN);
      
      /* Enter low power run mode */
      PWR_EnterLowPowerRunMode(ENABLE);
      
      /* Waiting wake-up interrupt */
      /* Needed delay to have a correct value on capacitor C25.
        Running NOP during waiting loop will decrease the current consumption. */
      do 
      {
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
        __NOP();  __NOP();    __NOP();  __NOP();
      }while(LowPowerStatus == 0x00);

      /* Exit low power run mode before setting the clock to 32MHz */
      PWR_EnterLowPowerRunMode(DISABLE);
      while(PWR_GetFlagStatus(PWR_FLAG_REGLP) != RESET)
      {
      }  
    }       
    break;

    /*=========================================================================*
     *                        SLEEP MODE HSI 16MHz                             * 
     *========================================================================*/        
    case STM32L_SLEEP:
    {            
      mode = STM32L_MODE_RUN;      
      /* Enable Ultra low power mode */
      PWR_UltraLowPowerCmd(ENABLE);
      
      /* Diable FLASH during SLeep LP */
      FLASH_SLEEPPowerDownCmd(ENABLE);
            
      RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_CLOCK, ENABLE);
      RCC_APB1PeriphClockLPModeCmd(RCC_APB1Periph_CLOCK, ENABLE);
      RCC_AHBPeriphClockLPModeCmd(RCC_AHBPeriph_CLOCK, ENABLE);
      
      /* Configure the System Clock to 16MHz */
      SetHCLKToHSI();
      
      Demo_SysTickConfig();
      Demo_Delay(5);
      
      /* Request to enter SLEEP mode with regulator on */
      PWR_EnterSleepMode(PWR_Regulator_ON, PWR_STOPEntry_WFI); 
    }   
    break;

    /*=========================================================================*
     *                      SLEEP LOW POWER MODE MSI 32KHz                     *
     *========================================================================*/     
    case STM32L_SLEEP_LP:
    {            
      if(!RTCState)
      {  
        RCC_LSEConfig(RCC_LSE_OFF);
      }
      else
      {
        if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET)
        {
          /* RTC Configuration ************************************************/
          /* Reset RTC Domain */
          RCC_RTCResetCmd(ENABLE);
          RCC_RTCResetCmd(DISABLE);
          
          /* Enable the LSE OSC */
          RCC_LSEConfig(RCC_LSE_ON);
          
          /* Wait till LSE is ready */
          while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
          {}
          
          /* Select the RTC Clock Source */
          RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
          
          /* Enable the RTC Clock */
          RCC_RTCCLKCmd(ENABLE);
          
          /* Wait for RTC APB registers synchronisation */
          RTC_WaitForSynchro();
        }        
      }
      
      /* Enable Ultra low power mode */
      PWR_UltraLowPowerCmd(ENABLE);
      
      /* Diable FLASH during SLeep LP */
      FLASH_SLEEPPowerDownCmd(ENABLE);
      
      /* Disable HSI clock before entering Sleep LP mode */ 
      RCC_HSICmd(DISABLE);
      /* Disable HSE clock */
      RCC_HSEConfig(RCC_HSE_OFF);
      /* Disable LSI clock */
      RCC_LSICmd(DISABLE);
      
      RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_CLOCK, ENABLE);
      RCC_APB1PeriphClockLPModeCmd(RCC_APB1Periph_CLOCK, ENABLE);
      RCC_AHBPeriphClockLPModeCmd(RCC_AHBPeriph_CLOCK, ENABLE);
      
      /* Clear IDD_CNT_EN pin */
      GPIO_ResetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN);
      
      /* Reconfigure the System Clock at MSI 64 KHz */
      SetHCLKToMSI_64KHz();
      RCC_HCLKConfig(RCC_SYSCLK_Div2); 
      
      /* Request to enter SLEEP mode with regulator low power */
      PWR_EnterSleepMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);        
    }
    break;
    
    /*=========================================================================*
     *                          STOP LOW POWER MODE                            *
     *========================================================================*/    
    case STM32L_STOP:
    {                 
      /* Enable Ultra low power mode */
      PWR_UltraLowPowerCmd(ENABLE);

      if(!RTCState)
      {  
        RCC_LSEConfig(RCC_LSE_OFF);
      }
      else
      {
        if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET)
        {
          /* RTC Configuration ************************************************/
          /* Reset RTC Domain */
          RCC_RTCResetCmd(ENABLE);
          RCC_RTCResetCmd(DISABLE);
          
          /* Enable the LSE OSC */
          RCC_LSEConfig(RCC_LSE_ON);
          
          /* Wait till LSE is ready */
          while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
          {}
          
          /* Select the RTC Clock Source */
          RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
          
          /* Enable the RTC Clock */
          RCC_RTCCLKCmd(ENABLE);
          
          /* Wait for RTC APB registers synchronisation */
          RTC_WaitForSynchro();
        }        
      }
      
      /* Clear IDD_CNT_EN pin */
      GPIO_ResetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN);
      
      /* Request to enter STOP mode with regulator in low power */
      PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
      
      /* Reset the counter by programming IDD_CNT_EN High in less than 70ms after
      the wakeup to avoid 1Kohm to be connected later on VDD_MCU */
      GPIO_SetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN);
      
      /* Configures system clock after wake-up from STOP: enable HSE, PLL and select PLL
         as system clock source (HSE and PLL are disabled in STOP mode) */
      IDD_Measurement_SYSCLKConfig_STOP();      
    }
    break;

    /*=========================================================================*
     *                          STANDBY LOW POWER MODE                         *
     *========================================================================*/        
    case STM32L_STANDBY:
    {      
      if (RTC_GetFlagStatus(RTC_FLAG_INITS) == RESET)
      {
        /* RTC Configuration **************************************************/
        /* Reset RTC Domain */
        RCC_RTCResetCmd(ENABLE);
        RCC_RTCResetCmd(DISABLE);
        
        /* Enable the LSE OSC */
        RCC_LSEConfig(RCC_LSE_ON);
        
        /* Wait till LSE is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
        {}
        
        /* Select the RTC Clock Source */
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
        
        /* Enable the RTC Clock */
        RCC_RTCCLKCmd(ENABLE);
        
        /* Wait for RTC APB registers synchronisation */
        RTC_WaitForSynchro();
      } 
      
      RTC_OutputTypeConfig(RTC_OutputType_PushPull);
      RTC_OutputConfig(RTC_Output_WakeUp, RTC_OutputPolarity_High);  
      
      /* To configure PC13 WakeUP output */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_400KHz;  
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_Init(GPIOC, &GPIO_InitStructure); 
      GPIO_PinAFConfig(GPIOC, GPIO_PinSource13, GPIO_AF_RTC_AF1);
      
      PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE);
      
      PWR_UltraLowPowerCmd(ENABLE); 
      
      RTC_ClearFlag(RTC_FLAG_WUTF | RTC_FLAG_ALRBF | RTC_FLAG_ALRAF | RTC_FLAG_TAMP1F | RTC_FLAG_TSF); 
      RTC_ITConfig(RTC_IT_WUT, DISABLE);

      if(!RTCState)
      {  
        RCC_LSEConfig(RCC_LSE_OFF);
      }
      
      /* Clear Wake Up flag */
      PWR_ClearFlag(PWR_FLAG_WU);  
      
      /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */
      PWR_EnterSTANDBYMode();
    }     
    break; 
  }

  /* Configure the System Clock to 32MHz */
  SetHCLKTo32();
      
  /* Reset lowpower status variable*/  
  LowPowerStatus = 0x00;
      
  RCC->AHBENR = RCC_AHBENR;
  RCC->APB2ENR = RCC_APB2ENR;
  RCC->APB1ENR = RCC_APB1ENR;
      
  /* Reset the counter by programming IDD_CNT_EN High in less than 70ms after
     the wakeup to avoid 1Kohm to be connected later on VDD_MCU */
  GPIO_SetBits(IDD_CNT_EN_GPIO_PORT, IDD_CNT_EN_PIN);
  
  /* Measure the Voltage using the ADC */    
  adcdata = IDD_Measurement_ADC_ReadValue();
  
  /* Write the ADC converted value in the DATA EEPROM memory for Bias Measurement */
  if(CalibrationState == BIAS_CALIB_ON)
  {
    /* Unlock EEPROM write access*/
    DATA_EEPROM_Unlock();
  
    /* Store the value in EEPROM for application needs */
    DATA_EEPROM_ProgramHalfWord(DATA_EEPROM_BIAS_ADDR, adcdata);
  
    /* Lock back EEPROM write access */
    DATA_EEPROM_Lock(); 
  }

  IDD_Measurement_ADC_DisplayValue(adcdata, mode);
    
  /* Clear Wake Up flag */
  PWR_ClearFlag(PWR_FLAG_WU);
      
  /* Enable PVD */
  PWR_PVDCmd(ENABLE);
      
  /* Restore Demonstration Context. */
  IDD_Measurement_RestoreContext();
  
  LCD_SetBackColor(LCD_COLOR_GREEN);  
  LCD_DisplayStringLine(LCD_LINE_6, "STM32L LowPower Mode");          
  LCD_DisplayStringLine(LCD_LINE_7, Str);

  LCD_DisplayStringLine(LCD_LINE_8, "Press JoyStick to   ");
  LCD_DisplayStringLine(LCD_LINE_9, "continue.           ");  

  /* Wait until Joystick pressed. */
  while (Menu_ReadKey() == NOKEY)
  {} 
  
  /* Disable ADC1 */
  ADC_Cmd(ADC1, DISABLE);

  LCD_Clear(LCD_COLOR_WHITE);
  
  LCD_GLASS_DisplayString(" STM32L ");

  /* Enable the Wakeup Interrupt */
  RTC_ITConfig(RTC_IT_WUT, ENABLE);

  /* Enable the JoyStick interrupts */
  Demo_IntExtOnOffConfig(ENABLE);
  
  /* Display the previous menu */
  Menu_DisplayMenu(); 
}
Beispiel #16
0
static void adcInstanceInit(ADCDevice adcDevice)
{
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;

    adcDevice_t * adc = &adcHardware[adcDevice];

    RCC_ClockCmd(adc->rccADC, ENABLE);
    RCC_ClockCmd(adc->rccDMA, ENABLE);

    DMA_DeInit(adc->DMAy_Channelx);

    DMA_StructInit(&DMA_InitStructure);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc->ADCx->DR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues[adcDevice];
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = adc->usedChannelCount * ADC_AVERAGE_N_SAMPLES;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = ((adc->usedChannelCount > 1) || (ADC_AVERAGE_N_SAMPLES > 1)) ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(adc->DMAy_Channelx, &DMA_InitStructure);

    DMA_Cmd(adc->DMAy_Channelx, ENABLE);

    // calibrate
    ADC_VoltageRegulatorCmd(adc->ADCx, ENABLE);
    delay(10);

    ADC_SelectCalibrationMode(adc->ADCx, ADC_CalibrationMode_Single);
    ADC_StartCalibration(adc->ADCx);
    while (ADC_GetCalibrationStatus(adc->ADCx) != RESET);

    ADC_VoltageRegulatorCmd(adc->ADCx, DISABLE);

    ADC_CommonStructInit(&ADC_CommonInitStructure);
    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
    ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
    ADC_CommonInit(adc->ADCx, &ADC_CommonInitStructure);

    ADC_StructInit(&ADC_InitStructure);

    ADC_InitStructure.ADC_ContinuousConvMode    = ADC_ContinuousConvMode_Enable;
    ADC_InitStructure.ADC_Resolution            = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
    ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
    ADC_InitStructure.ADC_DataAlign             = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_OverrunMode           = ADC_OverrunMode_Disable;
    ADC_InitStructure.ADC_AutoInjMode           = ADC_AutoInjec_Disable;
    ADC_InitStructure.ADC_NbrOfRegChannel       = adc->usedChannelCount;

    ADC_Init(adc->ADCx, &ADC_InitStructure);

    uint8_t rank = 1;
    for (int i = ADC_CHN_1; i < ADC_CHN_COUNT; i++) {
        if (!adcConfig[i].enabled || adcConfig[i].adcDevice != adcDevice) {
            continue;
        }

        ADC_RegularChannelConfig(adc->ADCx, adcConfig[i].adcChannel, rank++, adcConfig[i].sampleTime);
    }

    ADC_Cmd(adc->ADCx, ENABLE);

    while (!ADC_GetFlagStatus(adc->ADCx, ADC_FLAG_RDY));

    ADC_DMAConfig(adc->ADCx, ADC_DMAMode_Circular);

    ADC_DMACmd(adc->ADCx, ENABLE);

    ADC_StartConversion(adc->ADCx);
}
Beispiel #17
0
/**
  * @brief  ADC configuration
  * @note   This function Configure the ADC peripheral  
            1) Enable peripheral clocks
            2) Configure ADC Channel 12 pin as analog input
            3) DMA2_Stream0 channel2 configuration
            4) Configure ADC1 Channel 12
            5) Configure ADC2 Channel 12
            6) Configure ADC3 Channel 12
  * @param  None
  * @retval None
  */
static void ADC_Config(void)
{
  GPIO_InitTypeDef       GPIO_InitStructure;
  DMA_InitTypeDef        DMA_InitStructure;
  ADC_InitTypeDef        ADC_InitStructure;
  ADC_CommonInitTypeDef  ADC_CommonInitStructure;  
  
  /* Enable peripheral clocks *************************************************/
  RCC_AHB1PeriphClockCmd( ADC1_2_CHANNEL_GPIO_CLK , ENABLE);
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA2 , ENABLE);
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 , ENABLE);
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC2 , ENABLE);
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC3 , ENABLE);  

  /* Configure ADC Channel 12 pin as analog input *****************************/ 
  GPIO_InitStructure.GPIO_Pin = GPIO_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIO_PORT, &GPIO_InitStructure);

  /* DMA2 Stream0 channel0 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_CHANNELx;  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC_CDR_ADDRESS;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&aADCTripleConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = 3;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA_STREAMx, &DMA_InitStructure);

  /* DMA2_Stream0 enable */
  DMA_Cmd(DMA_STREAMx, ENABLE);

  /* ADC Common configuration *************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_Interl;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_2;  
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; 
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 regular channel 12 configuration ************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init(ADC1, &ADC_InitStructure);

  ADC_RegularChannelConfig(ADC1, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles);
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);

  /* ADC2 regular channel 12 configuration ************************************/
  ADC_Init(ADC2, &ADC_InitStructure);
  /* ADC2 regular channel12 configuration */ 
  ADC_RegularChannelConfig(ADC2, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles);

  /* ADC3 regular channel 12 configuration ************************************/
  ADC_Init(ADC3, &ADC_InitStructure); 
  /* ADC3 regular channel12 configuration */
  ADC_RegularChannelConfig(ADC3, ADC_CHANNEL, 1, ADC_SampleTime_3Cycles);

  /* Enable DMA request after last transfer (multi-ADC mode) ******************/
  ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);

  /* Enable ADC1 **************************************************************/
  ADC_Cmd(ADC1, ENABLE);

  /* Enable ADC2 **************************************************************/
  ADC_Cmd(ADC2, ENABLE);

  /* Enable ADC3 **************************************************************/
  ADC_Cmd(ADC3, ENABLE);
}  
Beispiel #18
0
/* Setups --------------------------------------------------------------------*/
void setup_ADC1_with_DMA2( ){

 // RC.
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOCEN, ENABLE); 	// Clock for the ADC port!! Do not forget about this one ;)

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);   
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);   

 // GPIO
 	GPIO_InitTypeDef GPIO_InitStruct; 

	GPIO_StructInit(&GPIO_InitStruct);
	GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AN;
	//GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_NOPULL;

	// PA : Pin 0-7.
	GPIO_InitStruct.GPIO_Pin  = (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 |
								 GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
	GPIO_Init(GPIOA, &GPIO_InitStruct);

	// PB : 0-1.
	GPIO_InitStruct.GPIO_Pin  = (GPIO_Pin_0 | GPIO_Pin_1);
	GPIO_Init(GPIOB, &GPIO_InitStruct);

	// PC : 0-5.
	GPIO_InitStruct.GPIO_Pin  = (GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | 
								 GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5);
	GPIO_Init(GPIOC, &GPIO_InitStruct);

 // DMA
	DMA_InitTypeDef DMA_InitStruct; // Skapar en struct av typ DMA_InitTypeDef som heter DMA_InitStruct.
	DMA_DeInit(DMA2_Stream0); // Nollar.

	DMA_InitStruct.DMA_Channel            = DMA_Channel_0;
	DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
	DMA_InitStruct.DMA_Memory0BaseAddr    = (uint32_t)&ADC_RawBuffer[0];
	DMA_InitStruct.DMA_DIR                = DMA_DIR_PeripheralToMemory;
	DMA_InitStruct.DMA_BufferSize         = 16;
	DMA_InitStruct.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
	DMA_InitStruct.DMA_MemoryInc          = DMA_MemoryInc_Enable;
	DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStruct.DMA_MemoryDataSize     = DMA_MemoryDataSize_HalfWord;
	DMA_InitStruct.DMA_Mode               = DMA_Mode_Circular;
	DMA_InitStruct.DMA_Priority           = DMA_Priority_High;
	DMA_InitStruct.DMA_FIFOMode           = DMA_FIFOMode_Disable;
	DMA_InitStruct.DMA_MemoryBurst        = DMA_MemoryBurst_Single;
	DMA_InitStruct.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;

	DMA_Init(DMA2_Stream0, &DMA_InitStruct);

	DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);

 // NVIC.
	NVIC_InitTypeDef NVIC_InitStruct;

	NVIC_InitStruct.NVIC_IRQChannel = DMA2_Stream0_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);


	DMA_Cmd(DMA2_Stream0, ENABLE);

 // ADC
	ADC_DeInit( );
	ADC_InitTypeDef ADC_InitStruct; 

	ADC_InitStruct.ADC_Resolution           = ADC_Resolution_12b;
	ADC_InitStruct.ADC_ScanConvMode         = ENABLE;
	ADC_InitStruct.ADC_ContinuousConvMode   = ENABLE;
	ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	ADC_InitStruct.ADC_ExternalTrigConv     = ADC_ExternalTrigConv_T1_CC1;
	ADC_InitStruct.ADC_DataAlign            = ADC_DataAlign_Right;
	ADC_InitStruct.ADC_NbrOfConversion      = 16;
	ADC_Init(ADC1, &ADC_InitStruct);

	ADC_CommonInitTypeDef ADC_CommonStruct;

	ADC_CommonStruct.ADC_Mode 			  = ADC_Mode_Independent;
	ADC_CommonStruct.ADC_Prescaler		  = ADC_Prescaler_Div2;
	ADC_CommonStruct.ADC_DMAAccessMode	  = ADC_DMAAccessMode_Disabled;
	ADC_CommonStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
 	ADC_CommonInit(&ADC_CommonStruct);

	ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 9, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 10, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 11, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 12, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 13, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 14, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 15, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 16, ADC_SampleTime_480Cycles);

	ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
	ADC_Cmd(ADC1, ENABLE);
	ADC_DMACmd(ADC1, ENABLE);   	
	ADC_SoftwareStartConv(ADC1);
}
Beispiel #19
0
/**
  * @brief  Configures the ADC1 channel7 in continuous mode.
  * @param  None
  * @retval None
  */
static void ADC_Config(void)
{
  GPIO_InitTypeDef   GPIO_InitStructure;
  ADC_InitTypeDef    ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;

  /* Enable the GPIOC Clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  
  /* Configure the ADC clock */  
  RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);

  /* ADC1 Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
  
  /* Setup SysTick Timer for 1 µsec interrupts  */
  if (SysTick_Config(SystemCoreClock / 1000000))
  { 
    /* Capture error */ 
    while (1)
    {}
  }
  
  /* Configure PC.1 (ADC Channel7) in analog mode */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);  

  ADC_StructInit(&ADC_InitStructure);
  
  /* Calibration procedure */
  ADC_VoltageRegulatorCmd(ADC1, ENABLE);
  
  /* Insert delay equal to 10 µs */
  Delay(10);
  
  ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
  ADC_StartCalibration(ADC1);
  
  while(ADC_GetCalibrationStatus(ADC1) != RESET );
  calibration_value = ADC_GetCalibrationValue(ADC1);
  
  /* Configure the ADC1 in continuous mode */  
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                                                                    
  ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                  
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;             
  ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;                  
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;          
  
  ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
  
  ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 
  ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;         
  ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   
  ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;   
  ADC_InitStructure.ADC_NbrOfRegChannel = 1;
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /* ADC1 regular channel7 configuration */ 
  ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 1, ADC_SampleTime_181Cycles5);
  
  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);

  /* wait for ADRDY */
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));
  
  /* ADC1 DMA Enable */
  ADC_DMACmd(ADC1, ENABLE);
  ADC_DMAConfig(ADC1, ADC_DMAMode_Circular);
  
  /* Start ADC1 Software Conversion */ 
  ADC_StartConversion(ADC1);
}
Beispiel #20
0
void VBatInit()
{
    ADC_InitTypeDef       ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    GPIO_InitTypeDef      GPIO_InitStructure;
    GPIO_StructInit(&GPIO_InitStructure);
    /* ADC Channel configuration */
    RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);

    /* GPIOC Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

    /* Configure ADC Channel11 as analog input */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    ADC_StructInit(&ADC_InitStructure);

    /* Calibration procedure */
    ADC_VoltageRegulatorCmd(ADC2, ENABLE);

    /* Insert delay equal to 10 µs */
    delay_us(10);

    ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single);
    ADC_StartCalibration(ADC2);

    while(ADC_GetCalibrationStatus(ADC2) != RESET );
    //calibration_value = ADC_GetCalibrationValue(ADC2);

    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0;
    ADC_CommonInit(ADC2, &ADC_CommonInitStructure);

    ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0;
    ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;
    ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;
    ADC_InitStructure.ADC_NbrOfRegChannel = 1;
    ADC_Init(ADC2, &ADC_InitStructure);

    /* ADC2 regular channel11 configuration */
    ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_19Cycles5);

    /* Enable ADC2 */
    ADC_Cmd(ADC2, ENABLE);

    /* wait for ADRDY */
    while(!ADC_GetFlagStatus(ADC2, ADC_FLAG_RDY));

    ADC_StartConversion(ADC2);
}
//******************************************************************************
int ADC_device_init(void)
{
    /* TODO: Ditch malloc here, this can be done statically */
    size_t adcBufferSize = sizeof(uint16_t) * TOTAL_ADC_CHANNELS;
    ADCConvertedValues = portMalloc(adcBufferSize);
    memset(ADCConvertedValues, 0, adcBufferSize);

    ADC_InitTypeDef ADC_InitStructure;
    ADC_CommonInitTypeDef ADC_CommonInitStructure;
    DMA_InitTypeDef DMA_InitStructure;

    ADC_DeInit();
    ADC_GPIO_Configuration();

    /* Enable peripheral clocks ************************************************ */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);

    /* DMA2_Stream0 channel0 configuration ************************************* */
    DMA_DeInit(DMA2_Stream2);
    DMA_InitStructure.DMA_Channel = DMA_Channel_1;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValues[0];
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = 9;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
    DMA_Init(DMA2_Stream2, &DMA_InitStructure);
    /* DMA2_Stream0 enable */
    DMA_Cmd(DMA2_Stream2, ENABLE);

    /* ADC Common Init ********************************************************* */
    ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
    ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&ADC_CommonInitStructure);

    /* ADC2 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_ExternalTrigConv_T1_CC1;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion = 9;
    ADC_Init(ADC2, &ADC_InitStructure);

    /* ADC2 regular channel configuration ***************************** */
    ADC_RegularChannelConfig(ADC2, ADC_Channel_14, 1,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_15, 2,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_8, 3,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_9, 4,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_13, 5,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 6,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 7,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 8,
                             ADC_SampleTime_480Cycles);
    ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 9,
                             ADC_SampleTime_480Cycles);

    /* Enable DMA request after last transfer (Single-ADC mode) */
    ADC_DMARequestAfterLastTransferCmd(ADC2, ENABLE);

    /* Enable ADC2 DMA */
    ADC_DMACmd(ADC2, ENABLE);

    /* Enable ADC2 ************************************************************* */
    ADC_Cmd(ADC2, ENABLE);

    /* Start ADC2 Software Conversion */
    ADC_SoftwareStartConv(ADC2);

    return 1;
}
Beispiel #22
0
//ADC1  DMA 配置
void ADC1_DMA_Init(void)
{
 ADC_InitTypeDef ADC_InitStructure;
 DMA_InitTypeDef DMA_InitStructure;
   /* 使能DMA1时钟 */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
	ADC1_GPIO_Configuration();
  /* 复位DMA1 通道1的配置 */
  DMA_DeInit(DMA1_Channel1); 
  //设定从ADC外设的数据寄存器(ADC1_DR_Address)转移到内存(ADCConcertedValue)
  /* 每次传输大小16位,使用DMA循环传输模式 */
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue;//数据缓冲区的地址
  /* 外设为数据源 */
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  /* 数据缓冲区,大小为6 */
  DMA_InitStructure.DMA_BufferSize = 6;
  /* 外设地址固定 */ 
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  /* 内存地址增加,多组adc时,使能,数据传输时,内存增加 */
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  /* 数据大小为半字,16bit */
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  /* DMA循环传输 */
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  /* 优先级高 */
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  /* 禁止内存到内存模式 */
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  /* 初始化DMA1 通道1 */
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  
  /* 使能DMA通道1 */
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  /* ADC独立模式	 相对于双重模式 */
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  /* 扫描模式用于多通道采集 */
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  /* 相对于单次模式:转换一次后就结束 */
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  /* 不使用外部触发转换 */
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  /* 采集数据右对齐 */
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  /* 转换组的通道数目 */
  ADC_InitStructure.ADC_NbrOfChannel =6;
  /* 初始化配置ADC1 */
  ADC_Init(ADC1, &ADC_InitStructure);
  
  /* 配置ADC时钟,为PCLK2的8分频,即9MHz */
  RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  //ADC1,ch0,序号1,55.5.。。
  ADC_RegularChannelConfig(ADC1, ADC_Channel_0,1, ADC_SampleTime_239Cycles5);
  //ADC1,ch1,序号1,55.5.。。
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1,2, ADC_SampleTime_239Cycles5);
  //ADC1,ch2,序号1,55.5.。。
  ADC_RegularChannelConfig(ADC1, ADC_Channel_2,3, ADC_SampleTime_239Cycles5);
  //ADC1,ch3,序号1,55.5.。。
  ADC_RegularChannelConfig(ADC1, ADC_Channel_3,4, ADC_SampleTime_239Cycles5);
  //ADC1,ch8,序号1,55.5.。。
  ADC_RegularChannelConfig(ADC1, ADC_Channel_8,5, ADC_SampleTime_239Cycles5);
   //ADC1,ch16,序号1,55.5.。。
  ADC_RegularChannelConfig(ADC1, ADC_Channel_16,6, ADC_SampleTime_239Cycles5);


  /* 使能片内温度传感器 */
  ADC_TempSensorVrefintCmd(ENABLE);

  /* 使能ADC_DMA */
  ADC_DMACmd(ADC1, ENABLE);
  
  /* 使能ADC */
  ADC_Cmd(ADC1, ENABLE);

  /* 使能ADC1的复位校准寄存器 */
  ADC_ResetCalibration(ADC1);
	
  /* 等待校准完成 */
  while(ADC_GetResetCalibrationStatus(ADC1));

  /* 使能ADC1的开始校准寄存器 */
  ADC_StartCalibration(ADC1);
	
  /* 等待完成 */
  while(ADC_GetCalibrationStatus(ADC1));
     
  /* 使用软件触发,由于没有采用外部触发 */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

}
Beispiel #23
0
void adcInit(adcConfig_t *config)
{
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;

    uint8_t i;
    uint8_t configuredAdcChannels = 0;

    memset(&adcOperatingConfig, 0, sizeof(adcOperatingConfig));

    if (config->vbat.enabled) {
        adcOperatingConfig[ADC_BATTERY].tag = config->vbat.ioTag;
    }

    if (config->rssi.enabled) {
        adcOperatingConfig[ADC_RSSI].tag = config->rssi.ioTag;  //RSSI_ADC_CHANNEL;
    }

    if (config->external1.enabled) {
        adcOperatingConfig[ADC_EXTERNAL1].tag = config->external1.ioTag; //EXTERNAL1_ADC_CHANNEL;
    }

    if (config->currentMeter.enabled) {
        adcOperatingConfig[ADC_CURRENT].tag = config->currentMeter.ioTag;  //CURRENT_METER_ADC_CHANNEL;
    }

    ADCDevice device = adcDeviceByInstance(ADC_INSTANCE);
    if (device == ADCINVALID)
        return;

    adcDevice_t adc = adcHardware[device];

    bool adcActive = false;
    for (int i = 0; i < ADC_CHANNEL_COUNT; i++) {
        if (!adcOperatingConfig[i].tag)
            continue;

        adcActive = true;
        IOInit(IOGetByTag(adcOperatingConfig[i].tag), OWNER_ADC_BATT + i, 0);
        IOConfigGPIO(IOGetByTag(adcOperatingConfig[i].tag), IO_CONFIG(GPIO_Mode_AN, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL));
        adcOperatingConfig[i].adcChannel = adcChannelByTag(adcOperatingConfig[i].tag);
        adcOperatingConfig[i].dmaIndex = configuredAdcChannels++;
        adcOperatingConfig[i].sampleTime = ADC_SampleTime_480Cycles;
        adcOperatingConfig[i].enabled = true;
    }

    if (!adcActive) {
        return;
    }
    
    RCC_ClockCmd(adc.rccADC, ENABLE);

    dmaInit(dmaGetIdentifier(adc.DMAy_Streamx), OWNER_ADC, 0);

    DMA_DeInit(adc.DMAy_Streamx);

    DMA_StructInit(&DMA_InitStructure);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc.ADCx->DR;
    DMA_InitStructure.DMA_Channel = adc.channel;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adcValues;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
    DMA_InitStructure.DMA_BufferSize = configuredAdcChannels;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_Init(adc.DMAy_Streamx, &DMA_InitStructure);

    DMA_Cmd(adc.DMAy_Streamx, ENABLE);

    ADC_CommonInitTypeDef ADC_CommonInitStructure;

    ADC_CommonStructInit(&ADC_CommonInitStructure);
    ADC_CommonInitStructure.ADC_Mode             = ADC_Mode_Independent;
    ADC_CommonInitStructure.ADC_Prescaler        = ADC_Prescaler_Div8;
    ADC_CommonInitStructure.ADC_DMAAccessMode    = ADC_DMAAccessMode_Disabled;
    ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
    ADC_CommonInit(&ADC_CommonInitStructure);

    ADC_StructInit(&ADC_InitStructure);

    ADC_InitStructure.ADC_ContinuousConvMode       = ENABLE;
    ADC_InitStructure.ADC_Resolution               = ADC_Resolution_12b;
    ADC_InitStructure.ADC_ExternalTrigConv         = ADC_ExternalTrigConv_T1_CC1;
    ADC_InitStructure.ADC_ExternalTrigConvEdge     = ADC_ExternalTrigConvEdge_None;
    ADC_InitStructure.ADC_DataAlign                = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfConversion          = configuredAdcChannels;
    ADC_InitStructure.ADC_ScanConvMode             = configuredAdcChannels > 1 ? ENABLE : DISABLE; // 1=scan more that one channel in group

    ADC_Init(adc.ADCx, &ADC_InitStructure);

    uint8_t rank = 1;
    for (i = 0; i < ADC_CHANNEL_COUNT; i++) {
        if (!adcOperatingConfig[i].enabled) {
            continue;
        }
        ADC_RegularChannelConfig(adc.ADCx, adcOperatingConfig[i].adcChannel, rank++, adcOperatingConfig[i].sampleTime);
    }
    ADC_DMARequestAfterLastTransferCmd(adc.ADCx, ENABLE);

    ADC_DMACmd(adc.ADCx, ENABLE);
    ADC_Cmd(adc.ADCx, ENABLE);

    ADC_SoftwareStartConv(adc.ADCx);
}
Beispiel #24
0
void ADC1_Init(uint16_t *ADC_Buffer)
{
	DMA_InitTypeDef DMA_InitStructure;
	ADC_CommonInitTypeDef ADC_CommonInitStructure;
	ADC_InitTypeDef ADC_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;

	/* enable clocks for DMA2, ADC1, GPIOA ----------------------------------*/
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
	//	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

	ADC_DeInit();

	/* DMA2 stream0 channel0 configuration ----------------------------------*/
	DMA_InitStructure.DMA_Channel = DMA_Channel_0;  
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC_Buffer;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = 5;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; // was enable         
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream0, &DMA_InitStructure);
	DMA_Cmd(DMA2_Stream0, ENABLE);

	/* ADC Common Init ------------------------------------------------------*/
	ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
	ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
	ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
	ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; // was 5?
	ADC_CommonInit(&ADC_CommonInitStructure);

	/* ADC1 Init ------------------------------------------------------------*/
	ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
	ADC_InitStructure.ADC_ScanConvMode = ENABLE;
	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; // was enable
	ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Left; // was right - in CLOUDS is left?
	ADC_InitStructure.ADC_NbrOfConversion = 5;
	//	ADC_InitStructure.ADC_NbrOfChannel = 10; not existing
	ADC_Init(ADC1, &ADC_InitStructure);
	
	/* Configure analog input pins ------------------------------------------*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 |GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;// | GPIO_Pin_10 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOA, &GPIO_InitStructure);


	// adc8 is adc1_10 =pc0, next is adc1_11 =pc1
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	/* ADC1 regular channel configuration -----------------------------------*/ 
	ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_480Cycles);
	ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_480Cycles);
	

	/* Enable Complete DMA interrupt  */
	
	DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE); // do we need this?
    
	/* ADC DMA IRQ Channel configuration */
	NVIC_EnableIRQ(DMA2_Stream0_IRQn);
	
	/* Enable DMA request after last transfer (Single-ADC mode) */
	ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
	
	/* Enable ADC1 DMA */
	ADC_DMACmd(ADC1, ENABLE);

	/* Enable ADC1 */
	ADC_Cmd(ADC1, ENABLE);
	
	/* Start ADC1 Software Conversion */ 
	ADC_SoftwareStartConv(ADC1);

}
void SENSOR_Force_Init(void)
{
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	ADC_InitTypeDef ADC_InitStructure;
	ADC_CommonInitTypeDef ADC_CommonInitStructure;
	
	DMA_InitTypeDef DMA_InitStructure;
	
	/* GPIO Config ------------------------------------------------------------------*/
	/* Enable GPIOF Clock */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	
  // Configure ADC3 Channel13 pin as analog input for New Force sensors' (1,2,3,4) I/Os 
  GPIO_StructInit(&GPIO_InitStructure); // Reset init structure, if not it can cause issues...
	//ADC3 														CH9  			CH14			CH15				CH4					CH5				CH6					CH7				CH8
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|
																GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;		
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  GPIO_Init(GPIOF, &GPIO_InitStructure);	
	
	/* DMA2 Config ------------------------------------------------------------------*/
	/* Enable DMA2 Clock */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
	/* DMA2 Stream0 channel0 configuration */ 
	
	DMA_DeInit(DMA2_Stream1);
	
	DMA_InitStructure.DMA_Channel = DMA_Channel_2;      
	DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;   
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;    
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; 
	DMA_InitStructure.DMA_BufferSize = 8;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;    
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;   
	
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;    
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;            
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;   
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;   
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;   
	DMA_Init(DMA2_Stream1, &DMA_InitStructure);    
	DMA_Cmd(DMA2_Stream1, ENABLE); 

	/* ADC Config -------------------------------------------------------------------*/
	/* Enable ADC3 Clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3,ENABLE);
  /* ADC Common Init */
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;    //
  //ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; 
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_15Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);

  /* ADC1 Init */
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  //ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
// gtz02nov  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;  //gtz01nov 
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  //ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_InitStructure.ADC_NbrOfConversion = 8;
  ADC_Init(ADC3, &ADC_InitStructure);

  /* ADC3 regular channelx configuration */
  ADC_RegularChannelConfig(ADC3, ADC_Channel_9,  1, ADC_SampleTime_144Cycles);// PF3
  ADC_RegularChannelConfig(ADC3, ADC_Channel_14, 2, ADC_SampleTime_144Cycles);// PF4
  ADC_RegularChannelConfig(ADC3, ADC_Channel_15, 3, ADC_SampleTime_144Cycles);// PF5
  ADC_RegularChannelConfig(ADC3, ADC_Channel_4,  4, ADC_SampleTime_144Cycles);// PF6
  ADC_RegularChannelConfig(ADC3, ADC_Channel_5,  5, ADC_SampleTime_144Cycles);// PF7
  ADC_RegularChannelConfig(ADC3, ADC_Channel_6,  6, ADC_SampleTime_144Cycles);// PF8
  ADC_RegularChannelConfig(ADC3, ADC_Channel_7,  7, ADC_SampleTime_144Cycles);// PF9
  ADC_RegularChannelConfig(ADC3, ADC_Channel_8,  8, ADC_SampleTime_144Cycles);// PF10

 /* Enable DMA request after last transfer (Single-ADC mode) */
 	ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE);//gtz01nov 

	ADC_ContinuousModeCmd(ADC3, ENABLE);

  /* Enable ADC3 DMA */
  ADC_DMACmd(ADC3, ENABLE);

  /* Enable ADC3 */
  ADC_Cmd(ADC3, ENABLE);

  /* Start ADC3 Software Conversion */ 
  ADC_SoftwareStartConv(ADC3);
}
Beispiel #26
0
static void ADC_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	ADC_InitTypeDef ADC_InitStructure;
	__IO uint16_t ADCConvertedValue;

    /* 使能 ADC1 and GPIOC clock */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

	/* 
	配置PC4为模拟输入(ADC Channel14)
	PC4 ADC123_IN14
	 */
	//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

	/*
	PC0 ADC123_IN10
	PC1 ADC123_IN11
	PC2 ADC123_IN12
	*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
	GPIO_Init(GPIOC, &GPIO_InitStructure);		  

	/* 配置ADC1, 不用DMA, 用软件触发 */

	/*设置ADC工作在独立模式*/
	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

	/*规定AD转换工作在单次模式,即对一个通道采样*/
	ADC_InitStructure.ADC_ScanConvMode = DISABLE;

	/*规定AD转换工作在扫描模式,即对多个通道采样*/
	//ADC_InitStructure.ADC_ScanConvMode = ENABLE;
	/*设定AD转化在连续模式*/
	ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
	/*不使用外部促发转换*/ 
	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
	/*采集的数据在寄存器中以右对齐的方式存放*/ 
	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

	/*设定要转换的AD通道数目*/
	ADC_InitStructure.ADC_NbrOfChannel = 1;
	//ADC_InitStructure.ADC_NbrOfChannel = 3;

	ADC_Init(ADC1, &ADC_InitStructure);

	/* 配置ADC1 规则通道14 channel14 configuration */
	//ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);

	/*配置ADC1的通道10和11、12的转换先后顺序以及采样时间为为55.5个采样周期 */   
	//ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);   
	//ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_55Cycles5); 
	//ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_55Cycles5); 

	/* 使能ADC1 DMA功能 */
	ADC_DMACmd(ADC1, ENABLE);

	/* 使能 ADC1 */
	ADC_Cmd(ADC1, ENABLE);

	/* 使能ADC1 复位校准寄存器 */
	ADC_ResetCalibration(ADC1);
	/* 检查ADC1的复位寄存器 */
	while(ADC_GetResetCalibrationStatus(ADC1));

	/* 启动ADC1校准 */
	ADC_StartCalibration(ADC1);
	/* 检查校准是否结束 */
	while(ADC_GetCalibrationStatus(ADC1));

	/* 由于没有采用外部触发,所以使用软件启动ADC转换 */
	//ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
Beispiel #27
0
void
AGC_Init(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  ADC_InitTypeDef ADC_InitStructure;
  DMA_InitTypeDef DMA_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  GPIO_InitStructure.GPIO_Pin = ADC_CHANNEL10_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(ADC_PORT, &GPIO_InitStructure);

  RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);
  ADC_DeInit(ADC3);
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T8_TRGO;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC3, &ADC_InitStructure);
  ADC_RegularChannelConfig(ADC3, ADC_Channel_10, 1, ADC_SampleTime_71Cycles5);
  ADC_DMACmd(ADC3, ENABLE);
  ADC_Cmd(ADC3, ENABLE);
  ADC_ResetCalibration(ADC3);
  while (ADC_GetResetCalibrationStatus(ADC3))
    ;
  ADC_StartCalibration(ADC3);
  while (ADC_GetCalibrationStatus(ADC3))
    ;
  ADC_ExternalTrigConvCmd(ADC3, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
  DMA_DeInit(DMA2_Channel5);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC3_DR_ADDRESS;
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32) (&AgcSampleBuf);
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = AGC_BUF_NUM;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA2_Channel5, &DMA_InitStructure);
  DMA_ClearFlag(DMA2_IT_TC5 | DMA2_IT_HT5);
  DMA_ITConfig(DMA2_Channel5, DMA_IT_TC | DMA_IT_HT, ENABLE);
  DMA_Cmd(DMA2_Channel5, ENABLE);

#ifdef FILTER
  ch1_iir_reset();
#endif
  PGA113_Init();
}
Beispiel #28
0
//==============================================================================
//      ADC 初始化
//==============================================================================
void F_InitialADC(void)
{
    ADC_InitTypeDef          ADC_InitStructure;
    GPIO_InitTypeDef         GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;	// 聲明定時器初始化結構體
    TIM_OCInitTypeDef        TIM_OCInitStructure;

    /* GPIOC Periph clock enable */
    RCC_AHBPeriphClockCmd(P_VR2_GPIO_CLK, ENABLE);

    /* ADC1 Periph clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

    /* TIM3 Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* Configure ADC Channel0 as analog input */
    GPIO_InitStructure.GPIO_Pin = P_VR2_PIN ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
    GPIO_Init(P_VR2_GPIO_PORT, &GPIO_InitStructure);

    /* TIM3 Configuration *******************************************************/
    TIM_DeInit(TIM3);

    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

    TIM_OCStructInit(&TIM_OCInitStructure);

    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 0xFF;
    TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* TIM3 TRGO selection */
    TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);

    /* ADC2 Configuration *******************************************************/
    /* ADCs DeInit */
    ADC_DeInit(ADC1);

    /* Configure the ADC1 in continous mode withe a resolutuion equal to 8 bits*/
    ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
    ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
    ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
    ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
    ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
    ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
    ADC_Init(ADC1, &ADC_InitStructure);

    /* Convert the ADC1 Channel 0 with 239.5 Cycles as sampling time */
    ADC_ChannelConfig(ADC1, P_VR2_Channel , ADC_SampleTime_28_5Cycles);

    /* ADC Calibration */
    ADC_GetCalibrationFactor(ADC1);

    /* Enable the auto delay feature */
    ADC_WaitModeCmd(ADC1, ENABLE);

    /* Enable the Auto power off mode */
    ADC_AutoPowerOffCmd(ADC1, ENABLE);

    /* Enable ADCperipheral[PerIdx] */
    ADC_Cmd(ADC1, ENABLE);

    /* Wait the ADCEN falg */
    while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));

    /* TIM2 enable counter */
    TIM_Cmd(TIM3, ENABLE);

    /* ADC1 regular Software Start Conv */
    ADC_StartOfConversion(ADC1);
}
Beispiel #29
0
// init ADC
void init_adc() {
  // enable clocks
  RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOC, ENABLE );
  RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM2, ENABLE );
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE );

  // configure GPIO
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init( GPIOC, &GPIO_InitStructure );

  // setup interrupts
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init( &NVIC_InitStructure );

  // setup tim2
  // freq on 70Khz
  TIM_TimeBaseInitTypeDef TIM_InitStructure;
  TIM_TimeBaseStructInit( &TIM_InitStructure );
  TIM_InitStructure.TIM_Period = (84000000 / 70000) - 1; 
  TIM_InitStructure.TIM_Prescaler = 0;
  TIM_InitStructure.TIM_ClockDivision = 0;
  TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit( TIM2, &TIM_InitStructure );
  // TIM2 TRGO Selection
  TIM_SelectOutputTrigger( TIM2, TIM_TRGOSource_Update );
  TIM_Cmd( TIM2, ENABLE );

  // dma configuration
  DMA_InitTypeDef DMA_InitStructure;
  DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t) &ADCDualConvertedValues;
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) 0x40012308;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  DMA_InitStructure.DMA_BufferSize = BUFFERSIZE;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init( DMA2_Stream0, &DMA_InitStructure );
  // Enable DMA Stream Half/ Transfer Complete interrupt
  DMA_ITConfig( DMA2_Stream0, DMA_IT_TC | DMA_IT_HT, ENABLE );
  DMA_Cmd( DMA2_Stream0, ENABLE );

  // configure ADC
  ADC_CommonInitTypeDef ADC_CommonInitStructure;
  ADC_InitTypeDef ADC_InitStructure;

  ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; // 2 half words one by one
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit( &ADC_CommonInitStructure );
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_TRGO;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 1;
  ADC_Init( ADC1, &ADC_InitStructure );
  ADC_Init( ADC2, &ADC_InitStructure );
  // ADC1 Regular Channel 10
  ADC_RegularChannelConfig( ADC1, ADC_Channel_10, 1, ADC_SampleTime_15Cycles ); // PC0
  // ADC2 Regular Channel 11
  ADC_RegularChannelConfig( ADC2, ADC_Channel_11, 1, ADC_SampleTime_15Cycles ); // PC1
  ADC_MultiModeDMARequestAfterLastTransferCmd( ENABLE );
  ADC_Cmd( ADC1, ENABLE );
  ADC_Cmd( ADC2, ENABLE ); 
}
Beispiel #30
0
void init_adc( void )
{
	ADC_InitTypeDef ADC_InitStructure;
	DMA_InitTypeDef DMA_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
		
  // GPIOA Periph clock enable 
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  
	GPIO_StructInit(&GPIO_InitStructure);
  //Configure ADC Channel1/2/3/4 PA1/2/3/4 as analog input 
  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 );
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  // ADC1 DeInit    
  ADC_DeInit(ADC1);
  
  // ADC1 Periph clock enable  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  
  // DMA1 clock enable  
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE);
  
  // DMA1 Channel1 Config  
  DMA_DeInit(DMA1_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = NO_SAMPLES * NO_CHANNELS;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(DMA1_Channel1, &DMA_InitStructure);
  
  // DMA1 Channel1 enable  
  DMA_Cmd(DMA1_Channel1, ENABLE);
  
  // ADC DMA request in circular mode  
  ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular);
  
  // Enable ADC_DMA  
  ADC_DMACmd(ADC1, ENABLE);  
  
  // Initialize ADC structure  
  ADC_StructInit(&ADC_InitStructure);
  
  // Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits   
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
  ADC_Init(ADC1, &ADC_InitStructure); 
	
	ADC_JitterCmd(ADC1, ADC_JitterOff_PCLKDiv4, ENABLE);
 
	//ADC Frequency set as 12MHz
	//With 5 ADC readings at 239.5 + 12.5 ADC Cycles 
	//this gives a sampling rate of 
	
  // Convert the ADC_SOL_V  with 239.5 + 12.5 = ADC Cycles as sampling time      
  ADC_ChannelConfig(ADC1, ADC_SOL_V , ADC_SampleTime_239_5Cycles);
  
  // Convert the ADC_SOL_I  with 239.5 + 12.5 = ADC Cycles as sampling time      
  ADC_ChannelConfig(ADC1, ADC_SOL_I , ADC_SampleTime_239_5Cycles);
  
	// Convert the ADC_BATT_V  with 239.5 + 12.5 = ADC Cycles as sampling time      
  ADC_ChannelConfig(ADC1, ADC_BATT_V , ADC_SampleTime_239_5Cycles);
  
  // Convert the ADC_BATT_I  with 239.5 + 12.5 = ADC Cycles as sampling time   
  ADC_ChannelConfig(ADC1, ADC_BATT_I , ADC_SampleTime_239_5Cycles);
	
	//Enable Temperature Sensor
	//>2.2us Sampling time required
	ADC_TempSensorCmd(ENABLE);
	ADC_ChannelConfig(ADC1, ADC_TEMP, ADC_SampleTime_239_5Cycles);
	
	//Get Temp Calibration Values
	ts_cal1 = *( (uint16_t*) 0x1FFFF7B8 );
  ts_cal2 = *( (uint16_t*) 0x1FFFF7C2 );
	
  // ADC Calibration  
  ADC_GetCalibrationFactor(ADC1);
  
  // Enable ADC1  
  ADC_Cmd(ADC1, ENABLE);     
  
  // Wait the ADCEN falg  
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); 
	
	//adc_init_analog_watchdog();
		
  // ADC1 regular Software Start Conv   
  ADC_StartOfConversion(ADC1);
}