' **************************************************************** ' * File: uk_gmb_ser.pbas - Ver. 1.1 * ' * Compiler: mikroBasic for PIC by mikroElektronica * ' * IDE: mikroBasic for PIC by mikroElektronica * ' * Compiler Version: 1.1.5.0 * ' * Boards: GMM 4620 + GMB HR168 * ' * GRIFO(R) via Dell'Artigiano 8/6 * ' * 40016 S. Giorgio di Piano (BO) * ' * Tel. +39 051 892052 Fax. +39 051 893661 * ' * http://www.grifo.com http://www.grifo.it * ' * by Graziano Gaiba 22.02.05 * ' **************************************************************** ' ' This demo is a simple example of communication capableto work ' with all electric protocols available on CN2 (RS 232, RS 422, ' RS 485, current loop or TTL). ' In detail, through high level functions, it is possible to ' program baud rate from console, then each character received ' from serial port is sent to the port itself; reception of ' character 'r' decides how to manage the communication direction ' (signal DIR) for RS 422 and RS 485. ' ' Rel 1.1 - by Graziano Gaiba ' Demo program for low level EUSART usage with GMB HR168 driven ' by Mini Module GMM 4620 ' ' ' ******************** Compiler definitions ********************** ' program uk_gmb_ser ' ' ******************* Constants declaration ********************** ' const ASC_r = 114 const ASC_rU = 82 ' ' ******************** Variables declaration ********************* ' ' Generic variables dim d_in as byte ' ' ' **************** Procedures definition ****************** ' ' ' CPU and signals directions initialization sub procedure Init_cpu ADCON1=$0f ' Imposta come I/O digitale i pin AN0..12 CMCON=$07 ' Imposta come I/O digitale RA0..4 ' Optocoupled Inputs of CN1 are: ' IN1-1 <-> RA0 ' IN2-1 <-> RA1 ' IN3-1 <-> RB0 ' IN4-1 <-> RB1 ' IN5-1 <-> RA4 ' IN6-1 <-> RC0 ' IN7-1 <-> RC1 ' IN8-1 <-> RC5 trisa.0 = 1 trisa.1 = 1 trisa.4 = 1 trisb.0 = 1 trisb.1 = 1 trisc.0 = 1 trisc.1 = 1 trisc.5 = 1 ' Optocoupled Inputs of CN2 are: ' IN1-2 <-> RD0 ' IN2-2 <-> RD1 ' IN3-2 <-> RD2 ' IN4-2 <-> RD3 ' IN5-2 <-> RD4 ' IN6-2 <-> RD5 ' IN7-2 <-> RD6 ' IN8-2 <-> RD7 trisd = $ff ' Relay outputs of CN3 are: ' OUT A1 <-> RB4 ' OUT A2 <-> RB5 ' OUT B1 <-> RB6 ' OUT B2 <-> RB7 ' OUT C1 <-> RB3 ' OUT C2 <-> RB2 trisb = trisb and $03 ' Relay outputs of CN4 are: ' OUT D1 <-> RA3 ' OUT D2 <-> RC2 (J10 in position 3-4) trisa.3 = 0 trisc.2 = 0 end sub ' ' ' Send a string of characters to the serial port sub procedure print_USART(dim byref txt as char[255]) dim i as byte dim l as byte l = txt[0] for i = 1 to l USART_Write(txt[i]) next i end sub ' ' ' Send CR + LF sub procedure print_CRLF USART_Write(10) USART_Write(13) end sub ' ' ' Clear screen, sending 25 times CR + LF. sub procedure clrscr dim i as byte for i = 0 to 24 print_CRLF next i end sub ' ' ' Asks for a key press sub procedure wait_key dim c as byte print_usart("Press a key...") do nop loop until USART_Data_Ready = 1 c = USART_Read end sub ' ' ' Procedure to set the status of the relays on connectors CN3 and CN4. ' According to the bits of port_val, each relay is turned ON ' (contact closed) or OFF (contact open). ' Bits of port_val have this meanging: ' -- CN3 ' port_val.0 drives relay OUT A1 ' port_val.1 drives relay OUT A2 ' port_val.2 drives relay OUT B1 ' port_val.3 drives relay OUT B2 ' port_val.4 drives relay OUT C1 ' port_val.5 drives relay OUT C2 ' -- CN4 ' port_val.6 drives relay OUT D1 ' port_val.7 drives relay OUT D2 ' ' Each bit has the following meaning: ' bit Meaning ' 0 Relay turned OFF (contact open) ' 1 Relay turned ON (contact closed) sub procedure set_relays(dim port_val as byte) ' Relays are driven in complemented logic, so port_val must be ' complemented too port_val=port_val xor $ff portb.4=port_val.0 portb.5=port_val.1 portb.6=port_val.2 portb.7=port_val.3 portb.3=port_val.4 portb.2=port_val.5 porta.3=port_val.6 portc.2=port_val.7 end sub ' ' ' Transmission of a signle character using low level EUSART manipulation. ' Char to transmit must be in variable . ' It blocks until transmitters is free. sub procedure tx_char(dim char_to_tx as byte) ' Wait for transmitter available do nop loop until txsta.1 = 1 txreg = char_to_tx end sub ' ' ' Check if a character has been received. ' If a character reception has been completed, returns 1, ' otherwise 0. sub function rx_status as byte dim kbhit as byte if pir1.5 = 1 then pir1.5 = 0 kbhit = 1 else kbhit = 0 end if Result = kbhit end sub ' ' ' Reception of a signle character using low level EUSART manipulation. ' Does NOT check if a character reception has been completed. sub function rx_char as byte Result = rcreg end sub ' '************************** Main Program ******************************* ' main: Init_cpu ' Turn OFF relays set_relays(0) ' We need low level communication because DIR signal is not managed by ' PIC BASIC PRO high level instructions ' Disable auto baud-rate detection, 8 bit baud-rate generator baudctl = 0 ' Value for baud rate 19200 and above settings ' Fosc ' SPBRG = -------------- - 1 ' 16 * BaudRate spbrg = 31 ' High baud rate, Asynchronous mode, Enable transmitter, 8 bit transmission txsta = $24 ' Enable receiver, 8 bit reception, Enable serial port rcsta = $90 clrscr print_USART("Low level EUSART demo Rel 1.1 for GMM 4620 rel 120304 and GMB HR168 rel 110104") print_CRLF print_CRLF print_USART("This demo shows how to configure the boards to use RS 232, TTL, RS 422, RS 485") print_CRLF print_USART("serial communication.") print_CRLF print_USART("Following the instructions allows to test RS 232 communication at low level,") print_CRLF print_USART("that means controlled by assembly instructions rather than BASIC commands.") print_CRLF print_USART("Next, RS 422 or RS 485 is configured and used. Also a direction signal") print_CRLF print_USART("is used in case of RS 485.") print_CRLF wait_key print_CRLF print_CRLF print_USART("In RS 232: on GMM 4620 set ON switches from 1 to 3, set OFF all others,") print_CRLF print_USART("EUSART management is made by PIC BASIC PRO, see documentation for more info.") print_CRLF print_USART("Set OFF switches 2 and 3, set ON switches 4 and 5, to have UART TTL signals") print_CRLF print_USART("directly to the socket pins.") print_CRLF print_USART("Connect J10 in 1-2.") print_CRLF wait_key print_CRLF print_CRLF print_USART("Test in RS232, then configure GMB for other protocol:") print_CRLF print_USART("turn off power supply, set DSW1:") print_CRLF print_USART("2=3=OFF,4=5=ON, then:") print_CRLF print_CRLF print_CRLF print_USART("RS422:") print_CRLF print_USART("-install SN75176 or MAX483 on IC10 and IC11") print_CRLF print_USART("J5=2-3 J6,J7,J8=1-2 J10=1-2") print_CRLF print_CRLF print_CRLF print_USART("RS485:") print_CRLF print_USART("-install SN75176 or MAX483 on IC10") print_CRLF print_USART("-J5=1-2 J6,J7,J8=1-2 J10=1-2") print_CRLF print_CRLF print_CRLF print_USART("If at net termination->J3 and J4 connected") print_CRLF print_USART("R switches transmitter") print_CRLF ' DIR signal for RS 485 is PORTE.2 trise.2 = 0 porte.2 = 0 for_ever: if rx_status = 1 then d_in = rx_char if (d_in = ASC_r) or (d_in = ASC_rU) then if porte.2 = 0 then porte.2 = 1 else porte.2 = 0 end if end if tx_char(d_in) end if goto for_ever end.