servo not working

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

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

void main()
{TCCR1B = 0x00;
    //Configure TIMER1
    TCNT1=0x00;
    TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11); //NON Inverted PWM
    TCCR1B|=(1<<WGM13)|(1<<WGM12)|(0<<CS11)|(1<<CS10); //PRESCALER=1 MODE 14(FAST PWM)
    
    ICR1=19999; //fPWM=50Hz (Period = 20ms Standard).
    
    DDRB|=(1<<PB6)|(1<<PB5)|(1<<PB7); //PWM Pins as Out
    
    while(1)
    {
        
        OCR1A=0; //0 degree
        _delay_ms(1000);
        
        OCR1A=600; //45 degree
        _delay_ms(1000);
        
        OCR1A=950; //90 degree
        _delay_ms(1000);
        
        OCR1A=1425; //135 degree
        _delay_ms(1000);
        
        OCR1A=1900; //180 degree
        _delay_ms(1000);
        
        OCR1A=1425; //135 degree
        _delay_ms(1000);
        
        OCR1A=950; //90 degree
        _delay_ms(1000);
        
        OCR1A=650; //45 degree
        _delay_ms(1000);
        
    }
}
 

 

servo is not moving..i have checked that servo is not damaged. so what is problem in code

Kunal Gupta

github.com/gkunalupta

Last Edited: Sun. Feb 9, 2020 - 06:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What chip is this?  Make sure the bits are in the proper registers (I didn't check).

Can you check the PWM on a scope?

Always use =  instead of  |= if possible

 

what % duty are you trying to get for each angle?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

It is atmega 2560 and i have checked the proper placing of registers.
And for checking the pwm on scope i trying to use data visualizer of the atmel studio ..but on their it is not configuring my interferences (i am using usbasp and serial port).
So if any suggestions on what should i do so that i can configure my interferences.
.
And regarding duty cyle i am giving different ocsr1 values to generate different duty cycle

Kunal Gupta

github.com/gkunalupta

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

what % duty are you trying to get for each angle?  You need to be specific...maybe you have the wrong % for the angle for that servo.

 

A scope can help to see what is really going on--use one, if possible!!  A cheapo logic analyzer can work too.

 

Try setting COM1B0 & COM1A0 AS WELL  (so you get 11, instead of 10)

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Feb 9, 2020 - 09:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For scope only i am trying to use data visualizer.
And regarding duty cycle ...i want motor to move at different angles that i can control ...so that's why i suppose duty cycle is directly depend on the angle to which i want it or in short the value of theOCR1A
value will directly control the duty cycle and angle to which i want it ...
I suppose my concept is right??

Kunal Gupta

github.com/gkunalupta

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

Kunalgupta wrote:
For scope only i am trying to use data visualizer.
As in your other thread about this I fear you have misunderstood what "data visualizer" actually is - it is NOT a substitute for a scope. If you are generating PWM to drive servos you need a real scope (or a logic analyser) to probe the pin where the signal is being generated and see if it is really a 20ms/50Hz cycle wave with a 1ms..2ms active high period. 

 

Data visualizer is only intended (when you have the right bits in the chain for it to be active) to externally visual data that is changing inside the AVR. That does not really apply to things like PWM output signals.

 

As I said in another thread, a logic analyser may be all you need - you can buy some cheap and cheerful ones on ebay for $5..$10 and use with the "sigrok" program that supports most such devices.

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

For scope only i am trying to use data visualizer.
 

Not sure what you mean by this...In any case, what do you see?  Is it the desired Duty cycle & freq? 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:
Not sure what you mean by this

See the other thread: https://www.avrfreaks.net/forum/...

 

He's trying to use the Data Visualiser in Atmel Studio

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

These values don't seem right:

Kunalgupta wrote:
 ICR1=19999; //fPWM=50Hz (Period = 20ms Standard).
    
    DDRB|=(1<<PB6)|(1<<PB5)|(1<<PB7); //PWM Pins as Out
    
    while(1)
    {
        
        OCR1A=0; //0 degree
        _delay_ms(1000);
        
        OCR1A=600; //45 degree
        _delay_ms(1000);
        
        OCR1A=950; //90 degree
        _delay_ms(1000);
        
        OCR1A=1425; //135 degree
        _delay_ms(1000);
        
        OCR1A=1900; //180 degree
        _delay_ms(1000);
        
        OCR1A=1425; //135 degree
        _delay_ms(1000);
        
        OCR1A=950; //90 degree
        _delay_ms(1000);
        
        OCR1A=650; //45 degree
        _delay_ms(1000);

 

A servo is looking for a pulse with a width between 1ms and 2ms, so in your timer ocr counts would be between 1000-2000 where 1500 would be 50%!

 

Jim

 

 

 

 

 

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

Simply try stepping all possible OCR values & see what happens...who know what that servo needs.

  go up by a count of 50 each time, maybe every 100 ms

 

Again, what do you see on your scope/visualizer?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Feb 17, 2020 - 04:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hook up a LED to the PWM signal to see if it changes according to the duty cycle.

/Jakob Selbing