' ********************************************************************** ' * File: uk_BAS51_054.BAS * ' * Version: 1.1 * ' * Date: 05.12.11 * ' * Development Tools: Bascom 8051 COMP.,IDE 2.0.14.0 + FLIP 2.4.6 * ' * Cards: GMM 5115 + GMM TST3 * ' * Developed by: GRIFO(r) Italian Technology * ' * via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * Author: Gianluca Angelini * ' ********************************************************************** ' Example program 054 of BASCOM 8051 course. ' It manages an additional asynchronous serial line, with interrupts. ' The Mini Module GMM AM08 is provided with only one hw serial line, connected ' to USART peripheral device, and this program adds a second sw serial line, ' connected to two I/O lines. The sw serial line is managed through the high ' level instructions of BASCOM, joined with interrupt, in order to become not ' suspensive, or in other words, they doesn't stop program execution during ' transmission and reception of characters. ' By comparing a sw serial line to an hw one, many differences came out as ' limits on communication speed (Baud rate), a really higher execution load ' for the microcontroller and the denied possibility to transmit and receive ' contemporaneously. ' The program supports the following operations on sw serial line: ' - comunicate with 4800 Baud, 8 Bits x chr, 1 Stop bit, No parity physical ' protocol; ' - each received character is recogized by an interrupt, then received and ' saved inside a circular receive buffer; ' - the received characters in a not suspensive mode, are displayed on console; ' - transmit the characters typed on console. ' About electric protocol, the sw serial line of Mini Module is at TTL level ' and normally it must be buffered in order to communicate with other external ' serial devices. For this scope we remind the MSI 01 interface that convert a ' TTL serial line in RS 232, RS 422, RS 485 o Current Loop electric standard. ' The user can comunicate with sw serial line by using a console provided of ' monitor and keyboard, connected to hw serial line, with a fixed physical ' protocol at 19200 Baud, 8 Bits x chr, 1 Stop bit, No parity. ' This console can be another system capable to support a serial RS 232 ' communication. In order to simplify the use it can be used a PC provided of ' one COMx line, that execute a terminal emulation program as HYPERTERMINAL or ' the homonym modality provided by BASCOM 8051 (see IDE Configuration). ' The program works only when the GMM 5115 is mounted on Z2 socket of GMM TST3!! ' ' Added instructions: ON INT1, ENABLE INT1. ' ' 05/12/11: uk_BAS51_054.BAS - Ver 1.1 - By G.A. ' First version. ' ' '**************************** IDE Configurations ******************************* ' NOTE: in order to correctly use this demo program, please execute the following ' steps: ' 1) Check the availability of 89C5115.DAT file into the directory where the ' BASCOM 8051 is installed and copy it if not present. ' 2) Into the window "Options | Compiler | Misc" set: ' Register File = 89C5115.DAT ' Byte End(Hex) = A0 ' Size warning = selected at 16384 (=4000H) ' 3) Into the window "Options | Communication" set: ' COM port = the PC line connected to GMM 5115, through GMM TST3 ' Baudrate = 19200 ' Parity = None ' Databits = 8 ' Stopbit = 1 ' Handshake = None ' Emulation = TTY ' Font = Terminal, Normal, 12 points, white colour ' Backcolor = Navy ' Run emulator modal = not selected ' 4) At the end of compilation, after the code is programmed on GMM 5115, select ' RUN mode and open the terminal emulation window of BASCOM 8051 with the ' option: Tools | Terminal emulator (Ctrl+T) and then reset or powen on the ' Mini Module. '************************* Compiler directives ********************************* $regfile "89C5115.DAT" ' Definitions file for used microcontroller $romstart = &H0 ' Code start address on FLASH $iramstart = &H0 ' Data start address on internal RAM $ramstart = &H0 ' Data start address on external RAM $ramsize = &H100 ' External RAM size $crystal = 14745600 ' Microcontroller crystal frequency $large ' Code size > 2K $map ' Generate debug information $baud = 19200 ' Serial communication speed: 19200 Baud ' Other parameters fixed to: 8 bit x chr ' 1 Stop bit ' No parity '******************************* Definitions *********************************** ' The resources used by program are connected as described in following table. ' !!! Note: On GMM TST3 the jumpers must be set in following positions: ' J1 in 2-3 ; J2 in 1-2 ; J3 in 1-2 ; J5 in 2-3 ; J7 in 2-3 ; J8 in 2-3 ' J9 in 2-3!!! ' ' Software GMM TST3 pin Z2 pin Signal uP used ' serial line resource GMM TST3 GMM 5115 GMM 5115 signal ' RX SW CN4.16 24 18 P3.3 INT1 P3.3 ' TX SW CN4.13 23 17 P3.4 T0 P3.4 ' ' Signal pin COMx pin CN5 pin Z1 pin Signal Used up ' PC DB9 GMM TST3 GMM TST3 GMM 5115 GMM 5115 signal ' TX 3 3 9 3 RxD RS232 P3.0 ' RX 2 2 10 4 TxD RS232 P3.1 ' GND 5 5 20 14 GND GND ' This table shows that the connection cable between PC COM line and CN5 of ' GMM TST3 is a normal pin to pin cable or direct. Grifo(r) can supply it by ' requesting the CCR 9+9E code. Pinrx Alias P3.0 ' Signal connected to GMM 5115 RxD Pintx Alias P3.1 ' Signal connected to GMM 5115 TxD '************************* Constants declaration ******************************* '************************* Variables declaration ******************************* Dim Crxsw As Byte ' Character received from sw serial line Dim Ctxsw As Byte ' Character to transmit on sw serial line Dim Rxcsw As Byte ' Character received by interrupt Dim Bufrx(5) As Byte ' Receive buffer for 5 characters Dim Wpntrx As Byte ' Write pointer to receive buffer Dim Rpntrx As Byte ' Read pointer to receive buffer Dim Isrxsw As Bit ' Flag for character received from sw serial line Dim Key As Byte ' Key pressed on console '************************ Subroutines declaration ****************************** On Int1 Ricev_sw ' INT1 service routine Declare Sub Iniser_sw() ' Initialize and arrange the sw serial line use Declare Sub Closeser_sw() ' Close and finish sw serial line use Declare Sub Isrxser_sw() ' Manage sw serial line reception Declare Sub Txser_sw() ' Transmit character on sw serial line '****************************** Main program *********************************** Main: Pinrx = 1 ' Initialize signals for serial communication Pintx = 1 ' as digital inputs Print ' Separate from previous visualization by showing 2 empty new line on console Print Print " Sw serial line management with interrupt on GMM 5115 + GMM TST3" Print "Mount Mini Module on Z2 of GMM TST3 and connect the external serial device" Print "(configured at 4800 Baud, 8 Bits, 1 Stop, No parity) as described in electric" Print "diagram." Print "The program waits the reception of characters from sw serial line, it shows" Print "them on console, and it transmits the keys pressed on sw serial line" Print Print "The reception of sw serial line is not suspensive and the program goes on even" Print "when the serial device is not connected or it doesn't transmit characters!!" ' Initializes the sw serial line for 4800 Baud on the I/O signals defined in ' previous table. The other parameters of the physic protocol (8 Bits x chr, ' 1 stop Bit, No parity) are fixed and not modificable. ' In the program source the following two instructions must be used before each ' other instructions that use the sw serial line. Open "com3.4:4800" For Output As #1 ' Associate signal used as transmission line, to channel 1 Open "com3.3:4800" For Input As #2 ' Associate signal used as reception line, to channel 2 Call Iniser_sw() ' Initialize and arrange the sw serial line use Do ' Begin endless loop Call Isrxser_sw() ' Verify and receive character from sw serial line If Isrxsw = 1 Then Printbin Crxsw ' Shows received characters on console End If Key = Inkey() ' Get possible key pressed on console If Key <> 0 Then ' If key pressed Ctxsw = Key ' Copy key pressed on character to transmit Call Txser_sw() ' Transmit character on sw serial line, associated to channel 1 End If Loop ' End endless loop Disable Interrupts ' Interrupt general disable Call Closeser_sw() ' Close and finish sw serial line use End '*************************** End of main program ******************************* '*********************** Subroutines used by program *************************** ' INT1 interrupt service routine, activated by Start bit of received character. ' This method can be used only when the receive signal of the sw serial line ' (RX SW) is capable to generate interrupt on falling edge. Moreover this method ' makes the reception from the same serial line not suspensive and it frees up ' the microcontroller from useless work. ' Input: Wpntrx = current write pointer to receive buffer ' Output: Wpntrx = updated write pointer to receive buffer ' Bufrx() = receive buffer with received character Ricev_sw: Get #2 , Rxcsw ' Receive chr just started with sw serial, associated to channel 2 Bufrx(wpntrx) = Rxcsw ' Save received chr into the receive buffer Incr Wpntrx ' Increase write pointer in circular manner If Wpntrx > 5 Then Wpntrx = 1 End If Tcon.3 = 0 ' Clear INT1 flag to erase all the chr's transitions Return ' Initialize and arrange sw serial line use ' Input: None ' Output: Wpntrx = write pointer to receive buffer initialized ' Rpntrx = read pointer to receive buffer initialized Sub Iniser_sw() Wpntrx = 1 ' Initialize write pointer to receive buffer Rpntrx = 1 ' Initialize read pointer to receive buffer Tcon.2 = 1 ' INT1 active on falling edge Enable Int1 ' Enable INT1 interrupt Enable Interrupts ' General interrupt enable End Sub ' Manage sw serial reception ' Input: Rpntrx = current read pointer to receive buffer ' Output: Rpntrx = updated read pointer to receive buffer ' Isrxsw = flag for character received from sw serial line ' Crxsw = possible received character Sub Isrxser_sw() If Wpntrx <> Rpntrx Then ' Verify if character received through INT1 and saved in circular buffer Crxsw = Bufrx(rpntrx) ' Acquire received character from receive buffer Incr Rpntrx ' Increase read pointer in circular manner If Rpntrx > 5 Then Rpntrx = 1 End If Isrxsw = 1 ' Signalize character received and returned Else Isrxsw = 0 ' Signalize character not received End If End Sub ' Transmit character on sw serial line, by ensuring all interrupts disabled in ' order to mantain timings and correct framing of character under transmission. ' Input: Ctxsw = character to transmit ' Output: None Sub Txser_sw() Disable Interrupts ' General interrupt disable Put #1 , Ctxsw ' Transmit character on sw serial line, associated to channel 1 Enable Interrupts ' General interrupt enable End Sub ' Close and finish sw serial line use ' In the program source the following two instructions must be used after each ' other instructions that use the sw serial line. ' Input: None ' Output: None Sub Closeser_sw() Close #1 ' Close channels associated to sw serial line Close #2 End Sub '******************* End of subroutines used by program ************************