cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4DISCOVERY Timers Compare Match Interrupt Issue

Yadav
Associate II
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
1 ACCEPTED SOLUTION

Accepted Solutions
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

View solution in original post

11 REPLIES 11
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

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),

}

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;

}
Posted on June 17, 2018 at 06:58

I think its wrong. Because if i increment a variable inside the interrupt, its not incrementing more than 2. 

Posted on June 17, 2018 at 08:45

I got the output. Problem was with i forgot to enable the CEN bit. Now everything working fine.. Thankz for your support. Will start work with UART.

Posted on June 17, 2018 at 15:47

Did you check the link I gave you in the other thread?

https://community.st.com/0D50X00009XkWGpSAN

And for enabling/disabling UART transmission, use TXE. With TC you will have 'glitches' at each start.

Posted on June 24, 2018 at 08:36

I am facing issue with USART.

I am sending a string through usart. But i could not transmit anything. I checked with many threads but couldnt get solution for register level programming.Kindly help me out.

&sharpinclude 'stm32f4xx.h'

&sharpinclude <stdlib.h>

&sharpinclude <math.h>

void GPIO_Init(void);

void UART2_Init();

void USART2_Transmit_String(volatile char *s);

void USART2_Trans_Data(unsigned char C);

unsigned int a,set_flag=0,delay;

void delay_ms(unsigned int wait)

{

uint32_t j;

// b=1000*wait;

for(j=0;j<=wait*1000;j++);

}

int main(void)

{

GPIO_Init();

UART2_Init();

while (1)

{

USART2_Transmit_String('HAI');

}

}

void GPIO_Init(void)

{

RCC->AHB1ENR|=(1<<3);

// GPIOD->MODER|=(1<<0)|(1<<4)|(1<<8)|(1<<10)|(1<<12)|(1<<14); //

// GPIOD->OTYPER&=0X00;// as push pull

// GPIOD->OSPEEDR|=(1<<1)|(1<<5)|(1<<9)|(1<<11)|(1<<13)|(1<<15);//High speed

// GPIOD->PUPDR&=0X00;//No push pull

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

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

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

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

RCC->AHB1ENR|=(1<<0);

GPIOA->MODER|=(1<<5)|(1<<7);

GPIOA->OTYPER&=0X00;// as push pull

GPIOA->OSPEEDR|=(1<<4)|(1<<6);//High speed

GPIOA->PUPDR&=(1<<4);//No push pull

// GPIOA->AFR[0]=(1<<8)|(1<<12);

}

void UART2_Init()

{

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

USART2->BRR|=8000000/9600;//

USART2->CR1|=(1<<13)|(1<<3);// transmitter enable, usart enable

//USART1->CR2|=(1<<11);

// USART2->GTPR|=(1<<1);//prescalar dev 2

// NVIC_EnableIRQ(USART1);

//

}

void USART2_Transmit_String(volatile char *s)

{

while(*s)

{

// wait until data register is empty

while( !(USART2->SR & 0x00000040) );

USART2->DR = *s++;

}

}
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

Posted on June 24, 2018 at 16:12

'

I am facing issue with USART.'

you need to read the datasheet more and comment more diligently on your code.

to get you started, look at the uart related files here:

https://github.com/dannyf00/My-MCU-Libraries---2nd-try/tree/master/STM32F1xx

I didn't implement interrupt-based transmission for the F1 chips but did for the F0 here:

https://github.com/dannyf00/My-MCU-Libraries---2nd-try/tree/master/STM32F0

the basic principles would apply and the code structure would be largely the same if you are to port the F0 code to F1.