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

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

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

Добавлен: 12.06.2025

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

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

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

© 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-


© MCS Electronics, 1995-2007

Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Config Com2 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

'setup to use a serial output buffer 'and reserve 20 bytes for the buffer Config Serialout = Buffered , Size = 254

'It is important since UDRE interrupt is used that you enable the interrupts

Enable Interrupts Print "Hello world" Print "test1"

Do Wait 1

'notice that using the UDRE interrupt will slown down execution of waiting loops like waitms

Print "test"

Loop

End

CONFIG SERIALOUT1

Action

Configures the second hardware UART to use a buffer for output

Syntax

CONFIG SERIALOUT1 = 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_PTRW1 , byte that stores the head of the buffer _RS_TAIL_PTRW1 , byte that stores the tail of the buffer

_RS232OUTBUF1, array of bytes for the ring buffer that stores the printed data. _RS_BUFCOUNTW1, 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

page -379-


© MCS Electronics, 1995-2007

disadvantage is that it blocks program execution just like a normal un-buffered PRINT would do.

ASM

Routines called from MCS.LIB :

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

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

When you use the PRINT statement to send data to the serial port, the UDRE1 interrupt will be enabled. And so the _CheckSendChar1 routine will send the data fromthe buffer.

See also

CONFIG SERIALIN1

Example

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

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

: rs232bufferout1.bas

'name

'copyright

: (c) 1995-2005, MCS Electronics

'purpose

: how to use a serial output buffer on the second

UART

this sample will only work for chips with a seond

'

UART like

the M161 and M128

'

'micro

: Mega161

'suited for demo

: yes

'commercial addon needed

: no

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

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

$regfile = "m162def.dat"

' specify the used

micro

' used crystal

$crystal = 4000000

frequency

' use baud rate

$baud = 9600

$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

Config Com1 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Config Com2 = Dummy , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

'setup to use a serial output buffer 'and reserve 20 bytes for the buffer Config Serialout1 = Buffered , Size = 20

Open "Com1:" For Binary As #1

'It is important since UDRE interrupt is used that you enable the interrupts

Enable Interrupts

Print #1 , "Hello world"

Do Wait 1

page -380-


© MCS Electronics, 1995-2007

'notice that using the UDRE interrupt will slown down execution of waiting loops like waitms

Print #1 , "test"

Loop

End

Close #1

CONFIG SINGLE

Action

Instruct the compiler to use an alternative conversion routine for representation of a single.

Syntax

CONFIG SINGLE = SCIENTIFIC , DIGITS = value

Remarks

Digits

A numeric constant with a value between 0 and 7.

A value of 0 will result in no trailing zero's.

A value between 1-7 can be used to specify the number of digits behind the

comma.

When a conversion is performed from numeric single variable, to a string, for example when you PRINT a single, or when you use the STR() function to convert a single into a string, a special conversion routine is used that will convert into human readable output. You will get an output of digits and a decimal point.

This is well suited for showing the value on an LCD display. But there is a downside also. The routine is limited in the way that it can not shown very big or very small numbers correct.

The CONFIG SINGLE will instruct the compiler to use a special version of the conversion routine. This version will use scientific notation such as : 12e3.

You can specify how many digits you want to be included after the decimal point.

See also

NONE

ASM

Uses single.lbx library

Example

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

'

(c)1995-2005,MCS

'

single_scientific.bas

' demonstation of scientific , single output '----------------------------------------------------------------

$regfile= "m88def.dat" $crystal= 8000000

page -381-