'****************************************************************** '* Name : gmbiote.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 * '****************************************************************** ' ' This demo allows to use immediatly the TTL I/O digital signals ' available on connector CN4. A list of available signals is shown ' in start menu. ' After a signal has been selected its status can be set (high or low) ' or shown on the console. ' ' For serial communication, the hardware serial port management code ' taken from example program Usart.bas is used, opportunely ' modified. ' ' ' ********************************************************************* ' * Definizioni e Costanti * ' ********************************************************************* ' ' 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 ' Used to select pin and action symbol Pin_scelto = B10 symbol Port_scelto = B11 symbol Azione = B12 ' ' ' ********************************************************************* ' * Main program * ' ********************************************************************* ' Main: gosub Module_init ' Initialisation ' Opens all relays B0 = 0 gosub set_relays 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 ' Selection of pin that can be used as I/O TTL ' 1) Pin 2 ' 2) Pin 3 ' 3) Pin 5 ' 4) Pin 6 ' 5) Pin 8 for i=0 to 95 lookup i,("These pins of CN4 feature I/O TTL:",13,10,"1) Pin 2",13,10,"2) Pin 3",13,10,"3) Pin 5",13,10,"4) Pin 6",13,10,"5) Pin 8",13,10,13,10,"Choice: "),usartout gosub charout next i ' Waits for a character between "1" and "5" then prints it Mainloop1: gosub CharIn if usartin < "1" or usartin > "5" then Mainloop1 pin_scelto = usartin - "0" - 1 usartout = usartin gosub charout gosub nlcr lookup pin_scelto,(PORTA, PORTB, PORTB, PORTC, PORTA),port_scelto if pin_scelto <> 4 then main_continua for i=0 to 121 lookup i,("A pull-down resistor and a voltage divider are always on this pin.",13,10,"Please refer to manual GMB HR84 for further information."),usartout gosub charout next i gosub nlcr gosub nlcr Main_continua: for i=0 to 30 lookup i,("1) Input",13,10,"2) Output",13,10,13,10,"Choice: "),usartout gosub charout next i ' Waits for a character between "1" and "2" then prints it Mainloop2: gosub CharIn if usartin < "1" or usartin > "2" then Mainloop2 azione = usartin - "0" - 1 usartout = usartin gosub charout gosub nlcr gosub nlcr branch azione,(Demo_in, Demo_out) goto main end ' ' ' ********************************************************************* ' * Program End * ' ********************************************************************* ' ' ' 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 ' ' ' Demo to use TTL signals of CN4 as inputs Demo_in: ' Sets as inputs the signals RA3, RA5, RB2, RB3 and RC2 peek trisa, B0 bit3 = 1 bit5 = 1 poke trisa, B0 ' peek trisb, B0 bit2 = 1 bit3 = 1 poke trisb, B0 ' peek trisc, B0 bit2 = 1 poke trisc, B0 ' for i=0 to 49 lookup i,("Status of digital input is continuously visualized"),usartout gosub charout next i gosub nlcr gosub chieditasto gosub nlcr Demo_in_loop: for i=0 to 7 lookup i,("Status: "),usartout gosub charout next i ' Gets the whole port of the selected TTL input B0 = 0 peek port_scelto, B1 ' Masks the bit of the selected signals and puts it in B0 branch pin_scelto, (isola_b3,isola_b3,isola_b2,isola_b2) ' Masks bit 5 (selected pin = pin 8 which means RA5) bit0 = bit13 goto stampa_stato Isola_b2: ' Masks bit 2 (selected pin = pin 5 or 6 which means RB2 or RC2) bit0 = bit10 goto stampa_stato Isola_b3: ' Masks bit 3 (selected pin = pin 2 or 3 which means RA3 or RB3) bit0 = bit11 Stampa_stato: ' Prints value got in hexadecimal valore = B0 gosub stampanibblehex ' move to a new line gosub cr pause 300 ' Check for a key pressed gosub charin ' If a key has been pressed, loop if usartin = 0 then demo_in_loop ' Exit gosub nlcr gosub nlcr goto main ' ' ' Demo to use TTL signals of CN4 as oututs Demo_out: ' Sets as outputs the signals RA3, RA5, RB2, RB3 and RC2 peek trisa, B0 bit3 = 0 bit5 = 0 poke trisa, B0 ' peek trisb, B0 bit2 = 0 bit3 = 0 poke trisb, B0 ' peek trisc, B0 bit2 = 0 poke trisc, B0 ' for i=0 to 58 lookup i,("Status of selected signal is set (0 or 1), press 2 to exit."),usartout gosub charout next i gosub nlcr gosub nlcr Demo_out_loop: for i=0 to 7 lookup i,("Status: "),usartout gosub charout next i ' Reads the value input by the user valorehex = 0 gosub inputhex2 if valorehex > 1 then demo_esci B0 = valorehex ' Puts selected value in the bit corresponding to selected pin branch pin_scelto, (poni_b3,poni_b3,poni_b2,poni_b2) ' Put value in bit 5 (selected pin = pin 8 which means RA5) peek port_scelto, B1 bit13 = bit0 goto imposta_stato Poni_b2: ' Put value in bit 2 (selected pin = pin 5 or 6 which means RB2 or RC2) peek port_scelto, B1 bit10 = bit0 goto imposta_stato Poni_b3: ' Put value in bit 3 (pin scelto = pin 2 or 3 which means RA3 or RB3) peek port_scelto, B1 bit11 = bit0 Imposta_stato: ' Set status of pin selected by the user poke port_scelto, B1 ' Loop goto demo_out_loop ' Exit Demo_esci: gosub nlcr gosub nlcr goto main ' ' ' This procedure sets the status of the relays on a GMB HR84. ' Correspondance between bit of parameter, I/O signal and relay is: ' Bit 0 -> RB4 -> A1 ' Bit 1 -> RB5 -> A2 ' Bit 2 -> RB6 -> B1 ' Bit 3 -> RB7 -> B2 ' Input parameter is variable B0, also content of B1 is distroyed. ' For each signal, the respective bit has the following meaning: ' bit a 0 -> Relay open ' bit a 1 -> Relay closed Set_relays: B0 = B0 ^ $0F peek portb, B1 bit12 = bit0 bit13 = bit1 bit14 = bit2 bit15 = bit3 poke portb, B1 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