1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Strange problem encountered with AVR and interrupts - 1 attachment

Discussion in 'Embedded' started by Michael, Jul 11, 2004.

  1. Michael

    Michael Guest

    Hi - I was attempting to get the two external interrupts working on an
    AVR. I have attached the code I wrote. My problem is that it seems it is
    only detecting when INT0 is pressed - but I'm not sure about this. The
    way I wrote it I never use the RETI statement - and I don't know if
    other interrupts can be called when a interrupt handler is running.

    But also - another problem - so to try to reset the AVR - I just would
    unplug the power - then plug power back in. But for some odd reason this
    didn't work! This has worked just fine for me before - but for some
    reason after doing this - I would have to write a different piece of
    code to the AVR - then rewrite the program I was working on - for it to
    work again. If I simply rewrote the program I was working on to the AVR
    without first writing in a different program, the LEDs would just remain
    off, and it wouldn't work at all. I simply can't figure out what is
    going on here.

    Oh - if it matters - my setup right now is that I have an AT90S8515. I
    am programming it with an Atmel AVR ISP. On Port B I have 8 LEDs
    connected to vcc. On port C I have 8 normally closed buttons connected
    to ground. On port D I have two normally open buttons connected to
    ground and to INT0 and INT1.

    Also - to use the external interrupts - I set them as inputs with pull
    up resistors. Now I'm starting to think that might not be necessary - is
    it?

    Lastly - is it necessary to include both the .device and the .include
    statements? I feel like I've seen code with just one of them.

    Thanks!

    -Michael






    In case the attachment doesn't work properly - I'll include the code
    below as well:



    ..device AT90S8515
    ..include "8515def.inc"

    ..ORG $000

    RJMP RESET
    RJMP INTER0 ;int0
    RJMP INPUTLOOP ;int1

    ..ORG $00D

    RESET:
    LDI R24, low(RAMEND) ;stack
    OUT SPL, R24
    LDI R24, high(RAMEND)
    OUT SPH, R24
    CLR R24 ;enable interrupts
    OUT MCUCR, R24
    LDI R24, 0xC0
    OUT GIMSK, R24
    SEI
    CLR R24 ;set inputs and outputs and pull up resistors
    OUT DDRC, R24
    OUT DDRD, R24
    SER R24
    OUT PORTC, R24
    OUT PORTD, R24
    OUT DDRB, R24

    INPUTLOOP:
    IN R24, PINC ;write pinc to portb
    OUT PORTB, R24
    RJMP INPUTLOOP

    INTER0:
    LDI R24, 0xAA ;write 10101010 to portb
    OUT PORTB, R24
    RJMP INTER0
     
    Michael, Jul 11, 2004
    #1
    1. Advertisements

  2. Michael

    tanya Guest

    Interrupts are automatically disabled on entry to INTER0 Interrupt Service
    Routine.
    Since you are not enabling interrupts in the INTER0 loop, no further
    interrupts will be serviced.
    If you used the RETI instruction, flags (including interrupt status) and PC
    would be restored.
    A suggested approach would be to have a main loop that executes with
    interrupts enabled, with Interrupt Service Routines returning to this loop
    via RETI instructions.

    Tanya
     
    tanya, Jul 12, 2004
    #2
    1. Advertisements

  3. Michael

    Michael Guest

    Instead of using RETI - could I simply set the global interrupt flag by
    executting SEI? Or is there more to it than that? Thanks,

    Michael
     
    Michael, Jul 12, 2004
    #3
  4. Michael

    R Adsett Guest

    You need to restore the entire processor status not just the interrupt
    enable bit(s) to what it was before the interrupt occurred. That's why
    there is a separate unique instruction for returning from an interrupt as
    opposed to returning from a subroutine. If you don't use it then
    generally "Bad Things Happen".

    Robert
     
    R Adsett, Jul 12, 2004
    #4
  5. Michael

    Michael Guest

    LOL that's what I expected - but I thought I should check to be sure :)

    But does anyone have any idea about what was going on with not being able
    to reset it? I tested it out today after the AVR had been unpowered for
    about 12 hours - and it did the same exact thing. I just don't understand.
    Perhaps does it remember all the states of the registers when it turns on
    or something?

    Thanks,

    Michael
     
    Michael, Jul 12, 2004
    #5
  6. Michael

    Jim Stewart Guest

    Here's just a guess. You may have a sneak path
    for power back to your board. Unplug *all* cables,
    wait a minute, and see if it's reset.
     
    Jim Stewart, Jul 12, 2004
    #6
  7. Michael

    Dave Hansen Guest

    Well, the subject _is_ the AVR. From Atmel's instruction set manual:

    RETI - Return from Interrupt
    [...]
    Returns from interrupt. The return address is loaded from the
    STACK and the global interrupt flag is set.
    Note that the status register is not automatically stored when
    entering the interrupt routine. This must be handled by the
    application program. The stack pointer uses a pre-increment
    scheme during the RETI.

    [...]

    Operation:
    PC(15:0) <- STACK Devices with 16 bits PC[...]

    [...]

    Status Register (SREG) and Boolean Formula

    [...I bit set, other bits unaffected...]

    That's it. No more. The processor status is not saved on entry into
    the ISR, and RETI does not "restore" any such state, other than
    setting the I bit in SREG.

    So SEI should take care of you. You should clear the stack every now
    and again to ensure it doesn't overflow, but I'm not sure that would
    affect your example code anyway...

    Regards,

    -=Dave
     
    Dave Hansen, Jul 12, 2004
    #7
  8. But does anyone have any idea about what was going on with not being able
    This is a known issue with the STK500. If you have it plugged into a
    PC that provides a ground return, the power switch is inoperative -
    both positions are "on".
     
    Lewin A.R.W. Edwards, Jul 13, 2004
    #8
  9. Michael

    R Adsett Guest

    Figures, it would be one of the architectures that don't fit in the
    'generally'. I'd still want to use the reti for symmetry, documentation
    and parsimony but it appears the evil of avoiding it doesn't run very
    deep.

    Robert
     
    R Adsett, Jul 13, 2004
    #9
  10. Michael

    Michael Guest

    (Lewin A.R.W. Edwards) wrote in
    I don't have an STK500 - I'm just using a simple 5V power supply and a AVR
    ISP. I guess maybe it was getting power through the avr isp? It was still
    connected to the computer...

    -Michael
     
    Michael, Jul 13, 2004
    #10
  11. I don't have an STK500 - I'm just using a simple 5V power supply and a AVR
    Quite possible that it could have been getting power through the
    protection diodes on the I/O lines.
     
    Lewin A.R.W. Edwards, Jul 13, 2004
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.