Society of Robots - Robot Forum
Software => Software => Topic started by: Resilient on February 15, 2009, 05:49:19 PM
-
I am reading through the code and am a bit confused about what my Ping))) code is actually doing.
Take for example, the following:
#define PINGPIN 7 // assign a pin to the Ping Sensor
#define DDR DDRF
PORT_ON(DDR, PINGPIN); // Switch PingPin to OUPUT
I follow this over to SoR_Utils.h and see that
#define PORT_ON( port_letter, number ) port_letter |= (1<<number)
is there. I understand that this runs the PORT_ON macro using DDRF and 7 as the arguments. So it does a bitwise operation on them. But what happens then? I don't see where something is done with that new bitwise piece. It seems like some function must be called with it, but I don't see where that's happening.
Thanks
J
-
The answer lies in the use of the "|=" operator as opposed to just the "|" operator.
So the last part:-
port_letter |= (1<<number)
is the same thing as
port_letter = port_letter | (1<<number)
So it reads the port, uses a bitwise OR to set the relevant bit, and writes the answer back to the port
-
But it seems like there needs to be some sort of function using port_letter that would actually write it to the port?
Or is the variable DDRF in this case, actually the port, so when you change DDRF you change the port?
-
Yes - the microcontrollers have variables at the low end of the flash memory that are mapped onto the physical IO ports. So reading/writing these 'variables' are actually reading/writing the port.
In this case DDRF is the 'data direction register' for 'Port F'. So the example code will change F7 to be an output pin. To change the output pin value you will need to write to the port itself. ie
PORT_ON(PORTF, 7); // will set the output pin higih
PORT_OFF(PORTF,7); // set the output pin low