Skip to main content
ncolincoli
Associate
September 21, 2013
Question

Hi All I needed an urgent help on Timer Interrupt. Thanksc STM32F407VG

  • September 21, 2013
  • 10 replies
  • 1928 views
Posted on September 21, 2013 at 03:40

Hi all I have a project (@my uni) due in late November and i am really stretching all possible time to see something going.

Could Someone please help me as to why this below code ISR only happens once?

Thus the Orange LED on  Pin 13 flashes One tenth of a sec but the Blue one on Pin 15 which is meant to be flashing every sec isn't (ie: Stay on) i have been carefully following the code and the problem seems to be in the ISR itself, ie: it only happens once even though

(

TIM2->

EGR

|= TIM_EGR_UG

) is meant to be updating the flag every time the Counter reaches ARR value. I really don't seems to see why it isn't working can someone help me?

Many Thanks again

Regards

Entropy

/*

Timer ISR running on  STM32F407VG with COIDE

Please ignore the comments were meant for other development code. Thanks

*/

#include<stdint.h>

#include<stdlib.h>

#include <stdbool.h>

#include''stm32f4xx.h''

#include''stm32f4xx_rcc.h''

#include''stm32f4xx_tim.h''

#include''stm32f4xx_gpio.h''

#include''core_cm4.h''

 

volatile bool Process_Task = false;

 

void _delay_us(uint32_t us);

void _delay_ms(uint32_t ms);

void

TIM2_IRQHandler(

void

);

int main()

{

SystemInit();

 

GPIO_InitTypeDef

GPIO_InitStructure; GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,

ENABLE

);

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3,

ENABLE

);

GPIO_InitStructure.

GPIO_Pin

= GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;

 GPIO_InitStructure.

GPIO_Mode

=

GPIO_Mode_OUT

;

 GPIO_InitStructure.

GPIO_OType

=

GPIO_OType_PP

;

GPIO_InitStructure.

GPIO_Speed

=

GPIO_Speed_100MHz

;

 GPIO_InitStructure.

GPIO_PuPd

=

GPIO_PuPd_NOPULL

;

 GPIO_Init(GPIOD, &GPIO_InitStructure);

NVIC_InitTypeDef

NVIC_InitStructure;

/* Enable the TIM2

gloabal

Interrupt */  NVIC_InitStructure.

NVIC_IRQChannel

=

TIM2_IRQn

;

NVIC_InitStructure.

NVIC_IRQChannelPreemptionPriority

= 0;

 NVIC_InitStructure.

NVIC_IRQChannelSubPriority

= 1;

NVIC_InitStructure.

NVIC_IRQChannelCmd

=

ENABLE

;

 NVIC_Init(&NVIC_InitStructure);

/* TIM2 clock enable */

TIM2->

PSC

= 41999;

 TIM2->

ARR

= 3999;

 TIM2->

CNT

= 0;

 TIM2->

EGR

|= TIM_EGR_UG;

 TIM2->

SR

&= ~ TIM_SR_UIF;

TIM2->

CR1

|= (TIM_CR1_OPM);

// Clear the update flag

TIM2->

DIER

|= TIM_DIER_UIE;

// Enable interrupt on update event

NVIC_EnableIRQ(

TIM2_IRQn

);

// Enable TIM6 IRQ

TIM2->

CR1

|= TIM_CR1_CEN; // Enable TIM6 counter

//while (!(TIM2->SR & TIM_SR_UIF));

 

while

(1)

{

GPIOD->

ODR

^= 1<<13;

_delay_ms(100);

}

}

void

