Rev 47 | Rev 52 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 47 | Rev 48 | ||
---|---|---|---|
Line 6... | Line 6... | ||
6 | */ |
6 | */ |
7 | #include "stm32f1xx_hal.h" |
7 | #include "stm32f1xx_hal.h" |
8 | #include "misc.h" |
8 | #include "misc.h" |
9 | #include "main.h" |
9 | #include "main.h" |
10 | 10 | ||
11 | unsigned volatile long RPM_Time[RPM_SAMPLES]; // sampled on both edges |
- | |
12 | unsigned volatile long RPM_Count; // incremented every reading |
- | |
13 | - | ||
14 | // this is set if there is a timer timeout interrupt |
11 | // this is set if there is a timer timeout interrupt |
15 | unsigned char volatile periodPulse = 0; |
12 | unsigned char volatile periodPulse = 0; |
16 | 13 | ||
- | 14 | // this is set when timer 3 was triggerd |
|
17 | unsigned char volatile tim3triggered = 0; |
15 | unsigned char volatile tim3triggered = 0; |
18 | 16 | ||
- | 17 | // this is exported |
|
19 | static void |
18 | void |
20 | triggerTim3(void) |
19 | triggerTim3(void) |
21 | { |
20 | { |
22 | htim3.Instance->CNT = 0; |
21 | htim3.Instance->CNT = 0; |
23 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
22 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
24 | } |
23 | } |
25 | 24 | ||
26 | void TIM2_IRQHandler(void) |
- | |
27 | { |
- | |
28 | static char level = 0; |
- | |
29 | char valid = 0; |
- | |
30 | uint16_t high_count = 0; |
- | |
31 | uint16_t low_count = 0; |
- | |
32 | // rising edge CB pulse |
- | |
33 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1)) |
- | |
34 | { |
- | |
35 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1); |
- | |
36 | low_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1); |
- | |
37 | valid = 1; // record we have a low_count val |
- | |
38 | - | ||
39 | // trigger timer some time after falling edge |
- | |
40 | if (tim3triggered == 0) |
- | |
41 | { |
- | |
42 | tim3triggered = 1; |
- | |
43 | triggerTim3(); |
- | |
44 | } |
- | |
45 | } |
- | |
46 | // falling edge trigger CB pulse |
- | |
47 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2)) |
- | |
48 | { |
- | |
49 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2); |
- | |
50 | - | ||
51 | high_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2); |
- | |
52 | valid |= 2; |
- | |
53 | // indicate that CB pulse is owning the timer 3 timing |
- | |
54 | periodPulse = 0; |
- | |
55 | } |
- | |
56 | - | ||
57 | switch (valid) |
- | |
58 | { |
- | |
59 | case 1: |
- | |
60 | // count width of a low period |
- | |
61 | RPM_Time[RPM_Count] = low_count; |
- | |
62 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
63 | level = 0; // remember level |
- | |
64 | break; |
- | |
65 | case 2: |
- | |
66 | // count width of a high period |
- | |
67 | RPM_Time[RPM_Count] = high_count | RPM_FLAG; |
- | |
68 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
69 | level = 1; // remember level |
- | |
70 | break; |
- | |
71 | case 3: |
- | |
72 | if (level == 1) // next level = 0 ,then 1 again |
- | |
73 | { |
- | |
74 | RPM_Time[RPM_Count] = low_count; |
- | |
75 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
76 | RPM_Time[RPM_Count] = high_count | RPM_FLAG; |
- | |
77 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
78 | } |
- | |
79 | else |
- | |
80 | { |
- | |
81 | RPM_Time[RPM_Count] = high_count | RPM_FLAG; |
- | |
82 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
83 | RPM_Time[RPM_Count] = low_count; |
- | |
84 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
85 | } |
- | |
86 | break; |
- | |
87 | default: |
- | |
88 | break; |
- | |
89 | } |
- | |
90 | } |
- | |
91 | - | ||
92 | // timer variable shared between TIM3 and TIM4 handler. |
25 | // timer variable shared between TIM3 and TIM4 handler. |
93 | static char chtTimer = 0; |
26 | static char chtTimer = 0; |
94 | 27 | ||
95 | void TIM3_IRQHandler(void) |
28 | void TIM3_IRQHandler(void) |
96 | { |
29 | { |