Teensy++ Timer1 OC1C CTC Mode

Status
Not open for further replies.

dreschel

Member
Using Teensy++ 2.0, Arduino Style, Windows...

I can generate an 8MHz square wave on PB7 (27) using timer0 CTC mode:

Code:
const byte timer0OutputA = 27;
  
void setup() {
   pinMode (timer0OutputA, OUTPUT); 
  TIMSK0 = 0;  // no interrupts
  OCR0A = 0;  // 0 - 8MHz, 1 - 4MHz, 2 - 2.66MHz, 3 - 2.0MHz
  TCCR0A = 0;
  TCCR0B = 0;
  TCCR0A |= _BV(COM0A0) | _BV(WGM01);  //Toggle OCOA
  TCCR0B |= _BV(WGM02) | 1; //Prescaler 1
}

This sets up OCOA on PB7. I would like to do the same thing with OC1C from Timer1.
(To allow some flexibility when also using Arduino delays, etc.)
Seems possible from the datasheet but I can't get it to work. Any suggestions would be greatly appreciated.
Thanks in advance....
 
Working Code for CTC and OC1C

So here is a reply to my own question.
This code may come in handy to those who would like to generate MHz waveforms and still use the Arduino delay functions.
If you put an oscilloscope probe on pin 27 of Teensy++ you should see a square wave cycling from 8MHz to 2MHz.
Code:
void setup() {
}

void Make8MHz(void){
  DDRB =0;
  TIMSK1 = 0;  // no interrupts
  ICR1 = 0; // MODE 12
  TCCR1A = _BV(COM1C0);  //Toggle OC1C
  TCCR1B = _BV(WGM13) | _BV(WGM12) | 1; //MODE 12
  TCCR1C = _BV(FOC1C);
  DDRB = 0x80;  
}
void Make4MHz(void){
  DDRB =0;
  TIMSK1 = 0;  // no interrupts
  ICR1 = 1; // MODE 12
  TCCR1A = _BV(COM1C0);  //Toggle OC1C
  TCCR1B = _BV(WGM13) | _BV(WGM12) | 1; //MODE 12
  TCCR1C = _BV(FOC1C);
  DDRB = 0x80;   
}
void Make2_6MHz(void){
  DDRB =0;
  TIMSK1 = 0;  // no interrupts
  ICR1 = 2; // MODE 12
  TCCR1A = _BV(COM1C0);  //Toggle OC1C
  TCCR1B = _BV(WGM13) | _BV(WGM12) | 1; //MODE 12
  TCCR1C = _BV(FOC1C);
  DDRB = 0x80;   
}
void Make2MHz(void){
  DDRB =0;
  TIMSK1 = 0;  // no interrupts
  ICR1 = 3; // MODE 12
  TCCR1A = _BV(COM1C0);  //Toggle OC1C
  TCCR1B = _BV(WGM13) | _BV(WGM12) | 1; //MODE 12
  TCCR1C = _BV(FOC1C);
  DDRB = 0x80;   
}

//Put the scope probe on Pin 27 of Teensy++
//You will see 4 different frequency squarewaves
//8MHz, 4MHz, 2.66MHz and 2MHz
//Cycling every 1.5 seconds
//Generating waveforms and not interrupting the Arduino timer

void loop() {
Make8MHz();
delay(1500);
Make4MHz();
delay(1500);
Make2_6MHz();
delay(1500);
Make2MHz();
delay(1500);
Make2_6MHz();
delay(1500);
Make4MHz();
delay(1500);
}
 
Hi everybody,

I'm upping this very old thread, to ask for some help about PWM generating on my Teensy2.0++.

I'm trying to use the routines described in this thread in order to drive the clock of a Z80 connected to the a Teensy 2.00++ pins.

I've tried to write a function (that I've called Make0MHz) that stops completely the clock.

This is the my code:

void Make0Mhz(void) {
DDRB =0;
TIMSK1 = 0; // no interrupts
ICR1 = 0; // MODE 12
TCCR1A = _BV(COM1C0); //Toggle OC1C
TCCR1B = 0; // TTCR1B to 0
TCCR1C = _BV(FOC1C);
DDRB = 0x80;
}

As you can see I've simply tried to put register TCCR1B to 0, in order to clear bits CS11, CS12 and CS10 of the register and stop the clock source.

Unfortunately the routine does not work, and I get from pin 27 is the same set before calling Make0MHz.

If for example the loop() is:

void loop() {
Make8MHz();
delay(3000);
Make4MHz();
delay(3000);
Make0Mhz();
delay(3000);
}

I get from pin 27 a 4MHz clock for 6 seconds, as the Teensy apparently totally ignores my 0MHz function.

My PWM programming knowledge is very limited: can someone help me ?

Thanks a lot in advance.
 
OK, solved !

My routine works, it was the multimeter I used for measurement that wasn't fast enough to register the absence of clock: using a scope I was able to see that the Make0MHz routine worked as expected :)
 
Glad you solved it.

Next time, please post a complete program that can be copied into Arduino and actually run on a Teensy++ 2.0 board. Much better help can be given when problems are reproducible.
 
Status
Not open for further replies.
Back
Top