Two interrupts in raspberry pi pico

408 Views Asked by At

The pico only takes into account one pin of my interrupts, I am trying to use two, I don't know if the solution if related to using two cores.

#include<stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
#include "hardware/pwm.h"
#include "hardware/timer.h"

#define POT_PIN 26
#define PWM_OUTPUT 12
#define ENCODER_14 14
#define ENCODER_15 15
#define FALLING_EDGE 0x04

uint8_t contador_14 = 0;
uint8_t contador_15 = 0;
uint8_t revoluciones_14 = 0;
uint8_t revoluciones_15 = 0;
uint8_t numeroAnterior_14 = 0;
uint8_t numeroAnterior_15 = 0;

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void button_callback_14()
{
    if(gpio_get_irq_event_mask(14) && GPIO_IRQ_EDGE_FALL)
    {
        contador_14+=1;
        printf("Interrupt occurred at pin 14 contador : %i \n", contador_14);
        revoluciones_14 = contador_14 % 20;  
    }
}

void button_callback_15()
{
    if(gpio_get_irq_event_mask(15) && GPIO_IRQ_EDGE_FALL)
    {
        contador_15+=1;
        printf("Interrupt occurred at pin 15 contador : %i \n", contador_15);
        revoluciones_15 = contador_15 % 20;
    }
}

bool lecturaPotenciometro(struct repeating_timer *t)
{
    uint slice_num = pwm_gpio_to_slice_num(PWM_OUTPUT);

    pwm_set_wrap(slice_num,255);
    pwm_set_chan_level(slice_num, PWM_CHAN_A, 0); 
    pwm_set_enabled(slice_num, true); 
    
    uint16_t result = adc_read();
    long pwm_value = map(result, 0, 4095, 0, 255);
    //printf("Raw value : %i PWM value : %i \n", result, pwm_value);
    pwm_set_chan_level(slice_num, PWM_CHAN_A, pwm_value);
    return true;
}

bool calculoRPM(struct repeating_timer *t)
{
    uint8_t RPM_14 = (revoluciones_14 - numeroAnterior_14) * 600;
    uint8_t RPM_15 = (revoluciones_15 - numeroAnterior_15) * 600; 
    
    //printf("RPM 14 : %i \n", RPM_14); 
    //printf("RPM 15 : %i \n", RPM_15); 
 
    numeroAnterior_14 = revoluciones_14;  
    numeroAnterior_15 = revoluciones_15; 
}

int main()
{
    //std input output configuracion ie serial monitor
    stdio_init_all();
    
    //adc configuracion
    adc_init();
    adc_gpio_init(POT_PIN);
    adc_select_input(0);

    //PWM configuracion
    gpio_set_function(PWM_OUTPUT, GPIO_FUNC_PWM);
   
    //Encoder 14 configuracion
    gpio_init(ENCODER_14);
    gpio_set_dir(ENCODER_14, GPIO_IN);
    gpio_pull_down(ENCODER_14);
    
    //Encoder 15 configuracion
    gpio_init(ENCODER_15);
    gpio_set_dir(ENCODER_15, GPIO_IN);
    gpio_pull_down(ENCODER_15);

    //Interrupt 
    gpio_set_irq_enabled(ENCODER_14, FALLING_EDGE, 1);
    gpio_set_irq_enabled(ENCODER_15, FALLING_EDGE, 1);
    gpio_add_raw_irq_handler(ENCODER_14, button_callback_14);
    gpio_add_raw_irq_handler(ENCODER_15, button_callback_15);
    irq_set_enabled(IO_IRQ_BANK0, true);


    /*Timer / Pot reading config
    read pot every  250 ms*/
    struct repeating_timer timer1;
    struct repeating_timer timer2;
    
    add_repeating_timer_ms(250, lecturaPotenciometro, NULL, &timer1);
    add_repeating_timer_ms(100, calculoRPM, NULL, &timer2);

    //Loop infinito
    while(1)
    {
        tight_loop_contents();
    }
}

Arduino Serial monitor

I am trying to read two optocouplers using them as encoders to measure RPM. I initially tried to do it by using gpio_set_irq_enabled_with_callback() but I realize it only takes into account one function call.

0

There are 0 best solutions below