0

I have written a class for generating a 25khz pwm signal for the Arduino Nano. This works great (pin 9 and 10 only) on the Nano. I want to switch to Arduino Every; this code does no longer work (the registers are unknown ‘not declared in this scope’ is the compiler error). The Every is suppose to be a drop in replacement for the Nano; how to generate a 25KHz PWM on the Arduino Every?

#include <Arduino.h>
#include "HixPinPWM25KHz.h"

HixPinPWM25KHz::HixPinPWM25KHz(int nPinNumber): HixPin(nPinNumber) {
}

void HixPinPWM25KHz::begin() {
  //25KHz PWM only supported on ping 9 and 10
  if ( (m_nPinNumber != 9) && (m_nPinNumber != 10) ) {
    return;
  }
  // Configure Timer 1 for PWM @ 25 kHz.
  TCCR1A = 0;           // undo the configuration done by...
  TCCR1B = 0;           // ...the Arduino core library
  TCNT1  = 0;           // reset timer
  TCCR1A = _BV(COM1A1)  // non-inverted PWM on ch. A
           | _BV(COM1B1)  // same on ch; B
           | _BV(WGM11);  // mode 10: ph. correct PWM, TOP = ICR1
  TCCR1B = _BV(WGM13)   // ditto
           | _BV(CS10);   // prescaler = 1
  ICR1   = 320;         // TOP = 320
  pinMode(m_nPinNumber, OUTPUT);
  analogWrite(50);
}

void HixPinPWM25KHz::analogWrite(float percent) {
  int value = (int) ( (percent * (float)320) / (float)100 );
  switch (m_nPinNumber) {
    case 9:
      OCR1A = value;
      break;
    case 10:
      OCR1B = value;
      break;
    default:
      // no other pin will work
      break;
  }
}
  • did you try the "Registers emulation" "ATMEGA328" in Tools menu of Arduino IDE? EDIT: I did. It doesn't work for Timer1 registers – Juraj Aug 31 at 11:04
  • A "drop-in replacement" does not mean "exactly the same". It means you get the same footprint and IO pins. It does not mean that you get the exact same internal architecture. Step one must always be read the datasheet for the chip and learn about the internal workings of it. – Majenko Aug 31 at 11:07
  • The Every uses a ATMega4809, which has a very different timers than the nano's ATmega328. The advantage is that it has a mux, so almost every pin can be selected as a output for one of the four timers. – Gerben Aug 31 at 12:29
  • @Juraj also tried emulation "ATMEGA328" (just to confirm), this does not solve the issue. Still getting the same compiler error. – HixField Aug 31 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.