๐Ÿ“ฃ๐Ÿ“ช ๐Ÿšš Adafruit will not be shipping USPS orders Monday October 14, 2019 ๐Ÿ—“. Expedited USPS orders placed after 11am ET ๐Ÿ•š Friday October 11 will go out Tuesday October 15 ๐Ÿ“ฃ๐Ÿ“ช๐Ÿšš
0

atmega168 servo control
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

atmega168 servo control

by wireframewolf on Fri Aug 22, 2008 1:33 am

I want to use an atmega168 outside of the Arduino to control a couple of servos, so I decided to try and just rip the code from Arduino's servo library and control servos on B1 and B2, but I can't seem to make it work. Ruling out it being a problem with the schematic, can anyone see what I'm missing from the code? I basically just took it from ServoTimer1.cpp and linearized it for a quick test, trying to control a servo on B2, but nothing happens (though the servo is getting power). I know that the make file has the correct settings (atmega168, F_CPU is 16000000, etc.)

Code: Select all | TOGGLE FULL SIZE
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )

uint16_t min16;
uint16_t max16;
int angle;

void write(int angleArg)
{
    uint16_t p;

    if ( angleArg < 0) angleArg = 0;
    if ( angleArg > 180) angleArg = 180;
    angle = angleArg;
    p = (min16*16L*clockCyclesPerMicrosecond() + (max16-min16)*(16L*clockCyclesPerMicrosecond())*angle/180L)/8L;
    OCR1B = p;
}

int main(void)
{

      
   DDRB = 4;
   min16 = 34;
   max16 = 150;
   uint8_t oldSREG = SREG;
   cli();
    TCCR1A = _BV(WGM11);
    TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS11);
    OCR1B = 3000;
    ICR1 = clockCyclesPerMicrosecond()*(20000L/8);
   TIMSK1 &=  ~(_BV(OCIE1B) | _BV(TOIE1) );
   SREG = oldSREG;
   
   TCCR1A = TCCR1A & ~_BV(COM1B0) | _BV(COM1B1);
   
   int i = 0;
    for(;;){
        for(i = 0; i < 100; i++){
         _delay_ms(30);
      }
      write(100);
      for(i = 0; i < 100; i++){
         _delay_ms(30);
      }
      write(180);
      for(i = 0; i < 100; i++){
         _delay_ms(30);
      }
      write(10);
    }
    return 0;   /* never reached */
}
wireframewolf
 
Posts: 21
Joined: Wed Mar 05, 2008 10:04 pm

by westfw on Fri Aug 22, 2008 2:32 am

I don't think I see you setting the pins to be output pins... I'm not SURE that that's necessary once you "attach" the timer to the pins, but it would be on SOME microcontrollers. Do you have a scope or logic probe so you can get an idea what (if anything) is coming out the pins?
westfw
 
Posts: 1568
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

by wireframewolf on Fri Aug 22, 2008 12:45 pm

As a matter of fact I did set the pins to be output pins. Since I'm only concerned about port B pin 2, I did DDRB = 4, which should theoretically make the third pin a 1 (pin 2, since they start at 0). Unfortunately, I don't have a scope of any sort. The only thing I know is that the servo initially gets electricity, but isn't putting up any fight when I try to turn it by hand.
wireframewolf
 
Posts: 21
Joined: Wed Mar 05, 2008 10:04 pm

Please be positive and constructive with your questions and comments.