STM32F103ZET6 之 ADC+TIM+DMA+USART 綜合實(shí)驗(yàn)
1、實(shí)驗(yàn)?zāi)康?/p>
1)使用 TIM1 觸發(fā) ADC,ADC 采集的數(shù)據(jù)通過DMA 傳至內(nèi)存,然后通過串口打印出采集的數(shù)據(jù);
2)學(xué)會 DMA 傳輸數(shù)據(jù)并將數(shù)據(jù)進(jìn)行保存;
3)驗(yàn)證ADC 的采樣率與實(shí)際設(shè)置的是否相符。
2、硬件資源
1)指示燈
2)ADC
3)DMA
4)TIM
5)杜邦線
3、軟件設(shè)計(jì)
voidAdc_Configuration(void)
{
DMA_InitTypeDefDMA_InitStructure;
ADC_InitTypeDefADC_InitStructure;
GPIO_InitTypeDefGPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*EnableDMA1clock*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_DeInit(DMA1_Channel1);/*復(fù)位DMA1寄存器到缺省狀態(tài)*/
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;/*選擇ADC1的數(shù)據(jù)寄存器作為源*/
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)&ADC_Sample.Ch1Buf;/*目標(biāo)地址*/
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;/*設(shè)置DMA傳輸方向,外設(shè)(ADC)作為源*/
DMA_InitStructure.DMA_BufferSize=SAMPLE_COUNT;/*設(shè)置緩沖區(qū)大小*/
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;/*外設(shè)地址不自增*/
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;/*存儲器地址需要自增*/
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;/*選擇外設(shè)傳輸單位:16bit*/
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;/*選擇內(nèi)存?zhèn)鬏攩挝唬?6bit*/
DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;/*無需循環(huán)模式*/
DMA_InitStructure.DMA_Priority=DMA_Priority_High;/*選擇DMA優(yōu)先級*/
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;/*DMA傳輸類型,不是內(nèi)存到內(nèi)存*/
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
/*EnableDMA1channel1*/
DMA_Cmd(DMA1_Channel1,ENABLE);
/*配置ADC1*/
/*ADCCLK=PCLK2/2*/
RCC_ADCCLKConfig(RCC_PCLK2_Div2);/*ADCCLK=PCLK2/2=36MHz*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode=DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;/*連續(xù)轉(zhuǎn)換靜止*/
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T1_CC3;/*選擇TIM1的CC3做觸發(fā)*/
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;/*數(shù)據(jù)右對齊,高位為0*/
ADC_InitStructure.ADC_NbrOfChannel=1;/*1個(gè)通道*/
ADC_Init(ADC1,&ADC_InitStructure);
/*ADC1regularchannelsconfiguration*/
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_1Cycles5);
/*EnableADC1externaltriggerconversion*/
ADC_ExternalTrigConvCmd(ADC1,ENABLE);
/*使能ADC1*/
ADC_Cmd(ADC1,ENABLE);
/*EnableADC1DMA*/
ADC_DMACmd(ADC1,ENABLE);
/*使能ADC1復(fù)位校準(zhǔn)寄存器*/
ADC_ResetCalibration(ADC1);
/*檢測復(fù)位校準(zhǔn)寄存器*/
while(ADC_GetResetCalibrationStatus(ADC1));
/*開始ADC1校準(zhǔn)*/
ADC_StartCalibration(ADC1);
/*等待校準(zhǔn)結(jié)束*/
while(ADC_GetCalibrationStatus(ADC1));
/*配置采樣觸發(fā)定時(shí)器,使用TIM1CC1*/
ADC_SampleRate(ADC_Sample.SampleFreq);/*修改采樣頻率*/
}
/*
*FunctionName:ADC_SampleRate
*Description:修改ADC的采樣頻率
*Input:-freq:采樣頻率單位Hz
*Output:None
*Return:None
*Attention:None
*/
voidADC_SampleRate(uint32_tfreq)
{
/*配置定時(shí)器1第3通道CC3作為ADC1的觸發(fā)源*/
uint16_tPeriod;
TIM_OCInitTypeDefTIM_OCInitStructure;
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_Cmd(TIM1,DISABLE);
/*采樣頻率計(jì)算公式:period=72000000/freq;1200==60KHz采樣頻率*/
Period=72000000/freq;
/*EnableTIM1clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period=Period;
TIM_TimeBaseStructure.TIM_Prescaler = 0; /* 計(jì)數(shù)頻率 = 72000 000 / 18 = 4000 000