DPRG
DPRG List  



[DPRG] AT90C2313-10PI for a line follower?

Subject: [DPRG] AT90C2313-10PI for a line follower?
From: Dale Wheat dale at dalewheat.com
Date: Sat Jul 15 17:18:07 CDT 2006

Walter,

I built a line follower that used just about exactly the same logic that 
I see in the code you've shown us.  It works great.

I have a couple of comments that might help you get it working.

1)  If, as your subject line suggests, you are wanting to use an Atmel 
AVR part, there's a couple of changes that you'll need to make to your 
register names.  The names in the code (trisa, trisb, porta) look 
suspiciously like Microchip PIC architecture, but the AVR has a very 
similar set, just with different names.

The AT90S2313 (now obsolete, and replaced by the ATtiny2313), has two 
I/O ports, called Port B and Port D.  Bigger parts with more pins have 
all A, B, C and D ports.

Be sure to use this line so the compiler will know what all the register 
names are:

#include <avr/io.h>

You have to first set up each port to tell it which pins are inputs and 
which are outputs.  Each port has a direction register (DDRB, DDRD), and 
writing a 1 to a bit makes that port pin an output, and writing a zero 
makes that port pin an input.

If the port pin is set up as an output, writing a 1 to that pin (using 
either PORTB or PORTD registers), turns that pin 'on', by raising the 
voltage to +5V or whatever Vcc is on your system.  Writing a zero to 
that bit turns that pin off.

If the port pin is an input, you can still write to the PORTB or PORTD 
registers, but then the ones and zeros just turn on or off, 
respectively, the built-in pullup resistors for that pin.

To read an input from the pin, you read the PINB or PIND registers. 
This is a completely different register from the PORTB or PORTD 
register.  I still make the mistake of trying to READ from the OUTPUT 
port and then wonder why the input is never changing!

I agree with Kenneth that a simpler loop that does the exact same thing 
could be written like this:

while(1) {
    switch(PINB) {
       case 0b000: // do something...
       case 0b001: // do something else...
       .
       .
    }
}

I like to define a set of enumerated values that correspond to the 
possible input values, using the 'enum' keyword, and then define it as a 
brand new data type, like this:

typedef enum {
     VALUE_0,
     VALUE_1,
     VALUE_2,
     .
     .
} VALUE;

You can also define the values that are used, if you want.  The compiler 
will just start counting from zero for you if you don't.  This way the 
compiler will complain if you leave out one of the possible values from 
a switch statement, unless you include a 'default:' case, as Kenneth 
also mentioned.

One more thing to consider is that the program you suggested, as 
written, is going to run *very very* fast.  You might want to add a 
delay in there so that it only executes maybe 20 times a second.  That's 
all a normal line follower needs.  Unless you're David Cook and want to 
enter a line follower in NASCAR.  :)

Good luck and let us know how it works for you.

Dale Wheat

Balter Wiggin wrote:
> As my next intended project, the bicycle computer was scrubbed because 
> of funding issues, i thought i would refine my line follower design 
> while still retaining my objective of using a microcontroller to 
> increase precision. I've snapped up a little bit of code that i think 
> should work, but im not quite sure. Trisb would be an input of three 
> line sensors: left, middle, and right respectively. Porta would be 
> connected to two motors, the left half to the left, and the right half 
> to the right. This approach at motor control would allow two settings on 
> the pager motors that i salvaged. Note that I'm still learning to program.
> 
> #include <system.h>
> 
> void main()
> {
> trisa = 0000b;
> trisb = 111b;
> porta = 0000b;
> do{
> switch(portb)
> {
> case 101b: porta = 1111b;break;
> case 110b: porta = 1110b;break;
> case 011b: porta = 0111b;break;
> case 001b: porta = 0110b;break;
> case 100b: porta = 0110b;break;
> case 000b: porta = 0000b;break;
> case 111b: porta = 0000b;break;
> }
> while (0 < 1);
> }
> }
> 
> 
> Thanks,
> Walter
> 
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today - it's 
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
> 
> _______________________________________________
> DPRGlist mailing list
> DPRGlist at dprg.org
> http://list.dprg.org/mailman/listinfo/dprglist
> 
> 
> 

More information about the DPRG mailing list