STM32复习题 下载本文

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;//电流电压取样 PA6是键盘

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS;//ADC1_BASE+0x4C

DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_Raw_Value; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 7*NPT;// 前三个是电流,中间是电压,最后一个是键盘

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_Disable; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_ITConfig(DMA2_Stream0,DMA_IT_TC,ENABLE); // DMA_Cmd(DMA2_Stream0, ENABLE);

//ADC1 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;// ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;//adcclk=30M

ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;

ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;

ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; //只用了一个通道

ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;

ADC_InitStructure.ADC_ExternalTrigConvEdge =ADC_ExternalTrigConvEdge_None;//使用TIM2中断软件触发

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 7; // ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_0 , 1, ADC_SampleTime_15Cycles ); //电流1

ADC_RegularChannelConfig(ADC1, ADC_Channel_1 , 2, ADC_SampleTime_15Cycles );//电流2

ADC_RegularChannelConfig(ADC1, ADC_Channel_2 , 3, ADC_SampleTime_15Cycles );//电流3

ADC_RegularChannelConfig(ADC1, ADC_Channel_3 , 4, ADC_SampleTime_15Cycles );//电压1

ADC_RegularChannelConfig(ADC1, ADC_Channel_4 , 5, ADC_SampleTime_15Cycles );//电压2

ADC_RegularChannelConfig(ADC1, ADC_Channel_5 , 6, ADC_SampleTime_15Cycles );//电压3

ADC_RegularChannelConfig(ADC1, ADC_Channel_6 , 7, ADC_SampleTime_15Cycles ); //键盘

//定时器软件触发是0.3125ms一次,ADCCLK 30M,12+15+5=497*7=3479Cycles/30M=0.116ms,一次采样需要0.116ms<0.3125ms,是可以的

/* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Wait until the ADC1 is ready */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_STRT) == RESET) { } }

1)该初始化程序实现的功能是。 2)该程序基于STM32F2,APB2设置为60MHz,ADCCLK最大不超过30MHz,该程序设置ADCCLK为MHz。

3)STM32F2一次ADC周期=12+采样周期+两次间隔,该程序对50Hz的电压电流进行采样,每个周期采样64点,试通过计算七个通道一次ADC所需要的时间(F2)是否符合要求。

4)参考手册推荐的ADC硬件电路如下图所示,由于ADC采集的时候需要有电流输入,因此在RAIN上会有一个压降,对于一次ADC来说,RAIN不能太大,试计算硬件电路上RAIN取值1K是否合适。

RAIN最大值计算公式

(式中K为采样周期,N为转换精度,fADC为30MHz,查手册知3.3V时CADC为4pF,RADC为1.5K欧,ln2=0.693147)