Rev 45 | Rev 48 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 45 | Rev 47 | ||
|---|---|---|---|
| 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 |
11 | unsigned volatile long RPM_Time[RPM_SAMPLES]; // sampled on both edges |
| 12 | unsigned volatile char RPM_Level[RPM_SAMPLES]; // active level when sampled |
- | |
| 13 | unsigned volatile long RPM_Count; // incremented every reading |
12 | unsigned volatile long RPM_Count; // incremented every reading |
| 14 | 13 | ||
| 15 | // this is set if there is a timer timeout interrupt |
14 | // this is set if there is a timer timeout interrupt |
| 16 | unsigned char volatile periodPulse = 0; |
15 | unsigned char volatile periodPulse = 0; |
| 17 | 16 | ||
| 18 | unsigned char volatile tim3triggered = 0; |
17 | unsigned char volatile tim3triggered = 0; |
| Line 24... | Line 23... | ||
| 24 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
23 | htim3.Instance->CR1 |= TIM_CR1_CEN; |
| 25 | } |
24 | } |
| 26 | 25 | ||
| 27 | void TIM2_IRQHandler(void) |
26 | void TIM2_IRQHandler(void) |
| 28 | { |
27 | { |
| - | 28 | static char level = 0; |
|
| - | 29 | char valid = 0; |
|
| - | 30 | uint16_t high_count = 0; |
|
| - | 31 | uint16_t low_count = 0; |
|
| 29 | // rising edge trigger CB pulse |
32 | // rising edge CB pulse |
| 30 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1)) |
33 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC1)) |
| 31 | { |
34 | { |
| 32 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1); |
35 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC1); |
| 33 | RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1); |
36 | low_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_1); |
| 34 | RPM_Level[RPM_Count] = 1; // rising so it is high now. |
37 | valid = 1; // record we have a low_count val |
| 35 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
- | |
| - | 38 | ||
| 36 | // trigger timer some time after falling edge |
39 | // trigger timer some time after falling edge |
| 37 | if (tim3triggered == 0) |
40 | if (tim3triggered == 0) |
| 38 | - | ||
| 39 | { |
41 | { |
| 40 | tim3triggered = 1; |
42 | tim3triggered = 1; |
| 41 | triggerTim3(); |
43 | triggerTim3(); |
| 42 | } |
44 | } |
| 43 | } |
45 | } |
| 44 | // falling edge trigger CB pulse |
46 | // falling edge trigger CB pulse |
| 45 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2)) |
47 | if (__HAL_TIM_GET_FLAG(&htim2, TIM_FLAG_CC2)) |
| 46 | { |
48 | { |
| 47 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2); |
49 | __HAL_TIM_CLEAR_FLAG(&htim2, TIM_FLAG_CC2); |
| - | 50 | ||
| 48 | RPM_Time[RPM_Count] = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2); |
51 | high_count = __HAL_TIM_GET_COMPARE(&htim2, TIM_CHANNEL_2); |
| 49 | RPM_Level[RPM_Count] = 0; // falling so it is low now. |
- | |
| 50 | RPM_Count = (RPM_Count + 1) % RPM_SAMPLES; |
52 | valid |= 2; |
| 51 | // indicate that CB pulse is owning the timer 3 timing |
53 | // indicate that CB pulse is owning the timer 3 timing |
| 52 | periodPulse = 0; |
54 | periodPulse = 0; |
| 53 | } |
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 | } |
|
| 54 | } |
90 | } |
| 55 | 91 | ||
| 56 | // timer variable shared between TIM3 and TIM4 handler. |
92 | // timer variable shared between TIM3 and TIM4 handler. |
| 57 | static char chtTimer = 0; |
93 | static char chtTimer = 0; |
| 58 | 94 | ||