Skip to main content
Yadav
Associate III
June 16, 2018
Solved

STM32F4DISCOVERY Timers Compare Match Interrupt Issue

  • June 16, 2018
  • 11 replies
  • 2560 views
Posted on June 16, 2018 at 13:49

Dear All,

   I am working on STM32F4Doscovery board timers. I want to make a timer interrupt for an application. It can be 1sec or 500ms interrupt. I have read the datasheet and written the program accordingly. But i dont know how to use interrupt in STM32. I have tried something, but nothing is happening. Can someone suggest me a solution??

&sharpinclude 'stm32f4xx.h'

// Component selection

void GPIO_Init(void);

void tim10_init(void);

int main(void)

{

GPIO_Init();

tim10_init();

while (1)

{

GPIOD->ODR|=(1<<15);

}

}

void GPIO_Init(void)

{

RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN;

GPIOD->MODER|=(1<<28); // pin 14 is output

GPIOD->OTYPER&=~(1<<14);//pin 14 as push pull

GPIOD->OSPEEDR|=(1<<29)|(1<<28);//LOW speed

GPIOD->PUPDR&=~(1<<29)|(1<<28);//No push pull

GPIOD->MODER|=(1<<30); // pin 15 is output

GPIOD->OTYPER&=~(1<<15);//pin 15 as push pull

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

}

void tim10_init(void)

{

RCC->APB2ENR|=(1<<17);

TIM10->CR1|=(1<<7);

TIM10->DIER|=(1<<1);

TIM10->SR|=(1<<1);

TIM10->EGR|=(1<<1);

TIM10->CCMR1|=(1<<3)|(0<<1)|(0<<0);

TIM10->CCER|=(1<<0);

//TIM10->CCR1=5;

TIM10->PSC|=4200;

TIM10->ARR|=1000;

NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);

}

void TIM10_IRQHandler(void)

{

GPIOD->ODR|=(1<<14);

}

#stm32f4-d #learning-stm32
This topic has been closed for replies.
Best answer by waclawek.jan
Posted on June 24, 2018 at 10:34

You need to set a USARTx_TX pin to AF in MODER, and then select proper function in AFR (see alternative functions table in the Datasheet to your chip, this is not in the Reference Manual).

As I've already said, using the header-defined constants increase readibility, e.g.

while( !(USART2->SR & USART_SR_TC) );

JW

11 replies

waclawek.jan
Super User
June 16, 2018
Posted on June 16, 2018 at 14:12

Please don't post the same request into multiple threads.

void TIM10_IRQHandler(void)

There is probably no such. Look at the vector table in the startup file.

It's most probably

void TIM1_UP_TIM10_IRQHandler(void)

Also, you need to check whether the respective bit in TIM10->SR is set and clear it by writing 1 into it (don't use |= ).

Also, a stylistic recommendation: using constants defined in the CMSIS-mandated header instead of numeric constants may result in a more readable source.

JW

Yadav
YadavAuthor
Associate III
June 17, 2018
Posted on June 17, 2018 at 04:19

 ,

 ,

>,>,There is probably no such. Look at the vector table in the startup file.

>,>,It's most probably

>,>,void TIM1_UP_TIM10_IRQHandler(void)

Hi.. I tried it. Now timer interrupt is working well. but while loop is not executing. What would be the issue ?

If i comment this ->, NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn), while loop is executing. But if i enable it, interrupt is working but while loop is not.

♯ include 'stm32f4xx.h'

 ,

// Component selection

 ,

void GPIO_Init(void),

void tim10_init(void),

 ,

int main(void)

 ,

{

 ,

GPIO_Init(),

 ,

tim10_init(),

 ,

while (1)

 ,

{

GPIOD->,ODR|=(1<,<,15),

 ,

 ,

}

}

 ,

void GPIO_Init(void)

 ,

{

 ,

RCC->,AHB1ENR|=RCC_AHB1ENR_GPIODEN,

GPIOD->,MODER|=(1<,<,28), // pin 14 is output

 ,

GPIOD->,OTYPER&,=~(1<,<,14),//pin 14 as push pull

 ,

GPIOD->,OSPEEDR|=(1<,<,29)|(1<,<,28),//LOW speed

 ,

GPIOD->,PUPDR&,=~(1<,<,29)|(1<,<,28),//No push pull

GPIOD->,MODER|=(1<,<,30), // pin 15 is output

 ,

GPIOD->,OTYPER&,=~(1<,<,15),//pin 15 as push pull

 ,

GPIOD->,OSPEEDR|=(1<,<,30)|(1<,<,31),//LOW speed

 ,

GPIOD->,PUPDR&,=~(1<,<,30)|(1<,<,31),//No push pull

 ,

}

