2020-09-12 16:47:58 +02:00
|
|
|
#include <Arduino.h>
|
2021-07-21 21:23:58 +02:00
|
|
|
#include "driver/pcnt.h"
|
2020-09-12 16:47:58 +02:00
|
|
|
|
2022-04-02 22:10:31 +02:00
|
|
|
#define PWM_FREQ 50000
|
|
|
|
#define PWM_BITS 8
|
|
|
|
|
|
|
|
#define OUTPUT_SENSOR 14
|
|
|
|
#define SENSOR_PLANT 17
|
2020-10-09 20:45:47 +02:00
|
|
|
|
2021-07-21 21:23:58 +02:00
|
|
|
int16_t pulses = 0;
|
|
|
|
int16_t pulses2 = 0;
|
2020-10-09 19:29:28 +02:00
|
|
|
|
2022-04-02 22:10:31 +02:00
|
|
|
int plantId = 0;
|
|
|
|
|
2020-09-12 16:47:58 +02:00
|
|
|
void setup() {
|
2021-12-05 02:36:03 +01:00
|
|
|
RTC_SLOW_ATTR uint8_t tick = 0;
|
|
|
|
RTC_SLOW_ATTR bool dir = true;
|
|
|
|
void setup()
|
2021-12-04 17:04:18 +01:00
|
|
|
{
|
2021-05-19 20:54:11 +02:00
|
|
|
Serial.begin(115200);
|
2020-10-09 19:29:28 +02:00
|
|
|
pinMode(OUTPUT_SENSOR, OUTPUT);
|
2020-09-12 16:47:58 +02:00
|
|
|
|
2022-04-02 22:10:31 +02:00
|
|
|
ledcSetup(plantId, PWM_FREQ, PWM_BITS);
|
|
|
|
ledcAttachPin(SENSOR_PLANT, plantId);
|
|
|
|
ledcWrite(plantId, plantId);
|
|
|
|
pinMode(SENSOR_PLANT, INPUT);
|
2021-05-19 20:54:11 +02:00
|
|
|
|
2020-09-12 16:47:58 +02:00
|
|
|
|
2022-04-02 22:10:31 +02:00
|
|
|
pcnt_config_t pcnt_config = { }; // Instancia PCNT config
|
|
|
|
pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + plantId);
|
|
|
|
pcnt_config.pulse_gpio_num = SENSOR_PLANT; // Configura GPIO para entrada dos pulsos
|
2021-07-21 21:23:58 +02:00
|
|
|
pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem
|
2022-04-02 22:10:31 +02:00
|
|
|
pcnt_config.unit = unit; // Unidade de contagem PCNT - 0
|
2021-07-21 21:23:58 +02:00
|
|
|
pcnt_config.channel = PCNT_CHANNEL_0; // Canal de contagem PCNT - 0
|
|
|
|
pcnt_config.counter_h_lim = INT16_MAX; // Limite maximo de contagem - 20000
|
|
|
|
pcnt_config.pos_mode = PCNT_COUNT_INC; // Incrementa contagem na subida do pulso
|
|
|
|
pcnt_config.neg_mode = PCNT_COUNT_DIS; // Incrementa contagem na descida do pulso
|
|
|
|
pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // PCNT - modo lctrl desabilitado
|
|
|
|
pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando
|
|
|
|
pcnt_unit_config(&pcnt_config); // Configura o contador PCNT
|
2021-02-04 23:52:54 +01:00
|
|
|
|
2020-10-09 19:29:28 +02:00
|
|
|
|
2021-07-21 21:23:58 +02:00
|
|
|
pcnt_counter_pause(PCNT_UNIT_0); // Pausa o contador PCNT
|
|
|
|
pcnt_counter_clear(PCNT_UNIT_0); // Zera o contador PCNT
|
2021-01-31 00:14:15 +01:00
|
|
|
|
2021-04-07 20:27:42 +02:00
|
|
|
|
2021-07-21 21:23:58 +02:00
|
|
|
digitalWrite(OUTPUT_SENSOR, HIGH);
|
2022-04-02 22:10:31 +02:00
|
|
|
Serial.println("Nodemcu ESP32 Start done");
|
2021-01-30 23:28:15 +01:00
|
|
|
}
|
2020-10-09 19:29:28 +02:00
|
|
|
|
2021-02-04 23:52:54 +01:00
|
|
|
void loop() {
|
2021-07-21 21:23:58 +02:00
|
|
|
pcnt_counter_resume(PCNT_UNIT_0);
|
|
|
|
|
|
|
|
delay(500);
|
|
|
|
|
|
|
|
pcnt_counter_pause(PCNT_UNIT_0);
|
|
|
|
pcnt_get_counter_value(PCNT_UNIT_0, &pulses);
|
|
|
|
pcnt_counter_clear(PCNT_UNIT_0);
|
|
|
|
|
|
|
|
Serial.println(pulses*2);
|
2022-06-25 17:28:01 +02:00
|
|
|
}
|