Skip to content

Commit 9ad8682

Browse files
committed
fixing typo, update AS5311 example
1 parent 0d88f4d commit 9ad8682

3 files changed

Lines changed: 98 additions & 8 deletions

File tree

ESP32/AS5311-PWM/AS5311-PWM.ino

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "Arduino.h"
2+
#include "driver/mcpwm.h"
3+
4+
5+
#include "Arduino.h"
6+
#include "driver/mcpwm.h"
7+
#include "driver/gpio.h"
8+
9+
#define GPIO_CAP0_IN GPIO_NUM_26
10+
#define GPIO_CAP1_IN GPIO_NUM_27
11+
#define GPIO_TRIGGER GPIO_NUM_14 // New GPIO pin for triggering the counter
12+
13+
14+
struct CaptureData {
15+
uint32_t time_rising_edge;
16+
uint32_t time_falling_edge;
17+
uint32_t period;
18+
bool last_was_rising;
19+
};
20+
21+
CaptureData cap_data[2];
22+
volatile float latest_duty_cycle[2] = {0.0f, 0.0f}; // Updated by the MCPWM capture ISR
23+
volatile int counter = 0;
24+
25+
void IRAM_ATTR capture_handler(void *arg) {
26+
uint32_t cap_unit = (uint32_t)arg;
27+
uint32_t curr_time = mcpwm_capture_signal_get_value(MCPWM_UNIT_0, (mcpwm_capture_signal_t)cap_unit);
28+
29+
if (mcpwm_capture_signal_get_edge(MCPWM_UNIT_0, (mcpwm_capture_signal_t)cap_unit) == MCPWM_POS_EDGE) {
30+
if (cap_data[cap_unit].last_was_rising) {
31+
cap_data[cap_unit].period = curr_time - cap_data[cap_unit].time_rising_edge;
32+
}
33+
cap_data[cap_unit].time_rising_edge = curr_time;
34+
cap_data[cap_unit].last_was_rising = true;
35+
} else {
36+
cap_data[cap_unit].time_falling_edge = curr_time;
37+
cap_data[cap_unit].last_was_rising = false;
38+
}
39+
40+
if (cap_data[cap_unit].period != 0) {
41+
uint32_t high_time = cap_data[cap_unit].time_falling_edge - cap_data[cap_unit].time_rising_edge;
42+
latest_duty_cycle[cap_unit] = ((float)high_time / (float)cap_data[cap_unit].period) * 100.0f;
43+
Serial.printf("CAP%d Duty cycle: %0.2f%%\n", cap_unit, latest_duty_cycle[cap_unit]);
44+
}
45+
}
46+
47+
void IRAM_ATTR trigger_handler(void* arg) {
48+
for (int i = 0; i < 2; i++) {
49+
if (latest_duty_cycle[i] > 50.0f) {
50+
counter++;
51+
} else {
52+
counter--;
53+
}
54+
}
55+
Serial.printf("Counter (triggered): %d\n", counter);
56+
}
57+
58+
void setup() {
59+
Serial.begin(115200);
60+
Serial.println("Testing MCPWM capture on two pins...");
61+
62+
// Initialize MCPWM capture
63+
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPIO_CAP0_IN);
64+
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_1, GPIO_CAP1_IN);
65+
66+
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, MCPWM_BOTH_EDGE, 0);
67+
mcpwm_capture_enable(MCPWM_UNIT_0, MCPWM_SELECT_CAP1, MCPWM_BOTH_EDGE, 0);
68+
69+
intr_handle_t cap0_intr_handle = NULL;
70+
intr_handle_t cap1_intr_handle = NULL;
71+
72+
mcpwm_isr_register(MCPWM_UNIT_0, capture_handler, (void*)MCPWM_SELECT_CAP0, ESP_INTR_FLAG_IRAM, &cap0_intr_handle);
73+
mcpwm_isr_register(MCPWM_UNIT_0, capture_handler, (void*)MCPWM_SELECT_CAP1, ESP_INTR_FLAG_IRAM, &cap1_intr_handle);
74+
75+
// Initialize the GPIO_TRIGGER pin for external interrupt
76+
gpio_config_t io_conf;
77+
io_conf.intr_type = GPIO_INTR_POSEDGE; // Trigger on rising edge
78+
io_conf.mode = GPIO_MODE_INPUT;
79+
io_conf.pin_bit_mask = (1ULL << GPIO_TRIGGER);
80+
io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
81+
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
82+
gpio_config(&io_conf);
83+
84+
// Register ISR for the trigger pin
85+
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
86+
gpio_isr_handler_add(GPIO_TRIGGER, trigger_handler, NULL);
87+
88+
// Clear capture data
89+
memset(cap_data, 0, sizeof(cap_data));
90+
}
91+
92+
void loop() {
93+
delay(1000);
94+
}

ESP32/ds1822-temperaturecontrol/ds1822-temperaturecontrol.ino

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
// GPIO where the DS18B20 is connected to
66
const int oneWireBus = GPIO_NUM_25;
7-
8-
9-
int8_t LASER_1 = GPIO_NUM_12;
10-
int8_t LASER_2 = GPIO_NUM_4;
11-
int8_t LASER_3 = GPIO_NUM_2;
127

138

149
// Setup a oneWire instance to communicate with any OneWire devices
@@ -27,13 +22,13 @@ float previousError = 0;
2722
float integral = 0;
2823

2924
// Desired temperature
30-
const float setpoint = 30.0; // for example, 50°C
25+
const float setpoint = 36.0; // for example, 50°C
3126

3227
// LEDC variables
3328
const int freq = 5000;
3429
const int ledChannel = 0;
3530
const int resolution = 8;
36-
const int heaterPin = 27; // PWM pin connected to the heater
31+
const int heaterPin = GPIO_NUM_12; // PWM pin connected to the heater
3732

3833
// Get the current temperature
3934
// Here you'll want to replace this with your actual temperature reading logic

uc2rest/mserial.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ def __init__(self, port, baudrate, timeout=1):
355355
self.manufacturer = "UC2Mock"
356356
self.BAUDRATES = -1
357357

358-
def isOpen(Self):
358+
def isOpen(self):
359359
return self.is_open
360360

361361
def open(self):
@@ -371,6 +371,7 @@ def readline(self, timeout=1):
371371
return b''
372372
data = self.data_buffer
373373
self.data_buffer = self.data_buffer
374+
time.sleep(.05)
374375
return bytes(data)
375376

376377
def read(self, num_bytes):

0 commit comments

Comments
 (0)