Using Timer1 to generate a PWM wave

I am very uncertain how Timer1 functions. I have some sample code here that controls LPD6803 led chipset. According to the datasheet, the clock expects a 20Mhz square wave. I'm not sure how that relate to this code, can someone please help me understand?

void LPD6803::setCPUmax(uint8_t m) {
  cpumax = m;
  // each clock out takes 20 microseconds max
  long time = 100;
  time *= 20;   // 20 microseconds per
  time /= m;    // how long between timers
  Timer1.initialize(time);
}

The documentation says to set cpumax to between 50 - 100.

Here's a link to the datasheet

the clock expects a 20Mhz square wave
...

  // each clock out takes 20 microseconds max

A 20 µs clock period is not 20 MHz frequency.


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the "Code" icon above the posting area. It is the first icon, with the symbol: </>

http://snippets-r-us.com/

I realize that. That's why I'm confused. I didn't write this code, I found it. When comparing it with the data sheet, I don't understand what it's achieving, although it does seem to work.

Notice that the Data Clock Frequency says 25Mhz.

All the information you need about the Timers is in the Atmel datasheet - for the Atmega 328 if you are using an Uno.

This Thread may help to illustrate the process. It uses Timer2 for PWM, but the same principles apply for all the Timers.

...R

Notice that the Data Clock Frequency says 25Mhz.

I think, it's maximum clock freq., but IC 'd be fine with lower value. What is the name of quoted table?

Farther down in the data sheet in the "Suggested Working Parameter" section it says
Data clock frequency FCLK 0-15 MHZ, so slower should be OK.

And that datasheet says 24MHz and 25MHz max clock frequency anyway, not 20MHz. Its poorly
translated and not even consistent about the value, so I would treat everything in it with a pinch
of salt. Go get a library to drive it - it can probably be driven best with the SPI hardware which
can push out bytes at 8Mhz - failing that bit-banging, but I wouldn't use a timer to drive it, it will
just clock continously, the point is the serial data has to be ready too.

So I think I am understanding this better. I'm still a little hazy on the relationship between µs and Mhz, but from what I can glean, a single µs is the cycle time for 1Mhz. Therefore interrupting the timer every one µs gives us a 1 Mhz waveform. Is that correct? If so, then the 20µs used in the example above gives us a 20Mhz waveform. That's the intended purpose. Is that correct?

I'm still a little hazy on the relationship between µs and Mhz,

You mean Lazy?

Magician:
You mean Lazy?

Google

Lazy is what I would be if I used any of the search results from that search. The first 5 pages are all calculators, and while its nice to have a tool to do the work for me, it's nicer if I can figure out what the actual relationship is and calculate it myself.

But thanks to some helpful people on this forum who did set me on the right track with helpful information. I have deduced that period and frequency have an inverse relationship.

So 20Mhz = 1/20,000,000 microseconds

or

μs(x) = 1/x * 10^-6

μs(20) = .05μs

richbai90:
But thanks to some helpful people on this forum who did set me on the right track with helpful information. I have deduced that period and frequency have an inverse relationship.

That's right. So a 16 MHz clock frequency is a 62.5 ns period.

"I have deduced that period and frequency have an inverse relationship."
Yup, basic science in action there. Period = 1/Frequency.
electronics, light, water waves, sound.

Awesome! Thanks so much for your help everyone. I'll be taking calculus and physics in 2 semesters. Look forward to learning more about this stuff.

Something interesting I've noticed that I'm investigating, that perhaps one of you might know the answer to. In every number, I plug into that calculation since it gives me the period in seconds for a given frequency, ends up being (1/x * 100)e-y

I understand (1/x * 100), what my fascination is with, is the occurrence of Euler's constant, a number I am only vaguely familiar with from my math 1050 class in problems dealing with exponential growth and decay. What is the relationship between e and your waveform?

I'm sure I can figure it out eventually though enough google searches but if you know, I'm just intrigued by it.

richbai90:
So 20Mhz = 1/20,000,000 microseconds

No, 20MHz = 20,000,000 cycles per second, or one cycle per 0.05us - in both cases the
units are inverse-time.

MarkT:
No, 20MHz = 20,000,000 cycles per second, or one cycle per 0.05us - in both cases the
units are inverse-time.

Yes, I understand that now. 20MHz has a period of .05us. Or in other words, a timer switching on and off at .05us intervals has a frequency of 20MHz

I plug into that calculation since it gives me the period in seconds for a given frequency

What? Which calculation?

ends up being (1/x * 100)e-y

What is x? What is y? Can you provide an example? Are you sure you're not just playing with exponential identities of some kind?

There is a bunch of math connecting periodic waveforms with exponential functions, but you have to throw complex numbers into the game: Phasor - Wikipedia