Society of Robots - Robot Forum

Software => Software => Topic started by: Pierre on November 26, 2009, 10:48:00 PM

Title: Servo coding for the ATmega8
Post by: Pierre on November 26, 2009, 10:48:00 PM
Hi

     I am having difficulties with C codes with the modified servo.

     My goal is to make the servo spin:
     
          1. Go left Counter clockwise(ccw) using OCR1A 1000 or 1ms;
          2. Go right clockwise(cw) using OCR1A 2000 or 2ms ; and
          3. stop.  using OCR1A 1500 or 1.5ms

The theory says so but not for me

     Problems I have are:

          Values for OCR1A: 

               1.  Stop:  825 stops but keep getting twitches(reset);

               2.  Left(ccw):  1200 goes left for one (1) seconds stop and goes again.  It goes fine if I unplug it for 2 sec then put it back on; and

               3.  Right(cw):  540 full right(cw) no problems .

I seem to be using the right telemetry in the register.

     Clock for ATMega = 1 000 000
     Prescale = 1
     getting 50Hz with prescale at 1 and ICR1 at 10 000
     WGM13 at 1

     Here is below C codes I am using.  It is poor C programming but it is a start.  Hope anyone could see what I am missing.

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

int main(void)
{
DDRB=(1<<PB1); //set OC1A as output pin
TCCR1A = 0x80; //set OC1A to bottom
// Ask for non-inverted PWM on OC1A and OC1B
   TCCR1A = (1 << COM1A1) | (1 << COM1B1);
TCCR1B =(1<<WGM13) | (1<<CS10);//phase and frequency correct PWM | Prescaler n=1
ICR1 = 10000;  //(MCU_FREQ = 1000000/(2 x n=1 x 10000)) = 50Hz
OCR1A = 2000;   //initial value
   
   
}

Now that I have wrote this.  Having a loop inserted could probably fix the reset I get every second or so.

Thank you in advance for any help I may get
Title: Re: Servo coding for the ATmega8
Post by: Webbot on November 26, 2009, 11:18:35 PM
See theory and code examples here http://www.societyofrobots.com/member_tutorials/node/228 (http://www.societyofrobots.com/member_tutorials/node/228)