/* * My first project * * It blinks LEDs like counting in binary, when the button is pressed, it reverses the order * * Build on top of libopenocm3, blackbox parts of code (setup()) copied from * libopencm3-examples/examples/stm32/f1/stm32vl-discovery/button project * * Open source licence, let's say Artistic Licence, although it doesn't matter, as it is local only */ #include #include #include //#define DELAY 8000000 // Should be a second, or maybe not (second iff 8MHz clk, 1/3second if 24MHz clk) // Too long!! #define DELAY 400000 // This is bad -- it's a value that was found to be OK #define STM32F1 1 // Needed for libopencm3 void setup (void) { // All the blackbox code rcc_clock_setup_in_hse_8mhz_out_24mhz(); rcc_periph_clock_enable(RCC_GPIOC); rcc_periph_clock_enable(RCC_GPIOA); gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8 | GPIO9); gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); } int main (void) { setup(); gpio_clear(GPIOC, GPIO8 | GPIO9); while (gpio_get(GPIOA, GPIO0) == 0); // Array of what leds should be on uint16_t states[4] = {0, GPIO9, GPIO8, GPIO9 | GPIO8}; enum {FORWARD, BACKWARD} direction = FORWARD; bool pressed = true; bool change = false; char state = 0; int timer; while (true) { timer = DELAY; change = false; repeat: if (/*Button pressed, but was not*/ gpio_get(GPIOA, GPIO0) && (pressed == false) ) { pressed = true; if (change == false) { direction = (direction == FORWARD ? BACKWARD : FORWARD); // Switch direction change = true; } } else if (/*Button not pressed*/gpio_get(GPIOA, GPIO0) == 0) { pressed = false; } timer--; if (timer > 0) goto repeat; state = (direction == FORWARD ? (state + 1)%4 : (/*state+4-1*/ state+3)%4 ); gpio_set(GPIOC, states[state]); gpio_clear(GPIOC, (GPIO8 | GPIO9) ^ states[state]); } return 0; }