The code works better

.gitignore was also added
master
LEdoian 9 years ago
parent 8ad81623c0
commit 3d3ecf6331

5
.gitignore vendored

@ -0,0 +1,5 @@
*.d
*.o
*.bin
*.elf
*.map

@ -13,7 +13,8 @@
#include <libopencm3/stm32/gpio.h> #include <libopencm3/stm32/gpio.h>
#include <stdbool.h> #include <stdbool.h>
#define DELAY 8000000 // Should be a second, or maybe not (second iff 8MHz clk, 1/3second if 24MHz clk) //#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 #define STM32F1 1 // Needed for libopencm3
void setup (void) { // All the blackbox code void setup (void) { // All the blackbox code
@ -24,12 +25,6 @@ void setup (void) { // All the blackbox code
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0); gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0);
} }
inline void wait (int ticks) { // This works in libopenocm3 examples
int i;
for (i=0;i<ticks;i++) __asm__("nop");
return;
}
int main (void) { int main (void) {
setup(); setup();
@ -40,21 +35,29 @@ int main (void) {
uint16_t states[4] = {0, GPIO9, GPIO8, GPIO9 | GPIO8}; uint16_t states[4] = {0, GPIO9, GPIO8, GPIO9 | GPIO8};
enum {FORWARD, BACKWARD} direction = FORWARD; enum {FORWARD, BACKWARD} direction = FORWARD;
bool pressed = true; bool pressed = true;
char state; bool change = false;
char state = 0;
int timer;
while (true) { while (true) {
timer = DELAY;
change = false;
repeat:
if (/*Button pressed, but was not*/ gpio_get(GPIOA, GPIO0) && (pressed == false) ) { if (/*Button pressed, but was not*/ gpio_get(GPIOA, GPIO0) && (pressed == false) ) {
pressed = true; pressed = true;
if (change == false) {
direction = (direction == FORWARD ? BACKWARD : FORWARD); // Switch direction direction = (direction == FORWARD ? BACKWARD : FORWARD); // Switch direction
} else /*Button not pressed, possibly released*/ { change = true;
}
} else if (/*Button not pressed*/gpio_get(GPIOA, GPIO0) == 0) {
pressed = false; pressed = false;
} }
timer--;
if (timer > 0) goto repeat;
state = (direction == FORWARD ? (state + 1)%4 : (/*state+4-1*/ state+3)%4 ); state = (direction == FORWARD ? (state + 1)%4 : (/*state+4-1*/ state+3)%4 );
gpio_set(GPIOC, states[state]); //FIXME: This doesn't work, as it does not write 0s to unmentioned pins!! gpio_set(GPIOC, states[state]);
gpio_clear(GPIOC, (GPIO8 | GPIO9) ^ states[state]); // Does this fix that? gpio_clear(GPIOC, (GPIO8 | GPIO9) ^ states[state]);
wait(DELAY);
} }
return 0; return 0;
} }

Loading…
Cancel
Save