1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
#include "stm32f0xx.h"
// 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,
* register GPIOx_MODER to configure pins as input, analog or output and
* GPIOx_PUPDR to configure pull-up and pull-down resistors.
*/
void shieldConfig() {
// 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_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)) |\
(0b00 << (PINOUT_BTN * 2));
// pull-up resistor for button
GPIOB->PUPDR &= ~(0b11 << (PINOUT_BTN * 2));
GPIOB->PUPDR |= (0b01 << (PINOUT_BTN * 2));
}
/*
* This function drives led <num> of the I/O-shield. <num> should be the number
* of the LED to drive (0, 1, 2 or 3) and <on> should be an integer with value
* 0 or 1 which indicates if the LED should be turned on (1) or off (0).
*/
void ledWrite(int num, int on) {
GPIOB->ODR &= ~(1 << leds[num]);
GPIOB->ODR |= (on << leds[num]);
}
/*
* This function returns the status of the pushbutton of the I/O-shield.
* Return value 1 indicates that the button is currently pressed and return
* value 0 indicates that the button is currently not pressed.
*/
int buttonRead() {
return (GPIOB->IDR & (1 << PINOUT_BTN)) > 0;
}
void delay()
{
int i;
for (i = 0; i < 50000; i++)
{
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();
uint8_t reverse = 0;
uint8_t led = 0;
while (1)
{
reverse = buttonRead();
for(int j = 0; j < 4; j++) ledWrite(j, 0);
led = mod(led + ((2 * reverse) - 1), 4);
ledWrite(led, 1);
delay();
}
}
|