Capture/Compare Input Mode is not being triggered by PD12 on STM32F407 Discovery - Page 1
Forum section: electronics / general

author: elefurtronik
author_rank: Regular Contributor
timestamp: May 18, 2023, 11:12:38 am
content: Hello,I am trying to set a pin with capture/compare input mode and I will get the trigger from the PD12 pin which we have a green LED.When I open the debugging mode, I see no change on the "timestamp" integer that I defined. I couldn't understand the reason.I also connect PD12 and PB5 with a jumper cable.First I tried to use the same timer but a different channel. When I saw it is not working(I was using the PD13 pin which we have the orange LED connected here). I decided to change the timer with an empty pin and not used time. However, this is not working, as well.I write bare-metal code here. Please help me to find my failure.Code File:time.cCode:#include "stm32f407xx.h"#define TIM2EN (1U<<0)#define TIM3EN (1U<<1)#define TIM4EN (1U<<2)#define TIM_CEN (1U<<0)#define TIM4_OC1M (1U<<4|1U<<5)#define TIM4_CC1E (1U<<0)#define TIM3_CC2E (1U<<4)#define TIM3_CC2S (1U<<8)#define RCC_GPIODEN (1U<<3)#define RCC_GPIOBEN (1U<<1)void tim2_1Hz_init(void){/*Enable the Timer clock*/RCC->APB1ENR |= TIM2EN;/*Set Prescaler PSC*/TIM2->PSC = 16000 - 1;/*Set Auto-Reload Register ARR*/TIM2->ARR = 1000 - 1;/*Set the counter value*/TIM2->CNT = 0; // As the default counter is upcounter/*Enable the Timer 2 counter*/TIM2->CR1 |= TIM_CEN;}void tim4_PD12_CCM_init(void){//LED GPIOD Clock ConfigurationRCC->AHB1ENR |= RCC_GPIODEN;//Set the PD12 as OUTPUTGPIOD->MODER &=~(1U<<24);GPIOD->MODER |= (1U<<25);/*AF2 Settings*/GPIOD->AFR |= (1U<<17);/*Enable the Timer clock*/RCC->APB1ENR |= TIM4EN;/*Set Prescaler PSC*/TIM4->PSC = 16000 - 1;/*Set Auto-Reload Register ARR*/TIM4->ARR = 1000 - 1;/*Set the CC mode as Toggle mode*/TIM4->CCMR1 |= TIM4_OC1M;/*Enable the CC output register*/TIM4->CCER |= TIM4_CC1E;/*Set the counter value*/TIM4->CNT = 0; // As the default counter is upcounter/*Enable the Timer 4 counter*/TIM4->CR1 |= TIM_CEN;}void tim3_PB5_CCM_init(void){//PB5 GPIOB Clock ConfigurationRCC->AHB1ENR |= RCC_GPIOBEN;//Set the PD12 as OUTPUTGPIOB->MODER &=~(1U<<10);GPIOB->MODER |= (1U<<11);/*AF2 Settings*/GPIOB->AFR |= (1U<<21);/*Enable the Timer clock*/RCC->APB1ENR |= TIM3EN;/*Set Prescaler PSC*/TIM3->PSC = 1600 - 1;/*Set the CC mode as Input mode*/TIM3->CCMR1 |= TIM3_CC2S;/*Enable the CC output register*/TIM3->CCER |= TIM3_CC2E;/*Enable the Timer 3 counter*/TIM3->CR1 |= TIM_CEN;}time.hCode:#ifndef TIME_H_#define TIME_H_void tim2_1Hz_init(void);void tim4_PD12_CCM_init(void);void tim3_PB5_CCM_init(void);#define TIM3_SR_CC2IF (1U<<2)#endif /* TIME_H_ */main.cCode:/*We'll connect PD12 pint to PB5 pin in this project.*//*When PD12 has the rising edge signal, PB5 will be triggered.*/#include <stdio.h>#include <stdint.h>#include "stm32f407xx.h"#include "uart.h"#include "adc.h"#include "systick.h"#include "time.h"int timestamp = 0;int main(void){tim4_PD12_CCM_init();tim3_PB5_CCM_init();while(1){while(!(TIM3->SR & TIM3_SR_CC2IF)){}timestamp = TIM3->CCR1;}}

author: wek
author_rank: Frequent Contributor
timestamp: May 18, 2023, 09:25:32 pm
content: Start with setting "timestamp" variable as volatile.LED blinks?If you enable pullup on PB5, and then connect PB5 to ground, does capture happen?In debugger, read out and check/post content of both TIM registers, and perhaps relevant GPIO registers.Consider using symbols defined in the CMSIS-mandated device header for things like registers bit definitions, instead of defining your own.JW

author: elefurtronik
author_rank: Regular Contributor
timestamp: May 18, 2023, 10:01:17 pm
content: Quote from: wek on May 18, 2023, 09:25:32 pmI trigger the input with LED signal. LED has toggle signal.what is CMSIS-mandated device headers?Thanks for your answer.

author: wek
author_rank: Frequent Contributor
timestamp: May 19, 2023, 08:24:35 am
content: In my posts, I mean every line I write.Have you already appliedvolatileto the variable you are observing? Did it make any difference?> I trigger the input with LED signal. LED has toggle signal.Does it mean that you see the LED blinking? Really slowly, like once a second?When reading GPIOB_IDR, do you see bit 5 changing as LED blinks?Read out and check/post the "receiving" timer registers' content.JWPS.> what is CMSIS-mandated device headers?The header which you include by#include "stm32f407xx.h"It contains for example#define RCC_APB1ENR_TIM2EN_Pos (0U)#define RCC_APB1ENR_TIM2EN_Msk (0x1U << RCC_APB1ENR_TIM2EN_Pos) /*!< 0x00000001 */#define RCC_APB1ENR_TIM2EN RCC_APB1ENR_TIM2EN_Mskso you don't need to define#define TIM2EN (1U<<0)instead, youRCC->APB1ENR |= RCC_APB1ENR_TIM2EN;