I want to use SENSOR_CAPN and SENSOR_VN pins to act as external interrupt pins to read a rotary dial. SENSOR_CAPN is ok, but SENSOR_VN always trigger the interrupt even the pin does not change state. Is it relate to the ADC_PRE_AMP function? Below is my code:
// Rotary encoder
#define PIN_ROT_A 38 /* SENSOR_CAPN */
#define PIN_ROT_B 39 /* SENSOR_VN */
static QueueHandle_t q1;
#if (PIN_ROT_A >= 0)
static void rota(void *arg) {
gpio_num_t gpio = PIN_ROT_A;
xQueueSendToBackFromISR(q1, &gpio, NULL);
}
#endif
#if (PIN_ROT_B >= 0)
static void rotb(void *arg) {
gpio_num_t gpio = PIN_ROT_B;
xQueueSendToBackFromISR(q1, &gpio, NULL);
}
#endif
void app_main() {
esp_err_t ret;
gpio_config_t gpioConfig;
q1 = xQueueCreate(10, sizeof(gpio_num_t));
gpio_install_isr_service(0);
gpioConfig.mode = GPIO_MODE_INPUT;
gpioConfig.pull_up_en = GPIO_PULLUP_ENABLE;
gpioConfig.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpioConfig.intr_type = GPIO_INTR_ANYEDGE;
#if (PIN_ROT_A >= 0)
gpioConfig.pin_bit_mask = 1ULL << PIN_ROT_A;
ret = gpio_config(&gpioConfig);
assert(ret == ESP_OK);
gpio_set_direction(PIN_ROT_A, GPIO_MODE_INPUT);
gpio_isr_handler_add(PIN_ROT_A, rota, NULL );
#endif
#if (PIN_ROT_B >= 0)
gpioConfig.pin_bit_mask = 1ULL << PIN_ROT_B;
ret = gpio_config(&gpioConfig);
assert(ret == ESP_OK);
gpio_set_direction(PIN_ROT_B, GPIO_MODE_INPUT);
gpio_isr_handler_add(PIN_ROT_B, rotb, NULL );
#endif
while(1) {
gpio_num_t gpio;
ESP_LOGI(tag, "Waiting on interrupt queue");
BaseType_t rc = xQueueReceive(q1, &gpio, portMAX_DELAY);
ESP_LOGI(tag, "Woke from interrupt queue wait: %d, gpio=%d", rc, gpio);
}
}
Colman
SENSOR_CAPN and SENSOR_VN pin used as external interrupt pins
Re: SENSOR_CAPN and SENSOR_VN pin used as external interrupt pins
I forget to state that I turn on the ADC function on pin ADC1_CHANNEL_7. After I don't use the ADC, the interrupt works fine. So, I need these two pins for interrupt with the ADC function, how to make it work?
The ADC code:
void adc_task(void *pvParameter) {
uint16_t buf[5];
uint16_t minValue = 0xFFFF;
uint16_t maxValue = 0;
int n = 0, c = 0;
do {
int value = adc1_get_voltage(PIN_ADC_KEY);
if (value != 4095) {
uint16_t average;
int j, sum = 0;
if (value > maxValue) maxValue = value;
if (value < minValue) minValue = value;
buf[n] = value;
if (++n > 4) n = 0;
if (++c > 5) c = 5;
for (j = 0; j < c; j++) sum += buf[n];
average = sum / c;
if (n == 0) {
ESP_LOGI(tag, "MIN=%d, MAX=%d, AV=%d", minValue, maxValue, average);
}
}
else {
maxValue = 0;
minValue = 0xFFFF;
n = 0;
c = 0;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
} while (1);
vTaskDelete(NULL);
}
// Start adc key in main()
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(PIN_ADC_KEY, ADC_ATTEN_11db);
xTaskCreate(adc_task, "adc_task", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);
Colman
The ADC code:
void adc_task(void *pvParameter) {
uint16_t buf[5];
uint16_t minValue = 0xFFFF;
uint16_t maxValue = 0;
int n = 0, c = 0;
do {
int value = adc1_get_voltage(PIN_ADC_KEY);
if (value != 4095) {
uint16_t average;
int j, sum = 0;
if (value > maxValue) maxValue = value;
if (value < minValue) minValue = value;
buf[n] = value;
if (++n > 4) n = 0;
if (++c > 5) c = 5;
for (j = 0; j < c; j++) sum += buf[n];
average = sum / c;
if (n == 0) {
ESP_LOGI(tag, "MIN=%d, MAX=%d, AV=%d", minValue, maxValue, average);
}
}
else {
maxValue = 0;
minValue = 0xFFFF;
n = 0;
c = 0;
}
vTaskDelay(10 / portTICK_PERIOD_MS);
} while (1);
vTaskDelete(NULL);
}
// Start adc key in main()
adc1_config_width(ADC_WIDTH_12Bit);
adc1_config_channel_atten(PIN_ADC_KEY, ADC_ATTEN_11db);
xTaskCreate(adc_task, "adc_task", 2048, NULL, (tskIDLE_PRIORITY + 2), NULL);
Colman
Re: SENSOR_CAPN and SENSOR_VN pin used as external interrupt pins
Could you try using ADC2 instead? The preamplifier pins seem to be related to ADC1...
Cheers
Cheers
Re: SENSOR_CAPN and SENSOR_VN pin used as external interrupt pins
I am happy to switch to ADC2 if there is enough output pins for used. I choose that pin because I have used up all the output pins. Since I will not use the hall effect sensor, so I guess I can disable the ADC preamplifier. The problem seems to be happened only on the interrupt, if I use polling, it return the correct state of that pin.
Colman
Colman
Who is online
Users browsing this forum: No registered users and 54 guests