void gather_adc_values(void) { static uint8_t last_step = 0; // TIM1 CC4 occuer adc_buffer_for_mosfet_test[126] = DMA1_Channel1->CNDTR; TP3_SET; ADC_StopOfConversion(ADC1); ADC_DMACmd(ADC1, DISABLE); // reset the DMA DMA_Cmd(DMA1_Channel1, DISABLE); //DMA_DeInit(DMA1_Channel1); DMA_ClearFlag(DMA1_FLAG_GL1); // DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; // DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adc_buffer_for_mosfet_test; // DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // DMA_InitStructure.DMA_BufferSize = 25; // 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_Normal; // DMA_InitStructure.DMA_Priority = DMA_Priority_High; // DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA1_Channel1->CNDTR = 50; adc_buffer_for_mosfet_test[127] = ADC1->DR; DMA_Cmd(DMA1_Channel1, ENABLE); ADC_DMACmd(ADC1, ENABLE); ADC_StartOfConversion(ADC1); adc_buffer_for_mosfet_test[127] = DMA1_Channel1->CNDTR; if(last_step == mosfet_test_step){ if(mosfet_test_step == 1){ adc_buffer_for_mosfet_test[50] = adc_buffer_for_mosfet_test[0]; adc_buffer_for_mosfet_test[51] = adc_buffer_for_mosfet_test[1]; adc_buffer_for_mosfet_test[52] = adc_buffer_for_mosfet_test[2]; }else{ adc_buffer_for_mosfet_test[100] = adc_buffer_for_mosfet_test[0]; adc_buffer_for_mosfet_test[101] = adc_buffer_for_mosfet_test[1]; adc_buffer_for_mosfet_test[102] = adc_buffer_for_mosfet_test[2]; } } last_step = mosfet_test_step; TP3_RESET; }
/** * @brief analogRead Read GPIO Arduino pin value * @param pin number * @retval 0 to max STM32 ADC value */ uint16_t analogRead(uint16_t pin){ uint16_t L_oldType = ArduinoPort[pin].PinMode; //Does the pin already configured? uint32_t L_adcChannel; uint16_t L_ADCConvertedValue=0; /* Check the parameters */ assert_param(IS_ADC_PIN(pin)); switch(pin){ //pin to configure or to get value from case A0: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; L_adcChannel = ADC_Channel_10; break; case A1: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; L_adcChannel = ADC_Channel_11; break; case A2: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; L_adcChannel = ADC_Channel_12; break; case A3: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; L_adcChannel = ADC_Channel_13; break; case A4: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; L_adcChannel = ADC_Channel_14; break; case A5: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; L_adcChannel = ADC_Channel_15; break; default: GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; L_adcChannel = ADC_Channel_10; break; } if(L_oldType != INPUT_AN) { //Pin was not correctly configured! Do it first! //ADC Config RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; /* GPIOC config for ADC */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //Config pin as AN GPIO_Init(GPIOC, &GPIO_InitStructure); //Config ADC ADC_Init(ADC1, &ADC_InitStructure); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); //Pin is configured as ADC INPUT now. ArduinoPort[pin].PinMode = INPUT_AN; } //Select channel to scan ADC_ChannelConfig(ADC1, L_adcChannel , ADC_SampleTime_239_5Cycles); // ADC1 regular Software Start Conv ADC_StartOfConversion(ADC1); // Wait for end of conversion while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Get value L_ADCConvertedValue = ADC_GetConversionValue(ADC1); //Stop conversion and return ADC value. ADC_StopOfConversion(ADC1); return(L_ADCConvertedValue); }