Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interrupt problems

Status
Not open for further replies.
Hey there,

I managed to create an Interrupt handler and some stuff to let the speaker ring a note while the main routine is to blink an LED, but it seems that the speaker took over and the LED didn't blink.

I could have missed something inside - what must I need to do to make it to trigger the speaker and do the LED altogether? :rolleyes:

Code:
#include <p18F1320.inc>
    
    CONFIG WDT=OFF; disable watchdog timer
    CONFIG MCLRE = OFF; MCLEAR Pin off
    CONFIG DEBUG = OFF; Enable Debug Mode
    CONFIG LVP = OFF; Low-Voltage programming disabled (necessary for debugging)
    CONFIG OSC = INTIO2;Internal oscillator, port function on RA6 
    CONFIG PWRT = ON
    
org 0x00
goto Init

cblock 0x20        
Delay1 
Delay2
count1
count2
note1
noteCount
endc

cblock    0x60
W_Save
STATUS_Save
endc    

org    0x08

ISR:
     movwf     W_Save
     movf      STATUS,w
     movwf     STATUS_Save
     
playNote:
    btfsc    INTCON,TMR0IF
    movlw    d'250'
    movwf    TMR0L

    clrf    W
    
    movlw    d'86'
    
    incf    noteCount
    cpfseq    noteCount
    goto    ExitISR
    btg        LATA,1
    
    clrf    noteCount
    goto    ExitISR
     
ExitISR:
     movf      STATUS_Save,w
     movwf     STATUS
     swapf     W_Save,f
     swapf     W_Save,w
     retfie     

Init:
    movlw    b'01100000'
    movwf    OSCCON
    
    clrf    T0CON
    movlw    b'11000111'
    movwf    T0CON
    
    movlw    0x00
    movwf    TRISB
    clrf    LATB
    
    movlw    b'00000001'
    movwf    TRISA
    clrf    LATA
    
    movlw    b'11100000'
    movwf    INTCON

main:
    btg        LATB,2
    call    Delay
    goto    main


Delay:    
DelayLoop:
    DECFSZ    count1,f ;Decrement Delay1 by 1, skip next instruction if Delay1 is 0 
    GOTO DelayLoop
    DECFSZ      count2,f
    GOTO DelayLoop
    return
end

note: PIC 18F1320, 4MHz OSC
 
so you are trying to blink led and play at same time?

you cant really but you can make it seem like so just add that btg in the play note code so it will play not and blink in the same function.
 
Code:
ISR:
     movwf     W_Save
     movf      STATUS,w
     movwf     STATUS_Save
     
playNote:
[B]    btfsc    INTCON,TMR0IF
[/B]
[COLOR="Blue"]    bcf    INTCON,TMR0IF ;Maybe you meant this.[/COLOR]
    movlw    d'250'
    movwf    TMR0L

[B]    clrf    W[COLOR="Blue"] ;This is not needed[/COLOR]
    
    movlw    d'86'[/B][COLOR="Blue"] ;This is not needed either[/COLOR]
    
    incf    noteCount
    cpfseq    noteCount
    goto    ExitISR
    btg        LATA,1
    
    clrf    noteCount
    [B]goto    ExitISR[/B] [COLOR="Blue"];Not necessary[/COLOR]
     
ExitISR:
     movf      STATUS_Save,w
     movwf     STATUS
     swapf     W_Save,f
     swapf     W_Save,w
     retfie
The ISR will steal cycles from your delay routine. You'll need to use a 2nd timer for the LED flashing or else the flash rate will slow, but not stop, when a "note" is played.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top