Could a newbie get some sample code?

Status
Not open for further replies.

smorehou

Member
Hi. I have a Teensy 2.0. I was hoping someone could give me a complete sample code that does the simple (I think its simple?) task of outputing a 1Mhz signal on one of the output pins? That will give me a good start for experimenting. I've tried a few things but its not working. If I can start with a solid basis, I should be good. Thanks.
 
It's easiest in Arduino with Teensyduino, since those libraries already exist, so all you have to do is call the already-made function.

Of course, if you want a more difficult non-Arduino way, you could just program it in C to write directly to the timer registers. You might look at that library's source code for a hint.
 
No, easy is good. I just thought learning the *duino stuff when I have no experience would be making it more difficult, not less. You've pointed me in a direction. Thank you.
 
OK, so I got teensyduino running, etc... but I don't get how to generate a clock wtih that library. Some REAL BASIC help would be appreciated. What exactly do I do?? How exactly to I control the Hz of that signal? What pin exactly am I outputting on?

I installed the libraries you suggested and they give me two options, neither of which are "clock". I get "fanspeed" and "Interrupt". I tried fanspeed. In the code it says the pin is "4". Huh? "4"? B4? F4? D4?. Or physically the 4th pin on the Teensy? I tried that and my scope shows some wave-ish output, but no matter what variables I change in the code, its the same wave.

Thoroughly confused. Which is why I wanted to see BASIC and complete code for generating a clock, so I have some basis for moving forward.
 
I installed the libraries you suggested and they give me two options, neither of which are "clock". I get "fanspeed" and "Interrupt". I tried fanspeed.

FanSpeed is the one you want. It creates a 25 kHz signal. All you need to do is change to 1 MHz, delete all the stuff about changing the duty cycle, and set a fixed 50% duty cycle.

Here's how easy that is:

Code:
#include <TimerOne.h>

const int fanPin = 4;

void setup(void)
{
  Timer1.initialize(1);  // 1 us = 1 MHz
  Timer1.pwm(fanPin, 512);
}

void loop(void)
{
}


In the code it says the pin is "4".

Your Teensy 2.0 came with a pinout reference card. Read the Arduino side.

If you lost the card, the info is here:

http://www.pjrc.com/teensy/pinout.html
http://www.pjrc.com/teensy/card2b.pdf


Thoroughly confused. Which is why I wanted to see BASIC and complete code for generating a clock, so I have some basis for moving forward.

Here it is. I did it all for you!

I even tested this with a frequency counter here, to confirm it does output a 1 MHz waveform on pin 4.
 
THANK YOU!

I actually managed it with the LED blink sample. Though only in math. My scope says it was maxing out at ~250khz. I'll try yours. Thanks! That should get me going.
 
Status
Not open for further replies.
Back
Top