cancel
Showing results for 
Search instead for 
Did you mean: 

Delay function not working in following led blinking code?

SGuja
Associate

#include "stm32f0xx.h"

void Delay_mS(int mTime);

void TIM3_IRQHandler(void);

/* Private macro */

/* Private variables */

int myTick=0;

/* Private function prototypes */

/* Private functions */

/***/

/**===========================================================================

**

** Abstract: main program

**

**===========================================================================

*/

int main(void)

{

RCC->AHBENR |= RCC_AHBENR_GPIOCEN;

RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;

GPIOC->MODER |= (GPIO_MODER_MODER8_0);

GPIOC->MODER |= (GPIO_MODER_MODER7_0);

TIM3->PSC =500 ;        //

TIM3->ARR = 592;          // Auto reload value 1000

TIM3->CR1 |= (0U << 4U);   //up counting

TIM3->CR1 |= TIM_CR1_URS;

TIM3->DIER = TIM_DIER_UIE; // Enable update interrupt (timer level)

TIM3-> EGR |= TIM_EGR_UG; /* update counter */

//TIM3->CR1 = TIM_CR1_CEN;  // Enable timer

NVIC_EnableIRQ(TIM3_IRQn); // Enable interrupt from TIM3 (NVIC level)

while (1) {

//

   GPIOC->ODR |= (1U << 8U);

   Delay_mS(1000);

   GPIOC->ODR |= (0U << 8U);

   Delay_mS(1000);

}

}

//=============================================================================

// mSec Delay function

//=============================================================================

void Delay_mS(int mTime)

{

 TIM3->CR1 |= TIM_CR1_CEN;

 myTick=0;

 while(myTick < mTime);

 TIM3->CR1 &= (0x0U << (0U));

}

//=============================================================================

// TIM3 Interrupt Handler

//=============================================================================

void TIM3_IRQHandler(void)

{

   if(TIM3->SR & TIM_SR_UIF) // if UIF flag is set

    {

    TIM3->SR &= ~TIM_SR_UIF; // clear UIF flag

    myTick++;

   

   }

}

2 REPLIES 2

Make myTick volatile.

Also..

> TIM3->CR1 &= (0x0U << (0U));

what was that?? [Click Show More]

> TIM3->SR &= ~TIM_SR_UIF; // clear UIF flag

All the bits in TIM3->SR are rc_w0, you can just do TIM3->SR = ~TIM_SR_UIF; to clear the bit instead of doing read-modify-write.

Also, this isn't doing what was intended:

GPIOC->ODR |= (0U << 8U);

OR-ing a number with zero will result in same number.