'****************************************************************** '* Name : gmbdae.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 * '****************************************************************** ' ' Il demo utilizza il timer 2 del Mini Modulo per generare, su CN4, ' un segnale PWM di frequenza prestabilita, con duty cycle programmabile in ' percentuale da console. Tale segnale, collegato ad una opportuna circuiteria ' integratrice (rete RC, amplificatore operazionale con capacita' su ramo di ' retroazione, ecc.) permette di ottenere un segnale analogico analogo a quello ' di un D/A. L'esecuzione del demo e' subordinata al Mini Modulo utilizzato e ' alla configurazione della linea seriale. ' ' 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 OPTION_REG = $81 ' bank 1 ' symbol PIE1 = $8C ' 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 ' ' Demo delle sezioni PWM (CCP) DemoPWM: for i=0 to 80 lookup i,("Demo PWM",13,10,"CCP1 section is managed, output is pin 6 of CN4, frequency is 1.22 kHz."),usartout gosub charout next i gosub nlcr gosub nlcr ' Here section CCP1 is managed, section CCP2 is mostly the same. ' Please refer to data sheet. ' Set pin 24 of GMM 876 (RC2) as output peek trisc, B0 bit2 = 0 poke trisc,B0 ' Enable section CCP 1 in PWM mode poke ccp1con, $0C ' Set 1.22 kHz of period and enable Timer 2 poke pr2, $FF peek t2con, B0 bit2 = 1 bit1 = 1 poke t2con, B0 DemoPWMLoop: for i=0 to 38 lookup i,("Insert duty cycle (000-3FF, 000 exit): "),usartout gosub charout next i valorehex = 0 gosub inputhex3 ' Set 10 bit of period of duty cycle: ' first set the two least significant bits... B0 = valorehex peek ccp1con, B1 bit12 = bit0 bit13 = bit1 poke ccp1con, B1 '...then set the remaining 8 bits B0 = B0 / 4 B1 = valorehexmsb bit6 = bit8 bit7 = bit9 poke ccpr1l, B0 if valorehexmsb <> 0 or valorehex <> 0 then demopwmloop ' Disable section CCP1 poke ccp1con, 0 ' Stop Timer 2 peek t2con, B1 bit2 = 0 poke t2con, B1 goto demopwm 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