cancel
Showing results for 
Search instead for 
Did you mean: 

What to change when changing from TIM2 to TIM5?

TrixCZE
Associate II

Hello, I would like to change TIM2 to TIM5 but my code didn't work after I changed that. Could someone help me please? Thanks a lot

Here is the code

#include "stm32f4xx.h"
/*
PA0 - TIM2_CH1_ETR/ TIM5_CH1/ TIM8_ETR/ USER BUTTON
PA1 - TIM5_CH2/ TIMM2_CH2/
*/
void _delay_us(uint32_t n) { for(volatile uint32_t dly = 0; dly < n; dly++); }
int main (void){
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; //CLKen D
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN; //CLKen A
 
GPIOD->MODER |= GPIO_MODER_MODER13_0; //port D.13 výstup oranzova LED
GPIOD->MODER |= GPIO_MODER_MODER12_0; //port D.12 výstup zelena LED
GPIOA->MODER |= GPIO_MODER_MODER1_1; //PA1 alternative mode
GPIOA->MODER |= GPIO_MODER_MODER0_1; //PA0 alternative mode
//RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
//str 244 //Clock for Timer3
RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
//str 244 //Clock for Timer2
//zdroj pulzu str 617 SLAVE MODE CONTROLLER - ten modry vpravo
TIM5->SMCR |= 0x0007; // SMS(Slave mode selection): Ext. clk mode 1 = Rising edges of
//the selected trigger (TRGI) clock the counter
//zdroj pulzu str 617 - ten modry vlevo pred SLAVE MODE CONTROLLER
//Varianta Ext. Pin
//TIM2->SMCR |= 0x0060; // TS(Trigger selection): 110b: Filtered Timer
//Input 2 (TI2FP2) as ext. clock
//Varianta UserButt
TIM5->SMCR |= 0x0050; // TS(Trigger selection): 101b: Filtered Timer Input
//1 (TI1FP1) as ext. clock
//AFR[0] = AFRL v dokumentaci str. 286
//typy alternativnich funkcí a multiplexing na str 273
GPIOA->AFR[0] |= 0x00000010; // select AF1 (TIM2) for PA01 -> TIM2_CH2
GPIOA->AFR[0] |= 0x00000001; // select AF1 (TIM2) for PA00 -> TIM2_CH1
//Zdroj pulzu PRED RUZOVYM BLOKEM str 622
//Varianta Ext. Pin
//TIM2->CCMR1 |= 0x0100; // Ch. 2 as TI2 01b: CC2 channel is
//configured as input, IC2 is mapped on TI2
//Varianta UserButt
TIM5->CCMR1 |= 0x0200; // Ch. 2 as TI2 10b: CC2 channel is
//configured as input, IC2 is mapped on TI1
TIM5->CNT=0;
TIM5->CR1 |= 0x0001; // enable counting
while (1) {
_delay_us(500000);
GPIOD->ODR ^= (1<<12); //prepni LED
if(TIM5->CNT>=3){
GPIOD->ODR ^= (1<<13); //prepni LED
TIM5->CNT = 0; // reset counter
 
}
};
} //main

4 REPLIES 4

> didn't work

What are the symptoms and how are they different from the expectations?

You probably want to change the AFR of PA0/PA1 to 2 for TIM5.

JW

Hi, when I run this program with TIM2 it worked, but when I tried to switch TIM5 it stopped working. I would like to know what i have to change in program for what so it would start working, i am very new to programming and don't know what to change.

Can you please tell me what value I need to set for AFR ? Because i was trying to change it from documentation and i don't get it how to change it :(

Probably helps not to blindly OR single bits in to a block using four bits

AFR[0] deals with the pins 0 thru 7

AFR[1] deals with the pins 8 thru 15

For AF2 on PA0/PA1

GPIOA->AFR[0] = (GPIOA->AFR[0] & 0xFFFFFF00) | 0x00000022;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..