' ********************************************************************** ' ** Program: gmbi2ce.BAS - Version : 1.1 - 14 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM32 ** ' ** 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 ** ' ** Written by: Graziano Gaiba ** ' ********************************************************************** ' ' 14.06.05 - Rel 1.1 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. ' ' 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: M32 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m32def.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 Dim A As Eram Byte At 1023 ' Used by low level procedures Dim Char As Byte Dim Rate As Long ' Baud rate required by the user Ri Alias Usr.rxc ' Reception completed Ti Alias Usr.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 Declare Sub Check_ready() ' '************************** Main program ****************************** ' Main: Call Init() ' Initialize demo Print "Demo 1.1 per GMM AM32 + GMB HR168" Print Call Check_ready() ' 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 J10 in 1-2." ' 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 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 connected" Print "R switches transmitter" Dir_ser = Disabled ' memorize status Portd.7 = 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 Portd.7 = 1 ' enable transmission ' of driver 422 or 485 Dir_ser = Disabled Else ' viceversa Portd.7 = 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() Ddrd.7 = 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 ' ' ' Check for hardware ready ' Sub Check_ready() Test: If A = &H55 Then Goto L2 Else Goto Test End If L2: End Sub