' ********************************************************************** ' ** Program: gmbsere.BAS - Version : 1.1 - 20 June 2005 ** ' ** Compiler : Bascom AVR IDE - LIB 1.11.7.4 ** ' ** Board : GMB HR168 and GMM AM128 ** ' ** 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 ** ' ********************************************************************** ' ' 20.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. ' ' ' ' !!!!!!!!!!!!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' In menu Options | Compiler | Chip, set: ' ' ' Chip: M128 ' HW Stack: at least 64 ' Soft Stack: at least 32 ' Framesize: at least 64 ' ' $regfile = "m128def.dat" $crystal = 7372800 $baud = 19200 ' ' ********************* Constans declarations ************************* ' Const Cret = 13 ' Carriage return Const Nl = 10 ' New line Const Clrscr = 12 ' Clear screen Const Bell = 7 ' Bell Const Enabled = 1 ' Value to activate driver 422 or 485 Const Disabled = 0 ' Value to deactivate driver 422 or 485 ' Time out in case a TWI command receives no response Const Ee_timeout = 50000 ' '****************** 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 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() Declare Function Check_ready_2() As Byte ' '************************** Main program ****************************** ' Main: Call Init() ' Initialize module Portf.4 = 1 ' Turn OFF relays Portf.5 = 1 Portf.6 = 1 Portf.7 = 1 Portb.3 = 1 Portb.2 = 1 Portb.1 = 1 Porte.2 = 1 Print Chr(clrscr); ' Clears the screen Print "Demo 1.1 for GMM AM128 + GMB HR168" Print Call Check_ready() ' UART management in RS 232 and TTL Print Print "In RS 232: set ON switches 1, 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 1, 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..." 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" ' No Interrupt Ucsr0a.1 = 0 Ucsr0b.4 = 1 Ucsr0b.3 = 1 Ucsr0b.2 = 0 Ucsr0c.6 = 0 Ucsr0c.5 = 0 Ucsr0c.4 = 0 Ucsr0c.3 = 0 Ucsr0c.2 = 1 Ucsr0c.1 = 1 ' Set Baud Rate 19200 Ubrr1h = 0 Ubrr1l = 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 Porte.3 = 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 Porte.3 = 1 ' enable transmission ' of driver 422 or 485 Dir_ser = Disabled Else ' viceversa Porte.3 = 0 ' disable transmission ' of driver 422 or 485 Dir_ser = Enabled End If End If End If Loop End ' '************************ Program end *************************** ' ' '****************************************************************************** '* Demo specific procedures * '****************************************************************************** ' ' ' Initializes demo ' Sub Init() ' Initializes directionality of ports that drive optocouped inputs and relay ' outputs: ' IN1-1 - Portb.7 ' IN2-1 - Portb.6 ' IN3-1 - Porte.4 ' IN4-1 - Porte.5 ' IN5-1 - Portd.7 ' IN6-1 - Porte.6 ' IN7-1 - Portb.4 ' IN8-1 - Portb.0 Ddrb.7 = 0 Ddrb.6 = 0 Ddre.4 = 0 Ddre.5 = 0 Ddrd.7 = 0 Ddre.6 = 0 Ddrb.4 = 0 Ddrb.0 = 0 ' IN1-2 - Portc.0 ' IN2-2 - Portc.1 ' IN3-2 - Portc.2 ' IN4-2 - Portc.3 ' IN5-2 - Portc.4 ' IN6-2 - Portc.5 ' IN7-2 - Portc.6 ' IN8-2 - Portc.7 Ddrc = 0 ' OUT A1 - Portf.4 ' OUT A2 - Portf.5 ' OUT B1 - Portf.6 ' OUT B2 - Portf.7 ' OUT C1 - Portb.3 ' OUT C2 - Portb.2 ' OUT D1 - Portb.1 ' OUT D2 - Porte.2 (by Default , that is When J10 is connected in 3-4) Ddrf.4 = 1 Ddrf.5 = 1 Ddrf.6 = 1 Ddrf.7 = 1 Ddrb.3 = 1 Ddrb.2 = 1 Ddrb.1 = 1 Ddre.2 = 1 Ddre.3 = 1 ' Dir signal End Sub ' '****************************************************************************** '* Procedures to manage low level USART0 hardware * '****************************************************************************** ' ' ' ' **************** 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 ' '****************************************************************************** '* Generic procedures * '****************************************************************************** ' ' ' ' Supporting function for Check_ready(). ' Function Check_ready_2() As Byte Local Time_out As Word , I As Byte Time_out = 0 ' Timeout counter Do Incr Time_out ' Increment counter Loop Until Twcr.twint = 1 Or Time_out = Ee_timeout I = Twsr I = I And &HF8 Check_ready_2 = I End Function ' ' ' Check for card ready ' Sub Check_ready() Local Check As Byte , Test As Byte Do ' Loop to check for card ready Twsr = Twsr And &HFC Check = Twdr Or &HF4 Test.4 = Not Check.3 Twbr = 72 Twcr = &HA4 Check = Check_ready_2() Test = Check Or Twcr If Check = &H09 Then Test = &H67 Else Test.1 = Not Test.1 End If Twdr = &HA0 Twcr = &H84 Check = Check_ready_2() Twcr = &H94 Waitms 27 Loop Until Check = &H18 Or Test <> 1 Twcr.twen = 0 End Sub