Hello everybody,
do someone of you have experience with Counter and Timer on MKR Platform?
I have a piece of code running on my Uno-Boards and now I want to use the MKR-Board to handle this.
But I have no idea how to config the MKR-timers.
I want to use three TC inputs to read 3 humidity-sensors which provides a frequency signal up to 500kHz.
This is the running code on Uno.
Hopefully someone can help me.
Thanks a lot!
/*
* Hardware Counting sketch for Arduino micro
*
* uses pin 12 on 32U4
*/
const int ledPin = 13;
unsigned int freq;
const int selectPin0 = 6;
const int selectPin1 = 7;
const int selectPin2 = 8;
const int maxRead = 25;
int readCount;
int currPort;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
//hardware counter setup
TCCR1A = 0; // reset timer/counter control register
pinMode(selectPin0, OUTPUT);
pinMode(selectPin1, OUTPUT);
pinMode(selectPin2, OUTPUT);
readCount = 0;
currPort = 0;
}
void loop() {
// put your main code here, to run repeatedly:
freq = 0;
digitalWrite(ledPin, LOW);
delay(200);
digitalWrite(ledPin, HIGH);
// start the counting
bitSet(TCCR1B, CS12); // Counter clock source is external pin
bitSet(TCCR1B, CS11); // Clock on rising edge
delay(40); // wait 40ms
// stop the counting
TCCR1B = 0;
freq = TCNT1/4; // devide by 4 to get number of oscillations per 10ms
TCNT1 = 0; // reset the hardware counter
Serial.println(freq);
/*
// Switch the sensor every 25 cycles
readCount++;
if(readCount > maxRead) {
currPort++;
if(currPort == 3) currPort = 0;
readCount = 0;
selectPort(currPort);
}
*/
}
// select port in multiplexer
// for simple understanding, the binary pattern is directly entered for the selected port
// and not calculated
/*
void selectPort(int port) {
if (port==0) {
digitalWrite(selectPin0, LOW);
digitalWrite(selectPin1, LOW);
digitalWrite(selectPin2, LOW);
}
else if (port == 1) {
digitalWrite(selectPin0, HIGH);
digitalWrite(selectPin1, LOW);
digitalWrite(selectPin2, LOW);
}
else if (port == 2) {
digitalWrite(selectPin0, LOW);
digitalWrite(selectPin1, HIGH);
digitalWrite(selectPin2, LOW);
}
}
*/