ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 12.06.2025
Просмотров: 1360
Скачиваний: 3
Basic for PIC Microcontrollers |
81 |
|
it. For example, if B equals 34 then #B sends ‘3‘ and ‘4‘. |
||
SEROUT works with 4MHz oscillator by default. In case of different oscillator it is necessary |
||
to make adjustment with following directive : DEFINE OSC. |
||
In cases of slower receiving device, it is necessary to wait for a certain amount of time when |
||
sending next data. DEFINE directive enables delay ranging from 1 to 65 535 microseconds |
||
(0.001 to 65.535 miliseconds) between sending 2 characters. |
||
DEFINE CHAR_PACING 1000 ‘ 1ms delay between 2 chars |
||
Example: |
B0 var byte |
|
Main: |
||
B0 = 25 |
||
serout PORTA.3, N2400, [#B0, 13] ‘ Send ASCII value of B0 and constant 13 to |
||
RA3 via serial line |
||
Loop : goto Loop |
||
End |
||
4.55 SEROUT2 Asynchronous serial output (like with BS2)
Syntax: |
SEROUT2 Pin{\FlowPin}, Mode, {Pace, }, {Timeout, Label}, [Item...] |
Description: SEROUT2 sends one or more values to pin determined with parameter "Pin". "Pin" is automatically designated output, while optional "FlowPin" is designated input. Optional "FlowPin" is used for indicating data loss at receiver. Level of permission depends on data transfer mode determined by "Mode".
Optional parameters "Timeout" and "Label" allow program to proceed and in case that "FlowPin" doesn‘t change to state of transfer allowed in a given time period. Wait time "Timeout" is entered in miliseconds.
In some cases transfer rate of SEROUT2 can be too high for receiving device. Then, it is more
Basic for PIC Microcontrollers |
82 |
efficient to set delay between 2 characters using the "pace" parameter instead of using extra pin as "FlowPin". In this way, it is possible to provide sufficient delay when sending data.
Mode is used to determine baud rate and important parameters of serial transfer. Lower 13 bits determine baud rate. Bit 13 selects (non)parity check. Bit 14 selects inverted or true level, while bit 15 is used to determine if connection is currently in transfer or not. Transfer rate determines bit duration in microseconds. To determine bit duration for a given transfer rate, following equation is used :
(1000000 / baud rate) - 20
Table below shows several standard transfer rates:
Baud Rate |
bits 0-12 |
300 |
3313 |
600 |
1646 |
1200 |
813 |
2400 |
396 |
4800 |
188 |
9600 |
84 |
19200 |
32 |
If set, bit 13 enables parity check. Transfer format is standard 8N1 (8 data bits, no parity check, one ‘stop‘ bit) and for bit13 = 1 format is 7E1 (7 data bits, parity bit and one ‘stop‘ bit).
Bit 14 selects data level of "flow control" pins. If bit 14 equals 0 data is received true, while bit14 = 1 receives inverted data (this can used to avoid installation of RS232 communication driver - MAX232).
Bit 15 determines if data pin is still connected (bit15 = 0) or disconnected from data transfer line. This option is useful in case of connecting multiple devices to common serial line.
Some of standard settings include :
Mode = 84 |
(9600 baud, no parity check, true) |
Mode = 16780 |
(2400 baud, no parity check, inverted) |
Mode = 27889 |
(300 baud, parity check, inverted) |
Basic for PIC Microcontrollers |
83 |
DEFINE directive SER2_BITS allows transfer of data with size different than 8 (7 with parity check). SER2_BITS allows transfer of data ranging from 4 to 8 bits. Default value is 8 bits.
SEROUT2 supports many different data modifiers that can be combined in order to allow various input data formats.
Modifier |
How it works |
|
{I}{S} BIN{1..16} |
Sends binary digits |
|
{I}{S} DEC{1.16} |
Sends decimal digits |
|
{I}{S} HEX{1..16} |
Sends hexadecimal digits |
|
REP c\n |
Sends character "c", "n" times |
|
STR ArrayVar\n{\c} |
Sends of "n" characters sequence that ends with the character "c" |
|
(optional) |
||
If prefix BIN is used ahead of variable, ASCII character in binary value of variable will be sent. For example, if we write BIN B0 and B0 = 8, bits 1000 will be sent serial.
If prefix DEC is used ahead of variable, ASCII character in decimal value of variable will be sent. For example, if we write DEC B0 and B0 = 123, data "123" will be sent.
If prefix HEX is used ahead of variable, ASCII character in hexadecimal value of variable will be sent. For example, if we write HEX B0 and B0 = 254, SEROUT2 will send "FE".
REP followed by a character and a number of repeating provides more compact form of writing long strings of same characters. For example, REP "0"\4 stands for "0000"
STR followed by variable of string type and an optional numerical parameter "count" executes sending of character string. String length is determined by "count" or by appearance of character "0" in a string.
Optional parameters can be used ahead or behind BIN, DEC and HEX. In case that "I" is used ahead of any of these, output data will begin with %@, #@ or $@ in order to mark current value as binary, decimal or hexadecimal.
In case that "S" (signed) is used ahead of BIN, DEC or HEX , output data will begin with "- " if highest data bit is set to 1. This allows transfer of negative values. You should bear in mind, though, that all mathematical and comparison operations work with unsigned numbers. Still, unsigned numbers arithmetic allows signed values as results. For example, in case of B0 = 9 - 10, DEC B0 gets value of "255", whereas SDEC B0 sends "1" after the trans fer of the highest bit.
BIN, DEC and HEX can be followed by a number. It is common practice to write numerical data in exact number of digits needed, so that leading zeros are erased and not sent. In case that BIN, DEC and HEX are followed by a number, SEROUT2 will always send that exact number of data, adding leading zeros if needed. For example, BIN6 8 sends BIN "001000",
Basic for PIC Microcontrollers |
84 |
while BIN2 8 sends "00". All these modifies can be used simultaneously (i.e. ISDEC4 B0).
Instruction SEROUT2 assumes that microcontroller clock works at 4MHz. In case of different oscillator it is necessary to make adjustment with following directive :
DEFINE OSC |
||
Example: |
B0 = 25 |
|
SEROUT2 |
PORTA.3, 16780, [DEC B0, 10] |
|
Send decimal value of variable B0 and "LineFeed" via serial line (2400 bauds) to pin RA3. |
||
B0 = 25 |
||
SEROUT2 |
PORTA.1, 84, ["B0=", IHEX4 B0] |
|
Send string "B=" and 4-character hexadecimal value of variable B0 to RA1 at 9600 |
||
bauds. |
||
4.56 SHIFTIN |
Synchronous serial input |
||||||
Syntax: |
SHIFTIN DataPin, ClockPin, Mode, [Var{\Bits}...] |
||||||
Description: |
Instruction SHIFTIN shifts receiving bits on a given pin in synchrony with "ClockPin" |
||||||
frequency signal and stores them to variable. "Var\Bits" optionally specifies the number of |
|||||||
bits to be shifted. If nothing is specified, default number of bits is 8. |
|||||||
Depending on shifting direction (from MSB to LSB or vice versa) various transfer modes |
|||||||
can be defined. |
|||||||
Transfer modes Mode are defined within MODEDEFS.BAS library. To use them, it is |
|||||||
necessary to include mentioned library at the beginning of the program with : Include |
|||||||
"modedefs.bas" |
|||||||
"Mode" |
Mode |
Operation |
|||||
number |
|||||||
MSBPRE |
0 |
First, the highest bit is shifted. Data is read ahead of sending clock. Clock is |
|||||
inactive on a logical zero. |
|||||||
LSBPRE |
1 |
First, the lowest bit is shifted. Data is read ahead of sending clock. Clock is |
|||||
inactive on a logical zero. |
|||||||
MSBPOST |
2 |
First, the highest bit is shifted. Data is read after sending clock. Clock is |
|||||
Basic for PIC Microcontrollers |
85 |
|||
LSBPOST |
3 |
First, the lowest bit is shifted. Data is read after sending clock. Clock is |
||
inactive on a logical zero. |
||||
4 |
First, the highest bit is shifted. Data is read ahead of sending clock. Clock is |
|||
inactive on a logical one. |
||||
5 |
First, the lowest bit is shifted. Data is read ahead of sending clock. Clock is |
|||
inactive on a logical one. |
||||
6 |
First, the highest bit is shifted. Data is read after sending clock. Clock is |
|||
inactive on a logical one. |
||||
7 |
First, the lowest bit is shifted. Data is read after sending clock. Clock is |
|||
inactive on a logical one. |
||||
Shifting frequency is about 50KHz, depending on oscillator used. Active state lasts for at least 2 microseconds. Using the directive DEFINE enables additional delay (up to 65.535 miliseconds) for slowing down the clock.
DEFINE SHIFT_PAUSEUS 100 ‘ Slowing down the cloc k for additional 100ms
Example: |
shiftin Data, Clock, MSBPRE, [RxData] |
Sends the contents of input SHIFT register to variable RxData so that the first bit is MSB.
4.57 SHIFTOUT Synchronous serial output
Syntax: |
SHIFTOUT DataPin, ClockPin, Mode, [Var{\Bits}...] |
Description: |
Instruction SHIFTOUT shifts bits of variable "Var" on a given pin in synchrony with |
"ClockPin" frequency signal. "Var\Bits" optionally specifies the number of bits to be |
|
shifted. If nothing is specified, default number of bits is 8. |
|
Transfer modes Mode are defined within MODEDEFS.BAS library. To use them, it is |
|
necessary to include mentioned library at the beginning of the program with : include |
|
modedefs.bas |
|
Shifting frequency is about 50KHz, depending on oscillator used. Active state lasts for at |
|
least 2 microseconds. Using the directive DEFINE enables additional delay (up to 65.535 |
|
miliseconds) for slowing down the clock. |
|
DEFINE SHIFT_PAUSEUS 100 ‘ Slowing the clock for additional 100ms |
"Mode" |
Mode number |
Operation |
LSBFIRST |
0 |
First, the lowest bit is shifted.. Clock is inactive on a logical zero. |
MSBFIRST |
1 |
First, the highest bit is shifted.. Clock is inactive on a logical zero. |
4 |
First, the lowest bit is shifted.. Clock is inactive on a logical one. |
|
5 |
First, the highest bit is shifted.. Clock is inactive on a logical one. |
|