Git Product home page Git Product logo

arduino-nano-every-timer-controller-b's Introduction

Arduino EveryTimerB library.

A drop in replacement for the TimerOne library for the Nano Every.
EveryTimerB is a library for the TCB timer of the AtMega4809 processor.
The library is tested on the Arduino Nano Every (AtMega4809) with the Arduino 1.8.12 IDE
To add support for the Every activate the 'Arduino MegaAVR' boards module (Tools | Board | Boards Manager)

usage:

#ifdef ARDUINO_ARCH_MEGAAVR
#include "EveryTimerB.h"
#define Timer1 TimerB2 // use TimerB2 as a drop in replacement for Timer1
#else // assume architecture supported by TimerOne ....
#include "TimerOne.h"
#endif

// code below will now work both on the MegaAVR and AVR processors
void setup() {
Timer1.initialize();
Timer1.attachInterrupt(myisr);
Timer1.setPeriod(1000000); // like the TimerOne library this will start the timer as well
}

void myisr() {
// do something useful every second
}

clock source options:

The TCB clock source is specified in the initialize() function with default value EveryTimerB_CLOCMODE.
define this macro before including this file to use a different default clock mode
e.g.:
#define EveryTimerB_CLOCMODE TCB_CLKSEL_CLKTCA_gc // 250 kHz ~ 4 us
#define EveryTimerB_CLOCMODE TCB_CLKSEL_CLKDIV2_gc // 8 MHz ~ 0.125 us
#define EveryTimerB_CLOCMODE TCB_CLKSEL_CLKDIV_gc // 16 MHz ~ 0.0625 us

timer options:

The 4809 has one A timer (TCA) and four B timers (TCB).
TCA is used by the arduino core to generate the clock used by millis() and micros().
By default Timer Control B2 is defined as TimerB2 in the EveryTimerB library
TCB0 and TCB1 control PWM of pins 6 and 3. If you don't use PWM on these pins
you can define a TimerB object for TCB0 and TCB1 and copy the ISR code from the
EveryTimerB.cpp file.
The Arduino Core is using timer B3 for millis() and micros() so don't mess with
TCB3 if you don't want to break the functionality of these functions.

for information on the 4809 TCA and TCB timers:

http://ww1.microchip.com/downloads/en/AppNotes/TB3217-Getting-Started-with-TCA-90003217A.pdf
http://ww1.microchip.com/downloads/en/Appnotes/TB3214-Getting-Started-with-TCB-90003214A.pdf
%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\megaavr\1.8.5\cores\arduino\wiring.c
%LOCALAPPDATA%\Arduino15\packages\arduino\hardware\megaavr\1.8.5\variants\nona4809\variant.c
%LOCALAPPDATA%\Arduino15\packages\arduino\tools\avr-gcc\7.3.0-atmel3.6.1-arduino5\avr\include\avr\iom4809.h

20 MHz system clock

to run the Every at 20 MHz, add the lines below to the nona4809 section of the boards.txt file
in %LOCALAPPDATA%\Arduino15\packages\arduino\hardware\megaavr\1.8.5.
they add the sub menu 'Tools | Clock' to choose between 16MHz and 20MHz.
menu.clock=Clock
nona4809.menu.clock.16internal=16MHz
nona4809.menu.clock.16internal.build.f_cpu=16000000L
nona4809.menu.clock.16internal.bootloader.OSCCFG=0x01
nona4809.menu.clock.20internal=20MHz
nona4809.menu.clock.20internal.build.f_cpu=20000000L
nona4809.menu.clock.20internal.bootloader.OSCCFG=0x02

On 20Mhz, the milis() and micros() implementation of the arduino core is inaccurate.
The functions in MegaAvr20Mhz.h correct for that.

to do:

there is no range check on the 'period' arguments of setPeriod ...

arduino-nano-every-timer-controller-b's People

Contributors

kees-van-der-oord avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

arduino-nano-every-timer-controller-b's Issues

disablePwm function on TimerOne not implemented in EveryTimerB

