2020-03-23 07:22 PM
Hi everyone,
I'm trying to capture a PWM signal's duty cycle and period using a timer (TIM3 in this case) in PWM input mode.
However, when I run the following code, I see no updates in the CCR1 and CCR2 registers, which should have the values of my duty cycle and frequency respectively.
Any help would be appreciated!
/** register manipulation with the PWM Input
References:
manual RM0091 (the STM32 F0 manual),
STM32F042x4 STM32F042x6 Datasheet, and UM1956 (the nucleo board)
**/
#include <stm32f042x6.h>
#include <timerdrv.h>
void TIM3_PWM_InputInit();
int main(){
TIM1_PWM_Init();
TIM3_PWM_InputInit();
while(1){
}
}
void TIM3_PWM_InputInit(){
// setting up GPIO for PWM input mode
RCC->AHBENR |= RCC_AHBENR_GPIOBEN
// set pin PB4 in alternate function mode
GPIOB->MODER |= GPIO_MODER_MODER4_1;
// set pin PB4 to AF1 for TIM1 CH1
GPIOB->AFR[0] |= (0x1UL << GPIO_AFRL_AFRL4_Pos);
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
//setting TIM3 for input capture
// with channel 1 mapping to TI1 (CC1S = 01)
// and channel 2 mapped to TI1 (CC2S = 10)
TIM3->CCMR1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_1;
// setting slave mode (SMS = 100)
// trigger input (TS = 101)
TIM3->SMCR |= TIM_SMCR_TS_0 | TIM_SMCR_TS_2 |TIM_SMCR_SMS_2;
// setting rising edge polarity for channel 1 (CC1P = 0, reset state and CC1NP = 0, reset value)
// setting falling edge polarity for channel 2 (CC2P = 1 and CC2NP = 0, reset value)
// enabling capture mode in channels 1 and 2 (CC1E = 1) and (CC2E = 1)
TIM3->CCER |= TIM_CCER_CC2P | TIM_CCER_CC1E | TIM_CCER_CC2E;
TIM3->CR1 |= TIM_CR1_CEN;
}
Solved! Go to Solution.
2020-03-23 08:28 PM
Update...figured it out. Was using the incorrect polarity with the PWM signal I was generating.
2020-03-23 08:28 PM
Update...figured it out. Was using the incorrect polarity with the PWM signal I was generating.