Go Down

Topic: Nano Every PWM frequency (Read 233 times) previous topic - next topic

Rick_Shoop

How do I change the PWM frequency? I tried the PWM library without success

68tjs

#1
Jul 29, 2019, 04:05 pm Last Edit: Jul 29, 2019, 04:05 pm by 68tjs
Arduino has never managed PWM  frequency change and does not manage it anymore with the Every.

Registry of 4809 are different from those of the 328p or 2560 you have to wait until the authors of PWM libraries meet their libraries up to date.

You can also read the 4809 datasheet to modify yourself the microcontroller registry value.

Ben84

How do I change the PWM frequency? I tried the PWM library without success
I´ve upgraded the "old" Nano with 328p to the nano Every board and have the same problem like you. I tried to increase the PWM-Frequency but without success so far. 

At the moment I´am struggling with the Datasheet so i tried to find help here :-). I will let you know when I succeed.

Ben84

I read a lot of application-notes yesterday. I hope this Timer-Config will help you. PWM-Freq. is 62.5kHz.

Code: [Select]
// Set PB0 as output (waveform output, pg. 204)
  PORTB.DIRSET = PIN0_bm;

  // Frequency: Fpwm_ss = Fclk_per/(N(PER+1))
  // Max resolution: Rpwm_ss = (log(PER+1))/(log(2))

  TCA0.SINGLE.PER = 256;

  // CMP sets the duty cycle of the PWM signal -> CT = CMP0 / PER
  // DUTY CYCLE is approximately 50% when CMP0 is PER / 2
  TCA0.SINGLE.CMP0 = 200;

  // Counter starts at 0
  TCA0.SINGLE.CNT = 0x00;

  // Configuring CTRLB register
  // Compare 0 Enabled: Output WO0 (PB0) is enabled
  // Single slope PWM mode is selected
  TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;

  // Using system clock (no frequency division, the timer clock frequency is Fclk_per)
  // Enable the timer peripheral
  TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;

Ben84

I found the better solution for Arduino. I think Arduino needs TCA for the system-control. If TCA is manipulated, the internal functions like millis() etc. wont work correctly.

Here is the better solution which use TCB:

Code: [Select]

 
pinMode(3, OUTPUT);   //Port F, Pin 5 = Arduino ~D3       
 
 
  /* set the alternate pin mux */
  PORTMUX.TCBROUTEA |= PORTMUX_TCB1_bm;
 
 
  TCB1.CCMPL = 255; /* PWM Period*/

  TCB1.CCMPH = 128;   /* PWM Compare*/

  TCB1.CTRLB = 0 << TCB_ASYNC_bp      /* Asynchronous Enable: disabled */
               | 1 << TCB_CCMPEN_bp   /* Pin Output Enable: enabled */
               | 0 << TCB_CCMPINIT_bp /* Pin Initial State: disabled */
               | TCB_CNTMODE_PWM8_gc; /* 8-bit PWM */

  // TCB1.DBGCTRL = 0 << TCB_DBGRUN_bp; /* Debug Run: disabled */

  // TCB1.EVCTRL = 0 << TCB_CAPTEI_bp /* Event Input Enable: disabled */
  //     | 0 << TCB_EDGE_bp /* Event Edge: disabled */
  //     | 0 << TCB_FILTER_bp; /* Input Capture Noise Cancellation Filter: disabled */

  // TCB1.INTCTRL = 0 << TCB_CAPT_bp; /* Setting: disabled */

  TCB1.CTRLA = TCB_CLKSEL_CLKDIV1_gc  /* CLK_PER (No Prescaling) */
               | 1 << TCB_ENABLE_bp   /* Enable: enabled */
               | 0 << TCB_RUNSTDBY_bp /* Run Standby: disabled */
               | 0 << TCB_SYNCUPD_bp; /* Synchronize Update: disabled */

               
  TCB1.CTRLA |= TCB_ENABLE_bm;


This code will provide a  62,5 kHz PWM with 50% DC (TCB1.CCMPH = 128;) on Pin D3 (which is PortF Pin 5). Further informations can be found in the datasheet 20.3.3.1.8 8-Bit PWM Mode and Atmel Start.




Go Up