hola alan980.
esto es lo que uso para interrupción serial y timer, espero te sirva. que como tarea que la estudies y apliques.
incluir los siguientes archivos para que te facilite las cosas:
'
http://web.archive.org/web/20141004065559/http://darreltaylor.com/DT_INTS-18/home.html'aquí se trata este tema de las interrupciones
http://www.picbasic.co.uk/forum/showthread.php?t=3251&page=19&highlight=timer+interrupsaludos
desde aquí el programa:
'estas librerias deben existir en la carpeta donde tienes instalado el PBP3
include "alldigital.pbp" ' definiciones para todos los puertos como I/O digitales si hay entradas anlogas no usarlo.
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System pic 18Fxx
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts pic 18Fxx
DEFINE OSC 8 ' Define clock freq. 8 MHz
'DECLARAS UN ARRAY DONDE VAS A METER LO QUE RECIBAS POR EL PUERTO SERIAL
'SI SON MUCHA LA INFORMACION, DE LO CONTRARIO DECLARAS UNA VARIABLE
MYBUFER VAR byte[100] 'AQUI VA TODO LO QUE LLEGUE
cuenta var byte ' ----> se incrementa cada vez que ocurre una interrupcion del timer
cuantos var byte
'//********************* Inicializo TXTSTA RXSTA *****************************
TXSTA = %00100100 ' abilitas el modo de transmision asincrono
RCSTA = %10010000 'preparas recepccion
'************************** INICIALIZO TIMER2 ES EL QUE USO**********************************
' PARA OBTENER ESTOS AJUSTES PUEDES USAR EL PROGRAMA FuseConfiguratorSetUp.exe
'LO BAJAS DE LA PAGINA
https://forum.mikroe.com/viewtopic.php?f=215&t=68172' COPIAS AQUI LOS VALORES QUE TE DA EL PROGRAMA SOLO LOS REGISTROS Y SUS
VALORES SELECCIONAS LA OPCION MILCROBASIC
' Prescaler=1:16; TMR2 PostScaler=1:1; PR2=208 - Freq = 1.495,21531Hz - Period = 0,6688 ms
T2CON = %00000110
PR2 = 208 ' PR2 (Timer2 Match value) (1 cycle adjusted)
TMR2 = 0 'TMR2 Holding register = 0
'********************* PREPARO INTERRUPCIONES ******************************
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RX_INT , _MI_INTERRUPCION, PBP, yes
INT_Handler TMR2_INT , _MI_INTERRUPCION, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
INT_ENABLE RX_INT ; enable external (INT) interrupts
INT_ENABLE TMR2_INT ; enable external (INT) interrupts
ENDASM
'********************* PROGRAMA PRINCIPAL ******************************
Main:
'AQUI TU PROGRAMA CON LO QUE VAS HACER
'manipulas en este ejemplo la variable cuenta
'usas los datos del bufer
'etc.....
GOTO MAIN
'*****************************************************************************
'!!!! Desabilito interrupcion no colocar ningun codigo del user !!!!
Disable
' ********************* OCURRIO INTERRUPCION LEO TRAMA ***********************
'AQUI LA INTERRUPCION SERIAL Y LA DEL TIMER LE DAS EL NOMBRE QUE QUIERAS
MI_INTERRUPCION:
If RCSTA.1 OR RCSTA.2 then ' SI OCURRE OVERRUN Y/O ERROR EN LA TRAMA LO BORRAS DE LO CONTRARIO DEJAS DE RECIBIR DATOS
RCSTA.4=0 ' // BORRO el error
RCSTA.4=1 ' // abilito la recepccion de nuevo
EndIf
if (PIR1.5 = 1) then ' OCURRIO INTERRUPCION DEL USART
while (PIR1.5 = 1) 'recibo los datos y los guardo en el bufer
MYBUFER [cuantos] = RCREG
cuantos= cuantos + 1
If cuantos>= 100 Then '---> para evitar se desborde el bufer
cuantos = 0
EndIf
Wend
EndIf
'********** ocurrio interrupcion del timer en mi caso el timer2 *************
if (PIR1.1 = 1) then
PIR1.1 = 0 ' lo reconozco
T2CON.2 = 0 ' desabilito el Timer2
CUENTA=CUENTA+1 '----> esta lleva las vece que ocurre la interrupción del timer
T2CON.2 = 1 'abilito el timer2
EndIf
@ INT_RETURN
End