cancel
Showing results for 
Search instead for 
Did you mean: 

set/reset one bit of a port

csameling
Associate II
Posted on April 22, 2003 at 12:45

set/reset one bit of a port

5 REPLIES 5
csameling
Associate II
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

coor
Associate II
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.

csameling
Associate II
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?
coor
Associate II
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)
sjo
Associate II
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