I'm porting MicLoc from Teensy 3.1 to 4.0. This is a project from kripthor posted on his "rural hacker" blog.
I'm at a loss. I'm an EE with decades of software development experience, but not in this field, so please forgive my ignorance.
I get the first "Debug: Pre-highSpeed8bitADCSetup()" message only on the Adruino IDE Serial Monitor.
Here's the code,
Teensy40FastADC.h:
And TeensySimultaneousADC.ino:
This is all under Ubuntu 20.04.2 LTS with Arduino IDE 1.8.13 and the Teensy Loader 1.53.
The workstation parameters are pretty much irrelevant, but for completeness, Intel® Core™ i7-5820K CPU @ 3.30GHz × 12, 16 GiB RAM, NVIDIA Corporation GK208B [GeForce GT 730], Gnome v 3.36.8, 64-bit OS.
I just want to know what's going on. I wasted three hours trying to find out why I kept getting this,
I fixed it in the Arduino IDE by just moving dht.setType(22) into the setup() routine. Okay, that was a quite stupid mistake I'd made - I originally had the statement sitting in global space which doesn't really make sense (well it did at the time since I was thinking about timing flowing linearly forward, except it shouldn't in Global space).
I'm at a loss. I'm an EE with decades of software development experience, but not in this field, so please forgive my ignorance.
I get the first "Debug: Pre-highSpeed8bitADCSetup()" message only on the Adruino IDE Serial Monitor.
Here's the code,
Teensy40FastADC.h:
Code:
#define _teensy40adc_h_
//FAST 8 BIT SETUP AND READING FOR 2 ADCs
//THIS IS TEENSY VERSION 4.0 SPECIFIC
// Teensy 3.1 & 4.0 have the LED on pin 13
#define LEDPIN 13
#include <ADC.h>
#include "kinetis.h"
//defined as a macro its faster
#define highSpeed8bitAnalogReadMacro(channel1, channel2, value1, value2) ADC0_SC1A = channel1;ADC1_SC1A = channel2;while (!(ADC0_SC1A & ADC1_SC1A & ADC_SC1_COCO)) {} value1 = ADC0_RA;value2 = ADC1_RA;
#define highSpeed8bitAnalogReadMacro1(channel1, value1) ADC0_SC1A = channel1;while (!(ADC0_SC1A & ADC_SC1_COCO)) {} value1 = ADC0_RA;
/*
FUNCTION PSEUDOCODE FOR MACRO, of course we could not pass value1 and value2 like this to a function (should be pointers or return a struct)
int highSpeed8bitAnalogRead(uint8_t channel1, uint8_t channel2, int value1, int value2){
ADC0_SC1A = channel1;
ADC1_SC1A = channel2;
while (!(ADC0_SC1A & ADC1_SC1A & ADC_SC1_COCO)) {}
value1 = ADC0_RA;
value2 = ADC1_RA;
}
*/
void highSpeed8bitADCSetup(){
digitalWrite(LEDPIN,1);
delay(2000);
digitalWrite(LEDPIN,0);
Serial.print("Debug: Entered highSpeed8bitADCSetup\n");
/*
0 ADLPC (Low-Power Configuration)
0 ADIV (Clock Divide Select)
0
0 ADLSMP (Sample time configuration)
0 MODE (Conversion mode selection) (00=8/9, 01=12/13, 10=10/11, 11=16/16 bit; diff=0/1)
0
0 ADICLK (Input Clock Select)
0
*/
ADC0_CFG1 = 0b00000000; ADC1_CFG1 = 0b00000000;
digitalWrite(LEDPIN,1);
delay(300);
digitalWrite(LEDPIN,0);
Serial.print("Debug: highSpeed8bitADCSetup: Line 47\n");
/*
0 MUXSEL (ADC Mux Select)
0 ADACKEN (Asynchrononous Clock Output Enable)
0 ADHSC (High-Speed Configuration)
0 ADLSTS (Long Sample Time Select) (00=+20 cycles, 01=+12, 10=+6, 11=+2)
0
*/
ADC0_CFG2 = 0b00010100; ADC1_CFG2 = 0b00010100;
/*
0 ADTRG (Conversion Trigger Select)
0 ACFE (Compare Function Enable)
0 ACFGT (Compare Function Greater than Enable)
0 ACREN (Compare Function Range Enable)
0 ACREN (COmpare Function Range Enable)
0 DMAEN (DMA Enable)
0 REFSEL (Voltage Reference Selection) (00=default,01=alternate,10=reserved,11=reserved)
*/
ADC0_SC2 = 0b00000000; ADC1_SC2 = 0b00000000;
/*
1 CAL (Calibration)
0 CALF (read only)
0 (Reserved)
0
0 ADCO (Continuous Conversion Enable)
1 AVGS (Hardware Average Enable)
1 AVGS (Hardware Average Select) (00=4 times, 01=8, 10=16, 11=32)
1
*/
ADC0_SC3 = 0b10000000; ADC1_SC3 = 0b10000000;
// Waiting for calibration to finish. The documentation is confused as to what flag to be waiting for (SC3[CAL] on page 663 and SC1n[COCO] on page 687+688).
while (ADC0_SC3 & ADC_SC3_CAL) {} ;
while (ADC1_SC3 & ADC_SC3_CAL) {} ;
}
#endif
Code:
//FOR 2 ADC
#include <ADC.h>
#include "Teensy40FastADC.h"
//FOR TEMPERATURE/HUMIDITY SENSOR
#include <dhtnew.h>
#define DHTTYPE 22 // 0= unknown, DHT 11 or 22 (AM2302), AM2321)
#define DHT22PIN 12
// static dht11 DHT11; // 'dht11' does not name a type
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHTNEW dht(DHT22PIN);
void setup() {
dht.setType(DHTTYPE);
pinMode(LEDPIN, OUTPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
digitalWrite(LEDPIN,1);
delay(300);
digitalWrite(LEDPIN,0);
Serial.begin(115200);
Serial.print("Debug: Pre-highSpeed8bitADCSetup()\n");
delay(2000);
digitalWrite(LEDPIN,1);
delay(300);
digitalWrite(LEDPIN,0);
highSpeed8bitADCSetup();
Serial.print("Debug: highSpeed8bitADCSetup");
//BLINK LED, WE ARE ALIVE
digitalWrite(LEDPIN,1);
delay(2000);
digitalWrite(LEDPIN,0);
Serial.println("Started...");
}
#define SAMPLES 2048
#define BUFFERSIZE 2048
const int channelA2 = ADC::channel2sc1aADC0[2];
const int channelA3 = ADC::channel2sc1aADC1[3];
const int channelA4 = ADC::channel2sc1aADC0[4];
byte THRESHOLD = 180;
byte value1;
byte value2;
byte value3;
byte buffer1[BUFFERSIZE];
byte buffer2[BUFFERSIZE];
byte buffer3[BUFFERSIZE];
int samples;
long startTime;
long stopTime;
long totalTime;
int event;
int i;
int k;
void loop() {
//BLINK LED, WE ARE ALIVE
digitalWrite(LEDPIN,1);
delay(4000);
digitalWrite(LEDPIN,0);
Serial.println("Inside loop...");
startTime = micros();
//START SAMPLING
//Strange init in this for, but the compiler seems to optimize this code better, so we get faster sampling
for(i=0,k=0,samples=SAMPLES,event=0;i<samples;i++) {
//TAKE THE READINGS
highSpeed8bitAnalogReadMacro(channelA2,channelA3,value1,value2);
//SHOULD ADJUST THIS 2nd READING. Single channel
highSpeed8bitAnalogReadMacro1(channelA4,value3);
buffer1[k] = value1;
buffer2[k] = value2;
buffer3[k] = value3;
//CHECK FOR EVENTS
if (value1 > THRESHOLD && !event) {
event = k;
//THERE IS AN EVENT, ARE WE REACHING THE END? IF SO TAKE MORE SAMPLES
if (i > SAMPLES-1024) samples = SAMPLES+1024;
//SHOULD AJUST TIME LOST IN THIS LOGIC TOO
}
if (++k == BUFFERSIZE) k = 0;
}
stopTime = micros();
//WAS AN EVENT BEEN DETECTED?
if (event != 0) {
printInfo();
printSamples();
}
//DID WE RECEIVE COMMANDS?
if (Serial.available()) parseSerial();
}
void parseSerial() {
char c = Serial.read();
switch (c) {
case 'p':
printInfo();
break;
case 's':
printSamples();
break;
case '+':
THRESHOLD += 5;
break;
case '-':
THRESHOLD -= 5;
break;
default:
break;
}
}
void printSamples() {
Serial.print("BUFFSIZE: ");
Serial.print(BUFFERSIZE,DEC);
Serial.print(" Event: ");
Serial.println(event);
serialWrite(buffer1,BUFFERSIZE);
serialWrite(buffer2,BUFFERSIZE);
serialWrite(buffer3,BUFFERSIZE);
Serial.flush();
}
//This should be optimized. Writing raw binary data seems to fail a lot of times
//and I ended up loosing bytes. Maybe some form of flow-control should be used.
void serialWrite(byte *buffer,int siz) {
int kk;
for (kk=0;kk<siz;kk++) {
Serial.print(buffer[kk],HEX);
Serial.print(" ");
}
Serial.println();
}
void printInfo() {
totalTime = stopTime-startTime;
double samplesPerSec = i*1000.0/totalTime;
//Take a temperature/humidity reading
//The DHT11 should be connected with a resistor for less errors in readings,
// but works without it if you take some readings untils you got an ok one.
while(dht.read() != DHTLIB_OK);
Serial.print("T: ");
Serial.print(totalTime);
Serial.print(" Samples: ");
Serial.print(i,DEC);
Serial.print(" Samples/uSec: ");
Serial.print(samplesPerSec,7);
Serial.print(" Temp: ");
Serial.print((float)dht.getTemperature(),2);
Serial.print(" Hum: ");
Serial.print((float)dht.getHumidity(),2);
Serial.print(" Threshold: ");
Serial.println(THRESHOLD,DEC);
Serial.flush();
}
This is all under Ubuntu 20.04.2 LTS with Arduino IDE 1.8.13 and the Teensy Loader 1.53.
The workstation parameters are pretty much irrelevant, but for completeness, Intel® Core™ i7-5820K CPU @ 3.30GHz × 12, 16 GiB RAM, NVIDIA Corporation GK208B [GeForce GT 730], Gnome v 3.36.8, 64-bit OS.
I just want to know what's going on. I wasted three hours trying to find out why I kept getting this,
TeensySimultaneousADC.cpp:23:1: error: ‘dht’ does not name a type
dht.setType(22);
Well, running it under Windows 10 Visual Studio 2017 confirmed there was nothing wrong with the C++ code!dht.setType(22);
I fixed it in the Arduino IDE by just moving dht.setType(22) into the setup() routine. Okay, that was a quite stupid mistake I'd made - I originally had the statement sitting in global space which doesn't really make sense (well it did at the time since I was thinking about timing flowing linearly forward, except it shouldn't in Global space).
Last edited by a moderator: