TCB0 as PWM o/p routed to D6 pin in Nano Every

i am configuring TCB0 as PWM o/p routed to D6 pin in nano every via the portmux settings. CLKPER/2 ~ 8Mhz option is the clk given to TCB0, dividing in 8BIT PWM mode by 255 approx 31Khz PWM

the PWM o/p is still not coming out.. here is the code below.. any help for debug is appreciated.`

PORTMUX.TCBROUTEA = 0x01 ; // TCB0 o/p routed to PF4/D6 - Arduino port

TCB0.CTRLA = 0x0 ;

TCB0.CTRLB = 0x0 ;

TCB0.CTRLA = 0x02 ; // CLKPER/2 Chosen - 8Mhz, but TCB disabled still

//TCB0.CTRLA = 0x00 ; // CLKPER Chosen - 16Mhz, but TCB disabled still

TCB0.CTRLB = 0x07 ; // 8 bit PWM mode chosen for TCB0 timer

TCB0.CCMPL = 255;

TCB0.CCMPH = 127 ;

TCB0.CNT = 0 ;

TCB0.INTFLAGS = 0x01 ;

TCB0.INTCTRL = 0x01 ; // interrupt enabled in 8bit PWM mode

TCB0.CTRLA |= 0x01 ; // enable timer

ISR(TCB0_INT_Vect)
{
    TCB0.INTFLAGS = 0x01 ; // flag cleared by writing 1
}

particularly are the CCMPH and CCMPL register loadings correct?

How do you know there is nothing coming out? What did you use to measure it?

Have you set pin 6 as an output?

1 Like

s pin 6 is a o/p

i have a multimeter with a Hz mode and it measures TTL/CMOS level signals very well.....upto 100Khz. It has given very good measurements in the past Arduino projects i have done.

First of all please post all your code so we can try it ourselves.
Second the Nano Every uses a different processor to the Nano Classic so it is likely to have different internal timer structure and so will not work with the register based code you seem to beus8 g.

Where are you providing definitions for TOPPWMVALUE and DUTYVALUE.

This example code uses the syntax of the data sheet to set register values instead of hex values, and uses TCA0 clock/4 instead of the system clock and is to output 25KHz on pin 6.


void setup() {
  Serial.begin(115200);
  pinMode(6, OUTPUT); //default output pin for TCB0
  // Setup TCB0 routing variant.c file
  //#if defined(TCB0)
  //  PORTMUX.TCBROUTEA |= PORTMUX_TCB0_bm; // Route signal to PF4
  //#endif

  // We use TCBO to generate the output. Note that you can change the duty cycle
  // by varying TCB0.CCMPH.
  TCB0.CTRLA = 0; // Turn off channel for configuring
  //TCB0.CTRLB = 0x17; // output enabled, PWM mode
  TCB0.CTRLB = TCB_CCMPEN_bm | TCB_CNTMODE_PWM8_gc; //output enable and pwm8 mode
  TCB0.CCMPL = 4000000L / 25000L - 1; // Set period as close to
  // giving 38kHz as we can achieve
  TCB0.CCMPH = (4000000L / 25000L) / 4; // 25% duty cycle
  //Use the TCA0 clock for timer B0
  //TCB0.CTRLA = 5; // 3 =Enable and use system clock/2  5 = use clkA
  TCB0.CTRLA = TCB_ENABLE_bm | TCB_CLKSEL_CLKTCA_gc;

  //Set TCA0 clock source to system clock/4
  //TCA0.SPLIT.CTRLA = 5; //16M/DIV4 = 4000000
  TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV4_gc | TCA_SINGLE_ENABLE_bm;





  pinMode(13, OUTPUT);
  Serial.println(TCB0.CTRLB, BIN);
  //Serial.println(TCB0.CTRLB.CCMPEN);
  Serial.println(TCB3.CTRLA, BIN);
  Serial.println(TCB3.CTRLB, BIN); //still uses sysclk/2

}

void loop() {
  //does not break millis() on TCB3
  static unsigned long millisLast = millis();
  if (millis() - millisLast >= 5000)
  {
    millisLast = millis();
    digitalWrite(13, !digitalRead(13));
  }
}
1 Like

no, all the registers are ATMega4809 relevant that is used on Nano Every. rest assured of that

pls try the code i have posted already

i am using 255 value for TOPPWMVALUE and 50 for duty cycle constant..

in my example i dont use TCA0 PRESCALED clk to TCB0, i directly use the CLKPER/2 8Mhz clk to TCB0

What you have posted is not a complete listing. If you can't be bothered posting the full code then I can't be bothered trying to reconstruct what you already have and could easily post.

Sorry but you are the one asking for help, and you are not being helpful.

3 Likes

the Only thing i missed is putting the single line ISR, i have updated it on the code tag in the post.

pls try the code...and help me.

U may use Value 255 for TOPPWMVALUE & DUTYVALUE anywhere b/w 1 and 250 . just to get a 50% approx duty cycle, we can use value 127

i have updated the ISR code, pls try the code..

since the signal is not coming out on the D6 pin, i definitely suspect my portmux settings...

i today used a picoscope oscilloscope to check the signal, PWM is indeed not coming out on the D6 pin of the arduino nano every board.

maybe my PORTMUX register programming is not correct as that is the step that shud route the signal to the D6 pin....

My guess is that is correct. I am not familiar with that part so this is the best I can do.

Your code is not a complete example which we can compile and run.

You say that pin6 is an output, but I don't see anything in your code which does that.

The default PORTMUX settings of the arduino core set the output to PF4, so you really should not need a statement in your code.

// Setup TCB0 routing
  #if defined(TCB0)
    PORTMUX.TCBROUTEA |= PORTMUX_TCB0_bm; // Route signal to PF4
  #endif

From what I see in the data sheet, section 15.3.6 PORTMUX CONTROL FOR TCB you have set the correct bit in the register with
PORTMUX.TCBROUTEA = 0x01

You can try the data sheet syntax, but I do not think it will make a difference.
PORTMUX.TCBROUTEA = PORTMUX_TCB0_bm;

That code will not compile.
Here are the error messages I get:-

Arduino: 1.8.19 (Mac OS X), Board: "Arduino Nano Every, ATMEGA328"











/Applications/Arduino.app/Contents/Java/arduino-builder -dump-prefs -logger=machine -hardware /Applications/Arduino.app/Contents/Java/hardware -hardware /Users/mikecook/Library/Arduino15/packages -hardware /Users/mikecook/Documents/Arduino/hardware -tools /Applications/Arduino.app/Contents/Java/tools-builder -tools /Applications/Arduino.app/Contents/Java/hardware/tools/avr -tools /Users/mikecook/Library/Arduino15/packages -built-in-libraries /Applications/Arduino.app/Contents/Java/libraries -libraries /Users/mikecook/Documents/Arduino/libraries -fqbn=arduino:megaavr:nona4809:mode=on -ide-version=10819 -build-path /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174 -warnings=none -build-cache /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_cache_465150 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.avr-gcc.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino5.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5 -prefs=runtime.tools.avrdude.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -verbose /Users/mikecook/Desktop/nano_every_problem/nano_every_problem.ino
/Applications/Arduino.app/Contents/Java/arduino-builder -compile -logger=machine -hardware /Applications/Arduino.app/Contents/Java/hardware -hardware /Users/mikecook/Library/Arduino15/packages -hardware /Users/mikecook/Documents/Arduino/hardware -tools /Applications/Arduino.app/Contents/Java/tools-builder -tools /Applications/Arduino.app/Contents/Java/hardware/tools/avr -tools /Users/mikecook/Library/Arduino15/packages -built-in-libraries /Applications/Arduino.app/Contents/Java/libraries -libraries /Users/mikecook/Documents/Arduino/libraries -fqbn=arduino:megaavr:nona4809:mode=on -ide-version=10819 -build-path /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174 -warnings=none -build-cache /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_cache_465150 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.arduinoOTA.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.arduinoOTA-1.3.0.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/arduinoOTA/1.3.0 -prefs=runtime.tools.avr-gcc.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5 -prefs=runtime.tools.avr-gcc-7.3.0-atmel3.6.1-arduino5.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5 -prefs=runtime.tools.avrdude.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -prefs=runtime.tools.avrdude-6.3.0-arduino17.path=/Users/mikecook/Library/Arduino15/packages/arduino/tools/avrdude/6.3.0-arduino17 -verbose /Users/mikecook/Desktop/nano_every_problem/nano_every_problem.ino
Using board 'nona4809' from platform in folder: /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6
Using core 'arduino' from platform in folder: /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6
Detecting libraries used...
/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega4809 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO_EVERY -DARDUINO_ARCH_MEGAAVR -DAVR_NANO_4809_328MODE -DMILLIS_USE_TIMERB3 -DNO_EXTERNAL_I2C_PULLUP -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/deprecated -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/variants/nona4809 /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/sketch/nano_every_problem.ino.cpp -o /dev/null
Generating function prototypes...
/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=atmega4809 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO_EVERY -DARDUINO_ARCH_MEGAAVR -DAVR_NANO_4809_328MODE -DMILLIS_USE_TIMERB3 -DNO_EXTERNAL_I2C_PULLUP -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/deprecated -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/variants/nona4809 /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/sketch/nano_every_problem.ino.cpp -o /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/preproc/ctags_target_for_gcc_minus_e.cpp
/Applications/Arduino.app/Contents/Java/tools-builder/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/preproc/ctags_target_for_gcc_minus_e.cpp
Compiling sketch...
/Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/bin/avr-g++ -c -g -Os -w -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=atmega4809 -DF_CPU=16000000L -DARDUINO=10819 -DARDUINO_AVR_NANO_EVERY -DARDUINO_ARCH_MEGAAVR -DAVR_NANO_4809_328MODE -DMILLIS_USE_TIMERB3 -DNO_EXTERNAL_I2C_PULLUP -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/deprecated -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino -I/Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/variants/nona4809 /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/sketch/nano_every_problem.ino.cpp -o /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/sketch/nano_every_problem.ino.cpp.o
In file included from /Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/avr/include/avr/io.h:677:0,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/tools/avr-gcc/7.3.0-atmel3.6.1-arduino5/avr/include/avr/pgmspace.h:90,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/String.h:31,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/Print.h:24,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/Stream.h:25,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/Client.h:22,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/api/ArduinoAPI.h:29,
                 from /Users/mikecook/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.6/cores/arduino/Arduino.h:23,
                 from /var/folders/nw/8ftfyrz54555t_cm5m55kkyr0000gn/T/arduino_build_245174/sketch/nano_every_problem.ino.cpp:1:
nano_every_problem:1:1: error: expected ')' before '*' token
 PORTMUX.TCBROUTEA = 0x01 ; // TCB0 o/p routed to PF4/D6 - Arduino port
 ^
nano_every_problem:1:1: error: expected ')' before '*' token
nano_every_problem:3:1: error: expected ')' before '*' token
 TCB0.CTRLA = 0x0 ;
 ^
nano_every_problem:3:1: error: expected ')' before '*' token
nano_every_problem:5:1: error: expected ')' before '*' token
 TCB0.CTRLB = 0x0 ;
 ^
nano_every_problem:5:1: error: expected ')' before '*' token
nano_every_problem:7:1: error: expected ')' before '*' token
 TCB0.CTRLA = 0x02 ; // CLKPER/2 Chosen - 8Mhz, but TCB disabled still
 ^
nano_every_problem:7:1: error: expected ')' before '*' token
nano_every_problem:11:1: error: expected ')' before '*' token
 TCB0.CTRLB = 0x07 ; // 8 bit PWM mode chosen for TCB0 timer
 ^
nano_every_problem:11:1: error: expected ')' before '*' token
nano_every_problem:13:1: error: expected ')' before '*' token
 TCB0.CCMPL = 255;
 ^
nano_every_problem:13:1: error: expected ')' before '*' token
nano_every_problem:15:1: error: expected ')' before '*' token
 TCB0.CCMPH = 127 ;
 ^
nano_every_problem:15:1: error: expected ')' before '*' token
nano_every_problem:17:1: error: expected ')' before '*' token
 TCB0.CNT = 0 ;
 ^
nano_every_problem:17:1: error: expected ')' before '*' token
nano_every_problem:19:1: error: expected ')' before '*' token
 TCB0.INTFLAGS = 0x01 ;
 ^
nano_every_problem:19:1: error: expected ')' before '*' token
nano_every_problem:21:1: error: expected ')' before '*' token
 TCB0.INTCTRL = 0x01 ; // interrupt enabled in 8bit PWM mode
 ^
nano_every_problem:21:1: error: expected ')' before '*' token
nano_every_problem:23:1: error: expected ')' before '*' token
 TCB0.CTRLA |= 0x01 ; // enable timer
 ^
nano_every_problem:23:1: error: expected ')' before '*' token
exit status 1
expected ')' before '*' token

I also see that this code has no setup function and no loop function.

So I recon you are still being very uncooperative.

You have failed to set the CCMPEN bit in TCB0.CTRLB

Bit 4 – CCMPEN Compare/Capture Output Enable
Writing this bit to ‘1’ enables the waveform output. This will make the waveform output available on the corresponding
pin, overriding the value in the corresponding PORT output register. The corresponding pin direction must be
configured as an output in the PORT peripheral.
Value Description
0 Waveform output is not enabled on the corresponding pin
1 Waveform output will override the output value of the corresponding pin

void setup()
{
pinMode(6,OUTPUT);
PORTMUX.TCBROUTEA = 0x01 ; // TCB0 o/p routed to PF4/D6 - Arduino port

TCB0.CTRLA = 0x0 ;
TCB0.CTRLB = 0x0 ;

TCB0.CTRLA = 0x02 ; // CLKPER/2 Chosen - 8Mhz, but TCB disabled still
//TCB0.CTRLA = 0x00 ; // CLKPER Chosen - 16Mhz, but TCB disabled still

TCB0.CTRLB = 0x07 ; // 8 bit PWM mode chosen for TCB0 timer
TCB0.CTRLB |= 0x10 ; //set CCMPEN bit

TCB0.CCMPL = 255;
TCB0.CCMPH = 127 ;
TCB0.CNT = 0 ;

TCB0.CTRLA |= 0x01 ; // enable timer

}

void loop()
{}

pls compile w/ATMEGA4809 OPTION , 328 option will not work

ur absolutely right, Cattledog...let me add that CCMPEN bit enable and try it.

I tried it just now and it worked. and i got the ~ 31.2Khz PWM

w/o this forum i cant imagine doing my projects!!

Thanks very much

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