Skip to content

Instantly share code, notes, and snippets.

@inkwisit
Last active June 18, 2017 18:06
Show Gist options
  • Save inkwisit/7e34ae23e44383c605f2110813289426 to your computer and use it in GitHub Desktop.
Save inkwisit/7e34ae23e44383c605f2110813289426 to your computer and use it in GitHub Desktop.
STM32_timer_and_interrupt
#include "stm32f10x.h"
//core peripherals of the CPU .. address taken from the data sheet
#define NVIC_ISER0 *((unsigned int*)0xE000E100)
int main()
{
//NVIC_InitTypeDef TIM2_INT;
//Reset and clock control : enabling the clock source for the timer2
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
//Reset and clock control : enabling the GPIO : PC13 for LED blinking.
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
//In output push-pull mode , 50MHz maximum operating frequency
GPIOC->CRH |= GPIO_CRH_MODE13_0|GPIO_CRH_MODE13_1;
//To stop the timer on arrival of breakpoint (optional)
//DBGMCU->CR |= DBGMCU_CR_DBG_TIM2_STOP;
//attempting to generate a clock of 1Hz
TIM2->PSC = 36000;
TIM2->ARR = 2000;
//other settings are default.
//counter is now enabled
TIM2->CR1 |= TIM_CR1_CEN;
//setting up the NVIC interrupt vector
//it can also be setup using NVIC structure (NVIC_InitTypeDef) provided in library
NVIC_ISER0 |= (1<<TIM2_IRQn);
//update interrupt enable
//no trigger event is used ..
TIM2->DIER |= TIM_DIER_UIE;
while(1);
//interrupt service routine definition is present in stm32f1xx_it.c
//interrupt service routine prototype is present in stm32f1xx_it.h
//interrupt service routine name is present in startup_stm32f10x_md.s
}
#include "stm32f1xx_it.h"
#include "stm32f10x.h"
uint32_t counter;
void TIM2_IRQHandler(void)
{
//clearing the update interrupt flag of the register
TIM2->SR &= ~TIM_SR_UIF;
static uint32_t counter = 0;
counter++;
if(counter%2==0)
{
GPIOC->BSRR = (1<<29);
}
else
{
GPIOC->BSRR = (1<<13);
}
}
#ifndef __STM32F1XX_IT_H
#define __STM32F1XX_IT_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
#define NVIC_ICPR0 *((unsigned int*)0xE000E280)
#define NVIC_ISER0 *((unsigned int*)0xE000E100)
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void EXTI15_10_IRQHandler(void);
//extra interrupt handler : added by subha sarkar
void TIM2_IRQHandler(void);
#ifdef __cplusplus
}
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment