# ISR timer pi pico

**URL:** <https://forum.arduino.cc/t/isr-timer-pi-pico/1086475>\
**Category:** Programming\
**Created:** [February 5, 2023, 1:39pm UTC](https://forum.arduino.cc/t/isr-timer-pi-pico/1086475 "2023-02-05T13:39:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mojtaba195](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@mojtaba195](https://forum.arduino.cc/u/mojtaba195)\
**Post date:** [February 5, 2023, 1:39pm UTC](https://forum.arduino.cc/t/isr-timer-pi-pico/1086475/1 "2023-02-05T13:39:59Z")

</div>

Hello friends  
I had written a project in arduino and i wanted to bring it to work on a raspberry pico.  
By using the timer, a frequency can be changed from 50 Hz to 500 Hz, with an adjustable duty cycle.  
When the frequency value became 0, I read a value in the analog base in ISR(TIMER1\_COMPB\_vect) (and in another program it was the opposite, when the frequency became 1, I did something).  
Now, by using the RP2040\_Slow\_PWM library, I can easily make the frequency with the desired duty, but I don't know what to do to read the corresponding analog base in this interval when the frequency value goes from 1 to 0.  
I am putting the program code of Arduino Uno and Pico. Please help me to solve my problem. thank you.

For uno

```cpp
uint16_t a;
uint16_t b;
void setup() {
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
  TCCR1A = 0;                 
  TCCR1B = 0;                
  TCCR1A = 0b10100010;  
  TCCR1B = 0b00011100;       
  TCNT1=0;       
  OCR1A = 30;               
  OCR1B = 10; //pulse with
  ICR1=400;
  TIMSK1 = 0b0000100;
  sei();
}

void loop() {

}

ISR(TIMER1_COMPB_vect){

  delayMicroseconds(100);
a=analogRead(A0);
  delayMicroseconds(100);
b=analogRead(A1);
  }

```

For pico

```cpp
#if !( ( defined(ARDUINO_ARCH_RP2040) defined(ARDUINO_RASPBERRY_PI_PICO) defined(ARDUINO_ADAFRUIT_FEATHER_RP2040) || defined(ARDUINO_GENERIC_RP2040) ) && !defined(ARDUINO_ARCH_MBED) )
  #error This code is intended to run on the non-mbed RP2040 arduino-pico platform! Please check your Tools->Board setting.
#endif

// These define's must be placed at the beginning before #include "ESP32_PWM.h"
// _PWM_LOGLEVEL_ from 0 to 4
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _PWM_LOGLEVEL_ 3

#define USING_MICROS_RESOLUTION true //false

// Default is true, uncomment to false
//#define CHANGING_PWM_END_OF_CYCLE false

// To be included only in main(), .ino with setup() to avoid Multiple Definitions Linker Error
#include "RP2040_Slow_PWM.h"

#define HW_TIMER_INTERVAL_US 20L

volatile uint64_t startMicros = 0;

// Init RPI_PICO_Timer
RP2040_Timer ITimer(0);

// Init MBED_RP2040_Slow_PWM, each can service 16 different ISR-based PWM channels
RP2040_Slow_PWM ISR_PWM;

//////////////////////////////////////////////////////

bool TimerHandler(struct repeating_timer *t)
{ 
  (void) t;
  
  ISR_PWM.run();

  return true;
}

//////////////////////////////////////////////////////

#define PIN_D1 10

//////////////////////////////////////////////////////

#define USING_PWM_FREQUENCY true

//////////////////////////////////////////////////////

// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11
uint32_t PWM_Pin = PIN_D1;
uint32_t PWM_Period = 50000L;

// You can assign any interval for any timer here, in Hz
float PWM_Freq = 250;

// You can assign any interval for any timer here, in milliseconds 0 to 100%
float PWM_DutyCycle =2.900;

////////////////////////////////////////////////

void setup()
{
 
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler))
    startMicros = micros();

    // You can use this with PWM_Freq in Hz
    ISR_PWM.setPWM(PWM_Pin, PWM_Freq, PWM_DutyCycle);
}

void loop()
{

```

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [February 6, 2023, 8:40am UTC](https://forum.arduino.cc/t/isr-timer-pi-pico/1086475/2 "2023-02-06T08:40:04Z")

</div>

Your topic has been moved to a more suitable location on the forum as this does not seem to be an IDE problem.

---

<div class="post-metadata">

**Author:** ![mojtaba195](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@mojtaba195](https://forum.arduino.cc/u/mojtaba195)\
**Post date:** [February 6, 2023, 11:12am UTC](https://forum.arduino.cc/t/isr-timer-pi-pico/1086475/3 "2023-02-06T11:12:29Z")

</div>

Thank you.  
I just became a member of the forum and I don't know much about the active members in the field of Raspberry Pico.  
Please help to solve the problem of my program.  
Thanks. 🌹

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 5, 2023, 11:13am UTC](https://forum.arduino.cc/t/isr-timer-pi-pico/1086475/4 "2023-08-05T11:13:01Z")

</div>

Closed on August 5, 2023, 11:13am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
