Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
BASCOM AVR, help reference (2007).PDF
Скачиваний:
305
Добавлен:
12.08.2013
Размер:
17.02 Mб
Скачать

© MCS Electronics, 1995-2007

'stored in a buffer. This way you will not loose characters providing that 'you empty the buffer

'So when you fast type abcdefg, they will be printed after each other with the '1 second delay

'Using the CONFIG SERIAL=BUFFERED, SIZE = 10 for example will 'use some SRAM memory

'The following internal variables will be generated :

'_Rs_head_ptr0

BYTE , a pointer to the location

of the start of the buffer

'_Rs_tail_ptr0

BYTE , a pointer to the location

of tail of the buffer

'_RS232INBUF0 BYTE ARRAY , the actual buffer with

the size of SIZE

CONFIG SERIALIN1

Action

Configures the second hardware UART to use a buffer for input

Syntax

CONFIG SERIALIN1 = BUFFERED , SIZE = size [,CTS=pin, RTS=pin , Threshold_full=num , Threshold_empty=num ]

Remarks

Size

A numeric constant that specifies how large the input buffer should

 

be. The space is taken from the SRAM.

CTS

The pin used for the CTS.(Clear to send). For example PIND.6

RTS

The pin used for RTS. (Ready to send). For example PIND.7

Threshold_full

The number of bytes that will cause RTS to be set to '1'. This is an

 

indication to the sender, that the buffer is full.

Threshold_empty

The number of free bytes that must be in the buffer before CTS may

 

be made '0' again.

 

 

The following internal variables will be generated : _RS_HEAD_PTR1 , a byte counter that stores the head of the buffer _RS_TAIL_PTR1 , a byte counter that stores the tail of the buffer.

_RS232INBUF1 , an array of bytes that serves as a ring buffer for the received characters. _RS_BUFCOUNTR1, a byte that holds the number of bytes that are in the buffer.

To clear the buffer, use CLEAR SERIALIN1. Do not read and write the internal buffer variables yourself.

CTS-RTS is hardware flow control. Both the sender and receiver need to use CTS-RTS when CTS-RTS is used. When on of the partie's does not use CTS-RTS, no communication will be possible.

CTS-RTS use two extra lines. The receiver must check the CTS pin to see if it may send. The CTS pin is a input pin as the receiver looks at the level that the sender can change.

The receiver can set the RTS pin to indicate to the sender that it can accept data.

In the start condition, RTS is made '0' by the receiver. The sender will then check this logic level with it's cts pin, and will start to send data. The receiver will store the data into the buffer and when the buffer is almost full, or better said, when the Threshold_ful is the same

page -375-

© MCS Electronics, 1995-2007

as the number of bytes in the receive buffer, the receiver will make RTS '1' to signal to the sender, that the buffer is full. The sender will stop sending data. And will continue when the RTS is made '0' again.

The receiver can send data to the sender and it will check the CTS pin to see if it may send data.

In order to work with CTS-RTS, you need both a serial input buffer, and a serialoutput buffer. So use both CONFIG SERIALIN1 and CONFIG SERIALOUT1 to specify the buffers. The CTS-RTS can only be configured with the CONFIG SERIALIN1 statement.

The thresholds are needed for high baud rates where it will take some time to react on a CTS-RTS.

You need to experiment with the thresholds but good start values are 80%full, and 20% empty.

ASM

Routines called from MCS.LIB :

_GotChar1. This is an ISR that gets called when ever a character is received. When there is no room for the data it will not be stored.

So the buffer must be emptied periodic by reading from the serial port using the normal statements like INKEY() and INPUT.

Since URXC1 interrupt is used by _GotChar1, you can not use this interrupt anymore. Unless you modify the _gotchar1 routine of course.

See also

CONFIG SERIALOUT1 , ISCHARWAITING , CLEAR

Example

'-----------------------------------------------------------------------------

 

------------

: rs232buffer1.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: shows the difference between normal and buffered

'

serial INPUT

'micro

: Mega161

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

------------

$regfile = "m161def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 6900

$hwstack = 32

' default use 32

for the hardware stack

' default use 10

$swstack = 10

