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.
******************************************************************************
- /
- define ButtonPin 0b0001000000000000 // pin 12
- include “stm32f10x.h”
- include
- include
int Button(void)
{
if ((GPIOB->IDR & ButtonPin) == 0)
{
return(1);
}
else
{
return(0);
}
}
- include “stm32f10x.h”
- include
- 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
}
}