23
23
#include " hardware/clocks.h"
24
24
#include " capture_edge.h"
25
25
26
- const float clk_div = 1 ;
27
- volatile uint counter , pin;
28
- volatile float frequency, duty, duration ;
26
+ float clk_div = 1 ;
27
+ volatile uint capture_counter , pin;
28
+ volatile float frequency, duty, duration_cycle ;
29
29
volatile bool is_captured;
30
30
volatile edge_type_t edge_type;
31
- char buffer[ 100 ];
31
+ char msg[ 200 ];
32
32
33
33
static void capture_pin_0_handler (uint counter, edge_type_t edge)
34
34
{
35
- static uint counter_edge_rising = 0 , counter_edge_falling = 0 ;
36
- pin = 0 ;
37
- is_captured = true ;
38
- edge_type = edge;
39
-
40
- if (edge == EDGE_RISING)
41
- {
42
- duration = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * clk_div * COUNTER_CYCLES;
43
- frequency = 1 / duration;
44
- counter_edge_rising = counter;
45
- }
46
- if (edge == EDGE_FALLING)
47
- {
48
- float duration_pulse = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * clk_div * COUNTER_CYCLES;
49
- duty = duration_pulse / duration * 100 ;
50
- counter_edge_falling = counter;
51
- }
35
+ static uint counter_edge_rising = 0 , counter_edge_falling = 0 ;
36
+ capture_counter = counter;
37
+ pin = 0 ;
38
+ is_captured = true ;
39
+ edge_type = edge;
40
+
41
+ if (edge == EDGE_RISING)
42
+ {
43
+ duration_cycle = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * COUNTER_CYCLES;
44
+ frequency = 1 / duration_cycle;
45
+ counter_edge_rising = counter;
46
+ }
47
+ if (edge == EDGE_FALLING)
48
+ {
49
+ float duration_pulse = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * COUNTER_CYCLES;
50
+ duty = duration_pulse / duration_cycle * 100 ;
51
+ counter_edge_falling = counter;
52
+ }
52
53
}
53
54
54
55
static void capture_pin_1_handler (uint counter, edge_type_t edge)
55
56
{
56
- static uint counter_edge_rising = 0 , counter_edge_falling = 0 ;
57
- pin = 1 ;
58
- is_captured = true ;
59
- edge_type = edge;
60
-
61
- if (edge == EDGE_RISING)
62
- {
63
- duration = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * clk_div * COUNTER_CYCLES;
64
- frequency = 1 / duration;
65
- counter_edge_rising = counter;
66
- }
67
- if (edge == EDGE_FALLING)
68
- {
69
- float duration_pulse = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * clk_div * COUNTER_CYCLES;
70
- duty = duration_pulse / duration * 100 ;
71
- counter_edge_falling = counter;
72
- }
57
+ static uint counter_edge_rising = 0 , counter_edge_falling = 0 ;
58
+ capture_counter = counter;
59
+ pin = 1 ;
60
+ is_captured = true ;
61
+ edge_type = edge;
62
+
63
+ if (edge == EDGE_RISING)
64
+ {
65
+ duration_cycle = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * COUNTER_CYCLES;
66
+ frequency = 1 / duration_cycle;
67
+ counter_edge_rising = counter;
68
+ }
69
+ if (edge == EDGE_FALLING)
70
+ {
71
+ float duration_pulse = (float )(counter - counter_edge_rising) / clock_get_hz (clk_sys) * COUNTER_CYCLES;
72
+ duty = duration_pulse / duration_cycle * 100 ;
73
+ counter_edge_falling = counter;
74
+ }
73
75
}
74
76
75
77
void setup ()
76
78
{
77
- Serial.begin (115200 );
79
+ Serial.begin (115200 );
78
80
79
- PIO pio = pio0; // values: pio0, pio1
80
- uint pin_base = 0 ; // starting gpio to capture
81
- uint irq = PIO0_IRQ_0; // values for pio0: PIO0_IRQ_0, PIO0_IRQ_1. values for pio1: PIO1_IRQ_0, PIO1_IRQ_1
81
+ PIO pio = pio0; // values: pio0, pio1
82
+ uint pin_base = 0 ; // starting gpio to capture
83
+ uint irq = PIO0_IRQ_0; // values for pio0: PIO0_IRQ_0, PIO0_IRQ_1. values for pio1: PIO1_IRQ_0, PIO1_IRQ_1
82
84
83
- capture_edge_init (pio, pin_base, clk_div, irq);
84
- capture_edge_set_handler (0 , capture_pin_0_handler);
85
- capture_edge_set_handler (1 , capture_pin_1_handler);
85
+ capture_edge_init (pio, pin_base, clk_div, irq);
86
+ capture_edge_set_handler (0 , capture_pin_0_handler);
87
+ capture_edge_set_handler (1 , capture_pin_1_handler);
86
88
}
87
-
89
+
88
90
void loop ()
89
91
{
90
- if (is_captured)
91
- {
92
- sprintf (buffer , " \n\r Capture pin %u. Counter: %u State: %s Duration(us): %.0f" , pin, counter , edge_type == EDGE_FALLING ? " High" : " Low " , duration * 1000000 );
93
- Serial.print (buffer );
94
- if (edge_type == EDGE_RISING)
95
- {
96
- sprintf (buffer , " Freq(Hz): %.1f Duty: %.1f" , frequency, duty);
97
- Serial.print (buffer );
98
- }
99
- is_captured = false ;
100
- }
101
- }
92
+ if (is_captured)
93
+ {
94
+ sprintf (msg , " \n\r Capture pin %u. Counter: %u State: %s Duration(us): %.0f" , pin, capture_counter , edge_type == EDGE_FALLING ? " High" : " Low " , duration_cycle * 1000000 );
95
+ Serial.print (msg );
96
+ if (edge_type == EDGE_RISING)
97
+ {
98
+ sprintf (msg , " Freq(Hz): %.1f Duty: %.1f" , frequency, duty);
99
+ Serial.print (msg );
100
+ }
101
+ is_captured = false ;
102
+ }
103
+ }
0 commit comments