Skip to content

Instantly share code, notes, and snippets.

@eleciawhite
Created November 1, 2018 19:20
Show Gist options
  • Save eleciawhite/5f648df2f9a36e935653cb9f8e55eba7 to your computer and use it in GitHub Desktop.
Save eleciawhite/5f648df2f9a36e935653cb9f8e55eba7 to your computer and use it in GitHub Desktop.
#include <stm32f0xx.h>
#include <stdint.h>
#include "../lib/generated_files/STM32F030R8_Init.h"
#define BIT_SET(PORT, BIT) (((PORT) -> BSRR) |= (1 << (BIT)))
#define BIT_RESET(PORT, BIT) (((PORT) -> BSRR) |= (1 << ((BIT) + 16)))
// Create a timer variable so it is easier to deal with in GDB
TIM_TypeDef * counter = TIM6;
// Returns the current value from the counter
static int16_t now()
{
return counter->CNT;
}
// Non-blocking counter that is used as a delay
void delay(volatile int16_t ticks)
{
// Sets the time that the timer will stop at
volatile int16_t deadline = ticks + now();
// Loop until the timer reaches the deadline
while ((int16_t)(deadline - now()) > 0);
;
return;
}
int main(void)
{
// Initialize pins
Init_STM32F030R8_GPIO(); // I have tested this line before, I know this works properly
// Initialize timer 6
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN; // Turn on timers clock
TIM6->PSC = 8000-1; // Set the prescaler to 8,000, making each tick 1 ms (if the clock is 8 MHz)
TIM6->ARR = 0xFFFF; // Set the max value to 0xFFFF
TIM6->CR1 |= TIM_CR1_CEN; // Enable the timer
// Blink the LED 5 times
for (int i = 0; i < 5; i++) {
BIT_SET(GPIOC, 1);
delay(1000);
BIT_RESET(GPIOC, 1);
delay(1000);
}
for (;;);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment