PDA

View Full Version : Could a newbie get some sample code?



smorehou
01-04-2013, 09:51 PM
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.

PaulStoffregen
01-04-2013, 10:02 PM
Have you tried the TimerOne or TimerThree library?

It has a setPeriod(us) function.

http://www.pjrc.com/teensy/td_libs_TimerOne.html

With such a fast period, do not use the Timer1.attachInterrupt() function.

smorehou
01-04-2013, 11:06 PM
is there a non teensyduino version? Or is this just easiest with teensyduino?

PaulStoffregen
01-04-2013, 11:19 PM
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.

smorehou
01-04-2013, 11:45 PM
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.

smorehou
01-05-2013, 11:59 PM
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.

PaulStoffregen
01-06-2013, 10:29 AM
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:



#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.

smorehou
01-06-2013, 04:02 PM
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.