void tim10_init(void)

 ,

{

 ,

RCC->,APB2ENR|=(1<,<,17),

 ,

TIM10->,CR1|=(1<,<,7),

 ,

TIM10->,DIER|=(1<,<,1),

 ,

TIM10->,SR|=(1<,<,1),

 ,

TIM10->,EGR|=(1<,<,1),

 ,

TIM10->,CCMR1|=(1<,<,3)|(0<,<,1)|(0<,<,0),

 ,

TIM10->,CCER|=(1<,<,0),

 ,

//TIM10->,CCR1=5,

 ,

TIM10->,PSC|=4200,

 ,

TIM10->,ARR|=1000,

 ,

NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn),

 ,

}

 ,

void TIM1_UP_TIM10_IRQHandler (void)

 ,

{

 ,

GPIOD->,ODR|=(1<,<,14),

}

Yadav
YadavAuthor
Associate III
June 17, 2018
Posted on June 17, 2018 at 04:41

Hi.. Meanwhile i tried like this . Now both are working. Hope this way is correct ?

void TIM1_UP_TIM10_IRQHandler (void)

{

if(TIM10->SR & (1<<1))

{

GPIOD->ODR|=(1<<14);

}

TIM10->SR =0X00;

}
Yadav
YadavAuthor
Associate III
August 10, 2018

Dear All,

NowI have been stuck with the sd card interface for last 3 weeks. I have used cubemx version 4.26 for creating this project. But F_mount function is not working.Please find the below code. I have tried everything based on youtube videos but nothing working out. kindly help me out to solve this issue.

#include "main.h"

#include "stm32f4xx_hal.h"

#include "fatfs.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

SD_HandleTypeDef hsd;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

sta

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_SDIO_SD_Init();

 MX_FATFS_Init();

UINT testbyte;

HAL_Delay(10);

if(f_mount(&SDFatFS,SDPath,1)==FR_OK)

{

HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_12);

char mypath[]="WRITE1.TXT\0";

f_open(&SDFile,mypath,FA_WRITE|FA_CREATE_ALWAYS);

char *mydata="HELLO WORLD\0";

f_write(&SDFile,&mydata,sizeof(mydata),&testbyte);

f_close(&SDFile);

HAL_Delay(1000);

HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_13);

}

 while (1)

 {

 }

}

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct;

 RCC_ClkInitTypeDef RCC_ClkInitStruct;

 __HAL_RCC_PWR_CLK_ENABLE();

 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  /**Initializes the CPU, AHB and APB busses clocks 

  */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = 16;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

 RCC_OscInitStruct.PLL.PLLM = 8;

 RCC_OscInitStruct.PLL.PLLN = 50;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

 RCC_OscInitStruct.PLL.PLLQ = 7;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Initializes the CPU, AHB and APB busses clocks 

  */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|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_DIV8;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)

 {

  _Error_Handler(__FILE__, __LINE__);

 }

  /**Configure the Systick interrupt time 

  */

 HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  /**Configure the Systick 

  */

 HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

 /* SysTick_IRQn interrupt configuration */

 HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* SDIO init function */

static void MX_SDIO_SD_Init(void)

{

 hsd.Instance = SDIO;

 hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;

 hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;

 hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;

 hsd.Init.BusWide = SDIO_BUS_WIDE_1B;

 hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;

 hsd.Init.ClockDiv = 0;

}

/** Configure pins as 

    * Analog 

    * Input 

    * Output

    * EVENT_OUT

    * EXTI

*/

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct;

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOD_CLK_ENABLE();

 __HAL_RCC_GPIOC_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13, GPIO_PIN_RESET);

 /*Configure GPIO pins : PD12 PD13 */

 GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_NOPULL;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

void _Error_Handler(char *file, int line)

{

 /* USER CODE BEGIN Error_Handler_Debug */

 /* User can add his own implementation to report the HAL error return state */

 while(1)

 {

 }

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *     where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(uint8_t* file, uint32_t line)

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

   tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */