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
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
#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()
{