Teensy 3.6 VS Teensy 4.0

JimF

Member
Some time back I built a frequency counter using a Teensy 3.6. Very minimal amount of code required and it worked great. Input if I remember right went directly to pin 13 (LED) (SCK0). Since the 3.6 version is out of stock, I purchased a 4.0. I went to compile the same code telling the IDE that it was a Teensy 4.0 and it seemed to crash all over the place. Can I not use a 4.0 in place of a 3.6 for this application?
 
Some time back I built a frequency counter using a Teensy 3.6. Very minimal amount of code required and it worked great. Input if I remember right went directly to pin 13 (LED) (SCK0). Since the 3.6 version is out of stock, I purchased a 4.0. I went to compile the same code telling the IDE that it was a Teensy 4.0 and it seemed to crash all over the place. Can I not use a 4.0 in place of a 3.6 for this application?

Teensy 4.0 can do frequency counting, but it's a different processor, so the underlying code is different. Paul did a great job of maintaining pin compatibility, but that doesn't mean that all of the same features are available on all pins. Can you please share (a) your code (see Forum Rule) and (b) your Arduino and TeensyDuino versions?
 
Teensy 4.0 can do frequency counting, but it's a different processor, so the underlying code is different. Paul did a great job of maintaining pin compatibility, but that doesn't mean that all of the same features are available on all pins. Can you please share (a) your code (see Forum Rule) and (b) your Arduino and TeensyDuino versions?

My code is as follows below. The Arduino version is 1.8.16 and the TeensyDuino version is 1.56.

/* This program is to read the Collins Receiver VFO
* and display it on the LCD. The VFO input read pin is 13.
* This is default in the Teensy 3.6. The VFO frequency is
* combined with the equivalent crystal frequency and IF
* frequency to display to operating frequency on the
* display. The data for crystal frequency has been adjusted
* to 'calibrate' the radio on the normal operating sideband.
*
* 12/01/2018 - Added locking feature where when locked
* one cannot select another band range.
*/

#include <LiquidCrystal.h>
#include <FreqCount.h>

// initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(2,3,4,5,6,7,8);

unsigned long VFO = 0; //Direct read of the VFO frequency
long Freq = 0; //Used in calculation
char buf[16]; //Array to convert to text
String Afreq; //String for converted text
int x = 0; //Misc variable
//int LockB = 29; //Lock button
//int LockLed = 30; //Lock Led
int LockS = 0; //Status of lock
// The pins the quadrature encoder is connected.
int in_a = 9;
int in_b = 10;
volatile int pulses = 0;


#define LockB 35
#define LockLed 30
//Following is the crystal frequencies for each band.
//Because the frequency count rate for the counter is 100
//instead of one second, one least significant digit is dropped.
//Frequencies can be adjusted so as to calibrate due to crystal age.
long Rfreq[27] = {655635, 675640,695643, 1015625, 1035642, 1315500, 1715348, 1735380, 1795378, 2115500, 2415358, 2435352, 2455350, 2795500, 2995500, 3015500, 3035500, 3055500, 3115287, 3135134, 3155210, 3175500, 3195500, 3215500, 3235500, 3255500, 3275500};

//Following is the bottom frequency range for each of the above
//crystal frequencies.
String Aband[27] = {"3.4","3.6","3.8","7.0","7.2","10.0","14.0","14.2","14.8","18.0","21.0","21.2","21.4","24.8","26.8","27.0","27.2","27.4","28.0","28.2","28.4","28.6","28.8","29.0","29.2","29.4","29.6"};


void count() {
// This function is called by the interrupt
// If in_b is HIGH increment the counter
// otherwise decrement it
if (LockS == 0) { //If LockS is 0 then it's not locked and we can change bands
if (digitalRead(in_b)) {
pulses--;
}
else {
pulses++;
}
if (pulses <0) pulses = 0; //Keep from going negative
if (pulses >26) pulses = 26; //Keep from going to far - 27 band positions - 0 - 26.
}
}


