please explain how to change this code

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tge1986
 
Posts: 28
Joined: Mon Jul 21, 2008 10:09 pm

please explain how to change this code

Post by tge1986 »

I am trying to get a avr-usb code that works on a atiny2313 to work on the attiny24 and in the source code it says change this code to fit your avr here is the code.

Code: Select all

PORTB = (PORTB & ~0xfc) | (b & 0xfc);

Code: Select all

PORTD = (PORTD & ~0x30) | ((b << 4) & 0x30);
I'm new to AVR stuff and am not sure what that means, i'm guessing it's some type of port masking or something but I'm not entirely sure. if anyone could explain it to me and explain how to change it so it fits the attiny24 i would greatly appreciate it. Thanks

Hazard
 
Posts: 196
Joined: Tue Oct 30, 2007 7:27 am

Re: please explain how to change this code

Post by Hazard »

Have you figured it out?
First you need to understand the logical operators. You can have a look about bitwise operations here: http://en.wikipedia.org/wiki/Bitwise_operations, http://en.wikipedia.org/wiki/Boolean_algebra_(logic)

Now, in that code you have 3 logic operations:

Code: Select all

 &  - AND  

Code: Select all

 |  - OR 

Code: Select all

 ~  - NOT 
You also have:

Code: Select all

<<   - shift left 
>>   -  shift right 
.

The second line of code:

Code: Select all

PORTD = (PORTD & ~0x30) | ((b << 4) & 0x30);

Code: Select all

(PORTD & ~0x30)
this takes the binary 1100 1111 (~0x30) and makes a bitwise AND operation with the output of PORTD.
For example, PORTD with arbitrary value:

Code: Select all

vvvv vvvv 
, the operation:

Code: Select all

  vvvv vvvv & 1100 1111 = vv00 0000 

Code: Select all

((b << 4) & 0x30)
This will take the contents of b variable (example: bbbb nnnn) shiftes left 4 bits, and the result: nnnn 0000.
The operation: nnnn 0000 & 0011 0000 = 00nn 0000.

Last operation:

Code: Select all

PORTD = vv00 0000 | 00nn 0000
PORTD takes the value of this operation. And the contents of bits 7 and 6 of PORTD are untouched after you write the contents of bits 5 and 4 of b variable.

Can you figure out the first one?

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”