' ********************************************************************** ' ** Program: gmbsere.BAS - Version : 1.1 - 29 January 2004 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Scheda : GMB HR84 and GMM AM08 ** ' ** Society: 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 ** ' ********************************************************************** ' ' Rel. 1.1 29.01.04 - By Graziano Gaiba ' ' This demo is a simple example of communication capable to 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. ' ' Compiled file is smaller than 2048 bytes, so it can becompliled with free ' demo of BASCOM AVR. ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' Chip: M8 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m8def.dat" $crystal = 7372800 $baud = 19200 ' ' ********************* Constans declarations ************************* ' Const Cret = 13 ' Codice di ritorno di carrello Const Nl = 10 ' Codice nuova linea Const Clrscr = 12 ' codice di clear screen Const Bell = 7 ' codice di Bell Const Enabled = 1 ' Value to activate driver 422 or 485 Const Disabled = 0 ' Value to deactivate driver 422 or 485 ' '****************** Variables declaration ********************** ' ' Generic use Dim Dir_ser As Bit ' Transmission driver status ' Used by low level procedures Dim Char As Byte Dim Rate As Long ' Baud rate required by the user Ri Alias Ucsra.rxc ' Reception completed Ti Alias Ucsra.txc ' Transmission completed ' '********************* Procedures declaration ************************* ' Declare Sub Init() ' Initialization Declare Sub Txa(byval C As Byte) ' Manages transmission on serial line A Declare Function Rxa() As Byte ' Manages reception on serial line A ' '************************** Main program ****************************** ' Main: ' UART management in RS 232 and TTL Print Print "In RS 232: set ON switches 1 and 2, set OFF switches 3 and 4 of DSW1" Print "Software management is made by Bascom,see documentation for more info" Print "Set OFF switches 1 and 2, set ON switches 3 and 4, to have UART TTL signals" Print "directly to the socket pins." Print Print "Press a key to continue..." Char = Waitkey() Print Print "Connect J7 in 2-3" ' connection of DIR ' Low level communication because BASCOM ' does not manage direction with high level instructions Input "Baud rate:" , Rate ' UBRR = (7372800 / (16 * BaudRate)) - 1 Rotate Rate , Left , 4 Rate = 7372800 / Rate ' Before reprogramming serial port, transmission buffer must be emptied Waitms 10 ' Bit x chr = 8" ' Stop bit = 1" ' Parity = No" ' Handshake = No" ' Baud rate = 19200" Ucsrc = &H86 ' Initialize Serial port 8 Bit , 1 Stop , No Interrupt Ubrrhi = &H00 ' Set Baud Rate 19200 Ubrr = Rate 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" Dir_ser = Disabled ' memorize status Portb.1 = 0 Do Char = Rxa() ' riceive from serial A If Char <> 0 Then ' is it a character? Call Txa(char) ' Send it to serial A Char = Char Or &H20 ' Turn in lowercase If Char = "r" Then If Dir_ser = Enabled Then ' if disabled Portb.1 = 1 ' enable transmission ' of driver 422 or 485 Dir_ser = Disabled Else ' viceversa Portb.1 = 0 ' disable transmission ' of driver 422 or 485 Dir_ser = Enabled End If End If End If Loop End ' '************************ Program end *************************** ' ' '**************************** Procedures ******************************** ' ' ' Initializes demo ' Sub Init() Ddrb.1 = 1 ' Dir signal End Sub ' ' ' **************** Manages transmission on serial line A *********************** ' * This procedure allows to send on character to serial line A, character to * ' * send in variable c. * ' ****************************************************************************** ' Sub Txa(byval C As Byte) ' Manages transmission on serial line A Bitwait Ti , Set ' Wait for previous character sent Ti = 0 ' reset transmision bit Udr = Char ' Send character End Sub ' ' ' ***************** Manages reception from serial line A *********************** ' * This procedure returns the character received from serial line A,and * ' * returns it, if no char received returns 0. * * ' ****************************************************************************** ' ' Function Rxa() As Byte ' Manages reception on serial line A If Ri = 1 Then Rxa = Udr ' Read received character Else Rxa = 0 ' No character received End If End Function