PaulStoffregen / FreqMeasure

Measures the elapsed time during each cycle of an input frequency.
http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
72 stars 31 forks source link

support for ATmega4808/4809 #17

Open iw2lsi opened 4 years ago

iw2lsi commented 4 years ago

Hi,

are there plans for supporting ATmega4808/4809 (ie Arduino every board) ?

     thanks in advance

          Giampaolo
PaulStoffregen commented 4 years ago

Does waiting and willing to merge an open source community contribution count as a "plan"?

iw2lsi commented 4 years ago

Hello Paul,

of course it does !    :-) :-) :-)

I'm looking at the datasheet of the ATmega4809 right now... hope I'll be able to help... even though I have very little experience on AVR.

Best Regards

       Giampaolo
PaulStoffregen commented 4 years ago

Please try to make all (or at least most) of your changes in FreqMeasureCapture.h, following the approach all the other boards use.

LLRI commented 4 years ago

iw2lsi : I know its an old thread, but have you finally found the correct parameter to insert in FreqMeasureCapture.h to make it work with ATmega4808/4809?

iw2lsi commented 4 years ago

Hello Paul,

this is what I've done for the project I was working on... unfortunately I had no time to integrate this into the library.

hope it can help

      Giampaolo

// La frequenza di default della nano every definita in C:\Users\\Documents\ArduinoData\packages\arduino\hardware\megaavr\1.8.6\boards.txt // e' di 16MHz contro i 20MHz massimi teorici, corrispondenti ad un periodo T=0.0625us; // Con un contatore a 16bit si raggiunge un Tmax=4096us pari a 244Hz... e i test fatti confermano che con queste impostazioni si // riescono a misurare frequenze dai 244Hz in su... // // Impostando il prescaler DIV2 si ottiene una frequenza massima misurabile 16MHz/2=8MHz 8MHz/65536=122Hz e anche in questo caso si possono quindi misurare // frequenze dai 122Hz in su... // // Per poter misurare frequenze inferiori occorre impostare il TCB affinche' non usi CLK_PER o CLK_PER/2 ma CLK_TCA da TCA0... // di default il TCA della every sembrerebbe essere configurato a 250kHz il che consente di misurare frequenze da 250kHz/65536=3.8Hz in su

void initTCB(void) { // pinMode(3, INPUT); // Port F, Pin 5 = Arduino ~D3

// pinMode(9, INPUT); // Port B, Pin 0 = Arduino ~D9

pinMode(10, INPUT); // Port B, Pin 1 = Arduino ~D10

PORTMUX.TCBROUTEA |= PORTMUX_TCB1_bm; // PORTMUX Control for TCB: Route TCB1 signal to PF5 aka D3 (default is PA3/SCL aka D19)

// ~~~

TCB1.CTRLB = 0 << TCB_ASYNC_bp // TCBn.CTRLB: Asynchronous Enable: disabled // | 1 << TCB_CCMPEN_bp // TCBn.CTRLB: Pin Output Enable: enabled // | 0 << TCB_CCMPINIT_bp // TCBn.CTRLB: Pin Initial State: disabled // | TCB_CNTMODE_PWM8_gc; // TCBn.CTRLB: 8-bit PWM | TCB_CNTMODE_FRQ_gc; // TCBn.CTRLB: Input Capture Frequency measurement

// TCB1.DBGCTRL = 0 << TCB_DBGRUN_bp; // TCBn.DBGCTRL: Debug Run (disabled)

TCB1.EVCTRL = 1 << TCB_CAPTEI_bp // TCBn.EVCTRL: Event Input Enable (enabled) | 0 << TCB_EDGE_bp // TCBn.EVCTRL: Event Edge: (positive edge) | 1 << TCB_FILTER_bp; // TCBn.EVCTRL: Input Capture Noise Cancellation Filter (enabled)

// TCB1.CTRLA = TCB_CLKSEL_CLKDIV1_gc // TCBn.CTRLA: CLK_PER (CLKDIV1 aka No-Prescaling) // TCB1.CTRLA = TCB_CLKSEL_CLKDIV2_gc // TCBn.CTRLA: CLK_PER/2 (CLKDIV2) TCB1.CTRLA = TCB_CLKSEL_CLKTCA_gc // TCBn.CTRLA: Use Clock from TCA | 1 << TCB_ENABLE_bp // TCBn.CTRLA: Enable bit position (enabled) | 0 << TCB_RUNSTDBY_bp // TCBn.CTRLA: Run Standby bit position (disabled) | 0 << TCB_SYNCUPD_bp; // TCBn.CTRLA: Synchronize Update bit position (disabled)

TCB1.INTCTRL = 1 << TCB_CAPT_bp; // TCBn.INTCTRL: Capture Interrupt Enable (enabled)

// WARNING! Not all generators can be connected to all channels! // By using the EVSYS, any event source, such as an external clock signal on any I/O pin, may be used as a control logic input. // When an event action controlled operation is used, the clock selection must be set to use an event channel as the counter input.

// EVSYS.CHANNEL3 = 0b01001100; // event channel 3 connects to pin PD4 // EVSYS.USERTCB1 = 4; // connect the counter to event channel 3 (4-1)

// EVSYS.CHANNEL5 = 0b01001101; // event channel 5 connects to pin PF5 (Port F, Pin 5 = Arduino ~D3) // EVSYS.USERTCB1 = 6; // connect the counter to event channel 5 (6-1)

// EVSYS.CHANNEL0 = 0b01001000; // event channel 0 connects to pin PB0 (Port B, Pin 0 = Arduino ~D9) // EVSYS.USERTCB1 = 1; // connect the counter to event channel 0 (1-1)

// EVSYS.CHANNEL1 = 0b01001000; // event channel 1 connects to pin PB0 (Port B, Pin 0 = Arduino ~D9) // EVSYS.USERTCB1 = 2; // connect the counter to event channel 1 (2-1)

EVSYS.CHANNEL1 = 0b01001001; // event channel 1 connects to pin PB1 (Port B, Pin 1 = Arduino ~D10) EVSYS.USERTCB1 = 2; // connect the counter to event channel 1 (2-1)

// ~~~

TCB1.CTRLA |= TCB_ENABLE_bm; // TCBn.CTRLA: bit0=Enable bit mask }

// TCB1_INT_vect ISR // The CAPT Interrupt flag is automatically cleared after the low byte of the Compare/Capture (TCBn.CCMP) register has been read. // TCBn.CCMP H/L: For Capture operation, these registers contain the captured value of the counter at the time the capture occurs

ISR(TCB1_INT_vect) { uint16_t value; static bool toggle;

TCB1_isr_count++; value = TCB1.CCMP; meanFilter.AddValue(value); digitalWrite(FREQ_TOGGLE_PIN , toggle ? HIGH : LOW); // diagnostic only toggle = !toggle; }

Il giorno gio 22 ott 2020 alle ore 04:58 LLRI notifications@github.com ha scritto:

iw2lsi : I know its an old thread, but have you finally found the correct parameter to insert in FreqMeasureCapture.h to make it work with ATmega4808/4809?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/PaulStoffregen/FreqMeasure/issues/17#issuecomment-714189082, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAODQQDB5E73HVXMVUUC3WDSL6NTXANCNFSM4OVM27WQ .

darrylnoakes commented 2 years ago

Any further progress with this?