I'm trying to get a timer interrupt without any pins changing. Unfortunately I can't understand your comment in the readme about how to do this for EveryTimerB.

" If you don't use PWM on these pins you can define a TimerB object for TCB0 and TCB1 and copy the ISR code from the
EveryTimerB.cpp file."

My UNO/Mega code uses the TimerOne disablePwm() like this:

  Timer1.initialize(NORMAL_SIGNAL_TIME);
   // See disablePwm in TinerOne.h for possible pins to disable
  #ifdef TIMER1_A_PIN
   Timer1.disablePwm(TIMER1_A_PIN);
  #endif
  #ifdef TIMER1_B_PIN
   Timer1.disablePwm(TIMER1_B_PIN);
  #endif
  #ifdef TIMER1_C_PIN
   Timer1.disablePwm(TIMER1_C_PIN);
  #endif
   Timer1.attachInterrupt(interruptHandler);
   Timer1.start();

Any help would be appreciated.

Invalid library error when trying to add

Tying to install the zip version of the library for the Nano Every. I am getting this error:
Specified folder/zip file does not contain a valid library

I also extracted the library and install it manually. The IDE does not see the library in the include directory and throws and invalid library error when I compile a sketch.

I am not very versed in programming so any comments appreciated.

invalid conversion from 'int' to 'TCB_CLKSEL_t

Hi

trying to verify the code, I get a lot of errors regarding conversions and "large integer implicitly truncated to unsigned type"

using Arduino 1.8.13

I have a new project file that only has the includes from your sample and I get this:

In file included from D:\users\XXX\Documents\Arduino\sketch_oct07a\sketch_oct07a.ino:3:0: D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h: In member function 'TCB_CLKSEL_t EveryTimerB::getClockSource()': D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h:88:25: warning: invalid conversion from 'int' to 'TCB_CLKSEL_t {aka TCB_CLKSEL_enum}' [-fpermissive] return timer->CTRLA & (TCB_CLKSEL_CLKTCA_gc | TCB_CLKSEL_CLKDIV2_gc | TCB_CLKSEL_CLKDIV1_gc); ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from D:\users\XXX\Documents\Arduino\sketch_oct07a\sketch_oct07a.ino:3:0: D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h: In member function 'void EveryTimerB::setPeriod(long unsigned int)': D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h:25:24: warning: large integer implicitly truncated to unsigned type [-Woverflow] #define TCB_RESOLUTION 65536UL // TCB is 16 bit ^ D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h:151:25: note: in expansion of macro 'TCB_RESOLUTION' countsPerOverflow = TCB_RESOLUTION; ^~~~~~~~~~~~~~ In file included from D:\users\XXX\Documents\Arduino\sketch_oct07a\sketch_oct07a.ino:3:0: D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h: In member function 'TCB_CNTMODE_enum EveryTimerB::getMode()': D:\users\XXX\Documents\Arduino\libraries\EveryTimerB/EveryTimerB.h:242:25: warning: invalid conversion from 'int' to 'TCB_CNTMODE_enum' [-fpermissive] return timer->CTRLB & 0x7; ~~~~~~~~~~~~~^~~~~

when adding more libraries, I get a lot more errors?
no errors appear when not importing EveryTimerB.

any idea what I'm doing wrong?
thanks

Change Frequency

Hello there :)
thanks for the NanoEvery Implementation.
I want to generate a PWM signal for a Servo Motor and need to change the frequency to change the motor speed.
While initialising the Timer object with other default values (default is 1s - i changed it to 30microseconds) the frequency changes like expected. But afterwards i can't change the period with setPeriod().

Do i have to diable the object in some way? Or is that not possible?

Greetings :)

File name case incorrect

The code imports MegaAvr20MHz.h but the file name is MegaAvr20Mhz.h with a lower case h in Mhz. This
breaks on linux where the file system is case sensitive.

variable frequency PWM

Dear Sir, can your library be used to varies PWM frequency, by using variable potentiometer. TQVM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.