You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.7 KiB
C
56 lines
1.7 KiB
C
9 years ago
|
/*
|
||
|
* 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 <libopencm3/stm32/rcc.h>
|
||
|
#include <libopencm3/stm32/gpio.h>
|
||
|
#include <stdbool.h>
|
||
|
|
||
|
#define DELAY 8000000 // Should be a second, or maybe not (second iff 8MHz clk, 1/3second if 24MHz clk)
|
||
|
#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);
|
||
|
}
|
||
|
|
||
|
void inline wait (int ticks) { // This works in libopenocm3 examples
|
||
|
int i;
|
||
|
for (i=0;i<ticks;i++) __asm__("nop");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
int main (void) {
|
||
|
setup();
|
||
|
|
||
|
// Array of what leds should be on
|
||
|
uint16_t states[4] = {0, GPIO9, GPIO8, GPIO9 | GPIO8};
|
||
|
enum {FORWARD, BACKWARD} direction = FORWARD;
|
||
|
bool pressed = false;
|
||
|
char state;
|
||
|
|
||
|
while (true) {
|
||
|
if (/*Button pressed, but was not*/ gpio_get(GPIOA, GPIO0) && (pressed == false) ) {
|
||
|
pressed = true;
|
||
|
direction = (direction == FORWARD ? BACKWARD : FORWARD); // Switch direction
|
||
|
} else /*Button not pressed, possibly released*/ {
|
||
|
pressed = false;
|
||
|
}
|
||
|
state = (direction == FORWARD ? (state + 1)%4 : (/*state+4-1*/ state+3)%4 );
|
||
|
|
||
|
gpio_set(GPIOC, states[state]);
|
||
|
wait(DELAY);
|
||
|
}
|
||
|
return 0;
|
||
|
}
|