Society of Robots - Robot Forum

Software => Software => Topic started by: SomeSaba on February 22, 2007, 12:15:22 PM

Title: Pins
Post by: SomeSaba on February 22, 2007, 12:15:22 PM
Hello, i've always been confused when it comes to setting a pin to high or low. I havent found a website that explains this to me well.

if i say PORTA = 0x1 what does that mean?

and what is the difference between PORTA or DDRA?
Title: Re: Pins
Post by: JonHylands on February 22, 2007, 12:26:42 PM
PORTA is the entire 8 pins on port A. Its a byte value, where each of the 8 bits in the byte represent the state of one I/O pin. Setting it to 0x1 is basically setting PIN_A0 to high, and all the other A pins to low.

Note that some PICs, like the 16F876, might only have 5 or 6 pins on port A.

DDRA is the direction byte. Each bit represents whether the corresponding pin is configured as an input or an output. I believe (at least for PICs) 1 is for input, 0 is for output...

- Jon
Title: Re: Pins
Post by: SomeSaba on February 22, 2007, 12:54:26 PM
Oh okay i see, when u said "0x1 is basically setting PIN_A0 to high, and all the other A pins to low."

How do i say set A2 to low? Is it safe to say the 0 in 0x1 is the pin # and 1 is high? So A2 to high is 2x0?
Title: Re: Pins
Post by: JonHylands on February 22, 2007, 02:09:55 PM
No.

0x is the way most C compilers specify that the number is in hexadecimal.

So, 0x1 is really just 1, which looks like this in binary: 00000001

So, if you wanted to set PIN_A2 to low, and you didn't care what happened to the other pins, you could just say:

PORTA = 0;

Realistically, you do, and depending on which C compiler you're using, they may provide different ways of doing it. I use CCS C, which means I can say this:

output_low (PIN_A2);

If you're bit banging, you could do this:

PORTA &= 0b11111011;

That would clear bit 2, and leave all the other bits unchanged.

I seem to recall that may have undersireable side effects if some of the other pins are inputs, but I can't remember. Best to check with the documentation of your compiler.

- Jon
Title: Re: Pins
Post by: Admin on February 22, 2007, 03:48:19 PM
this post might help
http://www.societyofrobots.com/robotforum/index.php?topic=564.0

and this link too
http://thedotcommune.com/home/bitmasking.html
Title: Re: Pins
Post by: SomeSaba on February 22, 2007, 05:32:59 PM
Thank you very much!  ;)
Title: Re: Pins
Post by: sambhav on February 24, 2007, 12:25:09 AM
yes dats right.. 0x represents a hexadecimal no. this means dat the no. dat follows is a hex no.
eg. 0x20 is equivalent to 20H