delay prob

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

Comments.
Yeah, I'm really into comments.

And code tags.

And lack of txt.

But apart from all that, and the poll, great post.

(deleted)

spycatcher2k:
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? :wink:

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

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)

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.

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

Read this before posting a programming question

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

What do these options even mean?

  • delay prog
  • c lang

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

#define F_CPU 12000000UL  // 8MHz crystal freq

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

/*
 * 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.

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

i need the delay for this motor in c lang

Whatever that means.