Author Topic: Need help with a simple code  (Read 3256 times)

0 Members and 1 Guest are viewing this topic.

Offline rahulTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Need help with a simple code
« on: September 01, 2008, 10:22:59 AM »
Hello everyone,

I have been working with AVR Atmega8 and since I am very new to robotics programming I was trying my hand with DC motors. This is what I have to

achieve at the moment:

1. When one pin on Atmega8 is applied 5v

2. another pin on Atmega8 turns on which is connected to L293D motor driver and this runs motor 1

3. then a delay of sometime

4. Then the same pin is turned off and thus motor 1 stops.

5. Another delay of same time

6. then another Pin on Atmega8 is turned on that is again connected to Motor Driver (L293D) and this time it runs second motor (Motor 2)

7. Then another Delay

8. And this pin is then turned off causing motor 2 to stop.

this procedure goes for ten times.

My attempt for the code is as follows:

// Testing with 2 motors

#include<avr/io.h>
#include<avr/delay.h>
void main()
{
int i;
DDRC=0x00  //set port c as input
DDRD=0xff  //set port D as output
DDRB=0xff  //set port B as output

for(i=1; 1<10; i++)
{
If(Pin28=1);   //getting input from 28th pin (PC5)
{
PortD=0x01;   //Setting PD0 as on which goes to motor driver
_delay_loop_2(2000);
PortD=0x00;   //setting PD0 as off which stops the motor
_delay_loop_2(2000);
PortB=0x01;   //Setting PB0 as on which goes to motor driver
_delay_loop_2(2000);
PortB=0x00;   //setting PB0 as off which stops the motor
_delay_loop_2(2000);


}

}
}


Can anyone tell me if this will work?

Or help me with a better code?

This is my second project after the blinky !

Please help !
« Last Edit: September 01, 2008, 10:29:42 AM by rahul »

Offline MadMax

  • Full Member
  • ***
  • Posts: 58
  • Helpful? 0
Re: Need help with a simple code
« Reply #1 on: September 01, 2008, 12:23:57 PM »
it looks okay, except for this line:

if (Pin28 = 1) {

You're assigning the value 1 to pin 28 (hint: this won't work when it's an input pin), if you're checking whether it equals 1, you should try:

if (Pin28 == 1) {

I'm not sure abut the Tris and Port naming, because I don't code with AVR. But when you adjust the error you made above, you should be doing just fine (assuming you're wiring the thing correctly)

Offline rahulTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: Need help with a simple code
« Reply #2 on: September 02, 2008, 06:36:18 AM »
there seems to be a serious problem with this code. Its giving errors.


http://link.imgshare.us/92jENa



My modified code:
++++++++++++++++++++
==================


// Testing with 2 motors

#include<avr/io.h>
#include<stdio.h>

int main(void)
{
int i;
DDRC=0x00;  //set port c as input
DDRD=0xFF;  //set port D as output
DDRB=0xFF;  //set port B as output


for(i=1; 1<100; i++)
{
If(Pin28==1)   //getting input from 28th pin (PC5)
{
PortD=0x01;   //Setting PD0 as on which goes to motor driver
delay(2000);
PortD=0x00;   //setting PD0 as off which stops the motor
delay(2000);
PortB=0x01;   //Setting PB0 as on which goes to motor driver
delay(2000);
PortB=0x00;   //setting PB0 as off which stops the motor
delay(2000);


}

}
}

_____________________________________________________________



This gives the following error .. I am a bit confused .. please help me out !


http://link.imgshare.us/92jEit

Its urgent !

Many thanks !
« Last Edit: September 02, 2008, 06:41:36 AM by rahul »

Offline Iron Man

  • Full Member
  • ***
  • Posts: 74
  • Helpful? 0
Re: Need help with a simple code
« Reply #3 on: September 02, 2008, 07:34:26 AM »
don't capitalize the i in the if statement

should just be

if ... not If

you can't just say Pin28.... at least not in any code i've done, typically you have to do something like
Code: [Select]
PORTCbits.PC5   if you want to access pin 28, which is pc5,

to find out exactly what to call look for the definition.
there should be a definition of it in io.h or stdio.h

you also seemed to have lost delay.h along the way somewhere, i'm not sure if you need it, but seeing as your calling a delay, you might just need it unless it's contained in io.h or stdio.h

Offline rahulTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: Need help with a simple code
« Reply #4 on: September 03, 2008, 02:54:42 AM »
I guess you are right. I figured out that now.

But I am not that good with programming and I am still learning. Its my second project; first was blinky!

So I tried another code .. it is :

________________________________________
// Blinky_modified to motor cntrol.c

#include <avr/io.h>
#include <util/delay.h>

int main(void)
{

// set PORTD for output

DDRD = 0xFF;

// Set Port B for output.
DDRB = 0xFF;

while(1)

{
PORTD = 0x01;
_delay_loop_2(30000);
_delay_loop_2(30000);
PORTD=0x00;
_delay_loop_2(30000);
_delay_loop_2(30000);
PORTB = 0x01;
_delay_loop_2(30000);
_delay_loop_2(30000);
PORTB = 0x00;

_delay_loop_2(30000);

}


}


_____________________________________________________



Stripping of all If's and for loops; I figured out if I provide a switch at the Vcc or AVcc of the Atmega, then turning it on and off would eliminate of getting an input from Pin28 of port C. But this is only the trick. I must rely on this till I learn programming properly! >:(

Now the trouble is ...
While testing the code I used LEDs instead of motors at the L293D output pins, and the code worked fine. The LEDs were blinking for the time for which the motors should run i.e. alternatively; and I was glad for that. But as soon as I connected a motor across the output terminals of L293D (Pin 11 and 14),  parallel to LEDs, it works fine for a few moments and after that either both LEds turn on or Either one glows continously .. and the one having a motor parallel to LEDs stops glowing.

The motor I am using is "DVD motor 5.9V ; code: RF-300C-12350 "

I am using "6F22,9V Alkaline" batteries.

here is the Schematic
 

The whole circuit works well with a single 9v battery (which now gives 7.5v because its used up) but the whole circuit gives the same above mentioned problem when I apply a higher voltage using two batteries to the 8th Pin of L293D ( The Vc Pin). Vcc is supplied with 5v using a 7805 voltage regulator. I have seperately checked the 7805 and it works fine.

Where am I going wrong? Any idea?
Pls. I need to get this done quick !
Help me out guys !


Rahul.

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
    • dunk's robot
Re: Need help with a simple code
« Reply #5 on: September 03, 2008, 03:34:54 AM »
sounds like your motors are drawing too much power and browning out your microcontroller.
try a fresh battery.

if that does not work, try a 5v source for your microcontroller and a separate battery for the motors (and motor drivers). if you use more than one power source, don't forget to join all the grounds together.


dunk.

Offline rahulTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: Need help with a simple code
« Reply #6 on: September 03, 2008, 04:18:44 AM »
Thanks pal, tried that and it works fine !

Thanks a lot !

 


Get Your Ad Here

data_list