This example shows how to use the BSP430 standard button definitions to track button presses and releases.
Note that this application uses a model where interrupts are only enabled when the MCU enters a low-power mode, i.e. inactive, and are disabled when the MCU becomes active again. It is this feature that ensures that interrupts correctly reflect press and release events in the presence of "bounce" in the button. ("Bounce" here means that momentary contact and release causes multiple high/low transitions in the port input signal on a signal press/release event.)
main.c
#if ! (BSP430_PLATFORM_BUTTON0 - 0)
#error No button available on this platform
#endif
typedef struct sButtonState {
const unsigned char bit;
volatile unsigned int in_mask;
volatile unsigned int count;
} sButtonState;
static int
void * context,
int idx)
{
sButtonState * sp = (sButtonState *)cb;
++sp->count;
sp->in_mask = (hpl->ies & sp->bit) ^ sp->bit;
hpl->ies ^= sp->bit;
}
static sButtonState button_state = {
.button_cb = { .callback_ni = button_isr_ni },
};
void main ()
{
cprintf(
"\nbutton " __DATE__
" " __TIME__
"\n");
cprintf(
"There's supposed to be a button at %s.%u\n",
b0pin);
if (! b0hal) {
cprintf(
"Whoops, guess it's not really there\n");
return;
}
#if (BSP430_PORT_SUPPORTS_REN - 0)
#endif
} else {
button_state.in_mask = 0;
}
cprintf(
"Button is configured. Try pressing it. No debouncing is done.\n");
#if ! (configBSP430_CORE_LPM_EXIT_CLEAR_GIE - 0)
cprintf(
"WARNING: Interrupts remain enabled after wakeup\n");
#endif
while (1) {
static const char * state_str[] = { "released", "pressed" };
cprintf(
"Count %u, in mask 0x%02x: %s\n", button_state.count, button_state.in_mask, state_str[!button_state.in_mask]);
#if ! (configBSP430_CORE_LPM_EXIT_CLEAR_GIE - 0)
#endif
}
}
bsp430_config.h
#define BSP430_PLATFORM_BOOT_CONFIGURE_LFXT1 1
#define configBSP430_PLATFORM_SPIN_FOR_JUMPER 1
#define configBSP430_CONSOLE 1
#ifndef configBSP430_CORE_LPM_EXIT_CLEAR_GIE
#define configBSP430_CORE_LPM_EXIT_CLEAR_GIE 1
#endif
#define configBSP430_UPTIME 1
#define configBSP430_PLATFORM_BUTTON0 1
Makefile
PLATFORM ?= exp430fr5739
MODULES=$(MODULES_PLATFORM)
MODULES += $(MODULES_UPTIME)
MODULES += $(MODULES_CONSOLE)
MODULES += periph/port
SRC=main.c
include $(BSP430_ROOT)/make/Makefile.common