telescience: correctly calculating rate values with higher resolution
[fw/altos] / src / avr / ao_pwmin.c
1 /*
2  * Copyright © 2012 Robert D. Garbee <robert@gag.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "ao_pwmin.h"
20
21 /* 
22  * This code implements a PWM input using ICP3.  
23  *
24  * The initial use is to measure wind speed in the ULA/Ball summer intern 
25  * project payload developed at Challenger Middle School.  
26  */
27
28 volatile __data uint16_t ao_icp3_count = 0;
29 volatile __data uint16_t ao_icp3_last = 0;
30
31 uint16_t ao_icp3(void)
32 {
33         uint16_t        v;
34         ao_arch_critical(
35                 v = ao_icp3_count;
36                 );
37         return v;
38 }
39
40 static void
41 ao_pwmin_display(void) __reentrant
42 {
43         /* display the most recent value */
44         printf("icp 3: %5u\n", ao_icp3());
45
46 }
47
48
49 ISR(TIMER3_CAPT_vect)
50 {
51         
52         uint8_t lo = ICR3L; 
53         uint8_t hi = ICR3H;
54         uint16_t ao_icp3_this = (hi <<8) | lo;
55         
56         /* handling counter rollovers */
57         if (ao_icp3_this >= ao_icp3_last)
58                 ao_icp3_count = ao_icp3_this - ao_icp3_last;
59         else 
60                 ao_icp3_count = ao_icp3_this + (65536 - ao_icp3_last);
61         ao_icp3_last = ao_icp3_this;
62 }
63
64 __code struct ao_cmds ao_pwmin_cmds[] = {
65         { ao_pwmin_display,     "p\0PWM input" },
66         { 0, NULL },
67 };
68
69 void
70 ao_pwmin_init(void)
71 {
72         /* do hardware setup here */
73         TCCR3A = ((0 << WGM31) |        /* normal mode, OCR3A */
74                   (0 << WGM30));        /* normal mode, OCR3A */
75         TCCR3B = ((1 << ICNC3) |        /* input capture noise canceler on */
76                   (0 << ICES3) |        /* input capture on falling edge (don't care) */
77                   (0 << WGM33) |        /* normal mode, OCR3A */
78                   (0 << WGM32) |        /* normal mode, OCR3A */
79                   (3 << CS30));         /* clk/64 from prescaler */
80
81         
82
83         TIMSK3 = (1 << ICIE3);         /* Interrupt on input compare */
84
85                 /* set the spike filter bit in the TCCR3B register */
86
87         ao_cmd_register(&ao_pwmin_cmds[0]);
88 }
89
90