Author Topic: Pins  (Read 3590 times)

0 Members and 1 Guest are viewing this topic.

Offline SomeSabaTopic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 0
Pins
« 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?

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: Pins
« Reply #1 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

Offline SomeSabaTopic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 0
Re: Pins
« Reply #2 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?

Offline JonHylands

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 562
  • Helpful? 3
  • Robot Builder/ Software Developer
Re: Pins
« Reply #3 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

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: Pins
« Reply #4 on: February 22, 2007, 03:48:19 PM »

Offline SomeSabaTopic starter

  • Full Member
  • ***
  • Posts: 75
  • Helpful? 0
Re: Pins
« Reply #5 on: February 22, 2007, 05:32:59 PM »
Thank you very much!  ;)

Offline sambhav

  • Jr. Member
  • **
  • Posts: 20
  • Helpful? 0
Re: Pins
« Reply #6 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

 


Get Your Ad Here

data_list