I'm using an Attiny84 with the Tiny Core. I'm try to use port manipulation to write the lower four bits of port A. I'm using i2c which occupies bits 5 and 7.
How can I write only to the lower 4 bits to avoid interfering with the i2c?
I'm using an Attiny84 with the Tiny Core. I'm try to use port manipulation to write the lower four bits of port A. I'm using i2c which occupies bits 5 and 7.
How can I write only to the lower 4 bits to avoid interfering with the i2c?
I don't think you need to worry. A simple piece of code:
void setup()
{
PORTA |= bit (0);
}
void loop() { }
Generates:
00000044 <setup>:
44: d8 9a sbi 0x1b, 0 ; 27
46: 08 95 ret
In other words, the compiler generates code to set that bit, ignoring all the other bits.
PORTA |= 0b00001111;
– Nick Gammon♦
Mar 10 '18 at 20:52
bit
macro sets a bit by shifting the number 1
left the number of bits in the argument, so bit (0)
is 1 << 0
which is the same as 1
.
– Nick Gammon♦
Mar 10 '18 at 20:53
PORTA &= ~0b00001111;
The ~
operator takes the ones-complement, and you AND that into the port. Alternatively: PORTA &= 0b11110000;
– Nick Gammon♦
Mar 10 '18 at 22:00