Software > Software


(1/2) > >>

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?

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

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?


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

this post might help

and this link too


[0] Message Index

[#] Next page

Go to full version