Author
Topic: PWM Servo code for my ATMega168 (Read 6291 times)

Hello, I've been struggling getting my servo to work correctly using the following PWM code. I've made a number of changes over the last week because I was at first under the impression my avr was clocked at 18.432mhz when infact it is 1mhz. The put the issue in a nutshell for the sake of this post, here it is: when I set OCR1A to 187 (Which is the center position) instead of moving to the center, my servo turns my sharp IR all the way to the right (And then some), then it moves it all the way to the left, the expected behavior would be to center itself. I've had one problem after another, I'm feeling slightly discouraged considering I'm using damn near the same code as everyone else.

After that code, I have some servo positioning functions, but even when I do something as simple as OCR1A = 187, it goes all the way right, then all the way left. Infact, it doesn't really mater WHAT value I give OCR1A, it always seems to do something like that. Any help would be highly appreciated, thanks.

My thought is that you either have a servo power supply issue (how are you powering your servo?) or, more likely, you are wrong about the clock frequency of your mega168. What makes you think it's 1 MHz? Have you verified this? What is your clock source? One of the simplest ways for you to check your clock speed (other than an oscilloscope or multimeter with a frequency mode) is to write code that uses a hardware timer to blink an LED once per second. Crunch the numbers ahead of time to get a 1 Hz blink rate, and then time the result to with a stopwatch to see if it matches what you expect. If instead you see eight blinks per second, you know your microcontroller's clock isn't what you thought it was.

Actually irobot create says my atmega168 is 18.432mhz everywhere (They must of modified it) the bottom line though, is that I tried writing correct code for 4 different speeds. 18.432mhz, 8mhz, 1mhz, and more. The only one where it moves smoothly is at 1mhz, but nonetheless it doesn't do anything remotely close to what I want. ANY value assigned to OCR1A makes it turn all the way right (If its not already there, then it still tries to move right), then it moves left, but at a slower and more twitchy manner. Me and some others have determined there may be something wrong with the servo at this point. When you say Servo power supply, do you mean a power supply in the actual servo? Or do you mean the 5v pin?

Actually irobot create says my atmega168 is 18.432mhz everywhere (They must of modified it) the bottom line though, is that I tried writing correct code for 4 different speeds. 18.432mhz, 8mhz, 1mhz, and more. The only one where it moves smoothly is at 1mhz, but nonetheless it doesn't do anything remotely close to what I want. ANY value assigned to OCR1A makes it turn all the way right (If its not already there, then it still tries to move right), then it moves left, but at a slower and more twitchy manner. Me and some others have determined there may be something wrong with the servo at this point. When you say Servo power supply, do you mean a power supply in the actual servo? Or do you mean the 5v pin?

Your code looks fine for an ATmega168 running at 1 MHz, but you should try to independently verify your clock speed rather than making assumptions or relying on trial and error. For example, if your ATmega168 is running at 18.432 MHz but has its divide-by-8 fuse bit programmed, your AVR would be running at 2.304 MHz. So once again, what is your clock source (crystal, resonator, internal RC oscillator)? Can you write some simple code that will let you verify the timing by flashing an LED updating an LCD or something?

When I say servo power supply, I mean: how are you powering your servo? Unless you have some kind of strange servo with a built-in battery, there is no power supply in your servo...

I adjusted the code for 2.304mhz, but I get your point. Yes, I could try putting something together that verifies the clockspeed. I'm not entirely sure how, I just began avr programming a month ago, but I'm sure I can figure it out. Thanks for the feedback.