'****************************************************************** '* Name : gmbade.bas * '* Author : Graziano GAIBA * '* Date : 15/05/04 * '* Versione : 1.1 * '* Board : GMM 876 * '* 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 shows continuously on the console the combination ' read from analog inputs available on pins 2 and 8 of CN4, ' which means AN3 ed AN4. ' Voltage reference is the internal reference (V+ = Vdd, V- = Vss), ' to set the corresponding full range connect J6 in 2-3. ' ' 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 ' A/D converter symbol ADCON0 = $1F symbol ADCON1 = $9F ' bank 1 symbol ADRESH = $1E symbol ADRESL = $9E ' 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 ' Used by A/D conversion procedure symbol Canale_AD = B10 ' ' ' ********************************************************************* ' * Main program * ' ********************************************************************* ' Main: gosub Module_init ' Initialisation ' AOpen all the 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 for i=0 to 193 lookup i,("Available analog inputs are pins 2 and 8 of CN4, set J6 in 1-2.",13,10,"Full range is 5 V, pin 8 features a pull-down and a voltage divider.",13,10,"Please refer to manual of GMB HR84 for further information."),usartout gosub charout next i gosub nlcr gosub nlcr for i=0 to 9 lookup i,(" AN3 AN4"),usartout gosub charout next i gosub nlcr Main_loop: ' Read analog input AN3 canale_ad = 3 gosub ad_conv ' Combination read in W0 ' Print combination result valore = B1 i = B0 ' Saves least significant byte of combination gosub stampahex valore = i gosub stampahex ' gosub duespazi ' ' Read analog input AN4 canale_ad = 4 gosub ad_conv ' Combination read in W0 ' Print combination result valore = B1 i = B0 ' Saves least significant byte of combination gosub stampahex valore = i gosub stampahex ' gosub cr pause 300 goto main_loop end ' ' ' ********************************************************************* ' * Program end * ' ********************************************************************* ' ' ' Initialisation of GMM 876 Module_init: ' ' Initialisation for A/D converter ' Configures as inputs all the signals of port A poke trisa, $FF ' Result of A/D conversion right justified, Vref+ <= Vdd, ' Vref- <= GND. poke adcon1, $C0 ' Turn ON A/D converter ' Conversion duration = 64*Tosc poke adcon0, $81 ' ' Initialise USART for 19200 Baud W0 = 19200 gosub calcolabrg_hs Poke SPBRG,valbrg ' Imposta baud rate a 19200 Poke RCSTA,%10010000 ' Abilita porta seriale e ricezione continua Poke TXSTA,%00100100 ' Abilita tramsissione, modalita' asincrona e ' baud rate elevati ' Switch back to bank 0 before exiting from asm 'ASM 'ENDASM pause 500 return ' ' ' This procedure read the value of analog voltage at A/D converter ' input whose number (from 0 to 4) is indicated in the input parameter ' Canale_AD. ' Returns 10 bit of conversion in the variable W0. AD_Conv: ' Selects i-th channel with Tad = 64*Tclock lookup canale_ad, ($81, $89, $91, $99, $A1), canale_ad poke adcon0, canale_ad ' Acquisition time (about 20 microseconds), then ' starts the conversion pause 1 canale_ad = canale_ad | $04 poke adcon0, canale_ad ' Wait for the conversion to complete AD_Conv_loop: peek adcon0, B0 if bit2 = 1 then AD_Conv_loop ' Reads the result and stores it in W0 peek adresh, B1 peek adresl, B0 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 ' ' ' 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