go_away

Author Topic: how to store hardware components as variables  (Read 706 times)

0 Members and 1 Guest are viewing this topic.

Offline ruben301088Topic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
how to store hardware components as variables
« on: November 24, 2011, 05:35:46 AM »
Hi, I've looked through the 50$ tutorial, namely the sourcecode for the robot. I'm currently at the University studying Computer Science, and have taken a slight interesst in robotics.
So, my questions is this: I'd like to know how your sourcecode knows that the servo_left variable is the left servo on the hardware. Or backwards, how does the chip know, what variable stores what command for what piece of hardware?
Is there a binary coded accesspoint to all points on the chip, or how does it work?

I would appreciate all you can tell me  :)
My goal is to write and construct myself, without using the programs listed in the tutorial. That is, compiling my own C code and transferring the .hex to the chip through the computers console  :D

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: how to store hardware components as variables
« Reply #1 on: November 24, 2011, 07:09:54 AM »
So, my questions is this: I'd like to know how your sourcecode knows that the servo_left variable is the left servo on the hardware. Or backwards, how does the chip know, what variable stores what command for what piece of hardware?
It does not, but You do, hence you manually assign variable name to the pin or certain register of the pin (or other required part of the controller, eg. timer) to be precise. When value of the variable changes in runtime, that certain register simply uses value stored in it and does all the calculations with new value. Is that what You were asking for?
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: how to store hardware components as variables
« Reply #2 on: November 24, 2011, 07:14:47 AM »
I've just realised what You were asking for. At the top of Your code You create macros that basically change the name say from PORTB1 to LEFT_SERVO. Since You declare that You want to give pin the other name, You can access that pin with its new name. This is used to make code more readable, as LEFT_SERVO is easier to understand than PORTB1.

Code: [Select]
#define LEFT_SERVO  PORTB1
« Last Edit: November 24, 2011, 07:44:41 AM by newInRobotics »
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline ruben301088Topic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: how to store hardware components as variables
« Reply #3 on: November 26, 2011, 11:32:55 AM »
That makes sense.

At the top of Your code You create macros that basically change the name say from PORTB1 to LEFT_SERVO. Since You declare that You want to give pin the other name, You can access that pin with its new name. This is used to make code more readable, as LEFT_SERVO is easier to understand than PORTB1.

Code: [Select]
#define LEFT_SERVO  PORTB1

However, how do i know that PORTB1 refers to a pin? or how does C know? is it stored in some library? or on the chip?
And thanks for your answer... It cleared up part of my confusion :P

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: how to store hardware components as variables
« Reply #4 on: November 26, 2011, 11:42:34 AM »
The macros are spelled out in header files. There's a header file for each chip type. Generally there's a port definition and a set of pin definitions for each port. You look at the data sheet and see that pin x does what you want, you connect device y to it, and use the macros to set the pin on or off, read a voltage from it, etc.

A port macro resolves to the address of the port in memory. The pin macros break out to the pin numbers within a port. So to set pin PB3 in PORTB you'd do

Code: [Select]
PORTB |= (1 << PB3);
If you download the avr-gcc toolchain you can examine the header files. If you compare those with the information in the data sheet for whatever chip you're using, you'll see the correspondence.

I'm thinking in terms of Atmel chips, I presume that PIC and other architectures work much the same way.

Joe
« Last Edit: November 26, 2011, 11:44:31 AM by joe61 »

Offline ruben301088Topic starter

  • Beginner
  • *
  • Posts: 6
  • Helpful? 0
Re: how to store hardware components as variables
« Reply #5 on: November 26, 2011, 12:26:04 PM »
That was very helpful. Thank you.

I also checked out the open source Arduino project. They pretty much cut it out for me :)
Also, people who are new to robotics might wanna check out Arduino. They offer simple boards already soldered together, with easy to use free open source software. Worth having a look at ;) I'm considering getting one of their boards

 


Get Your Ad Here

data_list