mixed input / output newbie advice

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

I want to run 2 pins input and 4 pins output on port-D of my mega8 AVR chip. Normally, when the port is all output I just assign - like;

portd=0xFF;
portd=0x001;

Now if the first two pins are input, and I assign a value to port-d, what effects will it have? Will it just ignore my code and report what the hardware inputs read? Will it tangle with interrupts if I am using them?

Please advise.

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

hi!
u r going to use two pins are input pins and 4 pins r going to use as an output pins. ok
u must know about DDR register. This one explains Data Direction Register.
if in this register u will set perticular bit as 1, it will act as output pin or u will set as 0 i will act as input pin.ok. r u accept!

u r going to use first two pins r input pins such that pin 0&1
eg:
DDRD=0XFC;//0X1111 1100
i hope now u cleared about this input and output port declaration.

M.Thulasibabu India

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

Setting PORTx bits corresponding to input pins turns on/off the internal
pullups, so you probably want to leave them alone. More generally, you
should avoid altering bits you're not interested in; this notion will become
more relevant as your program gets larger and you start apportioning bits
to different sections of your program.

To set an output pin in PORTD, say PD2:

PORTD |= (1 << PD2);    // Set pin PD2 high
PORTD &= ~(1 << PD2);   // Set pin PD2 low

which don't affect the other bits.

Quote:
Will it tangle with interrupts if I am using them?

Yes, it's possible (depending on what's connected to the pin) to trigger an
interrupt by wiggling the pullup on the (input) INTx pin.

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

Ok, I understand setting put the port as input pins and output using ddrd. And I know how to change individual pins, one at a time.

What I need to do is modify FOUR pins at ONE time, and leave the two inputs alone. Assign a char value, because all four must be modified; and not all set on or off; like "0011" or change to "0110" at times, and also randomly "1001"

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

Assuming PD0/1 input, PD2/3/4/5 output, PD6/7 unknown.

#define OUTPUT_LOWBIT   PD2
#define OUTPUT_ALLBIT   ((1 << PD5)|(1 << PD4)|(1 << PD3)|(1 << PD2))

unsigned char outputvalue = 3;            // Set PD5:2 to 0b0011, e.g.
PORTD = (PORTD & ~OUTPUT_ALLBIT)          // Mask the output bits
      | (outputvalue << OUTPUT_LOWBIT);   // set the output PD5:2
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok. u can set like below!
code:
PORTD=(0<<PD3)|(0<<PD2)|(1<<PD1)|(1<<PD0);//0011
PORTD=(0<<PD3)|(1<<PD2)|(1<<PD1)|(0<<PD0);//0110
PORTD=(1<<PD3)|(0<<PD2)|(0<<PD1)|(1<<PD0);//1001
ok. remaining pins are not affect.
u must know about bitfield (&,~,|,^).
i hope after u understand about this bitfields u can handle any type of program.
ok.

M.Thulasibabu India

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

mthulasibabu wrote:
u must know about bitfield (&,~,|,^).

Strictly, those are bit-wise operators - bit fields are something different.

Look up the terms "bitwise" and "bitfield" in a 'C' textbook...

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...