2018-08-28 08:42 AM
Can we take TIMER2, Channel3 for External clock mode1 for frequency counting upto 5-10 MHZ?
Board: STM32F401RE [Nucleo Board], PIN: PB_10 [Timer 2, channel3].
In case, yes.. Can anybody provide me refrence or sample code?
2018-08-29 05:31 AM
Hello @SRai.17 ,
In order to use the channel3 in your application, you should enable the XOR function, thus channels 1 and 2 shouldn't be used.
For more details, I advise you to refer to application note AN4776, section 2 "Timer clocking using external clock-source ".
Link:
Khouloud.
2018-08-29 08:28 AM
Hi Khouloud,
Thanks for your kind Information regards this. As per your suggestion, How to use XOR function for given below code:
RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOBEN);
GPIOB->AFR[1] &= 0xfffff0ff;
GPIOB->AFR[1] |= GPIO_AF1_TIM2 << 8;
GPIOB->MODER &= ~(GPIO_MODER_MODER10); // AF
GPIOB->MODER |= GPIO_MODER_MODER10_1;
// Initialize Timer2(32bit) for an external up counter mode
RCC->APB1ENR |= ((uint32_t)0x00000001);
TIM2->ARR = 0xFFFFFFFF;
TIM2->PSC = 0x0000;
// TIM2->SMCR |= (uint16_t)(TIM_TS_TI1FP1 | TIM_SMCR_SMS); // external mode 1 ---- How to set SMCR and Xoring of channel1 and channel 2 for channel3??? KIndly reply on this ..........
TIM2->CR1 |= TIM_CR1_CEN; //Enable the TIM Counter
printf("DEBUG: PB_10 is selected \r\n");
And I read TIM2->CNT after an interval of gate time that is 1 second.
Further, if you have any sample code for TIMer2, channel3 [PB_10] for baremetal, forward the sample or refrence.
...
Thanks
2018-08-29 08:58 AM
Hi @SRai.17 ,
Please have a look on pages 21 and 22 of the application note. All detais are well explained there.
Just note that in order to enable the XOR function, you have to set the bit TI1S (TIM2_CR2).
Khouloud.
2018-08-30 04:32 AM
I have been able to get frequency at PB_10 [Timer2, channel3]. with following configuration as you suggested to xoring the function: Well Support...!!!!
// PB_10 -> Counter frequency input pin as Timer2 TI3
RCC->AHB1ENR |= (RCC_AHB1ENR_GPIOBEN); //Enable clock on port_B
GPIOB->AFR[1] &= 0xfffff0ff;
GPIOB->AFR[1] |= GPIO_AF1_TIM2 << 8; //Set AFR for PB_10
GPIOB->MODER &= ~(GPIO_MODER_MODER10);
GPIOB->MODER |= GPIO_MODER_MODER10_1; //Set Mode for PB_10 as input to have external frequency
// Initialize Timer2(32bit) and channel3 for an external up counter mode
RCC->APB1ENR |= ((uint32_t)0x00000001); //Enable Clock on
TIM2->CR2 &= (uint16_t)(~(TIM_CR1_DIR | TIM_CR1_CMS | TIM_CR1_CKD)); // count_up + div by 1
TIM2->CR2 |= 0x0080; //Xoring of channel 3 to channel-1 for control register2
TIM2->ARR = 0xFFFFFFFF; //set the threshold value to register to rollover the timer
TIM2->PSC = 0x0000; //set prescale value
TIM2->SMCR |= (uint16_t)(TIM_TS_TI1FP1 | TIM_SMCR_SMS); // external mode 1
TIM2->CR1 |= TIM_CR1_CEN; //Enable the TIM Counter
and counting TIM2-> CNT after a moment ....and each time before having value I refresh value and again after an interval I read.