Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed error on first measurement + added averaging parameter #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 70 additions & 13 deletions FreqMeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
* Copyright (c) 2011 PJRC.COM, LLC - Paul Stoffregen <[email protected]>
*
* Version 1.1
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -33,9 +33,15 @@ static volatile uint8_t buffer_tail;
static uint16_t capture_msw;
static uint32_t capture_previous;

static volatile uint16_t nbPeriod = 1;
static volatile uint16_t cptPeriod = 0;


void FreqMeasureClass::begin(void)
void FreqMeasureClass::begin(uint16_t nbAverage) //MOD TG 20 april 2018
{
cptPeriod = 0; //MOD TG 20 april 2018 to count the number of averaged periods
nbPeriod = nbAverage;

capture_init();
capture_msw = 0;
capture_previous = 0;
Expand Down Expand Up @@ -122,10 +128,30 @@ ISR(TIMER_CAPTURE_VECTOR)
capture_overflow_reset();
capture_msw++;
}

if (capture_previous == 0) { //MOD TG 20 april 2018
capture_previous = ((uint32_t)capture_msw << 16) | capture_lsw;
return;
}

cptPeriod++; //MOD TG 20 april 2018

if (cptPeriod == nbPeriod) { //MOD TG 20 april 2018
// compute the waveform period
capture = ((uint32_t)capture_msw << 16) | capture_lsw;
period = capture - capture_previous;
capture_previous = capture;
capture = ((uint32_t)capture_msw << 16) | capture_lsw;
if (capture < capture_previous) {
period = 2^32 - capture_previous ;
period += capture;
} else {
period = capture - capture_previous;
}
capture_previous = capture;
cptPeriod = 0;

} else {
return; //MOD TG 20 april 2018
}

// store it into the buffer
i = buffer_head + 1;
if (i >= FREQMEASURE_BUFFER_LEN) i = 0;
Expand All @@ -147,16 +173,47 @@ void FTM_ISR_NAME (void)
capture_msw++;
inc = true;
}


if (capture_event()) {

capture = capture_read();
if (capture <= 0xE000 || !inc) {
capture |= (capture_msw << 16);
} else {
capture |= ((capture_msw - 1) << 16);

if (capture_previous ==0) { //MOD TG 20 april 2018

if (capture <= 0xE000 || !inc) {
capture_previous |= (capture_msw << 16);
} else {
capture_previous |= ((capture_msw - 1) << 16);
}

return;
}

cptPeriod++; //MOD TG 20 april 2018

if (cptPeriod == nbPeriod) { //MOD TG 20 april 2018
// compute the waveform period
period = capture - capture_previous;
capture_previous = capture;

if (capture <= 0xE000 || !inc) {
capture |= (capture_msw << 16);
} else {
capture |= ((capture_msw - 1) << 16);
}
// compute the waveform period
if (capture < capture_previous) {
period = 2^32 - capture_previous ;
period += capture;
} else {
period = capture - capture_previous;
}
capture_previous = capture;
cptPeriod = 0;

} else {
return; //MOD TG 20 april 2018
}

// store it into the buffer
i = buffer_head + 1;
if (i >= FREQMEASURE_BUFFER_LEN) i = 0;
Expand Down
2 changes: 1 addition & 1 deletion FreqMeasure.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class FreqMeasureClass {
public:
static void begin(void);
static void begin(uint16_t nbAverage = 1);
static uint8_t available(void);
static uint32_t read(void);
static float countToFrequency(uint32_t count);
Expand Down
12 changes: 6 additions & 6 deletions util/FreqMeasureCapture.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -370,7 +370,7 @@ static inline void capture_shutdown(void)


#define TIMER_OVERFLOW_VECTOR TIMER1_OVF_vect
#define TIMER_CAPTURE_VECTOR TIMER1_CAPT_vect
#define TIMER_CAPTURE_VECTOR TIMER1_CAPT_vect


#elif defined(CAPTURE_USE_TIMER3)
Expand Down Expand Up @@ -412,7 +412,7 @@ static inline void capture_shutdown(void)
}

#define TIMER_OVERFLOW_VECTOR TIMER3_OVF_vect
#define TIMER_CAPTURE_VECTOR TIMER3_CAPT_vect
#define TIMER_CAPTURE_VECTOR TIMER3_CAPT_vect



Expand Down Expand Up @@ -455,7 +455,7 @@ static inline void capture_shutdown(void)
}

#define TIMER_OVERFLOW_VECTOR TIMER4_OVF_vect
#define TIMER_CAPTURE_VECTOR TIMER4_CAPT_vect
#define TIMER_CAPTURE_VECTOR TIMER4_CAPT_vect



Expand Down Expand Up @@ -498,7 +498,7 @@ static inline void capture_shutdown(void)
}

#define TIMER_OVERFLOW_VECTOR TIMER5_OVF_vect
#define TIMER_CAPTURE_VECTOR TIMER5_CAPT_vect
#define TIMER_CAPTURE_VECTOR TIMER5_CAPT_vect



Expand Down