PWM to a Servo with a ATtiny2313

12 posts / 0 new
Author
Message

I've been trying to get a servo to work using PWM, but all the tutorials I've found on the google are based on the timers and not the PWM its self, and don't have allot of information. I don't know the registers, and don't know the interrupts---i think i need :?: . Can someone help me get my servo working please?

Life Is Like A Bucket Of Chicken.

You shold not only search the tutorials, very little code is needed and this I bet has been posted 10 or more times in the forums. BTW, no interrupt is needed. Note that the code below is actually for 2 servos. To move them just change the OCR1A and OCR1B values. The example is for an AVR running at 8Mhz (with the /8 prescaler this makes for nice values - in micro seconds).

```	// OC1A, OC1B outputs
DDRB |= (1<<PB4)|(1<<PB3);
// TOP, set for 50Hz (20ms)
ICR1 = 20000;
// Center outputs (1.5ms)
OCR1A = 1500;
OCR1B = 1500;
// Timer 1 fast PWM mode 14
// Clear on compare, set at TOP
// /8 prescaler
TCCR1A = (1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
TCCR1B = (1<<WGM13)|(1<<WGM12)|(1<<CS11);```

/Lars

I sort of get the code but could you explain the math and the register?

Life Is Like A Bucket Of Chicken.

Math is
n = (f/prescaler)*t
Where n is the the number of timer ticks (as written in ICR1, OCR1A, OCR1B above). f is the frequency you run the AVR at. t is the wanted time. You can compute this with f in Hz and t in seconds or often better with f in MHz and t in microseconds. Notice for an 8MHz AVR with /8 prescaler this becomes
n = 8/8*t
n = t
so here you can just write the time (in microseconds) in the registers.
BTW check out the timer tutorial if you have not already:
https://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=50106
/Lars

How do I change the prescaller?

Life Is Like A Bucket Of Chicken.

Taco_Bell wrote:
How do I change the prescaller?

You have read the datasheet, haven't you?

Regards,
Steve A.

The Board helps those that help themselves.

Taco_Bell

I suggest you read this application note from Atmel:

http://www.atmel.com/dyn/resourc...

It seems that you don't quite understand the use of the timers, as well as how PWM works based upon them. That app note will help you quite a bit.

Here's how I think the clocks works, correct me if I'm wrong. I have a 4 MHz Xtal on my micro, and the prescaller is set to 1024(I sort of get the registers for that), and every time the Xtal oscillates 1024 times the counter counts 1(witch is 3.90625 ms or 1 73906.25th of a second if I did the math right)?

I still don't get how to output the timer on the pin, but was I right so far?

p.s. The data sheet was confusing but there was a little information that I could understand in it.

Life Is Like A Bucket Of Chicken.

Last Edited: Wed. Jan 23, 2008 - 11:07 PM

Reading the datasheet is sort of like working a crossword puzzle.... by the time youve gone thru it a couple of times, you start to fill in the blank spots. But that reading stuff is hard work. Not many people know how to do it. Best thing is to make friends with a Real Smart Guy and dont let him out of your sight for the rest of your life.

Imagecraft compiler user

bobgardner wrote:
Reading the datasheet is sort of like working a crossword puzzle.... by the time youve gone thru it a couple of times, you start to fill in the blank spots.

That's a good way to put it!

That's why I recommended the app note to Taco_Bell. The app notes include background info, example code and discusses the function and use of particular elements of the microcontroller.

Taco_Bell did you read the app note that I linked to? It not only explains the prescaler function but how to apply it and how to do the math do get the desired value for the overflow. In regards to toggling an output pin after an overflow or output compare match, the app note explains how to do that and what registers are associated with it. From that you can use the datasheet for the 2313 and find the corresponding registers. Heck the last few pages of that particular app note explain the theory, calculation and application of pwm...exactly what you are trying to do.

The only thing it doesn't note is the timing for servos. Centering on the servo is at 1.5 ms, the extremities of the servo's range are roughly 1.0 and 2.0 ms, although you'll have to play with it a bit and see just how much play you have mechanically.

Quote:
I still don't get how to output the timer on the pin, but was I right so far?

Hmm, no. Did you read the suggested links (timer tutorial, app note)?
Quote:
I have a 4 MHz Xtal on my micro, and the prescaller is set to 1024
Are you looking at some other code or? Anyway, this would give you a timer counting at 4000000/1024 Hz which is 3906.25 Hz. So it counts every 1/3906.25 seconds which is every 256 microseconds. Now this is only true if you have set the fuses so the AVR is actually using the Xtal as its clock source.
/Lars

Old post I know..

Thanks "Lajon" ... I get it now.

Perfect info !! for a noob (like me)

And like "bobgardner" said

"Best thing is to make friends with a Real Smart Guy and don't
let him out of your sight for the rest of your life."

You could say that (AVR Freaks) is that smart guy..

"We look for things.. Things that make us go."