简体   繁体   中英

Arduino calculating with pins

I just searched for a library for an LCD. When I found one I tried to understand how it works. Then I saw

PORTD &= ~(0xF0>>(4-PD0));

I never saw this (4-PD0) and don't know what that will return. I would like to know how that works, what it returns, and what it's useful for.

So thanks for everyone who helps :D.

PD0 probably contains the bit number of the port, rather than directly a mask. eg "bit0", "bit1", "bit2"… etc. which can goes up to "3".

The mask described aside cover everything but these four first bits, so it needs to be shifted from the complement of this number to reach the given bit, thus « 4 - PD0 », which performs four shifts for bit0, three shifts for bit 1, and so on. Since the shift operation preserves the sign, all bits on the left remains sets.

After the final "~" complement operation, you obtain a mask that sets all bits to 1, up to PD0 position, excluded. This mask is supposed to locate PORTD.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM