how to control dc motor

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

Hi 

Iam peter zaher 

i begenner in programing 

i want to control dc 2  motor by atmega 32 a 

but i can't 

it my code 

i need help 

 

 

# define F_CPU 16000000UL
#include<avr/io.h>
#include<avr/delay.h>
#define get_bit(reg,bitnum) ((reg & (1<<bitnum))>>bitnum)
///////////////////
void PWM_Iint (void){
    TCCR1A|=(1<<COM1B1)|(1<<WGM11)|(1<<WGM10) ;            //fast pwm mode 10 bit 
    TCCR1B|=(1<<WGM12)|(1<<CS10);
    
    
}

int main (void){
    
    
    DDRA&=~(1<<DDA0); 
    DDRA&=~(1<<DDA1);
     PORTA=0b00000000; // Initialize PORTA to zeros
      DDRD=0b11111111;
       DDRC=0b11111111;
       
    PWM_Iint ();
    OCR1A=800;
    OCR1B=0;
    while (1){
 if (get_bit(PINA,0)==1)
 {
     PORTC=0b00011000;
while (OCR1B<=255)
{
    OCR1B=OCR1B+1;
    _delay_ms(5);
}
_delay_ms(1000);
}

  if (get_bit(PINA,1)==1)
  {
      PORTC=0b00011000;
while (OCR1B>0)
{
    OCR1B=OCR1B-1;
    _delay_ms(5);
}
_delay_ms(1000);
}

else

PORTC=0b00000000;

    }
}

Last Edited: Mon. Mar 23, 2020 - 08:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks!

 

Start by telling us which AVR your using, please!

So begin by telling us how you want to control your motor(s), is it one or two?

Does control mean simple on/off, or do you want to control speed and direction as well.

Can you post a picture or schematic of how your motors are connected to the AVR?

 

Yes, we can tell your a beginner, by the use of binary numbers (0b00000000), most freaks would use hex 0x00, much easier to read, or better define constants with names.

 

Jim

 

 

 

 

 

Last Edited: Mon. Mar 23, 2020 - 05:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use binary statics as 0bxxxxxxxx when I'm setting bit flags.  I am a crusty olde farte.  S.

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

ki0bk wrote:
 much easier to read

Not sure I'd agree that hex is necessarily easier to read ?

 

Certainly, in the case of all-zeros, all-ones, full nibbles - but for just the odd bit in a byte, binary is often clearer.

 

The fundamental issue with binary literals, though, is that they are just not standard 'C' [1]

 

Some compilers do support them as a proprietary extension - but they are not in the standard [1]

 

better define constants with names.

Absolutely! Irrespective of the radix. 

 

https://en.wikipedia.org/wiki/Magic_number_(programming)

 

[1] I think there is a recent update to add them, but I couldn't find a reference

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

ki0bk wrote:
Start by telling us which AVR your using, please!
The end of the 4th line in the OP's message.

Ross McKenzie ValuSoft Melbourne Australia

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

...and back on topic...

 

Hi Peter,

 

start by telling us how your hardware is set up. What are you driving the motors with? What is each port of your micro connected to?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

Well I find counting 1's and 0's in binary constants to be tedious and have seen more than once where the error in the code was having 7 or 9 bits in the constant where 8 was intended.

But I'll let this drop as it's not the OPs issue here, thanks for the feedback.

 

Jim

 

 

 

 

 

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

This is a mistake:

void PWM_Iint (void){
    TCCR1A|=(1<<COM1B1)|(1<<WGM11)|(1<<WGM10) ;            //fast pwm mode 10 bit 
    TCCR1B|=(1<<WGM12)|(1<<CS10);
 

You should use =   not |=     An or operation can only set bits to 1, not clear any bits to 0

Other bits need cleared to zero for the mode to be properly selected; here you are merely setting certain bits to 1, meaning other bits you need cleared to zero might not actually be set that way.

If that happens you will be in a different mode than what the init says it is....this is dangerous as it causes big debugging confusion.

Always use = for init, if possible.

 

Why did you do this?  Who told you to do this?

 

 

 

 

 

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

Last Edited: Tue. Mar 24, 2020 - 04:51 PM