main.c
1.72 KB
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
/*
* 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 <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#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;
}