Society of Robots - Robot Forum
Software => Software => Topic started by: greasemonkey94 on December 21, 2010, 09:40:23 AM
-
Hello,
Sorry for the weird title but i could'nt think of anything else,I really know very little about electronics,
could someone please explain what all of this is??
/AVRlib includes
#include "global.h" // include global settings
//#include "buffer.h" // include buffer function library
//#include "uart.h" // include uart function library
//#include "rprintf.h" // include printf function library
//#include "timer.h" // include timer function library (timing, PWM, etc)
#include "a2d.h" // include A/D converter function library
//define port functions; example: PORT_ON( PORTD, 6);
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)
#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)
#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)
#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)
#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )
#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )
//************CONFIGURE PORTS************
//configure ports for input or output - specific to ATmega8
void configure_ports(void)
{
DDRC = 0x00; //configure all C ports for input
PORTC = 0x00; //make sure pull-up resistors are turned off
DDRD = 0xFF; //configure all D ports for output
DDRB = 0xC7; //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex')
what is port_letter |= (1<<number), and the rest, how does it work, im sorry it doesnt make any sense to me at all,
and also how do you declare pins as input or output??In arduino its really easy using Pinmode, is it something like 1 for input and 0 for ouput?? what is DDRC,PORTC,DDRD,DDRB, and why do we have to type ob11000111 ??
Im sorry for all the questions ??? i know it must be frustratng to have a complete noob :(.
Thanks
-
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
Is a macro. Look them up in your favorite C programming guide.
-
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
Is a macro. Look them up in your favorite C programming guide.
Hehe, Thanks for the reply , but i know what a macro is, just why that macro is the question I=(1<<number) doesn't make sense to me :(
-
The line at the beginning is an example of the Macro usage:
//define port functions; example: PORT_ON( PORTD, 6);
They do the following as their names describe:
Turn a port bit on or off
Turn an entire port on or off
compliment all the bits on a port
Return the state of a port bit.
-
They are called 'bit operations'.
http://docs.hp.com/en/B3901-90007/ch05s04.html (http://docs.hp.com/en/B3901-90007/ch05s04.html)
http://en.wikipedia.org/wiki/Bitwise_operation (http://en.wikipedia.org/wiki/Bitwise_operation)
That code is basically controlling the 8 bit port registers, where each bit represents either an external signal pin (such as where you connect your servos), or some internal state switch (such as setting up the PWM frequency).
-
I=(1<<number)
I = 1 shifted left by "number" bits. So
I=(1<<1) == 00000001 << 1 == 00000010
I=(1<<3) == 00000001 << 3 == 00001000
etc
______
Rob
-
Hehe thanks for the reply,
Now i understand,
however last question, what is port c, port d etc
-
what is port c, port d etc
Just IO ports on the uC, usually 8 bits (but can be less on really small chips), so for example if you do
out PORTC, 0xAA
eight of the chip's pins will be written to with the pattern 0xAA (10101010)
-
The pinout of the Axon:
http://societyofrobots.com/axon/images/640pinout.gif (http://societyofrobots.com/axon/images/640pinout.gif)
You'll see letters like PE, PH, PB, PA, etc
That means port E, port H, port B, port A, etc.
After that you'll see a number, for example PE3 is port E #3.
-
Thanks everyone , got it :D