Setting The Baud Rate
Scamp UART baud rates are controlled by baud rate generators in the processor. A 16-bit unsigned value, BRG, generates the appropriate baud rate based on the following equation:
Baud = FCY / (4 * (BRG + 1))
where FCY is the oscillator frequency of the PIC24 processor (in MHz). For the PIC24 processors used in the Scamps, FCY is 16 MHz. Since BRG is an 16-bit integer, there may be a small error between the desired baud rate and the actual baud rate. (This is common to all serial communication devices.) Generally, UART-based systems are very tolerant of small variations in baud rate.
The word baud will calculate the BRG value from a desired baud rate. Baud rates can range from 61 bps to 4,000,000 bps. Since this range exceeds a 16-bit number, the baud rate must be specified as a double number (appended with a dot). To calculate the BRG for 9600 bps:
The word baud will calculate the BRG value from a desired baud rate. Baud rates can range from 61 bps to 4,000,000 bps. Since this range exceeds a 16-bit number, the baud rate must be specified as a double number (appended with a dot). To calculate the BRG for 9600 bps:
9600. baud
will give a BRG value of 415 on the stack. To calculate the BRG value for the maximum rate of 4 Mbps:
4000000. baud
will give a BRG value of 0 on the stack.
The word brg will take a BRG value and work out the actual baud rate used. It will return a double to the stack representing the real baud rate generated by the BRG value. For example,
The word brg will take a BRG value and work out the actual baud rate used. It will return a double to the stack representing the real baud rate generated by the BRG value. For example,
9600. baud brg d.
will calculate that the actual baud rate is 9615 bps, which has an acceptable error of only 0.16%.
The word u1baud places the address of UART1's baud rate register on the stack. To set the baud rate, for UART1, enter the desired baud rate as a double, then call the word baud to convert it to an appropriate setting value. u1baud places the address of the register on the stack and ! (store) writes the baud setting to that register.
The word u1baud places the address of UART1's baud rate register on the stack. To set the baud rate, for UART1, enter the desired baud rate as a double, then call the word baud to convert it to an appropriate setting value. u1baud places the address of the register on the stack and ! (store) writes the baud setting to that register.
115200. baud u1baud !
To recall the current setting baud setting for UART1, and output it as a meaningful baud rate:
u1baud @ brg d.
For UART2, use u2baud in the same way.
Mapping UARTs to Pins (Scamp1 and Scamp2)
On a Scamp1 or Scamp2 (only) the UARTs must be mapped to a pin on the connector. This page shows you how to do it.
On the Scamp2e, Scamp3 and Scamp3e, the UARTs are already mapped to dedicated pins. Pin allocation is not required.
On the Scamp2e only, UART2 is shared between the RS485 interface and the main connector. It is mapped to the RS485 interface by default. The word u2>ser maps UART2 to the main connector. Similarly, the word u2>485 maps UART2 back to the RS485 interface. Note this only applies to the Scamp2e and only to UART2. UART1 is always available on the Scamp2e main connector.
Sending and Receiving data with the UARTs
Chars can be sent and received from UART1 with the words tx1 and rx1. For example, to receive a char with UART1 and leave it on the stack:
rx1
Similarly, to transmit a char (in this case, an asterisk, ASCII 42) with UART1:
42 tx1
The word rx1? checks to see if a char has been received and returns the number of chars waiting. tx1? checks if UART1's transmitter is free or transmitting.
For UART2, use rx2, rx2?, tx2? and tx2. The USB console has corresponding words rxu, rxu? and txu.
Testing your UART Connection
Sometimes it's a useful test to confirm that your serial comms are working as expected. Wire the transmitter directly to the receiver (in this case Tx1 to Rx1 on the same Scamp), so that whatever is sent is also received by the same UART. This is known as a loopback test.
: loopback
begin
$41 tx1 \ send an 'A' via UART1
rx1 emit \ read from UART1 and print out the char
key? \ repeat until the user types a key
until
;
If you see a stream of the character A on the console, your UART is working as it should.
Using Your Scamp as a Terminal
Sometimes its useful to be able to interactively talk to another embedded system over its serial link during development. You might be interacting with an Arduino, or talking to a serial modem, such as a LoRa module.
The following word definition lets your Scamp act as a terminal, letting you interact with an external serial system:
The following word definition lets your Scamp act as a terminal, letting you interact with an external serial system:
: term ( -- , uses UART1 )
cr
2 fg \ green text, optional
begin
rx1? if \ has something come in?
rx1 emit \ then display it locally
then
key? if \ local user typing
key \ get the char
dup emit \ echo it locally
dup tx1 \ send it
dup $d = if \ return key?
$a emit \ print a new line as well
then
$1b = if \ ESC to quit
cr
plain \ plain text
exit
then
then
again
;
Redirecting the Forth Console
By default, FlashForth uses the USB port for its console, with the words rxu and txu for receiving and sending characters, respectively. However, it is possible to use one of the UARTs for the console. Connected to this UART could be a serial terminal, a USB bridge, a Bluetooth or WiFi module.
Forth uses the word emit to transmit a char, the word key? to see if a char has arrived, and the word key to receive a char. These are simply calls to txu, rxu? and rxu, respectively. By remapping emit, key? and key to a UART, the console is is redirected. The address of these words are stored in the variables 'key?, 'key and 'emit.
You can see this for yourself. If you fetch the value stored in 'emit:
Forth uses the word emit to transmit a char, the word key? to see if a char has arrived, and the word key to receive a char. These are simply calls to txu, rxu? and rxu, respectively. By remapping emit, key? and key to a UART, the console is is redirected. The address of these words are stored in the variables 'key?, 'key and 'emit.
You can see this for yourself. If you fetch the value stored in 'emit:
'emit @
and look up the address of txu
' txu
you will see they are the same address. 'emit points to txu. By storing the address of a UART tx word to the variable 'emit, emit is mapped to that UART. The address of a Forth word in the dictionary may be fetched using the ' (tick) word.
Words to change the Forth console to UART1, and back to the console:
Words to change the Forth console to UART1, and back to the console:
: >uart1
['] tx1 'emit !
['] rx1 'key !
['] rx1? 'key? !
;
: console
['] txu 'emit !
['] rxu 'key !
['] rxu? 'key? !
;
Be sure to get the spaces correct, and execute these words in one line (or within a word) so that the console is remapped all at once.
>uart1 $41 emit console
A reset of your Scamp will restore the console back to the USB port. If you want to make the remapping permanent for a deployed system, be sure to include the above in your turnkey word.
Adding LoRa to a Scamp
Ken Merk has done a great video on adding a LoRa module to a Scamp.
|
|
Learn : Interfacing : Scamp Serial Communication