aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-02-07 22:37:19 +0100
committerlonkaars <loek@pipeframe.xyz>2022-02-07 22:37:19 +0100
commite16e1fe32f61eac5f741d181facda712bf74cc69 (patch)
treeb09e8cbee3635ab06213f63b4c96945d260a8634
parentab6977f4685d0b88fc593273725a0db489c49a1e (diff)
light changes direction when button is down
-rw-r--r--src/main.c61
1 files changed, 43 insertions, 18 deletions
diff --git a/src/main.c b/src/main.c
index dea5b7a..a357387 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,12 +1,21 @@
#include "stm32f0xx.h"
-#define PINOUT_LED_1 3
-#define PINOUT_LED_2 5
-#define PINOUT_LED_3 4
-#define PINOUT_LED_4 6
+// GPIO A
+#define PINOUT_DISP_CLK (5)
+#define PINOUT_DISP_DIO (6)
+#define PINOUT_POT (7)
+
+// GPIO B
+#define PINOUT_LED_1 (3)
+#define PINOUT_LED_2 (5)
+#define PINOUT_LED_3 (4)
+#define PINOUT_LED_4 (6)
const unsigned short leds[] = {PINOUT_LED_1, PINOUT_LED_2, PINOUT_LED_3, PINOUT_LED_4};
+#define PINOUT_BTN (8)
+
+
/*
* This function configures the I/O-ports that are used by the I/O-shield. It
* uses register RCC_AHBENR to enable the clocks for the ports that are used,
@@ -14,18 +23,30 @@ const unsigned short leds[] = {PINOUT_LED_1, PINOUT_LED_2, PINOUT_LED_3, PINOUT_
* GPIOx_PUPDR to configure pull-up and pull-down resistors.
*/
void shieldConfig() {
- // enable clock for I/O port D
+ // enable clock for I/O ports A and B
RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
+ // clear mode register configuration bits
GPIOB->MODER &= ~((0b11 << (PINOUT_LED_1 * 2)) |\
(0b11 << (PINOUT_LED_2 * 2)) |\
(0b11 << (PINOUT_LED_3 * 2)) |\
- (0b11 << (PINOUT_LED_4 * 2)));
+ (0b11 << (PINOUT_LED_4 * 2)) |\
+ (0b11 << (PINOUT_BTN * 2)));
+ // set output mode register configuration bits
+ // 0b00 -> input mode (reset state)
+ // 0b01 -> general purpose output mode
+ // 0b10 -> alternate function mode
+ // 0b11 -> analog mode
GPIOB->MODER |= (0b01 << (PINOUT_LED_1 * 2)) |\
(0b01 << (PINOUT_LED_2 * 2)) |\
(0b01 << (PINOUT_LED_3 * 2)) |\
- (0b01 << (PINOUT_LED_4 * 2));
+ (0b01 << (PINOUT_LED_4 * 2)) |\
+ (0b00 << (PINOUT_BTN * 2));
+
+ // pull-up resistor for button
+ GPIOB->PUPDR &= ~(0b11 << (PINOUT_BTN * 2));
+ GPIOB->PUPDR |= (0b01 << (PINOUT_BTN * 2));
}
/*
@@ -44,7 +65,7 @@ void ledWrite(int num, int on) {
* value 0 indicates that the button is currently not pressed.
*/
int buttonRead() {
- return 0;
+ return (GPIOB->IDR & (1 << PINOUT_BTN)) > 0;
}
void delay()
@@ -55,22 +76,26 @@ void delay()
asm("nop");
}
}
+
+int mod(int a, int b) {
+ int m = a % b;
+ return m < 0 ? (b < 0) ? m - b : m + b : m;
+}
+
int main()
{
shieldConfig();
- // RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- // GPIOA->MODER &= ~(1 << (PINOUT_LED_1 * 2 + 1));
- // GPIOA->MODER |= (1 << (PINOUT_LED_1 * 2));
-
- // GPIOA_MODER &= ~(0 << (PINOUT_LED_1 * 2 + 1));
-
+ uint8_t reverse = 0;
+ uint8_t led = 0;
while (1)
{
- for(int i = 0; i < 2 * 4; i++) {
- ledWrite(i % 4, i >= 4);
+ reverse = buttonRead();
+ for(int j = 0; j < 4; j++) ledWrite(j, 0);
+
+ led = mod(led + ((2 * reverse) - 1), 4);
- delay();
- }
+ ledWrite(led, 1);
+ delay();
}
}