void setup() {
FreqCount.begin(100); //Set the rate for frequency reading. 1000 = 1 second.
// set up the LCD's number of columns and rows:
lcd.begin(16,2);
//configure encoder
pinMode(in_a, INPUT);
pinMode(in_b, INPUT);
//assign interrupt to pin 9 and 'count' to handle the interrupt on a rising pulse from A output (pin 9)
attachInterrupt(digitalPinToInterrupt (9), count, RISING);
Serial.begin(9600);

// configure data pins

pinMode(LockB, INPUT_PULLUP);
pinMode(LockLed, OUTPUT);

LockS = 0; //Start with lock status off
digitalWrite(LockLed, HIGH); //Start with LED off

lcd.print("Collins VFO"); //Sigh on
lcd.setCursor(0,1);
lcd.print("K7NCG 5/8/2018");
delay(1000);
lcd.clear();

}

void loop() {
if (FreqCount.available()) { //See if there is something to read
VFO = FreqCount.read(); //Read it
Calculate(); //Calculate operating frequency
Display(); //Show the results
}
if (digitalRead(LockB) == LOW) { //See if button pressed
delay(50); //Time for bounce
if (digitalRead(LockB) ==LOW) { //If still pressed
while(digitalRead(LockB) == LOW) { } //Wait for button release
if (LockS == 0) {
LockS = 1; //Set locked state
digitalWrite(LockLed,LOW); } //Light LED
else {
if (LockS ==1) {
LockS = 0; //Clear locked state
digitalWrite(LockLed,HIGH); } //Turn off LED
}
}
}
}

void Display() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print(Afreq); //Show the frequency
lcd.setCursor(0,1);
lcd.print("Band: ");
lcd.print(Aband[pulses]); //Show Band Range

}

void Calculate() {
//Calculate the frequency and convert to a printable form
Freq = Rfreq[pulses] - VFO - 45500; //Crystal freq minus VFO minus IF
if (Freq < 1000000) {
ltoa(Freq,buf,10);
Afreq = " ";
Afreq = Afreq + buf[0];
Afreq = Afreq + ".";
Afreq = Afreq + buf[1];
Afreq = Afreq + buf[2];
Afreq = Afreq + buf[3];
Afreq = Afreq + ".";
Afreq = Afreq + buf[4];
Afreq = Afreq + buf[5]; }
else {
ltoa(Freq,buf,10);
Afreq = "";
Afreq = Afreq + buf[0];
Afreq = Afreq + buf[1];
Afreq = Afreq + ".";
Afreq = Afreq + buf[2];
Afreq = Afreq + buf[3];
Afreq = Afreq + buf[4];
Afreq = Afreq + ".";
Afreq = Afreq + buf[5];
Afreq = Afreq + buf[6]; }
}
 
The example code in the library shows some significant differences when using the T4, pin used and time in microseconds.

Code:
/* FreqCount - Example with serial output
 * http://www.pjrc.com/teensy/td_libs_FreqCount.html
 *
 * This example code is in the public domain.
 *
 * For the Teensy 4.0 pin 9 is used to measure the freq
 * FreqCount.begin time is in microseconds vs milliseconds
 * for other boards.
 * As a test the sketch is setup to output 50Mhz on pin 11 sopen
 * add a jumper from pin 8 to pin 9.
 */
#include <FreqCount.h>

void setup() {
  Serial.begin(57600);
  
  delay(2000);
  analogWriteFrequency(8, 50000000);  // test jumper 11 to 25
  analogWrite(8, 128);
  
  FreqCount.begin(1000000);  //Time in microseconds
}

void loop() {
  if (FreqCount.available()) {
    unsigned long count = FreqCount.read();
    Serial.println(count);
  }
}
 
