Pwm and i2C frequency

Hi
I have a code that works on UNO with a modified pwm frequency and an I2C frequency at 400khz.
How modify it for nano every ?

	/* Change PWM frequency for heater pin to 122Hz */
 	TCCR1B = 1 << CS12;

	/* Set I2C frequency to 400kHz */
	TWBR = ((F_CPU / 400000L) - 16) / 2;

Use Wire.setClock() for the I2C clock speed on all the boards. I don’t know the PWM on the Nano Every.

Hi,

it depends on the pin from which you want to change the frequency. TCB has as own prescaler only 1 or 2, or the one from TCA. You can also change the TOP value. If you change TOP, analogWrite may not work properly. It is always better to reconfigure the TCBn manually. Then one knows what one does.

TCA has several usual prescalers.


  Arduino Nano Every PinOut (ATmega4809)
                  _
               __|_|__    
              |       |  
  (D13)  PE2  +       +  PE1  (D12)  
        3.3V  +       +  PE0  (D11)  
  (AREF) PD7  +       +  PB1  (D10)(~)(TCA0 Channel 1)
  (D14)  PD3  +       +  PB0  (D 9)(~)(TCA0 Channel 0)
  (D15)  PD2  +       +  PE3  (D 8)
  (D16)  PD1  +       +  PA1  (D 7) 
  (D17)  PD0  +       +  PF4  (D 6)(~)(TCB0)
  (D18)  PF2  +       +  PB2  (D 5)(~)(TCA0 Channel 2)
  (D19)  PF3  +       +  PC6  (D 4)  
  (D20)  PD4  +       +  PF5  (D 3)(~)(TCB1)  
  (D21)  PD5  +       +  PA0  (D 2) 
        5.0V  +       +  GND
       RESET  +       +  RESET
         GND  +       +  PC5  (D 0) RX1
         VIN  +       +  PC4  (D 1) TX1      
              |_______|  


           |    Event       |      CCL-LUTn     |       TCA0        |       TCBn        |
  Pin Port | Port | Channel | default | PortMux | default | PortMux | default | PortMux |
   0  PC5  |  0     2 or 3  |   ---       ---   |   ---      0-WO5  |   ---       ---   |
   1  PC4  |  0     2 or 3  |   ---       ---   |   ---      0-WO4  |   ---       ---   |
   2  PA0  |  0     0 or 1  |  0-IN0      ---   |  0-WO0      ---   |   ---       ---   |
   3  PF5  |  1     4 or 5  |   ---       ---   |   ---      0-WO5  |   ---      1-WO   |
   4  PC6  |  0     2 or 3  |   ---      1-OUT  |   ---       ---   |   ---       ---   |
   5  PB2  |  1     0 or 1  |   ---       ---   |   ---      0-WO2  |   ---       ---   |
   6  PF4  |  1     4 or 5  |   ---       ---   |   ---      0-WO4  |   ---      0-WO   |
   7  PA1  |  0     0 or 1  |  0-IN1      ---   |  0-WO1      ---   |   ---       ---   |
   8  PE3  |  0     4 or 5  |   ---       ---   |   ---      0-WO3  |   ---       ---   |
   9  PB0  |  1     0 or 1  |   ---       ---   |   ---      0-WO0  |   ---       ---   |
  10  PB1  |  1     0 or 1  |   ---       ---   |   ---      0-WO1  |   ---       ---   |
  11  PE0  |  0     4 or 5  |   ---       ---   |   ---      0-WO0  |   ---       ---   |
  12  PE1  |  0     4 or 5  |   ---       ---   |   ---      0-WO1  |   ---       ---   |
  13  PE2  |  0     4 or 5  |   ---       ---   |   ---      0-WO2  |   ---       ---   |
  14  PD3  |  1     2 or 3  |  2-OUT      ---   |   ---      0-WO3  |   ---       ---   |
  15  PD2  |  1     2 or 3  |  2-IN2      ---   |   ---      0-WO2  |   ---       ---   |
  16  PD1  |  1     2 or 3  |  2-IN1      ---   |   ---      0-WO1  |   ---       ---   |
  17  PD0  |  1     2 or 3  |  2-IN0      ---   |   ---      0-WO0  |   ---       ---   |
  18  PF2  |  1     4 or 5  |  3-IN2      ---   |   ---      0-WO2  |   ---       ---   |  Doppelbelegung mit PA2 beachten !!!
  19  PF3  |  1     4 or 5  |  3-OUT      ---   |   ---      0-WO3  |   ---       ---   |  Doppelbelegung mit PA3 beachten !!!
  20  PD4  |  1     2 or 3  |   ---       ---   |   ---      0-WO4  |   ---       ---   |
  21  PD5  |  1     2 or 3  |   ---       ---   |   ---      0-WO5  |   ---       ---   |

Thanks Koepel and Doc_Arduino
I used pin3 but i can change it.
I will also leave the clock at 16 Mhz by default.
TCA has an influence on milli it seems to me, It would therefore be necessary to use TCB but there is not enough prescaler!

Can you give me a code for please ?

Hello,

a simple example to think about. Note that for 100% duty cycle LSB must be one less than MSB. The formula and the more detailed explanation can be found in the ATmega4809 Manual

/*
  https://forum.arduino.cc/t/pwm-and-i2c-frequency/856544/5
  
  Doc_Arduino - german Arduino Forum
  Arduino IDE v1.8.13
  Arduino Nano Every
  03.05.2021

  Example 1
  MSB 0 und LSB 1 ..... 0% Duty Cycle
  MSB 1 und LSB 1 .... 50% Duty Cycle
  MSB 2 und LSB 1 ... 100% Duty Cycle

  Example 2  
  MSB 0 und LSB 5 ..... 0% Duty Cycle
  MSB 1 und LSB 5 .... 17% Duty Cycle
  MSB 2 und LSB 5 .... 33% Duty Cycle
  MSB 3 und LSB 5 .... 50% Duty Cycle
  MSB 4 und LSB 5 .... 67% Duty Cycle
  MSB 5 und LSB 5 .... 83% Duty Cycle
  MSB 6 und LSB 5 ... 100% Duty Cycle
*/

const byte pinTCB1 {3};

void setup (void)
{
  initPins();
  initTCB1();
  updateTCB1(128,254);
}

void loop (void)
{
  // updateTCB1(10,123);    // MSB Duty Cycle / LSB Periode
}

void initPins (void)
{
  pinMode(pinTCB1, OUTPUT);
}

void initTCB1 (void)
{
  TCB1.CTRLA = 0;
  TCB1.CTRLB = 0;
  TCB1.INTCTRL = 0;
  TCB1.CTRLA |= TCB_CLKSEL_CLKDIV2_gc;
  TCB1.CTRLB |= TCB_CNTMODE_PWM8_gc;
  TCB1.CTRLB |= TCB_CCMPEN_bm;
}


void updateTCB1 (byte duty, const byte periode)
{
  static byte lastMSB {0};
  static byte lastLSB {0};

  if ( (lastMSB != duty) || (lastLSB != periode) )
  {
    // safety check
    if ( (periode < 255) && (duty > periode+1) ) duty = periode+1;

    TCB1.CTRLA &= ~TCB_ENABLE_bm;       // stop
    TCB1.CNT = 0;                       // reset 
    TCB1.CCMP = (duty << 8) | periode;  // set new value, MSB Duty Cycle / LSB Periode
    TCB1.CTRLA |= TCB_ENABLE_bm;        // start

    lastMSB = duty;
    lastLSB = periode;
  }
}