for the SW stack

' default use 40

$framesize = 40

for the frame space

 

' Works only for chips with 2 UARTS

'first compile and run this program with the line below remarked

page -376-

© MCS Electronics, 1995-2007

Config Serialin1 = Buffered , Size = 20

'dim a variable

Dim Na As String * 10

Open "com1:" For Binary As #1

'the enabling of interrupts is not needed for the normal serial mode 'So the line below must be remarked to for the first test

Enable Interrupts

Print "Start"

 

 

Do

'was there a char?

If Ischarwaiting(#1) = 1 Then

na$ = Waitkey(#1)

'print it

 

Print #1 , Na

 

End If

 

 

Wait 1

'wait 1

second

Loop

 

 

Close #1

 

 

'You will see that when you slowly enter characters in the terminal emulator 'they will be received/displayed.

'When you enter them fast you will see that you loose some chars

'NOW remove the remarks from line 11 and 18 'and compile and program and run again

'This time the chars are received by an interrupt routine and are

'stored in a buffer. This way you will not loose characters providing that 'you empty the buffer

'So when you fast type abcdefg, they will be printed after each other with the '1 second delay

'Using the CONFIG SERIAL1=BUFFERED, SIZE = 10 for example will 'use some SRAM memory

'The following internal variables will be generated :

'_Rs_head_ptr1

BYTE , a pointer to the location

of the start of the buffer

'_Rs_tail_ptr1

BYTE , a pointer to the location

of tail of the buffer

'_RS232INBUF1 BYTE ARRAY , the actual buffer with

the size of SIZE

CONFIG SERIALOUT

Action

Configures the hardware UART to use a buffer for output

Syntax

CONFIG SERIALOUT = BUFFERED , SIZE = size

Remarks

size

A numeric constant that specifies how large the output buffer should be.

 

The space is taken from the SRAM.

 

 

The following internal variables will be used when you use CONFIG SERIALOUT

_RS_HEAD_PTRW0 , byte that stores the head of the buffer _RS_TAIL_PTRW0 , byte that stores the tail of the buffer

page -377-

© MCS Electronics, 1995-2007

_RS232OUTBUF0, array of bytes for the ring buffer that stores the printed data. _RS_BUFCOUNTW0, a byte that holds the number of bytes in the buffer.

Serial buffered output can be used when you use a low baud rate. It would take relatively much time to print all data without a buffer. When you use a buffer, the data is printed on the background when the micro UART byte buffer is empty. It will get a byte from the buffer then and transmit it.

As with any buffer you have, you must make sure that it is emptied at one moment in time. You can not keep filling it as it will become full. When you do not empty it, you will have the same situation as without a buffer !!! When the roof is leaking and you put a bucket on the floor and in the morning you empty it, it will work. But when you will go away for a day, the bucket will overflow and the result is that the floor is still wet.

Another important consideration is data loss. When you print a long string of 100 bytes, and there is only room in the buffer for 80 bytes, there is still a wait evolved since after 80 bytes, the code will wait for the buffer to become empty. When the buffer is empty it will continue to print the data. The advantage is that you do not loose any data, the disadvantage is that it blocks program execution just like a normal un-buffered PRINT would do.

ASM

Routines called from MCS.LIB :

_CHECKSENDCHAR. This is an ISR that gets called when ever the transmission buffer is empty.

Since UDRE interrupt is used , you can not use this interrupt anymore. Unless you modify the _CheckSendChar routine of course.

When you use the PRINT statement to send data to the serial port, the UDRE interrupt will be enabled. And so the _CheckSendChar routine will send the data from the buffer.

See also

CONFIG SERIALIN

Example

'-----------------------------------------------------------------------------

 

------------

: rs232bufferout.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: demonstrates how to use a serial output buffer

'micro

: Mega128

'suited for demo

: yes

'commercial addon needed

: no

'-----------------------------------------------------------------------------

 

------------

 

$regfile = "m128def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 9600

$hwstack = 40

' default use 32

for the hardware stack

' default use 10

$swstack = 40

for the SW stack

' default use 40

$framesize = 40

for the frame space

 

page -378-