Poll
Question: i am doin hexapod, i have wrote c code for 12 dc motors, all r running but the delay is the problem and motors r running beyond the limit, am using 60 rpm dc motor and i need the delay for this motor in c lang
delay prog - 0 (0%)
c lang - 2 (100%)
Total Voters: 2

Pages: [1]   Go Down
Author Topic: delay prob  (Read 678 times)
0 Members and 1 Guest are viewing this topic.
banglore
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#include <avr/io.h>       //for all avr atmega microcontrollers
#include <util/delay.h>    // busy-wait header file
#include <stdio.h>        // for c code
#include <math.h> 
#define F_CPU 12000000UL  // 8MHz crystal freq

void move_forward(void)
{
int i;
 PORTA=0x22;
 PORTC=0x02;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x84;
 PORTC=0x08;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x11;
 PORTC=0x01;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x48;
 PORTC=0x04;
 for(i=0;i<10;i++)
 _delay_ms(1000);

 PORTC=0x20;
 PORTD=0x22;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x40;
 PORTD=0x84;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x10;
 PORTD=0x11;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x80;
 PORTD=0x48;
 for(i=0;i<10;i++)
 _delay_ms(1000);
}

void move_backward(void)
{int i;
 PORTA=0x22;
 PORTC=0x02;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x48;
 PORTC=0x04;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x11;
 PORTC=0x01;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x84;
 PORTC=0x08;
 for(i=0;i<10;i++)
 _delay_ms(1000);

 PORTC=0x20;
 PORTD=0x22;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x80;
 PORTD=0x48;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x10;
 PORTD=0x11;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x40;
 PORTD=0x84;
 for(i=0;i<10;i++)
 _delay_ms(1000);

}

void turn_right(void)
{int i;
 PORTA=0x22;
 PORTC=0x02;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x48;
 PORTC=0x04;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x11;
 PORTC=0x01;
 for(i=0;i<10;i++)
 _delay_ms(1000);

 PORTC=0x20;
 PORTD=0x22;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x80;
 PORTD=0x48;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x10;
 PORTD=0x11;
 for(i=0;i<10;i++)
 _delay_ms(1000);

}

void turn_left(void)
{int i;
 PORTA=0x22;
 PORTC=0x02;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x84;
 PORTC=0x08;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTA=0x11;
 PORTC=0x01;
 for(i=0;i<10;i++)
 _delay_ms(1000);

 PORTC=0x20;
 PORTD=0x22;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x40;
 PORTD=0x84;
 for(i=0;i<10;i++)
 _delay_ms(1000);
 PORTC=0x10;
 PORTD=0x11;
 for(i=0;i<10;i++)
 _delay_ms(1000);

}

int main(void)
{
 unsigned int k,h;
 DDRA=0xff;               //all pins of portA made as output
 DDRB=0x00;               //all pins of portb made as input   
 DDRC=0xff;
 DDRD=0xff;
 while(1)
  {
   k=~PINA;
   h=k & 0x0f;
   PORTB=0x0f;            // pin made to exhibit high initially (pull up reg)
   
   switch (h)
   {
    case 0x02:
    {
     move_forward();
     break;
    }
    case 0x08:
    {
     move_backward();
     break;
    }
    case 0x06:
    {
     turn_right();
     break;
    }
    case 0x04:
    {
     turn_left();
     break;
    }
   }
  }
   return 0;
}






Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24286
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Comments.
Yeah, I'm really into comments.

And code tags.

And lack of txt.

But apart from all that, and the poll, great post.
« Last Edit: May 03, 2012, 02:23:38 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Scotland
Offline Offline
God Member
*****
Karma: 11
Posts: 988
Have you had your Arduino fix today?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

1 - You have started a poll, not asked a question.
2 - This is an arduino forum - use the AVR-Studio or BASCOM or whatever software your are using FORUMS.
Logged

Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have started a poll, not asked a question.
Why do you want a question? A question asks for an answer, a poll for an opinion.

Look at the alternatives given, what's your opinion? smiley-wink
Logged

banglore
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dude michael_x i started using this forum just yesterday, and dont know where to ask the que,if u have any program for dc motor delay code then please post it
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24286
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
if u have any program for dc motor delay code
If you can explain what "DC motor delay code" is, we'll probably be happy to help.
The code you posted above is not Arduino, and this is an Arduino forum, so our answers may not match your expectations.

(please try not to use txt)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Germany
Offline Offline
Faraday Member
**
Karma: 49
Posts: 2723
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry.

But you're on the wrong site, not only in the wrong forum.
Programming Arduino means main() is hidden, and for delays you deal with the millis() function or even use the delay() function.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dude michael_x i started using this forum just yesterday, and dont know where to ask the que,if u have any program for dc motor delay code then please post it

Read this before posting a programming question

We don't make these stickies just for our own amusement you know.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do these options even mean?

  • delay prog
  • c lang


Logged

Durham UK
Offline Offline
Full Member
***
Karma: 1
Posts: 167
aka Craig Turner
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
I don't know how the delay function works in C but this looks wrong 12000000 is 12MHz not 8MHz

Quote
Code:
#define F_CPU 12000000UL  // 8MHz crystal freq

Here is a C program I found that uses <util/delay.h> :

Code:
/*
 * Hello world of AVR. Blink led PORTB5 (Arduino digital 13).
 *
 * To compile and upload run: make clean; make; make program;
 *
 * Copyright 2009-2011 Mika Tuupola
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 */
 
#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 16000000UL  // 16MHz crystal freq

int main(void)
{
    /* Make PORTB5 (Arduino pin 13) an output. */
    DDRB |= _BV(PORTB5);

    for(;;){
        /* Toggle state of PORTB5 (Arduino pin 13). */
        PORTB ^= _BV(PORTB5);
       // _delay_ms(1000);
    }
   
    /* Never reached. */
    return 0;
}

I tried it and works.
« Last Edit: May 04, 2012, 03:29:58 am by trendski » Logged

Craig Turner, blog: http://gampageek.blogspot.co.uk/ It helps with my learning if I write things down, esp. for others to follow (constructive comments welcomed to improve)

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24286
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've pretty much given up trying to reconcile comments with code.
Too much cut-and-paste, too little comprehension.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i need the delay for this motor in c lang

Whatever that means.
Logged

Pages: [1]   Go Up
Jump to: