' ********************************************************************** ' ** Program : gmbsere.bas - Version : 1.2 ** ' ** Compiler : BASCOM 8051 DEMO, (IDE and LIB V.2.0.11.0) ** ' ** Board : GMB HR168 with GMM ACZero ** ' ** 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 ** ' ** ** ' ** Written by: Graziano Gaiba 02.02.2005 ** ' ********************************************************************** ' ' This demo is a simple example of communication capableto work with all ' electric protocols available on CN6 (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. ' ' Rel. 1.1 03.03.04 - By Graziano Gaiba ' ' Rel. 1.2 02.02.05 - By Graziano Gaiba ' ' Modified to work with GMM ACZero ' ' ' !!!!!!!!!!!!!!!!!!!!!!! ATTENTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options/Compiler/Misc set: ' - Byte End(hex) at least A0 ' ' ' The file compiled is smaller than 2048 bytes, so it can be compiled ' with free demo version of BASCOM 8051. ' ' '****************** Compiler directives ************************** ' $regfile = "8951cc03.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 Tia Alias Scon.1 ' flag of transmission happened on serial A Ria Alias Scon.0 ' flag of reception happened on serial A ' ' '****************** Constants declaration *********************** ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell Const Attivo = 1 ' Value to enable driver 422 or 485 Const Disattivo = 0 ' Value to disable driver 422 or 485 ' ' '*********************** Variables declaration ************************* ' ' Generic use Dim S1 As String * 1 ' One character strings Dim S0 As Bit ' Generic use bit Dim S As Byte ' Used by serial demo Dim Bitrate As Long ' ' '****************** Procedures declaration ********************** ' Declare Sub Ser_a ' Setting serial line A Declare Sub Txa(s As Byte) ' Manages transmission on serial line A Declare Sub Rxa ' Manages reception on serial line A ' '**************************** Main program ********************************* ' Main: Disable Interrupts ' Disables interrupts Auxr = &H0C ' Selects Eram on external data area Eecon = &H00 ' Disables write to micro Eeprom Call Setp01234input() Print Chr(clrscr) ' Clears screen Print "Demo 1.2 for Module GMB HR168 d.s. 110104" Print ' 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 J6, J7 e J8 in 2-3" ' We use low level communication because BASCOM ' does not manage communication direction with high level instructions Input "Baud rate:" , Bitrate ' Valore = 256 -((2*14745600) /(384 * BaudRate)) or ' Valore = 256 -((14745600) /(384 * BaudRate)) according to CKCON for UART Bitrate = 384 * Bitrate S = 14745600 / Bitrate S = 256 - S 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 IC10 and IC11" Print "-J5=2-3 J6,J7,J8=1-2 J10=1-2" Print Print "RS485:" Print "-install SN75176 or MAX483 on IC10" Print "-J5=1-2 J6,J7,J8=1-2 J10=1-2" Print Print "If at net termination, J3 and J4 must be connected" Print "R switches transmitter" S0 = Disattivo ' memorize status P2.3 = 1 ' Set as output Do Call Rxa ' Receive from serial line A If S <> 0 Then ' Character available on serial line A Call Txa(s) ' Send character to serial line A S = S Or &H20 ' Convert to lower case If S = "r" Then If S0 = Attivo Then ' If it was off P2.3 = 1 ' enable transmission ' of driver 422 or 485 S0 = Disattivo Else ' viceversa P2.3 = 0 ' disable transmission ' of driver 422 or 485 S0 = 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(s As Byte) ' Gestisce la trasmissione sulla seriale A Bitwait Tia , Set ' Wait for previous character sent Tia = 0 ' reset transmision bit Sbuf = S ' 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 S = Sbuf ' Read received character Else S = 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 = S ' Baud rate decided by the user Set Tcon.6 ' T1 on, start timer1, enable communication on A End Sub ' ' ' Set all ports as input ' Sub Setp01234input() P0 = &HFF ' Set P0 as input S = P0 Adcf = &H00 ' Set P1 as I/O P1 = &HFF ' Set P1 as input S = P1 P2 = &HFF ' Set P2 as input S = P2 P3 = &HFF ' Set P3 as input S = P3 P4 = &HFF ' Set P4 as input S = P4 End Sub