#include <stdint.h>
#define RCC_Base 0x40021000
#define GPIOC_Base 0x50000800
#define TIM2_Base 0x40000000
//Pointer declarations
uint32_t *AHB1ENR = (uint32_t*)(RCC_Base + 0x34);
uint32_t *APB1ENR = (uint32_t*) (RCC_Base + 0x3C);
uint32_t *GPIOC_Moder = (uint32_t*) (GPIOC_Base + 0x00);
uint32_t *GPIOC_OutReg = (uint32_t*)(GPIOC_Base + 0x14);
uint32_t *TIM2_SR = (uint32_t*)(TIM2_Base + 0x10);
uint32_t *TIM2_PSC = (uint32_t*)(TIM2_Base + 0x28);
uint32_t *TIM2_ARR = (uint32_t*)(TIM2_Base + 0x2C);
uint32_t *TIM2_CNT = (uint32_t*)(TIM2_Base + 0x24);
uint32_t *TIM2_CR1 = (uint32_t*)(TIM2_Base + 0x00);
void setup()
{
GPIO_init();
TIM2_init();
*TIM2_CR1 |= (1 << 0); //Start TIM2
}
void loop()
{
*GPIOC_OutReg |= (1 << 6);
TIM2_delay();
*GPIOC_OutReg &= ~(1 << 6);
delay(1000);
//TIM2_delay();
}
//GPIO Initialization
void GPIO_init()
{
*AHB1ENR = (1 << 2); //Enabling clock to GPIOC
*APB1ENR = (1 << 0); //Enabling clock to TIM2
*GPIOC_Moder |= (1 << 12);
*GPIOC_Moder &= ~(1 << 13);
}
void TIM2_init()
{
*TIM2_PSC = 84000-1; //Pre-scalar value
*TIM2_ARR = 1000-1; //Auto reload value
*TIM2_CNT = 0; //Counter is initialized to ZERO
}
//Delay function
void TIM2_delay()
{
while (!(*TIM2_SR & (1 << 0))); //Wait for TIM2 overflow
*TIM2_SR &= ~(1 << 0); //Reset SR.0 to ZERO
}