interrupt and output port!!

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

I am using Atmega16 microcontroller...I am using interrupt0 (portD.2) in my program..I wondering that can I use portD(whole) as output and interrupt0 at same time in my program...
If I define portb as output...my interrupt will work???(I don't think so)

Is it possible to redefine port configuration during progam....
it sounds weird but I want to know...

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

As long as you leave the DDRD bit 2 at 0 (input) then you can use the other 7 pins as output while still using that pin as an interrupt input.

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

Pin allocation is always an important part of an AVR design, as there are alternate function(s) on each general-purpose I/O pins.

It is often a quandary when you want a "whole port" for something--e.g., 8-bit graphics LCD data signals.

[Usually the UART port isn't a candidate. ;) But you must not be using that.]

So, why must you use INT0? INT2 is on PB2.

You can also generate the full equivalent of an INTn pin on ICP1 and with the analog comparator. Near-equivalents (single edge) can be generated on the T0 & T1 pins.

Another part of AVR design is picking the "best" model. In this case, why not use a Mega164 that has pin-change interrupts, more toyz all around, and is less expensive?

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

clawson wrote:
As long as you leave the DDRD bit 2 at 0 (input) then you can use the other 7 pins as output while still using that pin as an interrupt input.

DDRD = 0b11111011;
or
DDRD = 0xFB;

the above will make:

pin 0-1 = output
pin 2 = input
pin 3-7 = output

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

ATmega16 data sheet version 2466R–AVR–06/08 on page 68

Quote:
The External Interrupts are triggered by the INT0, INT1, and INT2 pins. Observe that, if enabled, the interrupts will trigger even if the INT0..2 pins are configured as outputs. This feature provides a way of generating a software interrupt.
This works fine if you want the AVR PD2 output to drive INT0, however if you have external INT0 hardware the problem to solve is how do you use PD2 as an AVR output with an external hardware output for INT 0 also connected to that same AVR output pin? This contention between two outputs connected together would cause a deadly short circuit whenever the two outputs don't agree.

You could do a wired OR by setting PD2 as a low output and only changing its output value using the data direction register (DDD2) to change PD2 to a high impedance input for a high value output. Then you need an external pull-up resistor for pin PD2. The external INT0 hardware would also need a wired OR (either driven to ground 0 volts or high impedance) output connected to PD2. In this configuration the PD2 high value would come from the external pull-up resistor when the AVR and external INT 0 hardware are both high impedance. The PD2 low value would be whenever either the AVR or external INT 0 outputs are driven to ground. You will never be able to use PORTD = value if it makes PD2 go high and shorts out with the external INT0 hardware output. If you do this it is easier to make the entire PORTD wired OR output pins with external pull-up resistors. Also when AVR output pin PD2 is low, the external INT 0 hardware output cannot be seen or responded to by the AVR since the AVR will already be holding PD2 low.

You could also use PORTD as a normal port with PORTD = value assignments. Then make your external INT 0 hardware tri-state with the AVR controlling the tri-state itself. Then you would switch PD2 to an input, set the external tri-state control to connect. Going the other way set the external tri-state control to disconnect and set PD2 back to an output. However, the external interrupt will only work when the tri-state control connects. This tri-state switching could also generate false INT0 interrupts from the switching itself.

I doubt this will solve your problem, but I thought I would try anyway.

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

then is it possible to define port as an input and use interrupt of that port...

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

Quote:

to define port as an input

Just to be pednatic but you mean "define a single bit of the port as an input". The lines you want to use as outputs must have 1 bits in the relevant position in the DDR register.

The one thing you cannot do is use the same pin as both an output and an (interrupt) input. (well not without a massive amount of jiggery-pokery)

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

Quote:

The one thing you cannot do is use the same pin as both an output and an (interrupt) input. (well not without a massive amount of jiggery-pokery)

True for Mega16 which the OP is using but not true for newer devices with Pin Change Interrupts.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

ATmega48 data sheet wrote:
Observe that, if enabled, the interrupts will trigger even if the INT0 and INT1 or PCINT23..0 pins are configured as outputs. This feature provides a way of generating a software interrupt.
You can also decipher all the I/O port alternate functions in Figure 13-5 Alternate Port Functions. If you look at pin PD2/INT0/PCINT18 you will see the interrupt input override functions the same way for both INT0 and PCINT18. This override is what allows the AVR interrupt to see its own output PORT pin value through the actual external pin.

Typically all of these are only used exactly as described in the data sheet (generating a software interrupt), only to read what the AVR wrote to its own output pin.