Microcontroller Tutorials

Arduino, Raspberry Pi, PIC, Beaglebone Resources

Forum

You need to log in to create posts and topics.

Issue with Timer code

/*After reset, the device is running from HSI (int high speed osci=16Mhz), There is no prescalars for AHB and APB busses, hance all peripherals connected to these buses are running at HSI(mentioned in hal_rcc doc) .*/

#include "stm32f4xx_hal.h"

#include "stm32f4xx.h"

#include "stm32f407xx.h"

#include "stdint.h"

#include "string.h"

#include "stm32f4xx_hal_rcc.h"

 

 

 

 

 

//NVIC_EnableIRQ(UART4_IRQn); // Enable IRQ for UART4 in NVIC

void Timer_init()

{

RCC->APB1ENR|=(1UL<<2);//TIM4 connected to APB1, hence enable clk though this register

RCC->AHB1ENR |= 1; //clock to GPIOA

RCC->AHB1ENR|=(1UL<<2); // Enable GPIOC clock

TIM4->PSC=42000;// 42MHZ/42000=1KHZ, 1microsec between two ticks

TIM4->ARR=10000;//count will increment for every one sec

//TIM4->DIER|=0X0001;//update interupt enable;

TIM4->CR1=0X0000;//UPCOUNTER;

GPIOA->MODER=0X00000040;

GPIOA->OSPEEDR=0X00000040;

TIM4->CR1|=0x0001;// enable counter

}

 

 

//#########################Clock Configuration#################

void SystemClock_Config(void) {

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

 

/* Enable Power Control clock */

__HAL_RCC_PWR_CLK_ENABLE();

 

/* The voltage scaling allows optimizing the power consumption when the

device is clocked below the maximum system frequency (see datasheet). */

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 

/* Enable HSE Oscillator and activate PLL with HSE as source */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 25;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 7;

HAL_RCC_OscConfig(&RCC_OscInitStruct);

 

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2

clocks dividers */

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |

RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

}

//########################################################

 

 

int main (void)

{

SystemClock_Config();

Timer_init();

while(1)

{

if(TIM4->SR&0x0001)// CHECK UIF FLAG

{

TIM4->SR&=!(0X0001);//CLEAR UIF FLAG

GPIOA->ODR^=(1<<3);//TOGGLE PIN A3

/* while(!(TIM4->SR&0X0001));

if((GPIOA->ODR)&(1<<3))

GPIOA->BSRR=((1<<3)+16);

else

GPIOA->BSRR=(1<<3);*/

 

}

}

 

 

}

Quote from SAI PRATYUSHA on April 12, 2019, 9:21 am

/* I wote this code to light up an LED  connected to PA3 of my discovery board every sec, however there seems to be an issue with this code , so after i flash the code once, it gives a "Target not connected" error .*/

#include "stm32f4xx_hal.h"

#include "stm32f4xx.h"

#include "stm32f407xx.h"

#include "stdint.h"

#include "string.h"

#include "stm32f4xx_hal_rcc.h"

 

 

 

 

 

//NVIC_EnableIRQ(UART4_IRQn); // Enable IRQ for UART4 in NVIC

void Timer_init()

{

RCC->APB1ENR|=(1UL<<2);//TIM4 connected to APB1, hence enable clk though this register

RCC->AHB1ENR |= 1; //clock to GPIOA

RCC->AHB1ENR|=(1UL<<2); // Enable GPIOC clock

TIM4->PSC=42000;// 42MHZ/42000=1KHZ, 1microsec between two ticks

TIM4->ARR=10000;//count will increment for every one sec

//TIM4->DIER|=0X0001;//update interupt enable;

TIM4->CR1=0X0000;//UPCOUNTER;

GPIOA->MODER=0X00000040;

GPIOA->OSPEEDR=0X00000040;

TIM4->CR1|=0x0001;// enable counter

}

 

 

//#########################Clock Configuration#################

void SystemClock_Config(void) {

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

 

/* Enable Power Control clock */

__HAL_RCC_PWR_CLK_ENABLE();

 

/* The voltage scaling allows optimizing the power consumption when the

device is clocked below the maximum system frequency (see datasheet). */

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 

/* Enable HSE Oscillator and activate PLL with HSE as source */

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 25;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 7;

HAL_RCC_OscConfig(&RCC_OscInitStruct);

 

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2

clocks dividers */

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 |

RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

}

//########################################################

 

 

int main (void)

{

SystemClock_Config();

Timer_init();

while(1)

{

if(TIM4->SR&0x0001)// CHECK UIF FLAG

{

TIM4->SR&=!(0X0001);//CLEAR UIF FLAG

GPIOA->ODR^=(1<<3);//TOGGLE PIN A3

/* while(!(TIM4->SR&0X0001));

if((GPIOA->ODR)&(1<<3))

GPIOA->BSRR=((1<<3)+16);

else

GPIOA->BSRR=(1<<3);*/

 

}

}

 

 

}