Society of Robots - Robot Forum

Software => Software => Topic started by: SmAsH on March 28, 2009, 04:52:51 PM

Title: how to define ports on an atmega8
Post by: SmAsH on March 28, 2009, 04:52:51 PM
hi all my question is in the programming to give a specific pin a name what code would i use? something like this?

//defines
#define   left_sensor    PD0 
#define   right_sensor   PD2
#define   right_servo    PD3
#define   left_servo      PD4

or what? sorry if this question has been asked before but i did search and i am a complete novice to programming in all languages :(
thanks in advance,
James.
Title: Re: how to define ports on an atmega8
Post by: GearMotion on March 28, 2009, 05:28:09 PM
hi all my question is in the programming to give a specific pin a name what code would i use? something like this?

//defines
#define   left_sensor    PD0 
#define   right_sensor   PD2
#define   right_servo    PD3
#define   left_servo      PD4

or what? sorry if this question has been asked before but i did search and i am a complete novice to programming in all languages :(
thanks in advance,
James.

It depends a lot on how you manipulate that port. I use defines, and specific code to set/clear the pins:

#define LCD_RESET_H (PORTB |= 0x04)
#define LCD_RESET_L (PORTB &= ~0x04)

then:

     LCD_RESET_L;

to set the line low.
   

To test the state of a line (on port c):

        if (( PINC & 0x01) == 0x01)
            {
            // Do something
            }

But I'm comfortable with shifting bits, masking, etc in my head. Other programmers prefer writing code in a way that hides these details.


ETA:

For that matter you can do this:

#define   left_sensor    (PIND & 0x01)

and in code:

    if ( left_sensor )
        {
        // do when on
        }
    else
        {
        // do when off
        }

Title: Re: how to define ports on an atmega8
Post by: SmAsH on March 28, 2009, 05:38:06 PM
im really sorry but i didnt understand any of that :( i think i might just look up a tutorial on c programming.
thanks for trying to help me anyway :-\
Title: Re: how to define ports on an atmega8
Post by: GearMotion on March 28, 2009, 05:48:13 PM
im really sorry but i didnt understand any of that :( i think i might just look up a tutorial on c programming.
thanks for trying to help me anyway :-\

Heh - OK. Feel free to bug me about any step that you need to know more about.

Yes, a C tutorial would help you a lot.

Your code:

#define   left_sensor    PD0 

tells the C compiler that anywhere you have "left_sensor" it should replace it with "PDO".

My code:

#define   left_sensor    (PIND & 0x01)

replaces "left_sensor" with reading the input register in the mega8 called PIND and masking out all but the lowest but with a "logical and". So in that way you are only looking at the lowest bit in port D.




Title: Re: how to define ports on an atmega8
Post by: SmAsH on March 28, 2009, 05:50:41 PM
okaaaay.... thanks for your help, i found this tutorial that seems to be really set up for beginners: http://www.cprogramming.com/tutorial/c/lesson1.html (http://www.cprogramming.com/tutorial/c/lesson1.html)
Title: Re: how to define ports on an atmega8
Post by: GearMotion on March 28, 2009, 07:24:46 PM
okaaaay.... thanks for your help, i found this tutorial that seems to be really set up for beginners: http://www.cprogramming.com/tutorial/c/lesson1.html (http://www.cprogramming.com/tutorial/c/lesson1.html)

Well, sorry. I apologize for going too fast. Please feel free to lean on me a bit for help.

Have you looked at the Arduino stuff? The Arduino uses the mega168 which is close to the mega 8. "Arduino" is more than just the hardware - it is also the free development environment. A LOT of people use the Arduino as a starting spot for learning hardware and software.

If you look at the Arduino tutorials, that might help a little bit. The language is basically C and a sample bit of that code studied might help you a bit. If you download the Arduino IDE, you will see that it supports the mega 8.



Title: Re: how to define ports on an atmega8
Post by: SmAsH on March 28, 2009, 09:06:12 PM
yup, ive got me a roboduino sitting in my drawer which is pretty much the same except for power buses?
Title: Re: how to define ports on an atmega8
Post by: GearMotion on March 29, 2009, 09:38:22 AM
yup, ive got me a roboduino sitting in my drawer which is pretty much the same except for power buses?

Yes, pretty much. Since there is a lot of Arduino information out there, you can use the tutorials/books to learn the Arduino (close to C) language and easily apply it to your board.
Title: Re: how to define ports on an atmega8
Post by: SmAsH on March 29, 2009, 03:04:35 PM
wait, so does arduino use a different language?
Title: Re: how to define ports on an atmega8
Post by: Razor Concepts on March 29, 2009, 03:12:53 PM
Arduino is basically C but at a higher level (more basic). The heart of Arduino is C, its just that the thing you actually code into the Arduino software is simpler, and the Arduino software translates that into normal C.
Title: Re: how to define ports on an atmega8
Post by: SmAsH on March 29, 2009, 03:15:00 PM
ahh, so if i learn how to code arduino's will it help me learn c significantly?
Title: Re: how to define ports on an atmega8
Post by: GearMotion on March 29, 2009, 08:11:34 PM
ahh, so if i learn how to code arduino's will it help me learn c significantly?

Yes. Arduino is just C with a lot of helpful library routines.
Title: Re: how to define ports on an atmega8
Post by: SmAsH on March 29, 2009, 11:14:47 PM
ahh good, ill be sure to learn that these holidays!