go away spammer

Author Topic: how to define ports on an atmega8  (Read 4665 times)

0 Members and 1 Guest are viewing this topic.

Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
how to define ports on an atmega8
« 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.
« Last Edit: March 28, 2009, 05:18:27 PM by SmAsH »
Howdy

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
    • CircuitGizmos
Re: how to define ports on an atmega8
« Reply #1 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
        }

« Last Edit: March 28, 2009, 05:32:42 PM by GearMotion »

Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: how to define ports on an atmega8
« Reply #2 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 :-\
Howdy

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
    • CircuitGizmos
Re: how to define ports on an atmega8
« Reply #3 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.





Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: how to define ports on an atmega8
« Reply #4 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
Howdy

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
    • CircuitGizmos
Re: how to define ports on an atmega8
« Reply #5 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

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.




Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: how to define ports on an atmega8
« Reply #6 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?
Howdy

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
    • CircuitGizmos
Re: how to define ports on an atmega8
« Reply #7 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.

Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: how to define ports on an atmega8
« Reply #8 on: March 29, 2009, 03:04:35 PM »
wait, so does arduino use a different language?
Howdy

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: how to define ports on an atmega8
« Reply #9 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.

Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: how to define ports on an atmega8
« Reply #10 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?
Howdy

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
    • CircuitGizmos
Re: how to define ports on an atmega8
« Reply #11 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.

Offline SmAsHTopic starter

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: how to define ports on an atmega8
« Reply #12 on: March 29, 2009, 11:14:47 PM »
ahh good, ill be sure to learn that these holidays!
Howdy

 


Get Your Ad Here