I'm trying to use the ESP32 to read the value of a Flex Sensor (Measures the percentage of ethanol in gasoline here in Brazil, gasoline has a blend with ethanol). The Arduino code works perfectly.
The Arduino code:
Code: Select all
#include <Wire.h>
int analyzer=0, comb1=0;
volatile int state = LOW;
volatile int state1 = LOW;
void analy()
{
analyzer++;
}
void setup()
{
Serial.begin(9600);
attachInterrupt(0, analy, FALLING);
delay(4000);
noInterrupts(); // disabilita todas as interrupções
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = 31250; // compare match register 16MHz/256/2Hz
TCCR1B |= (1 << WGM12); // CTC mode
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_COMPA_vect) // timer compare interrupt service routine
{
comb1=analyzer*2-50; ////
//////////////////////
analyzer=0;
}
void loop()
{
Serial.println("FUEL ANALYZER");
Serial.println("ETHANOL= ");
if(comb1>2){
Serial.print(comb1);
Serial.println("% ");
}else{
Serial.println("NO FUEL");
}
if(comb1>100){
Serial.println("ERROR");
}else{
Serial.println(" ");
}
}
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "driver/timer.h"
#include "esp_log.h"
#define PIN_INTERRUPT 15
static const char *TAG = "FUEL_ANALYZER";
int analyzer = 0;
int comb1 = 0;
void IRAM_ATTR analy_isr_handler(void *arg)
{
analyzer++;
}
void timer_init_custom()
{
timer_config_t config;
config.divider = 256; // Prescaler de 256
config.counter_dir = TIMER_COUNT_UP;
config.counter_en = TIMER_PAUSE;
config.alarm_en = TIMER_ALARM_DIS;
config.intr_type = TIMER_INTR_LEVEL;
config.auto_reload = true;
timer_init(TIMER_GROUP_0, TIMER_1, &config);
timer_set_counter_value(TIMER_GROUP_0, TIMER_1, 0x00000000ULL);
timer_set_alarm_value(TIMER_GROUP_0, TIMER_1, 31250);
timer_enable_intr(TIMER_GROUP_0, TIMER_1);
timer_isr_register(TIMER_GROUP_0, TIMER_1, timer_isr_handler, (void *) TIMER_1, ESP_INTR_FLAG_IRAM, NULL);
timer_start(TIMER_GROUP_0, TIMER_1);
}
void IRAM_ATTR timer_isr_handler(void *arg)
{
comb1 = analyzer * 2 - 50;
analyzer = 0;
}
void print_task(void *pvParameter)
{
while (true)
{
ESP_LOGI(TAG, "FUEL ANALYZER");
ESP_LOGI(TAG, "ETHANOL= ");
if (comb1 > 2)
{
ESP_LOGI(TAG, "%d%%", comb1);
}
else
{
ESP_LOGI(TAG, "NO FUEL");
}
if (comb1 > 100)
{
ESP_LOGI(TAG, "ERROR");
}
else
{
ESP_LOGI(TAG, "");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void app_main()
{
esp_log_level_set(TAG, ESP_LOG_INFO);
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.pin_bit_mask = (1ULL << PIN_INTERRUPT);
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
gpio_config(&io_conf);
gpio_install_isr_service(ESP_INTR_FLAG_IRAM);
gpio_isr_handler_add(PIN_INTERRUPT, analy_isr_handler, (void *)PIN_INTERRUPT);
timer_init_custom();
xTaskCreate(print_task, "print_task", 4096, NULL, 1, NULL);
}
https://1drv.ms/i/s!AsQnhUoqcXiQg9lgXVD ... A?e=thXoIG
Thanks.