cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupt stm32f103c8

Bicer
Associate II

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

(without hal library) ?

4 REPLIES 4
  • 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

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
}

> TIM2->SR =0x01;

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

TIM2->SR =~0x01;

JW

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
}