go_away

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

0 Members and 1 Guest are viewing this topic.

Offline lavrenTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
PWM Servo code for my ATMega168
« on: October 13, 2008, 02:56:34 PM »
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.

void init_pwm(void) {

   TCCR1A = _BV(WGM11) |  _BV(COM1A1);
   TCCR1B = _BV(CS11) | _BV(WGM12) | _BV(WGM13); /* prescaler of 8, fast PWM */

        // 3456 is the servo center position
        OCR1A = 187; /* 125 - 250 */
        ICR1 = 2500; // that evens out to 50hz

        DDRB = _BV(PB1); /* Set pin as output */
}

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.

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,632
  • Helpful? 169
    • Society of Robots
Re: PWM Servo code for my ATMega168
« Reply #1 on: October 16, 2008, 12:26:25 AM »
Got a multimeter with frequency Hz mode? It was really helpful when I did PWM my first time . . .

Look at how AVRlib does pwm (in timerx8.c), it will really help.

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 331
  • Helpful? 3
Re: PWM Servo code for my ATMega168
« Reply #2 on: October 16, 2008, 04:15:56 PM »
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.

- Ben

Offline lavrenTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: PWM Servo code for my ATMega168
« Reply #3 on: October 17, 2008, 11:38:20 AM »
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?

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: PWM Servo code for my ATMega168
« Reply #4 on: October 17, 2008, 02:05:06 PM »
Use the already written code in AVRlib for this, I believe its called servo.h

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 331
  • Helpful? 3
Re: PWM Servo code for my ATMega168
« Reply #5 on: October 17, 2008, 02:16:43 PM »
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...

- Ben

Offline lavrenTopic starter

  • Beginner
  • *
  • Posts: 4
  • Helpful? 0
Re: PWM Servo code for my ATMega168
« Reply #6 on: October 17, 2008, 03:31:18 PM »
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.

--Ryan

Offline pomprocker

  • Supreme Robot
  • *****
  • Posts: 1,430
  • Helpful? 16
  • Sorry miss, I was giving myself an oil-job.
Re: PWM Servo code for my ATMega168
« Reply #7 on: October 17, 2008, 05:34:18 PM »
Does it use AVRlib? what does F_CPU say in global.h? and what about your make file?

If the above files don't reveal anything then:

What are the fuses set for? Internal clock source? external clock source? etc... Read the datasheet, and check the fuses.

 


Get Your Ad Here

data_list