' ********************************************************************** ' ** Program: DEMO_INT.BAS - Versione : 1.1 - 29 Gennaio 1999 ** ' ** Compiler: BASCOM LT, (IDE V.1.20, LIB V.1.27) ** ' ** Card: GPC(r) F2 ** ' ** Firm: grifo(r) ITALIAN TECHNOLOGY ** ' ** Via Dell' Artigiano 8/6 40016 San Giorgio di Piano (BO) ** ' ** Tel.+39 051 892 052 Fax +39 051 893 661 ** ' ** http://www.grifo.com http://www.grifo.it ** ' ** sales@grifo.it tech@grifo.it grifo@grifo.it ** ' ** ** ' ** Developed by: Adriano Pedrielli ** ' ********************************************************************** ' ' Demo program for serial interrupt management. ' The interrupt transmission uses PRINT output redirection, while the ' interrupt reception saves the characters in a circular buffer. '-------------------------------------------------------------------------- ' '*********************************************************************** ' '****************** Direttive del compilatore ************************** ' $romstart = &H8050 ' Code area start address $ramstart = &HD000 ' Car area start address $ramsize = &H2800 ' Car area size = 10K $crystal = 11059200 ' Microcontroller clock frequency $large ' 16 bits addressing mode $noinit ' Disables BASCOM initializations $serialoutput = Send ' Defines the serial transmission redirection procedure ' '******************** Constant declaration ***************************** ' Dim Baud As Const 253 ' 9600 Baud at 11.0592 MHz ' '******************** Variable declaration ***************************** ' Dim Send_ok As Bit ' This variable follows the TI bit status; the serial line ' interrupt is enabled and it is not possible to use the ' TI bit to check if the transmission is completed. So: ' Send_ok = 1 -> one character is being transmitted; ' Send_ok = 0 -> no caracters is being transmitted. Dim Car As Byte ' Variable for the received characters Dim W_buf As Byte ' Buffer write pointer Dim R_buf As Byte ' Buffer read pointer Dim X As Byte ' Variabl for generic use Rx Alias Scon.0 ' RI bit Tx Alias Scon.1 ' TI bit Dim Rec_buf (15) As Byte ' Buffer for interrupt received characters Dim End_buf As Const 16 ' End of buffer pointer (buffer dimension + 1) ' '*********************** Procedure declaration ************************* ' Declare Sub Read_buf ' Procedure that get a character from the buffer On Serial Serial_int ' Defines the serial interrupt service routine ' '************************* Main program ******************************** ' Main: Waitms 1 ' Delay for signals adjustement '*************** Low level serial inizialization *********************** ' Tcon = 0 Scon = &B01010010 '01010000 '||||||||__ RI must be setted to 0. '|||||||___ TI must be setted to 0. '||||||____ Don't care (in this example). '|||||_____ Don't care (in this example). '||||______ Enables receive flag. '|||_______ Disables multiprocesor communication mode '||________ '|________> 8 bits UART ' Tmod = &B00100001 '00100001 '||||||||__ '|||||||__> T0 16 bits timer/counter register '||||||____ T0 used as Timer. '|||||_____ T0 off '||||______ '|||______> T1 8 bits timer/counter with autoreload '||________ T1 used as timer '|_________ T1 off ' Pcon = &B00000000 '00000000 The MSB in PCON mst be setted to 0 '|________Smod, If = 1, it doubles the baud rate defined in Baud ' Th1 = Baud ' Sets the baud rate Set Tcon.6 ' T1 on, start timer, enables communication ' '************* End of low level serial inizialization ****************** ' Rx = 0 : Tx = 0 ' Resets the serial interrupt flags W_buf = 1 : R_buf = 1 ' Initializes the receive beffer pointers Send_ok = 0 ' Resets the character in transmission flag Enable Serial ' Enables serial interrupt Priority Set Serial ' Serial interrupt with higher priority Enable Interrupts Print Do Print " Attesa caratteri" Print Do For X = 0 To 50 Print " W_buf=" ; W_buf ; " ";' Shows buffer write pointer Print Chr(13); Waitms 100 ' Delays 100 ms Next X Read_buf ' Checks the buffer and reads possible character Loop Until Car <> 0 Print While Car <> 0 ' Shows the new received characters Print Chr(Car) ; ' Displays the character Print " R_buf=" ; R_buf ; Print " W_buf=" ; W_buf ' Show pointers values Read_buf ' Checks the buffer Wend Loop End ' '************************ Main program end ***************************** ' ' '*************************** Procedure ********************************* ' ' ' Procedure that get a character from the buffer ' Input parameter: none ' Otput parameter: Car = possible character saved in the buffer; if the buffer is empty: Car=0 ' R_buf = buffer read pointer updated Sub Read_buf If R_buf <> W_buf Then ' Compares buffer read and write pointers Incr R_buf ' Increments the buffer read pointer If R_buf = End_buf Then ' Cecks buffer overflow R_buf = 1 ' If overflow points again to buffer start End If Car = Rec_buf(R_buf) ' Saves buffer character Else Car = 0 ' Character not available End If End Sub ' ' ' Serial interrupt service routine ' Input parameter: none ' Otput parameter: W_buf = buffer write pointer updated ' Rec_buf() = buffer with possible received character ' Send_ok = character in transmission flag updated Serial_int: If Tx = 1 Then ' Interrupt source = transmit completed Tx = 0 ' Resets the Tx interrupt flag Send_ok = 0 ' Resets the character in transmission flag End If If Rx = 1 Then ' Interrupt source = character received Rx = 0 ' Resets the Rx interrupt flag Incr W_buf ' Increments the buffer write pointer If W_buf = End_buf Then ' Cecks buffer overflow W_buf = 1 ' If overflow points again to buffer start End If Rec_buf(W_buf) = Sbuf ' Saves received character in the buffer End If Return ' ' ' Procedure that redirects the serial trasmission, i.e. the PRINT instruction ' Input parameter: acc = character to transmit ' Output parameter: none !Send: Bitwait Send_ok , Reset ' Waits until character in transmission flag is not setted Send_ok = 1 ' Enables the character in transmission flag MOV SBUF , acc ; ' Transmits the character ret