Skip to main content
Visitor II
April 22, 2003
Question

set/reset one bit of a port

  • April 22, 2003
  • 5 replies
  • 1125 views
Posted on April 22, 2003 at 12:45

set/reset one bit of a port

    This topic has been closed for replies.

    5 replies

    csamelingAuthor
    Visitor II
    April 10, 2003
    Posted on April 10, 2003 at 10:43

    Hello.

    I want to set one bit of a port. However the other bits of this port should not affected. I use Cosmic C Compiler.

    for example:

    _asm(''bset 0x00,#5'');

    or

    (PADR) &= ~0x20;

    What is the right line to set the bit in portA?

    Or can I use both?

    The other bits of portA are not affected?

    regards

    Visitor II
    April 10, 2003
    Posted on April 10, 2003 at 12:30

    PADR |= 0x20; // set 5 bit (other bits are not affected)

    PADR &= ~0x20; // reset 5 bit (other bits are not affected)

    PADR ^= 0x20; // invert 5 bit (other bits are not affected)

    _asm(''bset 0x00,#5''); - is equivalent of first line.

    csamelingAuthor
    Visitor II
    April 10, 2003
    Posted on April 10, 2003 at 13:01

    Thanks!

    And if I want to read an input of a port, can I write the code in this way:

    if(PADR & 0x04)

    {

    mark = TRUE;

    }

    else

    {

    mark = FALSE;

    }

    I think it's right, isn't it?
    Visitor II
    April 22, 2003
    Posted on April 22, 2003 at 11:58

    for example:

    PAOR = 0x00; PADDR = 0x00; // floating input

    < .. your code.. >

    PAOR = 0xFF; PADDR = 0xFF; // push-pull output (optional)
    Visitor II
    April 22, 2003
    Posted on April 22, 2003 at 12:45

    I notice that in your example you set the port option register first, doing this could trigger an unwanted interrupt.

    When setting follow the order PxDDR then PxOR, when resetting a port use PxOR then PxDDR.

    If you have a mixed port with both inputs/outputs then to be safe you need to use a port shadow register.

    Regards

    SJO