easy question, newbie help - trying to mix i/o on portD

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm kinda new to C programming, can someone advise here?

I am building a motor-controller project, and using atmeg8.

Using port-d, the first two pins are input and the next 4 are output; so I setup like this:

DDRD=0b00111111; 

Now normally on a port, I can just assign data to it; like so:

PORTD=0XFF; 
PORTD=0X08;

But this time, I am mixing input pins and output pins on the same port. I dont want any conflicts with the input - and dont know how avr chips behave. I dont know if I assign data, it will conflict with inputs - can someone please tell me the best way to assign the data only to pins 3,4,5,6 ? Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need to learn the C bitwise operators, namely:

Setting bits:

PORTD = PORTD | 0b11110000 // Sets bits 4567 of PORTD, but leaves the rest unchanged
PORTD |= 0b11110000 // Same as above, but uses the C shorthand "|=" operator

Clearing bits:

PORTD = PORTD & ~0b11110000 // Clears bits 4567 of PORTD, but leaves the rest unchanged
PORTD &= ~0b11110000 // Same as above, but uses the C shorthand "&=" operator

Flipping bits:

PORTD = PORTD ^ 0b11110000 // Flips bits 4567 of PORTD, but leaves the rest unchanged
PORTD ^= 0b11110000 // Same as above, but uses the C shorthand "^=" operator

For a indepth tutorial, see EW's carconical thread, at https://www.avrfreaks.net/index.p....

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Using port-d, the first two pins are input

What exactly do you mean by "first two pins". The example you gave was 0b00111111. That is setting bit 6 and bit 7 as inputs. I would call that the last two pins. This might just be your semantics, but I wouldn't want you to be scratching your head latter, trying to figure out why it wasn't working :)

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Koshchi:

Quote:
The example you gave was 0b00111111. That is setting bit 6 and bit 7 as inputs

Thanks for the clairification; this would have messed me up. I can see now on AVR the pins go from right to left, starting from pin 1 as the right most, and pin 8 is the left-most pin.

So I will make my adjustments. And I can also see that bits and bits/pins start with "0" and go through 7; (I think I was under the impression the pins start with 1 and go to 8)
---------

abcminiuser - Thanks for the starter advice; I have enough to advance my project now - that was very helpful of you both!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DrStein99 wrote:
I can also see that bits and bits/pins start with "0" and go through 7; (I think I was under the impression the pins start with 1 and go to 8 )

When you write a binary number, such as 00111111, the right-most bit is the least-significant bit (LSB); it has a "weight" of 2^0 - hence it is often known as "bit-0"

Similarly, the left-most bit is the most-significant bit (MSB); it has a "weight" of 2^7 - hence it is often known as "bit-7"

(in the above, "2^7" means "2 raised to the power 7")

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...