Skip to main content
Bicer
Associate II
July 24, 2021
Question

Timer interrupt stm32f103c8

  • July 24, 2021
  • 2 replies
  • 2523 views

How can I make PC 13 blink at a time I set using any timer in stm32f103 using interrupt

(without hal library) ?

This topic has been closed for replies.

2 replies

waclawek.jan
Super User
July 24, 2021
  • enable GPIOC clock in RCC
  • set PC13 as GPIO Output in GPIO
  • enable TIMx clock in RCC
  • set TIMx_ARR to the required period (if it's not sufficient, set also TIMx_PSC)
  • set TIMx_DIER.UIE to enable interrupts upon update (overflow)
  • enable timer by setting TIMx_CR1.CEN
  • enable TIMx interrupt by calling NVIC_EnableIRQ() with the appropriate interrupt number (see list of interrupt numbers in the CMSIS-mandated device header)
  • write an ISR with appropriate interrupt name (see vector table in startup code), in that ISR, check, if TIMx_SR.UIF is set, if yes, clear it by writing 0 into that bit and toggle the LED

JW

Bicer
BicerAuthor
Associate II
July 25, 2021

I tried to interrupt the pa3 pin but it didn't work. There is a problem with the interrupt function.

#include "stm32f10x.h" // Device header
 
int main(void)
{
	RCC->APB2ENR |= (1<<2); /// Enabling PORT A
	GPIOA->CRL &= 0xFFFF0FFF; /// Reset the pin 3
	GPIOA->CRL |= 0x00003000; /// Set PIN 3 as output
 
	
	__disable_irq();
	RCC->APB2ENR|=(1<<11);// timer1 active
	TIM2->ARR = 0xffff; // MAX ARR value
	TIM2->PSC = 72-1; // 72MHz/72 = 1 MHz ~~ 1 uS delay
	TIM2->DIER = 0x1;//enable interrupts upon update (overflow)
	TIM2->CR1 = 0x1; // enable timer by setting TIMx_CR1.CEN
	NVIC_EnableIRQ(TIM2_IRQn); // tim2_IRQHandler function
	__enable_irq();
	
	while(1)
	{
		
	}
	
}
 
void TIM2_IRQHandler()
{
	TIM2->SR =0x01;
	GPIOA->ODR ^=0x0008;//toggle pa3
}

waclawek.jan
Super User
July 25, 2021

> TIM2->SR =0x01;

No, you need to write 0 into TIMx_SR.UIF, i.e.

TIM2->SR =~0x01;

JW

Bicer
BicerAuthor
Associate II
July 25, 2021

Thanks ! pa3 pin can blink at exactly 10 hz. I also realized that I set RCC->APB1ENR wrong :p

#include "stm32f10x.h" // Device header
 
int main(void)
{
	RCC->APB2ENR |= (1<<2); /// Enabling PORT A
	GPIOA->CRL &= 0xFFFF0FFF; /// Reset the pin 3
	GPIOA->CRL |= 0x00003000; /// Set PIN 3 as output
 
	
	__disable_irq();
	RCC->APB1ENR|=(1<<0);// timer2 active
	TIM2->ARR = 0xffff; // MAX ARR value
	TIM2->PSC = 54; // 10hz frequency
	TIM2->DIER = 0x1;//enable interrupts upon update (overflow)
	TIM2->CR1 = 0x1; // enable timer by setting TIMx_CR1.CEN
	NVIC_EnableIRQ(TIM2_IRQn); // tim2_IRQHandler function
	__enable_irq();
	
	while(1)
	{
		
	}
	
}
 
void TIM2_IRQHandler()
{
	TIM2->SR =~0x01;//update interrupt flag
	GPIOA->ODR ^=0x0008;//toggle pa3
}