welcom ! Handel home

2014年7月1日 星期二

ST32 ADC demo Code 1



有關 ADC 啟動設定的兩個demo code

附說明備註

Ex1 採用 絕對值直接設定 不易閱讀 高效率

Ex2 採用 系統函數間接設定 為原廠範例程式易讀 





//the demo code for ADC1 on PC0 pin
void RCC_Configuration_Adc1(void)
{
//for ADC1 on PC0 using IN10
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);// enable GPIO_D Clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // enable ADC1 clock
}

void GPIO_Configuration_Adc1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);

//for ADC1 on PC0 using IN10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; // define the Pin to analog mode
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;//
GPIO_Init(GPIOC, &GPIO_InitStructure);
}

ADC_DeInit(); //de-initialises ALL the ADCs in the microcontroller

void ADC1_Configuration(void)
{
ADC_CommonInitTypeDef ADC_CommonInitStructure;
ADC_InitTypeDef ADC_InitStructure;

//common init defaults are mode = independent, prescaler = 2, DMA disabled, two sampling delay = 5
ADC_CommonStructInit(&ADC_CommonInitStructure);
//ADCCLK is common to all ADCs and generated from the APB2 clock divided by a prescaler /2, /4, /6 or /8.
//APB2 is HCLK/2 = 168 / 2 = 84MHz and ADC clock must be less than 30MHz so prescale by 4
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
ADC_CommonInit(&ADC_CommonInitStructure);

//adc init defaults are 12 bit, continuous = DISABLE, ExternalTrigConvEdge_None,
//ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1, Data aligned right, number of conversions = 1;
ADC_StructInit(&ADC_InitStructure);
ADC_Init(ADC1, &ADC_InitStructure);

ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);
ADC_EOCOnEachRegularChannelCmd(ADC1, ENABLE);

ADC_Cmd(ADC1, ENABLE); //The ADC is powered on by setting the ADON bit in the ADC_CR2 register.
//When the ADON bit is set for the first time, it wakes up the ADC from the Power-down mode.
}

//in main ...
while(1)
{
uint16_t ADCResult = 0x1234;

ADC_SoftwareStartConv(ADC1);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
ADCResult = ADC_GetConversionValue(ADC1);
}

沒有留言: