Hmmm, I tried to light the following three LEDs together:
PORTB = 0x10
PORTB = 0x40
PORTB = 0x80
So in hex, that would be PORTB = 0x82, right? Because I tried that and it didn't light the right three...
Really basic help with ATtiny2313
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 1119
- Joined: Wed Jun 13, 2007 9:17 am
Oh... Right... 10, 40 and 80 are already hex. Gotcha. Thank you!
And one more thing, is there any type of "+=" statement for PORTA, PORTB, etc? IE:
I have two 7-segments, at this point simply counting from 0 to 99. So I have two switch statements (i and j), and each one sets one digit on one of the displays, but it always clears the other display... So can I read what's already being output(ted?) and the add to it? Thanks
And one more thing, is there any type of "+=" statement for PORTA, PORTB, etc? IE:
I have two 7-segments, at this point simply counting from 0 to 99. So I have two switch statements (i and j), and each one sets one digit on one of the displays, but it always clears the other display... So can I read what's already being output(ted?) and the add to it? Thanks
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
depends a lot on the displays. i've never used anything but the old dumb ones that require you to tell it which segments to light. if you've got one with just four inputs, then, yeah, maybe. depends on how you've got them wired.
avr output ports are readable, but most of the time you'd only want to use bit-wise logic operators, since arithmetic operators will have an effect on possibly more bits than you want to modify.
say you're using the low order four bits of, oh, PORTZ to light four leds and at the moment you've got all four lit and so PORTZ is set xxxx1111, where the x's are the bits that aren't connected to your leds. PORTZ++ is going to cause a carry into bit 4 which may or may not matter to you but is definitely going to change something beyond the four bits that you are using for the leds.
so, yes, you can. but, no, maybe you shouldn't.
by way of an example of using bitwise operators, say you want to add a heartbeat to a project. every so often you want to flash an led connected to pin 5 of our hypothetical PORTZ. would do the trick without knowing the current state of the pin and without having an effect on any other pin in PORTZ.
often you will see code such as which will set pin 5 without affecting the rest of the port and which will clear pin 5.
these are, in fact, the currently approved methods of performing these operations by the authors of avr-libc, since they are more C-like than the now deprecated _BV macro.
clear as mud. right?
avr output ports are readable, but most of the time you'd only want to use bit-wise logic operators, since arithmetic operators will have an effect on possibly more bits than you want to modify.
say you're using the low order four bits of, oh, PORTZ to light four leds and at the moment you've got all four lit and so PORTZ is set xxxx1111, where the x's are the bits that aren't connected to your leds. PORTZ++ is going to cause a carry into bit 4 which may or may not matter to you but is definitely going to change something beyond the four bits that you are using for the leds.
so, yes, you can. but, no, maybe you shouldn't.
by way of an example of using bitwise operators, say you want to add a heartbeat to a project. every so often you want to flash an led connected to pin 5 of our hypothetical PORTZ.
Code: Select all
PORTZ ^= (1 << PZ5);
often you will see code such as
Code: Select all
PORTZ |= (1 << PZ5);
Code: Select all
PORTZ &= ~(1 << PZ5);
these are, in fact, the currently approved methods of performing these operations by the authors of avr-libc, since they are more C-like than the now deprecated _BV macro.
clear as mud. right?
-
- Posts: 1119
- Joined: Wed Jun 13, 2007 9:17 am
Oohh! Actually, that helped a lot! My problem was really simple, I was using the kind where you had to tell it each pin to light up, so I simply needed to replace += with |= and it worked great! Thank you.
(Just curious, do you know what the | character is called... It's one of those "can't look up how to spell a word in a dictionary since I can't spell it!" situations... I'd search Google, but I don't know what to type in since I don't know what it's called... I just call it the line-thing... But I'm sure there's a technical name for it...)
(Just curious, do you know what the | character is called... It's one of those "can't look up how to spell a word in a dictionary since I can't spell it!" situations... I'd search Google, but I don't know what to type in since I don't know what it's called... I just call it the line-thing... But I'm sure there's a technical name for it...)
-
- Posts: 1645
- Joined: Sat Nov 10, 2007 12:59 am
vertical bar?
the wikipedia article on bitwise operations refers to it as pipe. this is from its usage on the unix command line to pipe the output of one program to the input of another.
or you could call it "or."
oh, BANNED! it's even got its own wikipedia page, here.
the wikipedia article on bitwise operations refers to it as pipe. this is from its usage on the unix command line to pipe the output of one program to the input of another.
or you could call it "or."
oh, BANNED! it's even got its own wikipedia page, here.
-
- Posts: 1119
- Joined: Wed Jun 13, 2007 9:17 am
Please be positive and constructive with your questions and comments.