Skip to content

Commit

Permalink
Port to Teensy 3.0 & 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulStoffregen committed Jun 6, 2014
1 parent 7639683 commit 02fd4f6
Show file tree
Hide file tree
Showing 6 changed files with 493 additions and 243 deletions.
48 changes: 46 additions & 2 deletions FreqMeasure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* http://www.pjrc.com/teensy/td_libs_FreqMeasure.html
* Copyright (c) 2011 PJRC.COM, LLC - Paul Stoffregen <paul@pjrc.com>
*
* Version 1.0
* 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
Expand All @@ -24,7 +24,7 @@
*/

#include "FreqMeasure.h"
#include "util/capture.h"
#include "util/FreqMeasureCapture.h"

#define FREQMEASURE_BUFFER_LEN 12
static volatile uint32_t buffer_value[FREQMEASURE_BUFFER_LEN];
Expand Down Expand Up @@ -69,11 +69,21 @@ uint32_t FreqMeasureClass::read(void)
return value;
}

float FreqMeasureClass::countToFrequency(uint32_t count)
{
#if defined(__AVR__)
return (float)F_CPU / (float)count;
#elif defined(__arm__) && defined(TEENSYDUINO)
return (float)F_BUS / (float)count;
#endif
}

void FreqMeasureClass::end(void)
{
capture_shutdown();
}

#if defined(__AVR__)

ISR(TIMER_OVERFLOW_VECTOR)
{
Expand Down Expand Up @@ -110,6 +120,40 @@ ISR(TIMER_CAPTURE_VECTOR)
}
}

#elif defined(__arm__) && defined(TEENSYDUINO)

void FTM_ISR_NAME (void)
{
uint32_t capture, period, i;
bool inc = false;

if (capture_overflow()) {
capture_overflow_reset();
capture_msw++;
inc = true;
}
if (capture_event()) {
capture = capture_read();
if (capture <= 0xE000 || !inc) {
capture |= (capture_msw << 16);
} else {
capture |= ((capture_msw - 1) << 16);
}
// compute the waveform period
period = capture - capture_previous;
capture_previous = capture;
// store it into the buffer
i = buffer_head + 1;
if (i >= FREQMEASURE_BUFFER_LEN) i = 0;
if (i != buffer_tail) {
buffer_value[i] = period;
buffer_head = i;
}
}
}


#endif

FreqMeasureClass FreqMeasure;

3 changes: 2 additions & 1 deletion FreqMeasure.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#ifndef FreqMeasure_h
#define FreqMeasure_h

#include <inttypes.h>
#include <Arduino.h>

class FreqMeasureClass {
public:
static void begin(void);
static uint8_t available(void);
static uint32_t read(void);
static float countToFrequency(uint32_t count);
static void end(void);
};

Expand Down
2 changes: 1 addition & 1 deletion examples/LCD_Output/LCD_Output.pde
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void loop() {
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
double frequency = F_CPU / (sum / count);
float frequency = FreqMeasure.countToFrequency(sum / count);
lcd.setCursor(0, 1);
lcd.print(frequency);
lcd.print(" ");
Expand Down
2 changes: 1 addition & 1 deletion examples/Serial_Output/Serial_Output.pde
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void loop() {
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
double frequency = F_CPU / (sum / count);
float frequency = FreqMeasure.countToFrequency(sum / count);
Serial.println(frequency);
sum = 0;
count = 0;
Expand Down
Loading

0 comments on commit 02fd4f6

Please sign in to comment.