SERIN
Selecting the baud rate with Mode
This command is similar to the SERIN command but is used to send RS232
asynchronous serial data to a pin of PORTB (Pin can
be between 0 and 7). As before, Mode
is used to set the commu-
nications baud rate. In addition to the standard inverted and non-inverted modes, it is also possi- ble to set Open-Drain and Open-Collector
modes where a pull-up resistor will be required at the
output of the pin.s
Selecting the baud rate with Mode
Symbol Value Baud rate Mode
T2400 0 2400 True
T1200 1 1200 True
T9600 2 9600 True
T300 3 300 True
N2400 4 2400 Inverted
N1200 5 1200 Inverted
N9600 6 9600 Inverted
N300 7 300 Inverted
OT2400 8 2400 Open Drain
OT1200 9 1200 Open Drain
OT9600 10 9600 Open Drain
OT300 11 300 Open Drain
ON2400 12 2400 Open Source
ON1200 13 1200 Open Source
ON9600 14 9600 Open Source
ON300 15 300 Open Source
This command is used to generate sound on a specified PORTB pin of the
microcontroller (Pins are between
0 and 7). Note can take values from 0 to 255 and these values do not correspond to the musical notes. A
0 represents silence.
Values from 1 to 127 are tones (1 is lower frequency than 127), and values from 128 to 255 are white noise (128 is lower frequency than 255).
The sound continues for a length
of time specified by Duration. Duration is measured
in milliseconds and it can take values between 0 and 255. The SOUND command produces
TTL level square
waves and it is possible to connect a speaker
to the output pin
SERIN Pin, Mode, (Qual, Qual,, ), Item, Item,
This command is used to receive
RS232 serial asynchronous data on a PORTB
pin (pin is between 0 and 7) using 8-bit data, no parity bit, and one stop bit. As shown in Table 4.6, Mode defines the baud rate and whether or not the pin data is inverted. For example,
if Mode is N9600, the data is
inverted and the selected baud rate is 9600.
Symbol
|
Value
|
Baud rate
|
Mode
|
T2400
|
0
|
2400
|
True
|
T1200
|
1
|
1200
|
True
|
T9600
|
2
|
9600
|
True
|
T300
|
3
|
300
|
True
|
N2400
|
4
|
2400
|
Inverted
|
N1200
|
5
|
1200
|
Inverted
|
N9600
|
6
|
9600
|
Inverted
|
N300
|
7
|
300
|
Inverted
|
The RS232 signal levels are
12 V and level converter circuits (such as MAX232) are normally
used to convert the RS232 signal levels to TTL and the TTL levels back to RS232 levels. The I/O
specifications of PIC microcontrollers allow RS232
signals to be directly connected to a port pin. As shown in Figure 4.5, a
resistor is all that is needed to receive RS232-compatible signals on a pin. When used in this mode, the data is to be inverted (i.e. use the “N” versions
of the mode sig- nals in Table )
Connecting a RS232 signal to a port pin
A number of qualifiers, enclosed
in brackets, can be used with the SERIN command such that these bytes
must be received before
receiving the
data items. Once the qualifiers
are satisfied, SERIN
receives the serial data and stores in Items. The Item variable may be preceded
by the hash character
(“#”). This will convert the decimal number received into ASCII equivalent and store it in Item.
In the following example,
pin 1 of PORTB (RB1) is defined as the serial I/O pin and the port pin is
connected to the RS232 serial line using a resistor. The baud rate is assumed
to be 4800. The
microcontroller waits until the
character “X” is received from the line and then stores the next byte
in variable B0:
SERIN 1, N4800,
(“X”), B0
SEROUT
SEROUT Pin, Mode, (Item, Item,…)
Selecting the baud rate with Mode
Symbol Value Baud rate Mode
T2400 0 2400 True
T1200 1 1200 True
T9600 2 9600 True
T300 3 300 True
N2400 4 2400 Inverted
N1200 5 1200 Inverted
N9600 6 9600 Inverted
N300 7 300 Inverted
OT2400 8 2400 Open Drain
OT1200 9 1200 Open Drain
OT9600 10 9600 Open Drain
OT300 11 300 Open Drain
ON2400 12 2400 Open Source
ON1200 13 1200 Open Source
ON9600 14 9600 Open Source
ON300 15 300 Open Source
Data byte Item is sent to the specified port pin in serial format. The Item can be a string
constant or a numeric value.
A string constant
consists of characters and each character of the string is sent out. For example,
the string “COMPUTER” is sent out as 8 individual
characters. A numeric
value will send the corresponding ASCII
character. For example, 13
is the
carriage-return character, 65
is character
“A” and so on. A numeric value can be preceded
by the hash character “#” and this will send out the
ASCII representation of its decimal
value. For example, #345 will be sent as “3”, “4”, and “5”.
In the following example,
it is assumed that pin 1 of PORTB
(RB1) is used as the serial I/O pin and it is configured for 4800 baud. ASCII value of variable B0 is sent out from this pin, followed by
a carriage-return.
SEROUT 1, N4800, (#B0, 13)
SLEEP
SLEEP Period
The SLEEP command is used to put the microcontroller in low-power mode and stops the micro-
controller running for the specified Period. The Period is
a word and can range from 1 to 65,535
and represents increments of 2.3 s. For example,
a value of 1 will make the microcontroller sleep for 2.3 s, a value of 2 will make the microcontroller sleep for 4.6 s and so on. The maximum
value of 65,535 makes the microcontroller sleep just over
18 h.
In the following example, the microcontroller sleeps for 23 s: SLEEP 10
SOUND
SOUND Pin, (Note, Duration,
Note, Duration,…..)
Connecting a speaker for
the SOUND command
In the following example, a sound with note 20 and duration 100 ms is sent to pin 0 of PORTB. Then, another sound with note 23 and duration 200 ms is sent out from the same port pin.
SOUND 0, (20, 100, 23, 200)
TOGGLE
TOGGLE Pin
This command makes the specified Pin an output pin and inverts the state of this pin (Pin can take values from 0 to 7).
In the following example, bit 0 of PORTB (RB0) is first made low, and then changed to high using the TOGGLE command:
LOW 0
TOGGLE 0
WRITE
WRITE Address, Value
The WRITE command writes the Value byte to the specified EEPROM address. This command is only valid for the PIC microcontrollers which have built-in EEPROM memories.
In the following example byte in variable B0 is written to EEPROM address 2: WRITE 2, B0
In the following example, a sound with note 20 and duration 100 ms is sent to pin 0 of PORTB. Then, another sound with note 23 and duration 200 ms is sent out from the same port pin.
SOUND 0, (20, 100, 23, 200)
TOGGLE
TOGGLE Pin
This command makes the specified Pin an output pin and inverts the state of this pin (Pin can take values from 0 to 7).
In the following example, bit 0 of PORTB (RB0) is first made low, and then changed to high using the TOGGLE command:
LOW 0
TOGGLE 0
WRITE
WRITE Address, Value
The WRITE command writes the Value byte to the specified EEPROM address. This command is only valid for the PIC microcontrollers which have built-in EEPROM memories.
In the following example byte in variable B0 is written to EEPROM address 2: WRITE 2, B0


Comments
Post a Comment