' ********************************************************************** ' ** Program : gmbsere.bas - Version : 1.1 - 24 June 2003 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR84 with CAN GMx or GMM yyyy ** ' ** Corporate: 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 ** ' ** ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' This demo is a simple example of communication capableto work with all ' electric protocols available on CN2 (RS 232, RS 422, RS 485, current loop or ' TTL). ' In detail, through high level functions, it is possible to program baud rate ' from console, then each character received from serial port is sent to the ' port itself; reception of character 'r' decides how to manage the ' communication direction (signal DIR) for RS 422 and RS 485. ' ' 24.06.03 - Rel 1.1 By Graziano Gaiba ' ' The file compiled is smaller than 2048 bytes, so it can be compiled ' with free demo version of BASCOM 8051. ' '****************** Compiler directives ************************** ' ' Set to 80h internal memory limit by: ' Options/Compiler/Misc $regfile = "grifo_mm.dat" $romstart = &H0 ' start address of code for FLASH $ramstart = &H0 ' start address of external RAM $ramsize = &H100 ' 256 bytes of external RAM $crystal = 14745600 ' Microcontroller clock $baud = 19200 ' RS-232 baud rate '$large ' 16 bit addressing for jumps ' (Not for demo version) $map ' Generates address map ' '****************** Constants declatation *********************** ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell ' Board where the demo is running Const Can_gm1 = "1" Const Can_gm2 = "2" Const Gmm_5115 = "3" Const Attivo = 1 ' Value to activate driver 422 or 485 Const Disattivo = 0 ' Value to deactivate driver 422 or 485 Tia Alias Scon.1 ' Transmission flag for serial A Ria Alias Scon.0 ' Reception flag for serial A ' '****************** Variables declaration ********************** ' ' Generic use Dim S1 As String * 1 ' Generic input Dim V1bit As Bit ' Transmission driver status ' Used by low level procedures Dim Char As Byte Dim Rate As Long ' Baud rate required by the user Dim Valore As Byte ' Timer value for baud rate ' Board where the demo is running Dim Board As Byte ' '********************* Procedures declaration ************************* ' Declare Sub Ser_a ' Programming serial line A Declare Sub Txa(char As Byte) ' Manages transmission on serial line A Declare Sub Rxa ' Manages reception on serial line A ' '************************** Main program ****************************** ' Main: ' Inizializes demo Disable Interrupts ' Disabiles interrupts Auxr = &H0C ' Selects Eram on external data area Eecon = &H00 ' Disables Eeprom of Micro Adcf = &H00 ' Port 1 as I/O P3 = &HFF ' Sets port 3 as input Do Print Chr(clrscr); ' Clear screen Print "Demo 1.1 for GMB HR84 ds 100203" Print Print "Which Mini Modulo grifo(r) is the demo running on:" Print "1) GM1" Print "2) GM2 or 5115" Print Print "SELECT: "; Do Board = Inkey ' Stand by a key Loop Until Board <> 0 Print Chr(board) ' UART management in RS 232 and TTL Print Print "In RS 232: set ON switches 2 and 3, set OFF switches 4 and 5 of DSW1" Print "Software management is made by Bascom,see documentation for more info" Print "Set OFF switches 2 and 3, set ON switches 4 and 5, to have UART TTL signals" Print "directly to the socket pins." Print Print "Press a key to continue..." S1 = Waitkey Print Print "Connect J7 in "; If Board = Can_gm1 Then ' Correct connection of DIR Print "1-2" Else Print "2-3" End If ' Low level communication because BASCOM ' does not manage direction with high level instructions Input "Baud rate:" , Rate ' Value = 256 -((14745600) /(384 * BaudRate)) Rate = 384 * Rate Valore = 14745600 / Rate Valore = 256 - Valore Call Ser_a Print "Test in RS232, then configure GMB for other protocol:" Print "turn off power supply, set DSW1:" Print "2=3=OFF,4=5=ON, then:" Print Print "RS422:" Print "-install SN75176 or MAX483 on IC1 and IC2" Print "-J2=1-2 J3=1-2 J4=1-2 J5=2-3" Print Print "RS485:" Print "-install SN75176 or MAX483 on IC1" Print "-J2=1-2 J3=1-2 J4=1-2 J5=1-2" Print Print "If at net termination->J1 connected" Print "R switches transmitter" V1bit = Disattivo ' memorize status If Board = Can_gm1 Then P2.3 = 1 ' Set as output Else P1.3 = 1 ' Set as output End If Do Call Rxa ' Receive from serial line A If Char <> 0 Then ' Character available on serial line A Call Txa(char) ' Send character to serial line A Char = Char Or &H20 ' Convert to lower case If Char = "r" Then If V1bit = Attivo Then ' If it was off If Board = Can_gm1 Then P2.3 = 1 Else P1.3 = 1 ' enable transmission ' of driver 422 or 485 End If V1bit = Disattivo Else ' viceversa If Board = Can_gm1 Then P2.3 = 0 Else P1.3 = 0 ' disable transmission ' del driver 422 o 485 End If V1bit = Attivo End If End If End If Loop End ' '************************ Program end *************************** ' ' '**************************** Procedures ******************************** ' ' ' **************** Manages transmission on serial line A *********************** ' * This procedure allows to send on character to serial line A, character to * ' * send in variable char * ' ****************************************************************************** ' Sub Txa(char As Byte) ' Gestisce la trasmissione sulla seriale A Bitwait Tia , Set ' Wait for previous character sent Tia = 0 ' reset transmision bit Sbuf = Char ' Send character End Sub ' ' ***************** Manages reception from serial line A *********************** ' * This procedure returns the character received from serial line A,stores it * ' * in variable valore, if no char received returns 0 in variable char. * ' ****************************************************************************** ' ' Sub Rxa ' Manages reception on serial line A If Ria = 1 Then Ria = 0 ' Reset reception bit Char = Sbuf ' Read received character Else Char = 0 ' No character received End If End Sub ' Initialization baud rate generators Sub Ser_a Tcon = 0 Scon = &B01010010 '01010010 '||||||||__ RI must be initialized 0 '|||||||___ TI must be initialized 1 '||||||____ Not important in this case '|||||_____ Not important in this case '||||______ Enable reception flag '|||_______ Disables multiprocesor '||________ '|________> 8 bits UART ' Tmod = &B00100011 '00100011 '||||||||__ '|||||||__> T0 8 bits timer/counter with autoreload '||||||____ T0 as Timer. '|||||_____ T0 off '||||______ '|||______> T1 8 bits timer/counter with autoreload '||________ T1 as timer '|_________ T1 off ' Pcon = &B00000000 '00000000 MSB in PCON must be 0 '| '| '|________Smod, If= 1, doubles Baud Rate ' set on serial line A ' Th1 = Valore ' Baud rate decided by the user Set Tcon.6 ' T1 on, start timer1, enable communication on A End Sub