Loading...
 

System Workbench for STM32


Delays not working

Hey all,
I’m trying to alternately blink two leds on a STM32F103 on pins 13 and 14 of port C. In the while loop below, neither the myDelay() or myDelay2() functions have any effect - both LEDS turn on at once (perhaps blinking too fast to see them switching?) regardless of what delay time I specify. The commented-out section (switching the leds with a button press) works fine. Any help appreciated, thanks!

KatBar

code:

/**
******************************************************************************
* @file main.c
* @author KatBar
* @version V1.0
* @date 01-December-2018
* @brief Default main function.
******************************************************************************

  • /

  1. define ButtonPin 0b0001000000000000 // pin 12

  1. include “stm32f10x.h”
  2. include
  3. include


int Button(void)
{
if ((GPIOB->IDR & ButtonPin) == 0)
{
return(1);
}
else
{
return(0);
}
}

  1. include “stm32f10x.h”
  2. include
  3. include


static volatile uint32_t sysTickCount = 0;
volatile uint32_t i = 0; //volatile to avoid being optimized away

void myDelay(uint32_t nTime)
{
sysTickCount = nTime;
while(sysTickCount != 0);
}

void myDelay2(void)
{
for (i=0;i>5000000;i++) {}
}

void SysTick_Handler()
{
if (sysTickCount != 0) {
sysTickCount--;
}
}


int main(void)
{
/*
I2C_InitTypeDef i2c_struct;
I2C_StructInit(&i2c_struct);
I2C_Init(I2C1, &i2c_struct);
*/

//Enable clock on APB2 peripheral bus (ports) for button and LEDs
RCC->APB2ENR |= 0x0018; // enable clock on PORTC GPIO and PORTB GPIO
GPIOB->CRH = 0x00080000; // set PortB pin 12 as input, pullup
GPIOC->CRH = 0x02200000; // set PortC pins 13 and 14 as outputs
while(1)
{
/*
if (Button())
{
GPIOC->ODR |= 0b0010000000000000; // turn off PC13
GPIOC->ODR &= 0b1011111111111111; // turn on PC14 (active LOW)
}
else
{
GPIOC->ODR &= 0b1101111111111111; // turn on PC13 (active LOW)
GPIOC->ODR |= 0b0100000000000000; // turn off PC14
}
*/

// myDelay(500000);
myDelay2();
GPIOC->ODR |= 0b0010000000000000; // turn off PC13
GPIOC->ODR &= 0b1011111111111111; // turn on PC14
// myDelay(500000);
myDelay2();
GPIOC->ODR &= 0b1101111111111111; // turn on PC13}
GPIOC->ODR |= 0b0100000000000000; // turn off PC14

}

}

Hello,

In myDelay2, the error is obvious: you write your loop incorrectly: for (i=0;i>5000000;i++) {}
while it should be for (i=0;i != 5000000;i++) {} ; your loop is in fact never executed: i is initialized to 0 so it’s not greater than 5000000...

For myDelay1, I think the error is a bit more tricky: you probably never get out of myDelay as you don’t initialize the SysTick timer, so should not get any interrupt. Moreover, if you initialize the SysTick timer as usual, you will get interrupts every 10ms, meaning your delay count of 500000 means 5000 seconds: nearly 2 hours!

Note also that you do not initialize the LED outputs, so they are both in the same state (probably ON) at program start and as your delay is either too short or too long you don’t see them blinking...

To debug you can just set a breakpoint in your Delay functions or SysTick_Handler to check what happens.

Hope this helps,

Bernard (Ac6)

Thanks so much, Bernard - It works! Thank you.