cancel
Showing results for 
Search instead for 
Did you mean: 

what is Keil builtin Function/command to set the Prescaler for stm32f103 ?

ALE1
Associate II

reading the reference manual of stm32f103, i came to know that ADC prescaler is set using RCC_CFGR in keil.

hence in keil

xxxxxxxxxxxxxxxxxxx=RCC_CFGR_ADCPRE_DIV2;

i dont know what to put at the place of xxxxxxx in Keil ?

as in case of stm32f4 i know

ADC_CommonInitStructure.ADC_Prescaler    = ADC_Prescaler_Div2;

but it does not work for stm32f103

can you help me please regarding this.

thanks

6 REPLIES 6
Chris1
Senior III

ADC clock configuration is separate from other ADC configuration for F103.

The SPL had this function:

/**

 * @brief Configures the ADC clock (ADCCLK).

 * @param RCC_PCLK2: defines the ADC clock divider. This clock is derived from

 *  the APB2 clock (PCLK2).

 *  This parameter can be one of the following values:

 *    @arg RCC_PCLK2_Div2: ADC clock = PCLK2/2

 *    @arg RCC_PCLK2_Div4: ADC clock = PCLK2/4

 *    @arg RCC_PCLK2_Div6: ADC clock = PCLK2/6

 *    @arg RCC_PCLK2_Div8: ADC clock = PCLK2/8

 * @retval None

 */

void RCC_ADCCLKConfig(uint32_t RCC_PCLK2)

{

 uint32_t tmpreg = 0;

 /* Check the parameters */

 assert_param(IS_RCC_ADCCLK(RCC_PCLK2));

 tmpreg = RCC->CFGR;

 /* Clear ADCPRE[1:0] bits */

 tmpreg &= CFGR_ADCPRE_Reset_Mask;

 /* Set ADCPRE[1:0] bits according to RCC_PCLK2 value */

 tmpreg |= RCC_PCLK2;

 /* Store the new value */

 RCC->CFGR = tmpreg;

}

ALE1
Associate II

thanks for your reply,

i write you code below, the last 2 lines give me error, the commands that you told me RCC_PCLK2_Div2: ADC clock = PCLK2/2 is not working in this way.... the way i am writing the code,

i hope you can understand

#include <stdio.h>

#include "stm32f10x.h"         // Device header

#include "GPIO_STM32F10x.h"       // Keil::Device:GPIO

#include "stm32f10x_adc.h"       // Keil::Device:StdPeriph Drivers:ADC

#include "stm32f10x_rcc.h"       // Keil::Device:StdPeriph Drivers:RCC

#include "stm32f10x_gpio.h"       // Keil::Device:StdPeriph Drivers:GPIO

void ADC_Configuration(void);

GPIO_InitTypeDef    GPIO_InitStructure; 

ADC_InitTypeDef     ADC_InitStructure; 

int main () {          

unsigned int j;

//----------------------------------------------------------------------------------------

// Software triggered, dual, simultaneous sampling                    

//----------------------------------------------------------------------------------------

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); //Enabling ADC1 and ADC2 clock

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //Enabling GPIOA and GPIOB Clock

  

 GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_1 | GPIO_Pin_2; //Analog voltages to be measured are connected to pins 1 and 2 of port A, 

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Analog input mode for ADC

 GPIO_Init(GPIOA, &GPIO_InitStructure); // calls the function to actually configure the port A.

  

 //ADC_CommonInitStructure.ADC_DMAAccessMode  = ADC_DMAAccessMode_Disabled;

 //ADC_CommonInitStructure.ADC_Mode       = ADC_DualMode_RegSimult;

ADC_InitStructure.ADC_Mode=ADC_CR1_SCAN;

RCC_PCLK2_Div2:ADCclock = PCLK2/2; // ----->>> do not accept colon

RCC_PCLK2_Div2 = RCC_CFGR_ADCPRE_DIV2;

thanks for your reply

i think you are using '' Pro*C Embedded SQL '' for your code. am i right ?

i am not familiar with it, i have posted my code written in embedded C below in another post, if you get an idea please reply me.

Chris1
Senior III

You could just use the function, for example:

RCC_ADCCLKConfig(RCC_PCLK2_Div2); // set ADC clock to PCLK2 / 2

I'm not familiar with "Pro*C Embedded SQL", my project is just using embedded C and the ST standard peripheral library, which it appears you are also using.

thanks again,

it's okay that if i use for prescaler

RCC_ADCCLKConfig(RCC_PCLK2_Div2); // set ADC clock to PCLK2 / 2

but there is another function in Keil like ''RCC_CFGR_ADCPRE_DIV2''

can you please tell me , how are they related to each other, for example in which case i can use

RCC_CFGR_ADCPRE_DIV2

To change the ADC clock divider, the appropriate bits of RCC->CFGR need to be set to the desired value, that's what RCC_ADCCLKConfig() does.

As you can see from the definitions, RCC_CFGR_ADCPRE_DIVx is equivalent to RCC_PCLK2_Divx; you could use either (many people would prefer to use function arguments as the function documentation instructs, but the hardware only cares about numbers). 

#define RCC_CFGR_ADCPRE_DIV2   ((uint32_t)0x00000000) /*!< PCLK2 / 2 */

#define RCC_CFGR_ADCPRE_DIV4  ((uint32_t)0x00004000)  /*!< PCLK2 / 4 */

#define RCC_CFGR_ADCPRE_DIV6   ((uint32_t)0x00008000) /*!< PCLK2 / 6 */

#define RCC_CFGR_ADCPRE_DIV8   ((uint32_t)0x0000C000) /*!< PCLK2 / 8 */

#define RCC_PCLK2_Div2   ((uint32_t)0x00000000)

#define RCC_PCLK2_Div4   ((uint32_t)0x00004000)

#define RCC_PCLK2_Div6   ((uint32_t)0x00008000)

#define RCC_PCLK2_Div8   ((uint32_t)0x0000C000)