Please see this page on the PJRC website regarding library FreqCount. T3.x use pin 13, and T4.x uses pin 9. Library FreqMeasure is similar in that it supports only 1 pin per Teensy type. Depending on what you want to do, check out libraries FreqMeasureMulti and TeensyTimerTool, which support many timers and pins on both T3.x and T4.x

https://www.pjrc.com/teensy/td_libs_FreqCount.html
 
Code:
#define LockB 35
#define LockLed 30
These two pins are on the bottom of the T4.0. Are you actually using them? Looks like you have spare pins on the top which would be a lot easier to use.

Pete
 
I would definitely use other pins once the software compiled reasonably. But there are so many errors that the IDE doesn't stop at one and ends up saying "Error compiling for board Teensy 4.0". They all seem to be related around the FreqCount library so maybe I need to look and see if there is a later version.
 
Your code in #3 compiles for me with no errors. But I am using Arduino 1.8.19, not 1.8.16, and Teensyduino 1.56.

Pete
 
I also was able to compile it without any errors. Used Arduino 1.8.19 and the very latest dev code which is soon to become 1.57-beta1.

screenshot.png
 
But there are so many errors that the IDE doesn't stop at one and ends up saying "Error compiling for board Teensy 4.0". They all seem to be related around the FreqCount library so maybe I need to look and see if there is a later version.

Simplest thing to try it use another computer with a fresh installation of Arduino 1.8.19 and Teensyduino 1.56. This code does compile without errors on a good installation of the software, so just getting a fresh install may be the simplest way to solve this problem.

But if you want to investigate, look for the info Arduino prints about which libraries it actually used. Maybe you have an old or incompatible copy of a library installed. The ones Teensyduino installs will have a path like {Arduino}/hardware/teensy/avr/libraries. If Arduino is using the wrong library, just uninstall it or move the files to a location Arduino doesn't use.

If that doesn't work and you still want to figure out what's really wrong, maybe we could help if you show us the actual errors. When the compile fails, look for Arduino's "Copy error message" button. The in the forum editor, look for the "#" button which creates a pair of code tags and paste the huge error messages between those tags. You can also paste directly into your message, but using the code tags preserves the spacing and makes it easier for us to read the errors. Who knows, maybe there's some clues lurking in those error messages.

But again, I can confirm your code is fine and does compile without errors with a proper install of the software. Just making a fresh install might be the simplest way to solve this issue.
 
Simplest thing to try it use another computer with a fresh installation of Arduino 1.8.19 and Teensyduino 1.56. This code does compile without errors on a good installation of the software, so just getting a fresh install may be the simplest way to solve this problem.

But if you want to investigate, look for the info Arduino prints about which libraries it actually used. Maybe you have an old or incompatible copy of a library installed. The ones Teensyduino installs will have a path like {Arduino}/hardware/teensy/avr/libraries. If Arduino is using the wrong library, just uninstall it or move the files to a location Arduino doesn't use.

If that doesn't work and you still want to figure out what's really wrong, maybe we could help if you show us the actual errors. When the compile fails, look for Arduino's "Copy error message" button. The in the forum editor, look for the "#" button which creates a pair of code tags and paste the huge error messages between those tags. You can also paste directly into your message, but using the code tags preserves the spacing and makes it easier for us to read the errors. Who knows, maybe there's some clues lurking in those error messages.

But again, I can confirm your code is fine and does compile without errors with a proper install of the software. Just making a fresh install might be the simplest way to solve this issue.

Pete and Paul - Thanks so much for your very prompt responses. I started noodling around in the error messages and it lead me to believe that there was something wrong with the libraries. So a bit of searching and comparing took place. Finally I noted at the bottom of the error list some lines regarding libraries found and used. I had two FreqCount libraries, one of which was in the normal library location. Once I renamed it, the compiler apparently used the other one in the teensy/svr/libraries and all seems well now. So now I'll proceed to 'redesign' from teensy 3.6 to teensy 4.0. Thanks so much for your help!!!!
 
Back
Top