Go Down

Topic: delay prob (Read 997 times) previous topic - next topic

abhishek_s5s

#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;
}







AWOL

#1
May 03, 2012, 09:21 pm Last Edit: May 03, 2012, 09:23 pm by AWOL Reason: 1
Comments.
Yeah, I'm really into comments.

And code tags.

And lack of txt.

But apart from all that, and the poll, great post.
"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.

spycatcher2k

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.
Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly

michael_x


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? ;)

abhishek_s5s

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

AWOL

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)
"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.

michael_x

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.

Nick Gammon


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.
http://www.gammon.com.au/electronics

Nick Gammon

What do these options even mean?


  • delay prog

  • c lang




http://www.gammon.com.au/electronics

trendski

#9
May 04, 2012, 10:25 am Last Edit: May 04, 2012, 10:29 am by trendski Reason: 1
Hi
I don't know how the delay function works in C but this looks wrong 12000000 is 12MHz not 8MHz

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


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

Code: [Select]
/*
* 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.
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)

AWOL

I've pretty much given up trying to reconcile comments with code.
Too much cut-and-paste, too little comprehension.
"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.

Nick Gammon

Quote
i need the delay for this motor in c lang


Whatever that means.
http://www.gammon.com.au/electronics

Go Up