PDA

View Full Version : Playing with Interrupts



tacbanon
- 11th November 2011, 12:17
Hi everyone, I'm trying to understand the following code that triggers an interrupt when data received on PortC.7. I tried to modify it and incorporate USB CDC to send back the received data to pc.




asm
__CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_2_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_OFF_2H
__CONFIG _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 0
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 5
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 4
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50



DEFINE OSC 48
DEFINE HSER_RCSTA 90h ' enable serial port,
define HSER_TXSTA 24h ' enable transmit,
define HSER_SPBRG 25 ' set baudrate to 9600
DEFINE HSER_CLOERR 1 ' automatic clear overrun error

TRISC = %10000000 ' PORTC.7 is the RX input, PORTC.6 is the TX output


' Serial communication definition
' ===============================
'
ADCON1 = %00001111 'Set up ADCON1 register no matter what you're doing!!!!!!
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
INCLUDE "cdc_desc.bas" 'Descriptor file for CDC serial demo
INCLUDE "MODEDEFS.BAS" ' Include Shiftin/out modes
INCLUDE "DT_INTS-18.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP interrupts
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::
' Variable definition
'::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::

RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
TRISC = %10000000
PortC = %00000000
led var PORTC.0
led1 var PORTC.1
led2 var PORTC.2
holdoff var word
SerialData var byte[15]
DataRec var byte[15]
buffer var byte[20]
flag1 var byte
flag1 = 0
cnt var byte
cnt = 5
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RX_INT, _Getbytes, PBP, no
INT_Handler USB_INT, _DoUSBSERVICE, ASM, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM


;----[Initialise USB and Interrupts]----------------------------------------
PAUSE 100 ; Allow VUSB to stabilize
USBINIT ; initialize the USB driver
USBSERVICE ; service it once
UIE = $7F ; enable USB interrupts
UEIE = $9F ; enable USB Error interrupts
@ INT_ENABLE USB_INT
@ INT_ENABLE RX_INT ; enable RX_INT interrupts





USBInit
USBService
Mainloop:

USBService ' Must service USB regularly
toggle led 'toggle led every loop
if flag1 = 1 then
Gosub readser
endif
goto Mainloop






;--------['ISR for RX_int interrupt]-------------------------------------

Getbytes:
'###########################################

While RCIF = 1 ' clear the buffer
HSERIN 100,error,[Serialdata] ' take it
Wend
High led2 'led to confirm program went to RX ISR
flag1 = 1



@ INT_RETURN


;----[Interrupt handler -- Service USB]-------------------------------------
DoUSBSERVICE:
USBSERVICE ; Run the SERVICE routines
@ INT_RETURN


error:
Toggle led1
@ INT_RETURN




usbtx:
USBService
DataRec[0] =13
USBOut 3, DataRec,13,usbtx
return


readser:
serin2 PortC.7, 84,[str DataRec\10]
if DataRec > 0 then
gosub usbtx
flag1=0
Low Led2
ENdif
Return

end



The code works, but I think there is a better/right way to do it. Is there a way without the need of using "readser:" label? Any suggestion will be great.
And I also want to insert this code in the main loop..


cnt = 16 ' Specify input buffer size
USBIn 3, buffer, cnt, Mainloop


' Message received
buffer[0] = "H"
buffer[1] = "e"
buffer[2] = "l"
buffer[3] = "l"
buffer[4] = "o"
buffer[5] = " "
buffer[6] = "W"
buffer[7] = "o"
buffer[8] = "r"
buffer[9] = "l"
buffer[10] = "d"
buffer[11] = 13
buffer[12] = 10
buffer[13] = 0

But I get "usb device not recognize".

Thanks in advance,
tacbabnon

tacbanon
- 12th November 2011, 03:24
I can now receive data from pc the same time interrupts when data received on portc.7(I was not able to make any changes but any suggestions will be appreciated).
And also I got another question..can I make interrupt for two ports(for incoming data)?

Thanks again,
tacbanon