/* * Copyright (C) 2015 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level * directory for more details. */ /** * @ingroup examples * @{ * * @file * @brief Example application for demonstrating the RIOT network stack * * @author Hauke Petersen * * @} */ #include #include "shell.h" #include "msg.h" #include "board.h" #include "xtimer.h" #include "periph/gpio.h" #define ENCODER GPIO_PIN(PORT_A,8) uint32_t rpm; uint32_t pulses; uint32_t timeold; uint8_t pulsesperturn = 20; static void gpio_cb(void *pin) { pulses++; } int init_encoder(void){ if (gpio_init_int(ENCODER,GPIO_IN, GPIO_RISING, gpio_cb, (void *)ENCODER) < 0) { puts("Error while initializing ENCODER pin as external interrupt\n"); return 1; } printf("ENCODER GPIO initialized successfuly\n"); rpm=0; pulses=0; timeold=0; return 0; } int main(void) { init_encoder(); for(;;){ if (xtimer_now_usec() - timeold >= 1000000){ /*Uptade every one second, this will be equal to reading frecuency (Hz).*/ //Don't process interrupts during calculations gpio_irq_disable(ENCODER); //Note that this would be 60*1000/(millis() - timeold)*pulses if the interrupt //happened once per revolution rpm = ((60 * 1000000)/pulsesperturn )/(xtimer_now_usec()-timeold)*pulses; timeold = xtimer_now_usec(); pulses = 0; //Write it out to serial port printf("speed: %" PRIu32 " rpm\n",rpm); //Restart the interrupt processing gpio_irq_enable(ENCODER); } } return 0; }