avr 8515 delay implementation in c for servos

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

Hi all,

I am trying to implement full 180 degree control of a servo. For this I need to write a delay method which delays the execution by 1-2ms (variable). Did anybody already implement control of a servo on the stk500/8515. Or does anybody have any sample code to get started? I've been hacking away at this for 3 days but without a scope it seems hopeless.

Any help appreciated,

Jakub

admin's test signature
 

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

there may be more elegant approaches, but i developed some cycle-wasting routines. there is a version for 8MHz and for 16Mhz clock. i tested the waits very carefully and they are very accurate. just cut and paste this:

void Wait16ms(u16 ms)
{
if (ms == 0)
return;

ms--;

Wait08us(250);
Wait08us(250);
Wait08us(250);
Wait08us(246);
_NOP();
_NOP();
_NOP();

#ifdef MHZ16
_NOP();
_NOP();
_NOP();
#endif

while (ms--)
{
Wait08us(250);
Wait08us(250);
Wait08us(250);
Wait08us(249);
_NOP();
_NOP();
_NOP();
_NOP();
#ifdef MHZ16
_NOP();
_NOP();
_NOP();
_NOP();
#endif
}
}
// ---------------------------------------------------------------------------

void Wait16us( u16 microseconds)//delays > 255 uS
{
while( microseconds > 255 )
{
Wait08us(255);
microseconds -= 256;
}
Wait08us( (u08) microseconds );
}

#if F_CPU == 8000000 /* 8Mhz */

void Wait08us(u08 microseconds)//delays < 255 uS-its for 8 MHz clock
{
if (microseconds < 2) return;
microseconds = microseconds - 2;

_NOP();_NOP();_NOP();_NOP();

while (microseconds--)
{
_NOP();_NOP(); _NOP();_NOP(); _NOP();
}
}
#endif
#if F_CPU == 16000000 /* 16Mhz */

void Wait08us(u08 microseconds)//delays < 255 uS-its for 8 MHz clock
{
if (microseconds < 2)
{
_NOP(); _NOP(); _NOP();
_NOP();
return;
}
microseconds = microseconds - 1;

_NOP();

while (microseconds--)
{
_NOP();_NOP(); _NOP();_NOP(); _NOP();
_NOP();_NOP(); _NOP();_NOP(); _NOP();
_NOP();_NOP(); _NOP();
}
}
#endif

admin's test signature
 

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

A millisecond is almost infinity in the terms of AVR clock cycles. ;-)
It's IMHO really worth to use a hardware timer for it.

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

Did anybody already implement control of a servo on the stk500/8515.

Take a look at the Academy up top, at the various User Projects. You'll probably find libraries or sample code for it

Alternatively, check out AVRLib:
It contains many useful libraries including for timers, and... (quick look) RC Servos as well.

admin's test signature