CPU frequency with SG90 SERVO

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi friends,  I have a question. SG90 works slowly when cpu frequency increase. What is this reason? (I know  frequency values that write in SG90 datasheet )

AVR student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, an SG90 is a servo? and the cpu is what?

Your code is what?

What frequency are you changing? How are you changing it?

Post your code, tell us what works, and what does not work.

SpiderKenny
@spiderelectron
www.spider-e.com

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The crystal ball is real fuzzy right now, will try to divine answer later when it clears! 

 

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
/*
 * Servo.c
 *
 * Created: 14/02/2016 09:45:26 a.m.
 * Author : Ataberk
 */ 

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>


void timer_init(){
	//pwm mode 14
	TCCR1A|=(1<<WGM11)|(1<<COM1A1);//non inverting pwm
	TCCR1B|=(1<<WGM12)|(1<<WGM13)|(1<<CS10);// no prescaling
	ICR1=20000;//50Hz pwm signal @1Mhz
}

int main(void)
{
	timer_init();
	DDRB=0xff;
	
	while(1)
	{
		
		servo_angle(0);
		_delay_ms(500);
		servo_angle(90);
		_delay_ms(500);
		servo_angle(180);
		_delay_ms(500);
	}
}

void servo_angle(uint8_t degree){
	// one timer clk=20ms/20000=1us
	// servo 0 degree=388us
	// servo 180 degree=2140us
	//2140-388=1752
	//1752/180=9.73
	OCR1A=9.73*degree+388;
}

İn here, SG90 SERVO works slowly when I increase F_CPU frequency.Why? I dont understand it.

AVR student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The delay functions

_delay_ms();

are implemented as busy-wait loops. See the docs here http://www.nongnu.org/avr-libc/user-manual/group__util__delay.html

The loop count depends on F_CPU. As you increase the value of F_CPU , the loop count will increase proportionally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

İn here, SG90 SERVO works slowly when I increase F_CPU frequency.Why? I dont understand it.

 

What I do not understand is why would you change #define F_CPU.

It only tells the compiler on what frequency is your Avr running.

So why do you try to tell a lie?

 

 

Last Edited: Tue. Feb 16, 2016 - 08:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Visovian wrote:

İn here, SG90 SERVO works slowly when I increase F_CPU frequency.Why? I dont understand it.

 

What I do not understand is why would you change #define F_CPU.

It only tells the compiler on what frequency is your Avr running.

So why do you try to tell a lie?

 

 

I tried F_CPU 1000000UL then tried F_CPU 8000000UL.  while F_CPU is 1000000UL Servo faster than 8000000UL. I dont tell lie.However,"mikericetga" explained  it above.

AVR student

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ataberk wrote:

I tried F_CPU 1000000UL then tried F_CPU 8000000UL.  while F_CPU is 1000000UL Servo faster than 8000000UL. I dont tell lie.However,"mikericetga" explained  it above.

 

I think you misunderstood the intent of his statement.  He's not accusing you of lying to us.  He's saying you're lying to the avr.  F_CPU should always match the speed your chip is running at.  If it doesn't, then you're doing something wrong.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I dont tell lie

As Rezer wrote, I meant:

Why do you try to tell a lie to the compiler.
 

I tried F_CPU 1000000UL then tried F_CPU 8000000UL.

So one of those must be a lie.

Last Edited: Wed. Feb 17, 2016 - 05:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to add that some users seem confused about what F_CPU is and what it isn't.

 

Some seem to believe that it is a magical way to change the speed of the AVR. If you set F_CPU to 1234,567 then the AVR will then run at 1.234567MHz. It won't.

 

The speed at which the AVR runs is fixed by the electronics and in particular (a) the way the CKSEL fuse bits are set and (b) (if enabled by CKSEL) the speed of the oscillator feeding the XTAL1(/XTAL2) pins. So the speed at which the AVR runs is a fixed number. It is your responsibility as a programmer to ensure that the value you give F_CPU in the code is the actual speed.

 

So if the AVR has a 12MHz oscillator selected then F_CPU can only sensibly be set to 12000000 and so on. If you have a 12MHz oscillator but tell the code that F_CPU is 8MHz then calculations of UART baud rate and the delays in _delay_ms() and _delay_us() will be incorrect by the ration of 8:12. So delays wil be 1.5 too long and UARt baud rate will be 1.5 times too slow and so on. Similarly if the oscillator is 12MHz but you said F_CPU was 16000000 then the calculations will be out by a ratio of 16:12. So the delays will only be 3/4 of the length they should be and so on.

 

So you cannot "lie" to the code and just pick some random number for F_CPU. For the code to work right you must know what speed the AVR is running at and F_CPU must be set to that number.