Introduction
GCBASIC support programs to communicate easily using RS232.
GCBASIC included microcontroller hardware-based serial routines are intended for use on microcontrollers with built in serial communications modules - normally referred to in datasheets as USART or UART modules. Check the microcontroller data sheet for the defined transmit and receive (TX/Rx) pins. Make sure your program sets the Tx pin direction to Out and the Rx pin direction to In respectively. If the RS232 lines are connected elsewhere, or the microcontroller has no USART module, then the GCBASIC software based RS232 routines must be used.
Initialization of the USART module is handled automatically from your program by defining the chip, speed, and the baudrate. The baudrate generator values are calculated and set, usart is set to asynchronous, usart is enabled , the receive and transmit are enabled. See the table below.
Example:
#chip mega328p, 16 #define USART_BAUD_RATE 9600 #define USART_TX_BLOCKING
Command Availability:
Available on all microcontrollers with a USART or UART module.
Microchip PIC supports USART1 and 2.
Atmel AVR supports USART 1,2,3 and 4.
The following table explains the methods that can be implemented when using the GCBASIC serial routines.
Commands:
Command | Parameters | Example |
---|---|---|
Serially print numbers (byte, word, long) or strings. |
||
|
Number_constant or number_variable or string [,optional usart address] The optional usart address is microcontroller specific buy can be 1, 2, 3 or 4. |
This subroutine prints a variable value to usart 1. No additional parameter for the usart number is used.
To print a variable value to usart 2. Note the additional parameter for the usart address.
|
Serially receive ascii number characters and assign to a word variable. |
||
|
Number_variable [,optional usart address] The optional usart address is microcontroller specific buy can be 1, 2, 3 or 4. |
This subroutine ensures that the characters received are numbers. When a carriage return (CR or ASCII code 13) is received this signifies the end of the character stream. Defaults to usart1. To receive number characters use.
To receive number characters via usart2 use.
|
Serially receive characters as a string. |
||
|
User_string_variable [,optional usart address] The optional usart address is microcontroller specific buy can be 1, 2, 3 or 4. |
This subroutine ensures that the characters treated as a string. When a carriage return (CR or ASCII code 13) is received this signifies the end of the character stream. GCBASIC will determine the default buffering size for strings. See here for more help on string sizes. Defaults to usart1. To receive a string use. To a string via usart2 use. |
Serially receive a character using a subroutine. |
||
|
byte_variable |
This subroutine handles the incoming characters as raw ASCII values. The subroutine receives a single byte value in the range of 0 to 255. The subroutine can receive a byte from usart 1, 2, 3 or 4. The public variable comport can be set before the use of this method to select the desired usart address. If ‘#define USART_BLOCKING’ is defined then this methods will wait until it a byte is received. If ‘#define USART_BLOCKING’ is NOT defined then the method will returns ASCII value received or the method will return the value of 255 to indicate not ASCII data was received. You can change the value returned by setting redefining ‘#define DefaultUsartReturnValue = [0-255]’. When ‘#define USART_BLOCKING’ is NOT defined this method becomes a non- blocking method which allows for the testing and handling of incoming ASCII data within the user program. To receive an ASCII byte value in blocking mode use. Defaults to usart1 #define USART_BLOCKING To receive an ASCII byte value via usart 3 using blocking mode use #define USART_BLOCKING To receive an ASCII byte value use in non-blocking mode use. Ensure #define USART_BLOCKING is NOT defined. This method fefaults to usart1
To receive an ASCII byte value via usart 4 using non-blocking mode use. Ensure #define USART_BLOCKING is NOT defined. Comport = 4 |
Serially receive a character using a function specifically via usart1. |
||
|
none |
This function handles the incoming characters as raw ASCII values. The function receives a single byte value in the range of 0 to 255. The function can return only a byte value from usart 1. The blocking and non-blocking mode and the methods are the same as shown in the previous method. To receive an ASCII byte value via usart 1 using blocking mode use #define USART_BLOCKING To receive an ASCII byte value use in non-blocking mode use. Ensure #define USART_BLOCKING is NOT defined. user_number_variable = |
Serially receive a character using a function specifically via usart2 |
||
|
none |
This function handles the incoming characters as raw ASCII values. The function receives a single byte value in the range of 0 to 255. The function can receive only a byte value from usart 2. The blocking and non-blocking mode and the methods are the same as shown in the previous method. To receive an ASCII byte value via usart 2 using blocking mode use #define USART_BLOCKING To receive an ASCII byte value use in non-blocking mode use. Ensure #define USART_BLOCKING is NOT defined. user_byte_variable = |
Serially receive a character using a function from either usart ports using a parameter to select the usart. |
||
|
Usart_number, |
This function handles the incoming characters as raw ASCII values. The function return a single byte value in the range of 0 to 255. The function can receive only a byte value from usart 1 and usart 2 The blocking and non-blocking mode and the methods are the same as shown in the previous method. To receive an ASCII byte value via usart 1 using blocking mode use #define USART_BLOCKING To receive an ASCII byte value use in non-blocking mode use. Ensure #define USART_BLOCKING is NOT defined. 'Chosen_usart = 2 |
Serially send a byte using any of the usart ports. |
||
|
Byte or byte_variable [,optional usart address] + The optional usart address is microcontroller specific buy can be 1, 2, 3 or 4. |
This subroutine sends a byte value to usart 1. No additional parameter for the usart number is used.
To print a variable value to usart 2. Note the additional parameter for the usart address.
|
Serially send a byte and a CR&LF using any of the usart ports |
||
|
Byte or byte_variable + [,optional usart address] The optional usart address is microcontroller specific buy can be 1, 2, 3 or 4. |
This subroutine sends a byte value to usart 1.
|
Serially send CR&LF (can be multiple) using any of the usart ports |
||
|
Number of CR&LF to be sent + [,optional usart address] The optional usart address is microcontroller specific buy can be 1, 2, 3 or 4. |
This subroutine sends a CR&LF to port 2.
|
Constants These constants affect the operation of the hardware RS232 routines:
Constant Name | Controls | Default Value |
---|---|---|
|
Baud rate (in bps) for the routines to operate at. |
No default, user must enter a baud. Doesn’t have to be a standard baud. |
|
If defined, this constant will cause the USART routines to wait until data can be sent or received. |
No parameter needed. Use “#defining” it implement the action. |
|
If defined, this constant will cause the Transmit USART routines to wait until Transmit register is empty before writing the next byte which prevents over running the register and losing data. |
No parameter needed. Use “#defining” it implement the action. |
|
Baud rate (in bps) for the routines to operate at. |
No default, user must enter a baud. Doesn’t have to be a standard baud. |
|
If defined, this constant will cause the USART routines to wait until data can be sent or received. |
No parameter needed. Use “#defining” it implement the action. |
|
If defined, this constant will cause the Transmit USART routines to wait until Transmit register is empty before writing the next byte which prevents over running the register and losing data. |
No parameter needed. Use “#defining” it implement the action. |
|
Baud rate (in bps) for the routines to operate at. |
No default, user must enter a baud. Doesn’t have to be a standard baud. |
|
If defined, this constant will cause the USART routines to wait until data can be sent or received. |
No parameter needed. Use “#defining” it implement the action. |
|
If defined, this constant will cause the Transmit USART routines to wait until Transmit register is empty before writing the next byte which prevents over running the register and losing data. |
No parameter needed. Use “#defining” it implement the action. |
|
Baud rate (in bps) for the routines to operate at. |
No default, user must enter a baud. Doesn’t have to be a standard baud. |
|
If defined, this constant will cause the USART routines to wait until data can be sent or received. |
No parameter needed. Use “#defining” it implement the action. |
|
If defined, this constant will cause the Transmit USART routines to wait until Transmit register is empty before writing the next byte which prevents over running the register and losing data. |
No parameter needed. Use “#defining” it implement the action.
|
|
This is the delay between characters. |
To disable this delay between characters … Use #define |
|
If defined, this constant will cause the RX USART routines ( all USARTs) cease waiting after a specific timeout. PIC only. |
|
|
Instruct the compiler to show the real BPS to be used |
Not the default operation |
|
Instruct the compiler to show BPS calculation errors |
Not the default operation |
|
Causes a Carriage return to be sent after every HserPrint automatically. |
No parameter needed. User “#defining” it implements the action |
|
Causes a LineFeed to be sent after every HserPrint. Some communications require both CR and LF |
No parameter needed. User “#defining” it implements the action |