Skip to content

Commit b04209b

Browse files
committed
Fix main and ino
1 parent c11b39f commit b04209b

File tree

2 files changed

+73
-69
lines changed

2 files changed

+73
-69
lines changed

arduino/capture_edge/capture_edge.ino

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,79 +23,81 @@
2323
#include "hardware/clocks.h"
2424
#include "capture_edge.h"
2525

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;
2929
volatile bool is_captured;
3030
volatile edge_type_t edge_type;
31-
char buffer[100];
31+
char msg[200];
3232

3333
static void capture_pin_0_handler(uint counter, edge_type_t edge)
3434
{
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+
}
5253
}
5354

5455
static void capture_pin_1_handler(uint counter, edge_type_t edge)
5556
{
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+
}
7375
}
7476

7577
void setup()
7678
{
77-
Serial.begin(115200);
79+
Serial.begin(115200);
7880

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
8284

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);
8688
}
87-
89+
8890
void loop()
8991
{
90-
if (is_captured)
91-
{
92-
sprintf(buffer, "\n\rCapture 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\rCapture 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+
}

sdk/main.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,51 @@
2626
#include "capture_edge.h"
2727

2828
float clk_div = 1;
29-
volatile uint counter, pin;
30-
volatile float frequency, duty, duration;
29+
volatile uint capture_counter, pin;
30+
volatile float frequency, duty, duration_cycle;
3131
volatile bool is_captured;
3232
volatile edge_type_t edge_type;
3333

3434
static void capture_pin_0_handler(uint counter, edge_type_t edge)
3535
{
3636
static uint counter_edge_rising = 0, counter_edge_falling = 0;
37+
capture_counter = counter;
3738
pin = 0;
3839
is_captured = true;
3940
edge_type = edge;
4041

4142
if (edge == EDGE_RISING)
4243
{
43-
duration = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * clk_div * COUNTER_CYCLES;
44-
frequency = 1 / duration;
44+
duration_cycle = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * COUNTER_CYCLES;
45+
frequency = 1 / duration_cycle;
4546
counter_edge_rising = counter;
4647
}
4748
if (edge == EDGE_FALLING)
4849
{
49-
float duration_pulse = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * clk_div * COUNTER_CYCLES;
50-
duty = duration_pulse / duration * 100;
50+
float duration_pulse = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * COUNTER_CYCLES;
51+
duty = duration_pulse / duration_cycle * 100;
5152
counter_edge_falling = counter;
5253
}
5354
}
5455

5556
static void capture_pin_1_handler(uint counter, edge_type_t edge)
5657
{
5758
static uint counter_edge_rising = 0, counter_edge_falling = 0;
59+
capture_counter = counter;
5860
pin = 1;
5961
is_captured = true;
6062
edge_type = edge;
6163

6264
if (edge == EDGE_RISING)
6365
{
64-
duration = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * clk_div * COUNTER_CYCLES;
65-
frequency = 1 / duration;
66+
duration_cycle = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * COUNTER_CYCLES;
67+
frequency = 1 / duration_cycle;
6668
counter_edge_rising = counter;
6769
}
6870
if (edge == EDGE_FALLING)
6971
{
70-
float duration_pulse = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * clk_div * COUNTER_CYCLES;
71-
duty = duration_pulse / duration * 100;
72+
float duration_pulse = (float)(counter - counter_edge_rising) / clock_get_hz(clk_sys) * COUNTER_CYCLES;
73+
duty = duration_pulse / duration_cycle * 100;
7274
counter_edge_falling = counter;
7375
}
7476
}
@@ -89,7 +91,7 @@ int main()
8991
{
9092
if (is_captured)
9193
{
92-
printf("\n\rCapture pin %u. Counter: %u State: %s Duration(us): %.0f", pin, counter, edge_type == EDGE_FALLING ? "High" : "Low ", duration * 1000000);
94+
printf("\n\rCapture pin %u. Counter: %u State: %s Duration(us): %.0f", pin, capture_counter, edge_type == EDGE_FALLING ? "High" : "Low ", duration_cycle * 1000000);
9395
if (edge_type == EDGE_RISING)
9496
printf(" Freq(Hz): %.1f Duty: %.1f", frequency, duty);
9597
is_captured = false;

0 commit comments

Comments
 (0)