Interrupts

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

Hello! I am having a hard time finding the mistake. I have to turn on interrupts, enable INT1, set it to respond to negative transitions of its input pin, whose service routine counts down from 10 to 0 each time the interrupt is triggered. The code runs, but it doesn't stop at zero.

Any help will be appreciated.

 

 

.NOLIST
.INCLUDE "8515def.inc"     
.LIST

.org 0x0000            
.org INT1addr                       

rjmp IntV1       

Inicio:
    ldi    r16, 0x00
    out DDRD, r16        
    ldi    r16, 0xFF
    out    PORTD, r16                
    
    out DDRB, r16        
    
    ldi r16,LOW(RAMEND)
    out SPL,r16
    ldi r16,HIGH(RAMEND)
    out SPH,r16
    
    ldi r16, 0b00000010            
    out MCUCR, r16
    
    ldi r16, 0B11000000            
    out GIMSK, R16

    ldi r17,10                
    out PORTB, r17    
    
    sei                            
    
    rjmp Inicio

IntV1:
    dec r17
    out PORTB,r17
    reti   

Azul Garcia

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

decrementing does not stop at zero. 0x00 decremented is 0xFF.

 

If you want it to stop at zero, you need to test for zero and skip the decrement if it is zero.

 

Jim

 

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

 

 

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

Also, you do NOT want to keep jumping back to your init code.  Do this:

; all your init code here

main_loop:
; any additional looping code can go here
    rjmp    main_loop

 

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

Thanks so much for your commentaries! What do you think now? I have not idea what I'm doing but I think it works now.

 

.NOLIST
.INCLUDE "8515def.inc"     
.LIST

.org 0x0000    
rjmp Inicio        
.org INT1addr                       
rjmp IntV1       

Inicio:
    ldi    r16, 0x00
    out DDRD, r16        
    ldi    r16, 0xFF
    out    PORTD, r16                
    
    out DDRB, r16        
    
    ldi r16,LOW(RAMEND)
    out SPL,r16
    ldi r16,HIGH(RAMEND)
    out SPH,r16
    
    ldi r16, 0b00000010            
    out MCUCR, r16
    
    ldi r16, 0B11000000            
    out GIMSK, R16

    ldi r17,10                
    out PORTB, r17    
    
    sei                            

Loop:
    rjmp Loop

IntV1:
        tst r17
        breq Zero
        dec r17
        out PORTB,r17
        reti
    
    Zero:
        nop

Azul Garcia

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

Not quite

 

    
   IntV1:
        tst r17
        breq Zero
        dec r17
    Zero:
        out PORTB,r17
        reti

What you have never does reti if it is zero.

 

Jim

 

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

 

 

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

Thanks so much for your help! I will keep coding, it is the only way to learn.

 

Azul Garcia

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

ka7ehk wrote:
What you have never does reti if it is zero.
Jim., I don't follow what you are saying here? If R17 is 0 then the TST will set Z, the BREQ will be taken, it arrives immediately at the "Zero:" label, does the OUT and then the RETI. How would it be skipping RETI when 0 ??

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

azugr@outlook.com wrote:
I have not idea what I'm doing

Then sit down with a hot mug of tea and ww1.microchip.com/downloads/en/devicedoc/atmel-0856-avr-instruction-set-manual.pdf, to understand the effects of each instruction in your the code.

 

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

azugr@outlook.com wrote:
I will keep coding, it is the only (sic?) way to learn.

Some good thought & study before leaping into coding can help ...

 

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is one problem that you should learn to avoid.

One day /of your design/, Interrupt will happen any time during the code execution and will cause havoc, unless the code is protected from the Interrupt intrusion, for example:

IntV1:
        in rSREG,SREG
        tst r17
        breq Zero
        dec r17
        out PORTB,r17
	out SREG,rSREG
        reti

  Zero:
        nop
        out SREG,rSREG
        reti

 

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

it is a good idea to always push & pop the sreg...avoid troubles!  it is rare that you might not affect affect it, unless the irq is dirt simple

 

push ZL

in ZL, SREG

push ZL

 

do stuff

do stuff

 

pop ZL

out SREG, ZL

pop ZL

reti

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

clawson wrote:

Jim., I don't follow what you are saying here? If R17 is 0 then the TST will set Z, the BREQ will be taken, it arrives immediately at the "Zero:" label, does the OUT and then the RETI. How would it be skipping RETI when 0 ??

His code from post #4

IntV1:
        tst r17
        breq Zero
        dec r17
        out PORTB,r17
        reti
    
    Zero:
        nop

 

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

I assume that you are testing this with a push button on INT1.  When you press this button, you will get most likely many tiny pulses because the button has no debouncing.  Each of these tiny pulses will trigger an IRQ and decrease the count.  Put a 0.1 microFarad capacitor across the push switch connections to get rid of these tiny pulses.

 

Also, check if the 8515 uses JMP instead of RJMP instructions in the reset/IRQ vector section at the beginning of the flash code. I believe that the change point is for AVRs that use 16K of flash.