TIM2_IRQHandler(

void

)

 {

if

(TIM2->

SR

& (TIM_SR_UIF)) 

 

// If update flag is set

 

{ {

GPIOD->

ODR

^= 1<<15; TIM2->

SR

&= ~TIM_SR_UIF;

 //Clear it.

//Process_Task = true; // Set D15 high

 // GPIOD->ODR |= 1<<15; // Interrupt has been handled

//_delay_ms(15);

 }

//delay_ms(15);

}

void _delay_us(uint32_t us)

{

TIM3->

PSC

= 83 ;

 

 

TIM3->

ARR

= us -1

TIM3->

CNT

= 0;

TIM3->

EGR

|= TIM_EGR_UG;

TIM3->

SR

&= ~ TIM_SR_UIF;

TIM3->

CR1

|= (TIM_CR1_OPM);

// Clear the update flag

TIM3->

CR1

|= TIM_CR1_CEN;

while

(!(TIM3->

SR

& TIM_SR_UIF));

}

void _delay_ms(uint32_t ms)

{

while

( ms -- > 0) {

_delay_us(1000);

}

}

    This topic has been closed for replies.

    10 replies

    Tesla DeLorean
    Guru
    September 21, 2013
    Posted on September 21, 2013 at 05:26

    I'm not interest in debugging register level code however

    http://www.catb.org/esr/faqs/smart-questions.html#urgent

    it may be.

    You mustn't RMW the TIM SR registers :

    TIM2->SR &= ~TIM_SR_UIF; //Clear it.

    but rather

    TIM2->SR = ~TIM_SR_UIF; // etc

    Implement what you want to do with library code, if that works then you can get creative.

    If you want to spin for time, then just use a fast free running timer and compute a delta for CNT over the period you wish to dwell. Don't faff about with OPM
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    ncolincoli
    Associate
    September 22, 2013
    Posted on September 22, 2013 at 12:26

    I am sorry for messing up with registers but i still have that kind of AVR's habit ;) BTW Thank you for your Assistance, I am going to slightly change that line of clearing the flag by ''TIM2->SR = ~TIM_SR_UIF;''  and hopefully i will see the burger ;)

    ncolincoli
    Associate
    September 23, 2013
    Posted on September 23, 2013 at 18:10

    Hi Clive, Couldn't you advise if it's safe to hook up a Hex 2x16 LCD onto Stm32f407VG Discovery Board? I am hopping to power up the LCD separately but ofcourse connect the data lines to the LCD.

    Also as the board has 5V does it mean it's a 5 V source which for example I can connect to

    the LCD in this case? I would like to be very careful as I have one board and if I broke it ordering  the other would takes few weeks to get to me which is a pain as the I didn't have much time.

    Many Thanks

    Entropy.

    Tesla DeLorean
    Guru
    September 23, 2013
    Posted on September 23, 2013 at 18:39

    The 5V typically comes from the USB supply, the current budget is limited. Probably sufficient for an LCD, but check the specs.

    There are also displays that run from 3.3V, although the Discovery boards run a 3V, and I'm not sure of the current budget on that regulator.

    http://www.keil.com/mcbstm32/mcbstm32-schematics.pdf

    Should definitely order parts you might fry in Qty >2

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    ncolincoli
    Associate
    September 24, 2013
    Posted on September 24, 2013 at 13:07

    The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6d4&d=%2Fa%2F0X0000000bs4%2FG8fY7_6RlknmIaMrfdnY6jUS.EsKsGOxI8U15HioYIs&asPdf=false
    Tesla DeLorean
    Guru
    September 24, 2013
    Posted on September 24, 2013 at 18:04

    That's a mess, edit the post and use the ''Format Code Block'' icon (Paintbrush [<>])

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    ncolincoli
    Associate
    September 25, 2013
    Posted on September 25, 2013 at 19:18

    Hi again Clive.

    Hey I am sorry I tried to edit the indentation of the code but as I am using a clappy very outdated internet explorer the writing windows seems to fluctuate all the times while I am typing. anyway. I come to realize that the possible reason why the above code isn't working might be the result of not knowing how to handle Data Direction Register on Cortex,

    There is a function I created on AVR and it's like this:

    void Check_if_LCD_is_Busy()

    {

      DDRB = 0x00;

      PORTD |= 1<<RW;

      PORTD &= ~1<<RS;

      while(PORTB >= 0x80)

       {

        Set_LCD_up();

       }

      DDRB = 0xFF;

    }

    As I understand on Cortex we can directly also change the whole Port Data via ''MODER''

    Register but I am unsure!

    Could you please translate the above ''void Check_if_LCD_is_Busy(void)'' Function to suit the stm32f407?

    Many Thanks.

    That's a mess, edit the post and use the ''Format Code Block'' icon (Paintbrush [<>])

    Tesla DeLorean
    Guru
    September 26, 2013
    Posted on September 26, 2013 at 03:19

    The MODER bits for each pin need to be ''01'' for the output, and ''00'' for the input.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    ncolincoli
    Associate
    September 30, 2013
    Posted on September 30, 2013 at 11:53

    Hi again Clive,

    Hey can you help me to check that below syntax is correct?

    I have an AVR syntax that goes like this:  

    while(PORTB >= 0x80)

       {

        . . . . . .

       }

    // This obviously checks if Port B is ever greater than 128 then...

    Was above on a typical Stm32f407 equivalent to

    while

     

    ((GPIOA->

    IDR

    & (0x000000FF)) >= 0x00000080)

     

    {

     

         . . . . . .

     

    }

     

      ?

     

     

    if not can you help me to generate the correct Syntax?

     

     

    // Because ARM GPIOs are 32 bits I have masked it to only allow the last 8 bits to be read (Data)

     

    However the code still isn't working there seems to be many Bugs that I have to fix

     

     

     

    Thanks again.

     

     

    Cheers

     

     

    However the code still isn't working there seems to be many Bugs that I have to fixThanks again.Cheersclive1Thursday, September 26, 2013 3:19 AMHi All I needed an urgent help on Timer Interrupt. Thanksc {STM32F407VG}

    The MODER bits for each pin need to be ''01'' for the output, and ''00'' for the input.

    Tesla DeLorean
    Guru
    September 30, 2013
    Posted on September 30, 2013 at 15:20

    And what's that testing? Presumably a cheap way to test if a singular high order bit is high or low. Just test the bit directly.

    The connectivity in both cases is not sufficiently clearly stated to make much of a determination.

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..