本帖最后由 yklstudent 于 2020-1-1 11:33 编辑
用LL库吧,HAL库一直不习惯用
#include "driver_adc.h"
//ADC1+DMA读取缓存器
uint16_t g_ADC_ConvertedValue[cADC_MAX_CHANNELS];
void sADC_Configuration(void)
{
LL_GPIO_InitTypeDef GPIO_InitStructure;
LL_ADC_InitTypeDef ADC_InitStructure;
LL_ADC_REG_InitTypeDef ADC_REG_InitStructure;
LL_DMA_InitTypeDef DMA_InitStructure;
LL_TIM_InitTypeDef TIM_TimeStructInit;
LL_TIM_OC_InitTypeDef TIM_OC_InitStructInit;
/* Enable ADC1 Clock */
RCC->APBENR2 |= RCC_APBENR2_ADCEN;
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
/* Enable GPIO Clock */
RCC->IOPSMENR |= cADC1_CHANNEL00_CLK | cADC1_CHANNEL01_CLK | cADC1_CHANNEL02_CLK | cADC1_CHANNEL03_CLK /
cADC1_CHANNEL04_CLK | cADC1_CHANNEL05_CLK | cADC1_CHANNEL06_CLK | cADC1_CHANNEL07_CLK;
/*ADC12_Channel0*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL00_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL00_PORT, &GPIO_InitStructure);
/*ADC12_Channel1*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL01_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL01_PORT, &GPIO_InitStructure);
/*ADC12_Channel2*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL02_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL02_PORT, &GPIO_InitStructure);
/*ADC12_Channel3*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL03_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL03_PORT, &GPIO_InitStructure);
/*ADC12_Channel4*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL04_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL04_PORT, &GPIO_InitStructure);
/*ADC12_Channel5*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL05_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL05_PORT, &GPIO_InitStructure);
/*ADC12_Channel6*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL06_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL06_PORT, &GPIO_InitStructure);
/*ADC12_Channel7*/
GPIO_InitStructure.Mode = LL_GPIO_MODE_ANALOG;
GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Pin = cADC1_CHANNEL07_PIN;
GPIO_InitStructure.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStructure.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(cADC1_CHANNEL07_PORT, &GPIO_InitStructure);
LL_DMA_DeInit(DMA1, LL_DMA_CHANNEL_1);
DMA_InitStructure.PeriphOrM2MSrcAddress = cADC1_DR_Address;
DMA_InitStructure.MemoryOrM2MDstAddress = (uint32_t)&g_ADC_ConvertedValue[0];
DMA_InitStructure.Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
DMA_InitStructure.NbData = (uint32_t)cADC_MAX_CHANNELS;
DMA_InitStructure.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
DMA_InitStructure.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT;
DMA_InitStructure.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_HALFWORD;
DMA_InitStructure.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD;
DMA_InitStructure.Mode = LL_DMA_MODE_CIRCULAR;
DMA_InitStructure.PeriphRequest = LL_DMAMUX_REQ_ADC1;
DMA_InitStructure.Priority = LL_DMA_PRIORITY_HIGH;
LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &DMA_InitStructure);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1);
/* Set ADC group regular sequence: channel on the selected sequence rank. */
if (LL_ADC_IsEnabled(ADC1) == 0)
{
ADC_InitStructure.DataAlignment = LL_ADC_DATA_ALIGN_RIGHT;
ADC_InitStructure.LowPowerMode = LL_ADC_LP_MODE_NONE;
ADC_InitStructure.Clock = LL_ADC_CLOCK_SYNC_PCLK_DIV1;
ADC_InitStructure.Resolution = LL_ADC_RESOLUTION_12B;
LL_ADC_Init(ADC1, &ADC_InitStructure);
ADC_REG_InitStructure.TriggerSource = LL_ADC_REG_TRIG_EXT_TIM3_TRGO;
ADC_REG_InitStructure.SequencerLength = LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS;
ADC_REG_InitStructure.SequencerDiscont = LL_ADC_REG_SEQ_DISCONT_DISABLE;
ADC_REG_InitStructure.ContinuousMode = LL_ADC_REG_CONV_CONTINUOUS;
ADC_REG_InitStructure.DMATransfer = LL_ADC_REG_DMA_TRANSFER_NONE;
ADC_REG_InitStructure.Overrun = 0;
LL_ADC_REG_Init(ADC1, &ADC_REG_InitStructure);
/* Set ADC group regular sequencer length and scan direction */
//LL_ADC_REG_SetSequencerLength(ADC1, LL_ADC_REG_SEQ_SCAN_ENABLE_8RANKS);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_1, LL_ADC_CHANNEL_0);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_2, LL_ADC_CHANNEL_1);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_3, LL_ADC_CHANNEL_2);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_4, LL_ADC_CHANNEL_3);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_5, LL_ADC_CHANNEL_4);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_6, LL_ADC_CHANNEL_5);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_7, LL_ADC_CHANNEL_6);
LL_ADC_REG_SetSequencerRanks(ADC1, LL_ADC_REG_RANK_8, LL_ADC_CHANNEL_7);
}
if (LL_ADC_IsEnabled(ADC1) == 0)
{
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_0, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_1, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_2, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_3, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_4, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_5, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_6, LL_ADC_SAMPLINGTIME_COMMON_1);
LL_ADC_SetChannelSamplingTime(ADC1, LL_ADC_CHANNEL_7, LL_ADC_SAMPLINGTIME_COMMON_1);
}
/* Set ADC group regular conversion data transfer */
LL_ADC_REG_SetDMATransfer(ADC1, LL_ADC_REG_DMA_TRANSFER_UNLIMITED);
#if (cADCISR==1)
/* Enable JEOC interrupt */
ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
#endif
#if (cADCDMAISR==1)
/*Clear DMA1_FLAG_TC1*/
LL_DMA_ClearFlag_TC1(DMA1);
/*Enable DMA1_Channe1 Interrupt*/
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_1);
#endif
/* Enable ADC1 */
LL_ADC_Enable(ADC1);
/*ADC1 Calibration*/
LL_ADC_StartCalibration(ADC1);
while(LL_ADC_IsCalibrationOnGoing(ADC1));
/*Start ADC1 Software Conversion*/
LL_ADC_REG_StartConversion(ADC1);
/* Enable TIM3 Clock */
RCC->APBENR1 |= RCC_APBSMENR1_TIM3SMEN;
LL_TIM_StructInit(&TIM_TimeStructInit);
TIM_TimeStructInit.Autoreload = 50 - 1; /*50us*/
TIM_TimeStructInit.Prescaler = 72 - 1; /*1us*/
TIM_TimeStructInit.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
TIM_TimeStructInit.CounterMode = LL_TIM_COUNTERMODE_UP;
TIM_TimeStructInit.RepetitionCounter = 0;
LL_TIM_Init(TIM3, &TIM_TimeStructInit);
LL_TIM_OC_StructInit(&TIM_OC_InitStructInit);
TIM_OC_InitStructInit.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStructInit.OCState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStructInit.OCNState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStructInit.CompareValue = 25;
TIM_OC_InitStructInit.OCPolarity = LL_TIM_OCPOLARITY_LOW;
TIM_OC_InitStructInit.OCNPolarity = LL_TIM_OCPOLARITY_LOW;
TIM_OC_InitStructInit.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
TIM_OC_InitStructInit.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &TIM_OC_InitStructInit);
LL_TIM_EnableCounter(TIM3);
/* Enable the ADC1 gloabal Interrupt */
/*Enable the USART Interrupt*/
//NVIC_SetPriority(ADC1_2_IRQn, 2);
//NVIC_EnableIRQ(ADC1_2_IRQn);
/*Enable the USART DMA Interrupt*/
NVIC_SetPriority(DMA1_Channel1_IRQn, 2);
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}
#if (cADCISR==1)
uint8_t Index = 0;
uint32_t ADC_InjectedConvertedValueTab[32];
void sADC1_2_ISR(void)
{
/* Get injected channel11 converted value */
ADC_InjectedConvertedValueTab[Index++] = ADC_GetInjectedConversionValue(ADC1, ADC_InjectedChannel_1);
if(Index >= sizeof(ADC_InjectedConvertedValueTab)/sizeof(uint32_t))
{
Index = 0;
}
/* Clear ADC1 JEOC pending interrupt bit */
ADC_ClearITPendingBit(ADC1, ADC_IT_JEOC);
}
#endif
#if (cADCDMAISR==1)
/**
* @brief DMA transfer complete callback
* @NOTE This function is executed when the transfer complete interrupt
* is generated
* @retval None
*/
void AdcDmaTransferComplete_Callback()
{
}
/**
* @brief DMA half transfer callback
* @note This function is executed when the half transfer interrupt
* is generated
* @retval None
*/
void AdcDmaTransferHalf_Callback()
{
}
/**
* @brief DMA transfer error callback
* @note This function is executed when the transfer error interrupt
* is generated during DMA transfer
* @retval None
*/
void AdcDmaTransferError_Callback()
{
/* Error detected during DMA transfer */
}
void sDMA1_Channel1_ISR(void)
{
/* Check whether DMA transfer complete caused the DMA interruption */
if(LL_DMA_IsActiveFlag_TC1(DMA1) == 1)
{
/* Clear flag DMA transfer complete */
LL_DMA_ClearFlag_TC1(DMA1);
/* Call interruption treatment function */
AdcDmaTransferComplete_Callback();
}
/* Check whether DMA half transfer caused the DMA interruption */
if(LL_DMA_IsActiveFlag_HT1(DMA1) == 1)
{
/* Clear flag DMA half transfer */
LL_DMA_ClearFlag_HT1(DMA1);
/* Call interruption treatment function */
AdcDmaTransferHalf_Callback();
}
/* Check whether DMA transfer error caused the DMA interruption */
if(LL_DMA_IsActiveFlag_TE1(DMA1) == 1)
{
/* Clear flag DMA transfer error */
LL_DMA_ClearFlag_TE1(DMA1);
/* Call interruption treatment function */
AdcDmaTransferError_Callback();
}
}
#endif
|