'****************************************************************** '* Name : gmbsere.bas * '* Author : Graziano GAIBA * '* Date : 15/05/04 * '* Versione : 1.1 * '* Board : GMM 876 + GMB HR84 * '* Language : PIC BASIC Standard Ver. 1.45 * '* Society : Copyright (c) 2004 grifo(r) ITALIAN Technology * '* : All Rights Reserved * '* : Tel.: +39 051 892052 Fax: +39 051 893661 * '* : http://www.grifo.com http://www.grifo.it * '* : sales@grifo.it tech@grifo.it grifo@grifo.it * '****************************************************************** ' ' The demo is a simple example of communication that can work ' with all the electric protocols availabe on CN2 (RS 232, RS 422, ' RS 485, current loop or TTL). ' In detail, the low level funcions available allow to program the ' baud rate for console. ' Then, when the terminal emulator starts, every character received ' is sent back, and when character 'r' is received the status of the ' signal called DIR, used to drive the direction in RS 422, RS 485, ' current loop, is complemented. ' ' For serial communication, the hardware serial port management code ' taken from example program Usart.bas is used, opportunely ' modified. ' ' ' ********************************************************************* ' * Definitions and Constants * ' ********************************************************************* ' ' PIC registers symbol INDF = 0 symbol FSR = $04 ' I/O Ports symbol PORTA = $05 symbol TRISA = $85 ' bank 1 symbol PORTB = $06 symbol TRISB = $86 ' bank 1 symbol PORTC = $07 symbol TRISC = $87 ' bank 1 symbol ADCON0 = $1F symbol ADCON1 = $9F ' bank 1 ' symbol OPTION_REG = $81 ' banco 1 ' ' Registers used by hardware serial port management taken from ' demo Usart.bas del PIC Basic standard. ' USART registers Symbol PIR1 = $0C ' Peripheral Interrupt Flag register Symbol RCSTA = $18 ' Reception status and control register Symbol TXREG = $19 ' Data to send Symbol RCREG = $1A ' Data received Symbol TXSTA = $98 ' Transmission status and control register Symbol SPBRG = $99 ' Baud Rate Generator register ' ' ' ********************************************************************* ' * Variables used by the program * ' ********************************************************************* ' ' ' Used by USART SYMBOL UsartST = B0 ' USART status SYMBOL UsartOut = B1 ' Character to send symbol UsartIn = B2 ' Character received ' Value to be printed symbol Valore = B3 ' Generic use SYMBOL i = B4 SYMBOL c = B5 symbol z = B6 ' Used to input an hexadecimal value symbol ValoreHex = B7 symbol ValoreHexMSB = B8 ' Used to initialise the Baud Rate Generator symbol ValBRG = B9 ' ' ' ********************************************************************* ' * Main program * ' ********************************************************************* ' Main: gosub Module_init ' Initialisation for i=0 to 37 lookup i,("Demo for GMM 876 and GMB HR84 Rel. 1.1"),usartout gosub charout next i gosub nlcr gosub nlcr for i=0 to 114 lookup i,("Every character received from the serial line is sent back.",13,10,"Character R toggles the status of signal DIR (pin 12)."),usartout gosub charout next i gosub nlcr gosub nlcr ' List of Baud Rates available: '1) 1200 '2) 2400 '3) 9600 '4) 19200 for i=0 to 68 lookup i,("Insert the Baud Rate: ",13,10,"1) 1200",13,10,"2) 2400",13,10,"3) 9600",13,10,"4) 19200",13,10,"Choice: "),usartout gosub charout next i ' Wait for a character between "1" and "4" and prints it Mainloop1: gosub CharIn if usartin < "1" or usartin > "4" then Mainloop1 i = usartin - "0" - 1 usartout = usartin gosub charout gosub nlcr ' Resets bard rate with value input by the user and sets ' the low speed working mode. ' An eventual transmission in progress is stopped. lookup i,(1200,2400,4800,19200),W0 gosub calcolabrg_ls Poke SPBRG,valbrg Poke TXSTA,%00100000 ' gosub nlcr gosub nlcr for i = 0 to 66 lookup i,("For RS 232: set ON switches 1 and 2 and OFF switch 3 and 4 of DSW1."),usartout gosub charout next i gosub nlcr for i = 0 to 110 lookup i,("Setting OFF switches 1 and 2 and ON switches 3 and 4,",13,10,"TTL signals of USART are routed directly to socket pins."),usartout gosub charout next i gosub nlcr gosub nlcr for i = 0 to 128 lookup i,("Connect J7 in 2-3.",13,10, "Test in RS232,then configure GMB for another protocol:",13,10,"turn OFF power supply, set DSW1:",13,10,"1=2=OFF,3=4=ON,then:"),usartout gosub charout next i gosub nlcr gosub nlcr for i = 0 to 78 lookup i,("RS422:",13,10,"-install SN75176 or MAX483 on IC1 and IC2",13,10,"-J2=1-2 J3=1-2 J4=1-2 J5=2-3"),usartout gosub charout next i gosub nlcr gosub nlcr for i = 0 to 70 lookup i,("RS485:",13,10,"-install SN75176 or MAX483 on IC1",13,10,"-J2=1-2 J3=1-2 J4=1-2 J5=1-2"),usartout gosub charout next i gosub nlcr gosub nlcr for i = 0 to 84 lookup i,("If the board is at an end of a network -> J1 connected",13,10,"R toggles transmitter status."),usartout gosub charout next i gosub nlcr gosub NLCR Main_loop: gosub charin if usartin = 0 then main_loop usartout = usartin gosub charout if usartin <> "r" and usartin <> "R" then main_loop ' Commuta lo stato della linea peek portc, i i = i ^ %00000100 poke portc,i goto main_loop end ' ' '********************************************************************* ' * Fine del programma * ' ********************************************************************* ' ' ' Initialisation of GMM 876 Module_init: ' Turn OFF A/D converter poke adcon0, 0 ' Set Port A as digital input poke ADCON1, 7 ' ' Initialise USART for 19200 Baud W0 = 19200 gosub calcolabrg_hs Poke SPBRG,64 ' Set baud rate to 19200 Poke RCSTA,%10010000 ' Enable serial port and continuous reception Poke TXSTA,%00100100 ' Enable transmission, asynchronous mode ' and high baud rate ' Switch back to bank 0 before exiting from asm 'ASM 'ENDASM pause 500 return ' ' ' Determine the value to initialise the Rate Generator in high speed ' mode. ' Desired Baud Rate is in the variabile W0, the result, that is the ' value to write in register SPBRG, is in variable ValBRG. ' Please remark that to enable high speed mode bit 2 of register ' TXSTA (BRGH) must be set. CalcolaBRG_HS: ' Fquartz ' ValBRG = -------- - 1 ' 16 * BR valbrg = 0 if W0 = 9600 then val_9600_hs if W0 = 19200 then val_19200_hs if W0 = 38400 then val_38400_hs if W0 = 57600 then val_57600_hs if W0 = 115200 then val_115200_hs goto esci_hs val_9600_hs: valbrg = 129 goto esci_hs val_19200_hs: valbrg = 64 goto esci_hs val_38400_hs: valbrg = 32 goto esci_hs val_57600_hs: valbrg = 21 goto esci_hs val_115200_hs: valbrg = 10 goto esci_hs esci_hs: return ' ' ' Determine the value to initialise the Rate Generator in low speed ' mode. ' Desired Baud Rate is in the variabile W0, the result, that is the ' value to write in register SPBRG, is in variable ValBRG. ' Please remark that to enable low speed mode bit 2 of register ' TXSTA (BRGH) must be reset. CalcolaBRG_LS: ' Fquartz ' ValBRG = -------- - 1 ' 64 * BR valbrg = 0 if W0 = 1200 then val_1200_ls if W0 = 2400 then val_2400_ls if W0 = 9600 then val_9600_ls if W0 = 19200 then val_19200_ls if W0 = 76800 then val_76800_ls goto esci_hs val_1200_ls: valbrg = 255 goto esci_hs val_2400_ls: valbrg = 129 goto esci_hs val_9600_ls: valbrg = 32 goto esci_hs val_19200_ls: valbrg = 15 goto esci_hs val_76800_ls: valbrg = 3 goto esci_ls esci_ls: return ' ' ' Prints two space characters DueSpazi: usartout = " " ' Space gosub charout gosub charout return ' ' ' Transforms the value of variable Valore in hexadecimal and in ' ASCII characters then sends them to the serial port. ' Variable Valore must be in the range from 0 to 0FFh. StampaHex: usartout = valore / 16 gosub hexdecode gosub charout StampaNibbleHex: usartout = valore & $0F gosub hexdecode gosub charout return ' HexDecode: Lookup usartout,("0123456789ABCDEF"),usartout return ' HexEncode: lookDown usartin,("0123456789abcdef"),usartin return ' ' ' Moves to a new line NLCR: usartout = 10 ' New Line gosub charout CR: usartout = 13 ' Carriage Return gosub charout return ' ' ' Input an hexadecimal value from 000 to FFF then return it into the ' variables ValoreHexMSB and ValoreHex. InputHex3: gosub charin if usartin = 0 then InputHex3 usartout = usartin gosub charout usartin = usartin | $20 gosub hexencode valorehexMSB = usartin InputHex: gosub charin if usartin = 0 then InputHex usartout = usartin gosub charout usartin = usartin | $20 gosub hexencode valorehex = usartin * 16 InputHex2: gosub charin if usartin = 0 then InputHex2 usartout = usartin gosub charout usartin = usartin | $20 gosub hexencode valorehex = valorehex + usartin gosub nlcr return ' ' ' Prints a message that asks for a key to continue ChiediTasto: for i = 0 to 25 lookup i,("Press a key to continue..."),usartout gosub charout next i gosub NLCR return ' ' ' Waits for a character from hardware serial line AttendiTasto: gosub charin if usartin = 0 then AttendiTasto return ' ' ' Sends a character to USART transmitter ' (blocking) CharOut: Peek pir1,UsartST ' Get Flag in UsartST = B0 If Bit4 = 0 Then charout ' Wait for transmission register empty Poke TXREG,Usartout ' Put data in transmission register Return ' Return to caller ' ' ' Get a character from USART receiver ' (non blocking) CharIn: UsartIn = 0 ' Preset to no char received Peek PIR1,Usartst ' Get Flag in UsartST = B0 If Bit5 = 0 Then ciret ' If reception flag is 0, exit Peek RCREG,UsartIn ' Put received characther in UsartIn = B1 ciret: Return