stm32L151 tim trigger adc定時器觸發單通道adc取樣dma傳輸
“`
include “adc.h”
ifdef Debug
include “led.h”
include “string.h”
uint16_t tb=0;
//char dat[512]={0,};
uint8_t tc=0;
endif
__IO uint16_t ADC_ConvertedValue[40];
define ADC1_DR_ADDRESS ((uint32_t)0x40012458)
static void dma_adc_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/———————— 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 = 40;
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);
DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
DMA_ClearFlag(DMA1_FLAG_TC1);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void tim_trig_init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
TIM_TimeBaseStructure.TIM_ClockDivision=0;//
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Prescaler=320;//100k
TIM_TimeBaseStructure.TIM_Period=99;//1k
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//TIM_ITConfig(TIM2,TIM_IT_Trigger,ENABLE);
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Update);
TIM_Cmd(TIM2, ENABLE);
}
void adc_init(void)//tim觸發 1ms一個數據 dma資料搬運
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
dma_adc_init();
/* Enable the HSI oscillator */
RCC_HSICmd(ENABLE);
/* Check that HSI oscillator is ready */
while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* Enable ADC1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
//while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);
/* Configure PB.12 (ADC Channel18) in analog mode |GPIO_Pin_2|GPIO_Pin_3*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC1 configuration */
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_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_4Cycles);
//ADC_InjectedChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_4Cycles);
//ADC_InjectedChannelConfig(ADC1, ADC_Channel_13, 2, ADC_SampleTime_4Cycles);
//ADC_InjectedChannelConfig(ADC1, ADC_Channel_Vrefint, 3, ADC_SampleTime_4Cycles);
//ADC_ExternalTrigInjectedConvEdgeConfig(ADC1,ADC_ExternalTrigConvEdge_Rising);
//ADC_ExternalTrigInjectedConvConfig(ADC1,ADC_ExternalTrigInjecConv_T2_TRGO);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
//ADC_ExternalTrigConvCmd(ADC1, ENABLE);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1, ENABLE);
/* Wait until the ADC1 is ready */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET){}
ADC_SoftwareStartConv(ADC1);
tim_trig_init();
}
uint8_t check_dma_tc()
{
return tc;
}
void clear_dma_tc(){
tc=0;
}
uint16_t send2m(char *ptr,uint16_t d[])
{
char buf[10];
uint16_t l=0,i;
for(i=0;i<40-1;i++)
{
l=l+sprintf(buf,”%d,”,d[i]);
ptr=strcat(ptr,buf);
}
l=l+sprintf(buf,”%d”,d[39]);
ptr=strcat(ptr,buf);
return l;
}
void DMA1_Channel1_IRQHandler(void)
{
uint16_t i=0;
if(DMA_GetITStatus(DMA1_IT_TC1))
{
DMA_ClearFlag(DMA1_FLAG_TC1);
#ifdef Debug
tb++;
if(tb>5)
{
GPIO_ToggleBits(led_port,led_pin);
tb=0;
}
tc=1;
#endif
}
}
adc採集到50Hz訊號資料(疊加在直流上),傳送到PC,利用python對資料進行處理顯示:
“`