0

Really basic help with ATtiny2313
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

by magician13134 on Sun Apr 13, 2008 8:51 pm

Thanks! I'll let you guys in on what it is if I don't butcher it in the process! :wink:

I've been known to do that to many-a-project
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by magician13134 on Sun Jun 01, 2008 8:23 am

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...
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by mtbf0 on Sun Jun 01, 2008 9:07 am

Code: Select all | TOGGLE FULL SIZE
PORTB = 0xD0;
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by magician13134 on Sun Jun 01, 2008 9:22 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
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by mtbf0 on Sun Jun 01, 2008 10:21 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.
Code: Select all | TOGGLE FULL SIZE
PORTZ ^= (1 << PZ5);
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
Code: Select all | TOGGLE FULL SIZE
PORTZ |= (1 << PZ5);
which will set pin 5 without affecting the rest of the port and
Code: Select all | TOGGLE FULL SIZE
PORTZ &= ~(1 << PZ5);
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?
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by magician13134 on Sun Jun 01, 2008 11:24 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...)
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

by mtbf0 on Sun Jun 01, 2008 12:02 pm

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.
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

by magician13134 on Sun Jun 01, 2008 5:12 pm

Wow! I guess that answers my question pretty well, then!
magician13134
 
Posts: 1119
Joined: Wed Jun 13, 2007 9:17 am
Location: Ann Arbor

Please be positive and constructive with your questions and comments.