'****************************************************************** '* Name : gmbiob.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 program allows to se immediatly both the relay outputs ' and the optocoupled inputs available respectively on connnectors ' CN1 and CN6 of GMB HR84. ' It is possible to see the status of the eight NPN/PNP inputs on ' the console or to set the status of the four output relays. ' In addition the Mini Module peripherals can perform developed ' features like: hardware counted inputs, etc. ' ' 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 ' ' Timer and CCP registers symbol TMR0 = $01 symbol TMR1L = $0E symbol TMR1H = $0F symbol T1CON = $10 symbol TMR2 = $11 symbol PR2 = $92 ' bank 1 symbol T2CON = $12 ' symbol CCPR1L = $15 symbol CCPR1H = $16 symbol CCP1CON = $17 symbol CCPR2L = $1B symbol CCPR2H = $1C symbol CCP2CON = $1D ' symbol PIE1 = $8C ' bank 1 ' symbol OPTION_REG = $81 ' bank 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 ' 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 ' Choice of possible operations ' 1) Digial inputs ' 2) Relays outputs for i=0 to 103 lookup i,("Example of how to use inputs on CN6 and outputs on CN1",13,10,"1) Digital inputs",13,10,"2) Relays outputs",13,10,"Choice: "),usartout gosub charout next i ' Waits for a character between "1" and "2" then prints it Mainloop: gosub CharIn if usartin < "1" or usartin > "2" then Mainloop i = usartin - "0" - 1 usartout = usartin gosub charout gosub nlcr gosub nlcr branch i,(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 ' Set signals RB4..RB7 as outputs peek trisb, B0 bit4 = 0 bit5 = 0 bit6 = 0 bit7 = 0 poke trisb, B0 ' Set signals RA0,RA1,RB0,RB1,RA4,RC0,RC1 and RC5 as inputs peek trisa, B1 bit8 = 1 bit9 = 1 bit12 = 1 poke trisa, B1 ' peek trisc, B1 bit8 = 1 bit9 = 1 bit13 = 1 poke trisc, B1 ' peek trisb, B1 bit8 = 1 bit9 = 1 poke trisb, B1 ' ' 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 optocoupled inputs as digital inputs and digital inputs ' counted by hardware Demo_in: for i=0 to 54 lookup i,("Status of optocoupled inputs is continuously visualized"),usartout gosub charout next i gosub nlcr gosub chieditasto gosub nlcr Demo_in_loop: for i=0 to 6 lookup i,("Input: "),usartout gosub charout next i ' Get optocoupled inputs status gosub get_opto ' Prints the value got in hexadecimal valore = B0 gosub stampahex ' Move to a new line gosub cr pause 300 ' Check if a key is pressed gosub charin ' If no key pressed, loop if usartin = 0 then demo_in_loop ' Exit gosub nlcr gosub nlcr ' ' Optocoupled inputs 5 and 6 can be counted by hardware counters ' respectively using Timer 0 and 1. for i = 0 to 54 lookup i,("Count of optocoupled inputs 5 and 6 using Timer 0 and 1"), usartout gosub charout next i gosub nlcr gosub chieditasto for i=0 to 9 lookup i,("IN 5 IN 6"),usartout gosub charout next i gosub nlcr ' Reset counters poke tmr0, 0 poke tmr1l, 0 poke tmr1h, 0 ' Configures Timer 0 as counter peek option_reg, B0 bit7 = 0 ' Enables pull-up on port B bit5 = 1 ' Count transictions on pin 17 bit4 = 1 ' Count transictions from high level to low level bit3 = 1 ' No prescaler (assigned to WDT) poke option_reg, B0 ' Configuration of Timer 1 as counter peek t1con, B0 bit3 = 0 ' Count transictions on pin 16 bit2 = 1 ' Asynchronous count bit1 = 1 ' Count transictions from low level to high level on pin 16 bit0 = 1 ' Enable Timer 1 poke t1con, B0 Demo_in_CountLoop: gosub duespazi peek tmr0, valore gosub stampahex gosub duespazi peek tmr1h, valore gosub stampahex peek tmr1l, valore gosub stampahex gosub cr pause 300 gosub charin if usartin = 0 then Demo_in_CountLoop ' Stop Timer 1 peek t1con, B0 bit0 = 0 ' Disable Timer 1 poke t1con, B0 gosub nlcr goto main ' ' ' Demo to use relays outputs Demo_out: for i = 0 to 22 lookup i,("Data (Hex), 0 to exit: "), usartout gosub charout next i valorehex = 0 gosub inputhex2 ' Set relays status with data B0 = valorehex gosub set_relays if valorehex <> 0 then demo_out goto main ' ' ' 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 ' ' ' 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 ' ' ' This procedure sets the status of the relays of a GMB HR84. ' Correspondance between bit of parameter, I/O signal and optocoupled input is: ' Bit 0 -> RA0 -> Opto Input 1 ' Bit 1 -> RA1 -> Opto Input 2 ' Bit 2 -> RB0 -> Opto Input 3 ' Bit 3 -> RB1 -> Opto Input 4 ' Bit 4 -> RA4 -> Opto Input 5 ' Bit 5 -> RC0 -> Opto Input 6 ' Bit 6 -> RC1 -> Opto Input 7 ' Bit 7 -> RC5 -> Opto Input 8 ' Input parameter is variable B0, also content of B1 is distroyed. ' For each signal, the respective bit has the following meaning: ' bit a 0 -> Input contact closed ' bit a 1 -> Input contact open Get_opto: peek porta, B1 bit0 = bit8 bit1 = bit9 bit4 = bit12 ' peek portb, B1 bit2 = bit8 bit3 = bit9 ' peek portc, B1 bit5 = bit8 bit6 = bit9 bit7 = bit13 B0 = B0 ^ $FF 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