Would you like to react to this message? Create an account in a few clicks or log in to continue.

You are not connected. Please login or register

Servo movement on atmega328

2 posters

Go down  Message [Page 1 of 1]

1Servo movement on atmega328 Empty Servo movement on atmega328 Thu Aug 26, 2021 2:11 am

Furk



Hello guys,

I am new at this forum and I want to make a simulation of 2 servos on a atmega328. The servos have a period of 20ms and the pulse is between 1-2ms.
I wrote the following code :

Code:

#include <avr/io.h>
#include <util/delay.h>
//internal clock 1Mhz in the settings
int main(void)
{
DDRB |= 0xFF;

TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1A0 1<<COM1B1 | 1<<COM1B0;
TCCR1B |= 1<<WGM13 | 1<<WGM12 | 1<<CS10; //Prescaler=1,inverted, fast PWM,Mode 14
ICR1 = 19999;


while (1)
{

int i;
for(i=1000;i<=2000;i+100)
{
OCR1A = ICR1 - i;
OCR1B = ICR1 - i;
_delay_ms(100);
}

}
}
I believe that the code is correct but when I upload it to the simulide circuit which you can see in the attached file no movement happens.

I would be really grateful to any help or comment.



Last edited by arcachofo on Fri Dec 24, 2021 2:46 am; edited 1 time in total (Reason for editing : Marked as solved (green color))

2Servo movement on atmega328 Empty Re: Servo movement on atmega328 Thu Aug 26, 2021 1:38 pm

arcachofo

arcachofo

Hi.

Looks like there is a problem with this code in SimulIde_0.4.15.
I will have a look.

In 0.5.16 (development version) looks to be working ok.
Is this what you expect? :

Servo movement on atmega328 Servos10

Using this code, compiled with avr-gcc:
Code:
#define  F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRB |= 0xFF;

    TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1A0 | 1<<COM1B1 | 1<<COM1B0;
    TCCR1B |= 1<<WGM13 | 1<<WGM12 | 1<<CS10; //Prescaler=1,inverted, fast PWM,Mode 14
    ICR1 = 19999;

    while(1)
    {
        for( int i=1000; i<=2000; i+=100 )
        {
            OCR1A = ICR1 - i;
            OCR1B = ICR1 - i;
            _delay_ms(100);
        }
    }
}

3Servo movement on atmega328 Empty Re: Servo movement on atmega328 Thu Aug 26, 2021 2:12 pm

arcachofo

arcachofo

I think I found where is the problem.

As a workaround you can try to set ICR value before configuring the timer:
Code:
#define  F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRB |= 0xFF;

    ICR1 = 19999;
    TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1A0 | 1<<COM1B1 | 1<<COM1B0;
    TCCR1B |= 1<<WGM13 | 1<<WGM12 | 1<<CS10; //Prescaler=1,inverted, fast PWM,Mode 14
    
    while(1)
    {
        for( int i=1000; i<=2000; i+=100 )
        {
            OCR1A = ICR1 - i;
            OCR1B = ICR1 - i;
            _delay_ms(100);
        }
    }
}

4Servo movement on atmega328 Empty Re: Servo movement on atmega328 Thu Aug 26, 2021 3:30 pm

arcachofo

arcachofo

Issue solved at Rev 231.

5Servo movement on atmega328 Empty Re: Servo movement on atmega328 Thu Aug 26, 2021 6:54 pm

Furk



Thank you very much !!!! It works

arcachofo likes this post

Sponsored content



Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum