ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 12.06.2025

Просмотров: 1362

Скачиваний: 3

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Basic for PIC Microcontrollers

74

B var byte

Main :

FOR W=1 TO 9

SELECT CASE W

CASE 0

B=1

PORTB=B

Pause 3000

CASE 1,2,3

B=2

PORTB=B

Pause 3000

CASE IS > 5

B=3

PORTB=B

Pause 3000

CASE ELSE

B=FF

PORTB=B

Pause 3000

END SELECT

NEXT W

END

Basic for PIC Microcontrollers

75

The example above cycles numbers from 0 to 9 in the SELECT CASE branching. If W equals zero port B diodes will take value of 1. If W equals 1, 2 or 3 port B diodes will take value of 2.

If W equals 4 or 5 port B diodes will take value of 255 because 4 and 5 haven‘t been defined - therefore, value from CASE ELSE part of the instruction is taken.

If W is greater than 5, port B diodes will take value of 3.

4.52 SERIN Asynchronous serial input (like with BS1)

Syntax:

SERIN Pin, Mode, {Timeout, Label}, {[Qual...], }{Item...}

Description: SERIN receives one or more values on a specified pin "Pin" using the standard asynchronous format 8N1 (8 data bits, no parity check and one ‘stop‘ bit).

Instead of numerical value ranging from 0 to 15, Mode can be a name if "modedefs.inc" library is included ahead.

Mode

Mode number

Baud rate

State

T2400

0

2400

T1200

1

1200

True

T9600

2

9600

T300

3

300

N2400

4

2400

N1200

5

1200

Inverted

N9600

6

9600

N300

7

300

SERIN instruction can include label (parameter "Label") which the program will jump onto if there is no data received during the specified time period (parameter "Timeout" - default value is 1ms).

There can be qualifier within brackets [] ahead of every data. SERIN must receive these bytes in correct order before receiving data words. If any received byte doesn‘t match next byte‘s qualifier, marking process begins anew - next received byte is compared to the first on the qualifying list. Qualifying content can be a constant, variable or character string. Every character in a string is treated as a separate qualifier.


Basic for PIC Microcontrollers

76

When qualifiers are set, SERIN tries to save data to variables. If there is character # ahead of

variable SERIN converts decimal value to ASCII and stores the result in that variable.

SERIN works with 4MHz oscillator by default. In order to achieve certain transfer rate with

other oscillators, it is neccessary to use appropriate "DEFINE Osc" directive.

Example:

B0 var byte

Main :

Wait for character "A" to be received on serial line on pin RB0 and store next

received character to variable

serin PORTB.0, N2400, ["A"], B0

variable B0

lcdout B0

‘ Display content of B0 on LCD

Loop : goto Loop

End

4.53 SERIN2

Asynchronous serial input (like with BS2)

Syntax:

SERIN2 Pin{\FlowPin}, Mode, {ParityLabel}, {Timeout, Label}, [Item...]

Description:

SERIN2 receives one or more values on a specified pin "Pin" using the standard asynchronous

format 8N1 (8 data bits, no parity check and one ‘stop‘ bit).

Instead of numerical value from 0 to 15, Mode can be a name if "modedefs.inc" library is

included ahead.

Mode

Mode number

Baud rate

State

T2400

0

2400

True


Basic for PIC Microcontrollers

77

T9600

2

9600

T300

3

300

N2400

4

2400

N1200

5

1200

Inverted

N9600

6

9600

N300

7

300

Optional "FlowPin" can be used to prevent eventual data loss in high speed transfers. If used, "FlowPin" is automatically set to regular state (depends on polarity from Mode parameter - table above) in order to allow transfer of every character.

Mode can be used for defining baud rate and serial transfer parameters. Lower 13 bits determine baud rate. Bit 13 selects (non)parity check. Bit 14 selects inverted or true level, while bit 15 is unused. 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

bit 0 -12

300

3313

600

1646

1200

813

2400

396

4800

188

9600

84

19200

32

Bit 13 enables parity check if bit 13 equals 1 and disables it for 0. For bit13 = 0 transfer format is 8N1. In case that parity check is needed, following directive should be used :

DEFINE SER2_ODD

1

Bit 14 selects data level of flow control pins. If bit 14 equals 0 data is received true, while bit14 = 1 receives inverted data.


Basic for PIC Microcontrollers

78

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)

Optional label "ParityLabel" specifies label which program jumps onto if transfer error occurs (this label makes sense only if parity bit is on).

"Timeout" and "Label" allow program to proceed from designated label if there is no data in specified time period. Waiting time is expressed in miliseconds.

DEFINE directive allows transfer of data with size greater than 8, that is 7 with parity check. SER2_BITS allows transfe r of data ranging from 4 to 8 bits.

SERIN2 supports many different data modifiers that can be combined to allow various input data formats.

Modifier

How it works

BIN{1..16}

Takes binary digits

DEC{1..5}

Takes decimal digits

HEX{1..4}

Takes hexadecimal digits

SKIP n

Skips next n characters

STR ArrayVar\n{\c}

Takes the sequence of n characters that ends with the character c

(optional)

WAIT ( )

waits for character sequence

WAITSTR ArrayVar{ \n}

waits for a string

If prefix BIN is used ahead o f variable, ASCII character in binary value of variable will be received. For example, if we write BIN B0 and received value is "1000" B0 will take value of 8.

If prefix DEC is used ahead of variable, ASCII character in decimal value of variable will be received. For example, if we write DEC B0 and received value is "123" B0 will take value of

Basic for PIC Microcontrollers

79

123.

If prefix HEX is used ahead of variable, ASCII character in hexadecimal value of variable will be received. For example, if we write HEX B0 and received value is "FE" B0 will take value of 254.

Key word SKIP followed by a number enables that many characters from input row to be skipped. For example, SKIP 4 would skip 4 characters.

If key word STR is followed by variable of string type, number "n" and optionalending char, character string will be received. String length is defined with "n" or with appearing of final element of a string.

Data bytes received usually go after one or more identification bytes. Identification bytes come within small brackets after WAIT. It means that the sequence of received bytes must match the sequence of identification bytes. Otherwise, if one of received bytes doesn‘t match following byte in identifier sequence, identification process starts anew - next received byte is compared to the first identification byte.

Identification byte can be a constant, variable or array of constants. In the last case, every constant is treated as a separate identifier.

WAITSTR is used in a similar way as WAIT, except for the fact that the key is character string instead of byte sequence.

Instruction SERIN2 assumes that microcontroller clock works at 4MHz. In case of different oscillator it is necessary to make adjustment with following directive :

DEFINE OSC.

Example:

serin2 PORTB.0, 16780, [wait("A"), B0]

wait for character "A" to be received to RB0 pin and store next received character to variable B0.


Basic for PIC Microcontrollers

80

serin2 PORTB.0, 84, [skip 2, dec 4, B0]

Skip 2 characters and receive next 4 decimal numbers.

4.54 SEROUT Asynchronous serial output (like with BS1)

Syntax:

SEROUT Pin, Mode, [Item{, Item...}]

Description: SERIN sends one or more values to a specified pin "Pin" using the standard asynchronous format 8N1 (8 data bits, no parity check and one ‘stop‘ bit). Transfer modes ("Mode") include

:

Mode

Mode number

Baud Rate

State

T2400

0

2400

T1200

1

1200

Driven True

T9600

2

9600

T300

3

300

N2400

4

2400

N1200

5

1200

Driven Inverted

N9600

6

9600

N300

7

300

OT2400

8

2400

OT1200

9

1200

Open True

OT9600

10

9600

OT300

11

300

ON2400

12

2400

ON1200

13

1200

Open Inverted

ON9600

14

9600

ON300

15

300

Instead of numerical value from 0 to 15, Mode can be a name if "modedefs.inc" library is included ahead.

If there is character # ahe ad of variable SEROUT converts decimal value to ASCII and sends