Error with Timers when compiling for Teensy 2.0 on Arduino software

Status
Not open for further replies.

NuttyMonk

Well-known member
Hi all,

i am compiling some code in the Arduino software for loading onto a Teensy 2.0 and i get an error message telling me that there is an error with the timers

e.g. "'TCCR2A' was not declared in this scope"

All of the errors seem to relate to Timer2. Isn't there a Timer2 on the Teensy 2.0?

I didn't want to use Timer0 since it might affect some of the other functions of the Teensy. IIRC Timer0 is used by micros() and other parts of the software.

Cheers

NM

Code:
// NuttyMonk Simple VC Arduino Clock - Square Wave - 50% Duty Cycle
// This clock generator samples at 100kHz (100,000 samples per second) although it can be changed using "samplerate"
// BPM adjustable from settable values minBPM and maxBPM (preset as 10 - 220)
// Analog inputs sampled at 10Hz (10 samples per second)

// set the output pins
const int clockPin = 2;    // the number of the Clock pin

// set the analog input pins
const int coarsePin = 19;
const int finePin = 16;
const int CVPin = 13;

// set the digital input pins
const int resetPin = 5;
const int onOffPin = 8;

// initialise the wavestate and other useful variables
int waveState = LOW;   // waveState used to set the Clock signal
volatile int clockOnOff = 1;
int clockReset = 0;

// set variables for BPM control
const int minBPM = 10;
const int maxBPM = 220;
volatile float BPM = 120;
volatile float fineBPM = 0;
const int fineBPMRange = 10;
volatile float totalBPM = 120;
volatile float cvBPM = 0;
const int cvRange = 20;
unsigned long samplerate = 100000;
volatile unsigned long period = int((60.0 / totalBPM) * float(samplerate));  // how often a clock pulse takes place based on the samplerate
volatile unsigned long halfPeriod = int(period / 2);
volatile unsigned long t;  // init var to monitor the samples taken to activate a beat (clockPin to HIGH) and set the beat to off (clockPin LOW)

void setup() {
  analogReference(EXTERNAL);
  // initialise serial communication at 9600 bits per second:
  Serial.begin(9600);

  // set up the output pin
  pinMode(clockPin, OUTPUT);
  
  // set up the analog input pins
  pinMode(coarsePin, INPUT);
  pinMode(finePin, INPUT);
  pinMode(CVPin, INPUT);

  // set up the digital input pins
  pinMode(resetPin, INPUT);
  pinMode(onOffPin, INPUT);

  // get an initial reading of analog inputs
  BPM = map(analogRead(coarsePin), 0, 1023, minBPM, maxBPM);
  fineBPM = map(analogRead(finePin), 0, 1023, (fineBPMRange * -1) * 1000, fineBPMRange * 1000);
  cvBPM = map(analogRead(CVPin), 0, 1023, (cvRange * -1) * 1000, cvRange * 1000);
  
  // update BPM
  totalBPM = BPM + fineBPM + cvBPM;

  // update period based on BPM
  period = int((60.0 / totalBPM) * float(samplerate));
  halfPeriod = int(period / 2);
  
  // TIMER INTERRUPT SETUP
  cli(); // disable interrupts

  // Timer2 set up to sample at 100kHz for Clock signal
  TCCR2A = 0; // set entire TCCR2A register to 0
  TCCR2B = 0; // same for TCCR2B
  TCNT2  = 0; //initialize counter value to 0
  // set compare match register for 100kHz increments
  OCR2A = 19; // = (16,000,000) / (8 * 8,000) - 1 (must be <256)
  // turn on CTC mode
  TCCR2A |= (1 << WGM21);
  // Set CS21 bit for 8 prescaler
  TCCR2B |= (1 << CS21);
  // enable timer compare interrupt
  TIMSK2 |= (1 << OCIE2A);

  // Timer1 set up to sample at 10Hz for analog input reading
  TCCR1A = 0; // set entire TCCR1A register to 0
  TCCR1B = 0; // same for TCCR1B
  TCNT1  = 0; //initialize counter value to 0
  // set compare match register for 10hz increments
  OCR1A = 1561; // = (16,000,000) / (1024 * 10) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS10 and CS12 bits for 1024 prescaler
  TCCR1B |= (1 << CS12) | (1 << CS10);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

  sei();    // enable interrupts

  // interrupts for digital pins
  attachInterrupt(resetPin, resetClock, RISING);
  attachInterrupt(onOffPin, onOffClock, CHANGE);
}

void resetClock() {
  Serial.println("resetClock");
  // reset the timer count to 0
  t = 0;
  clockReset = 1;
  Serial.print("t : ");
  Serial.println(t);
  Serial.print("clockReset : ");
  Serial.println(clockReset);
}

void onOffClock() {
  Serial.println("onOffClock");
  // if the onOffPin changes
  clockOnOff = digitalRead(onOffPin);
  Serial.print("clockOnOff : ");
  Serial.println(clockOnOff);
  
  if (clockOnOff) {
    t = 0;
    waveState = HIGH;
    digitalWrite(clockPin, waveState);
  } else {
    t = 0;
    waveState = LOW;
    digitalWrite(clockPin, waveState);
  }
}

ISR(TIMER2_COMPA_vect) {   // timer 1 interrupt
  if (clockOnOff) {  // if clock is turned ON
    t++;  // increment t to monitor the next time to adjust the waveState in the loop() function
  }
}

ISR(TIMER1_COMPA_vect) {   // timer 1 interrupt
  // read the analog inputs
  BPM = map(analogRead(coarsePin), 0, 1023, minBPM, maxBPM);
  fineBPM = map(analogRead(finePin), 0, 1023, (fineBPMRange * -1) * 1000, fineBPMRange * 1000);
  fineBPM *= 0.001;
  cvBPM = map(analogRead(CVPin), 0, 1023, (cvRange * -1) * 1000, cvRange * 1000);
  cvBPM *= 0.001;

  // update BPM
  totalBPM = BPM + fineBPM + cvBPM;

  // update period based on BPM
  period = int((60.0 / totalBPM) * float(samplerate));
  halfPeriod = int(period / 2);
}

void loop() {
  if (clockOnOff) {
    if (clockReset) {
      clockReset = 0;
      waveState = HIGH;
      digitalWrite(clockPin, waveState);
    } else if (t >= halfPeriod){
      t = 0;
      if (waveState == LOW) {
        waveState = HIGH;
        digitalWrite(clockPin, waveState);
      } else {
        waveState = LOW;
        digitalWrite(clockPin, waveState);
      }
    }
  }
}
 
Isn't there a Timer2 on the Teensy 2.0?

Nope. Atmel put timers 0, 1, 3 and 4 into this chip. Why they skipped timer2, I don't know and can only guess.

But 2 likely guesses would be they made a mistake that ruined timer2 and just removed it from the specs (respinning a chip is crazy expensive) or perhaps they originally designed this chip for a very unique application (possibly 3 phase motor control or AC power synthesis judging from the special timer4) for a special large company and that application didn't need timer2, so they left it out to allow packing more chips onto each wafer.
 
Hi Paul, thanks for the quick reply.

So if i just change my Timer0 code to Timer3 will it still work ok? Is it an 8-bit timer like Timer0? If there are any changes to the code i need to make, where can i get a list of the Timer3 registers?

Cheers

NM
 
Status
Not open for further replies.
Back
Top