SEROUT

From MCS Wiki AVR
(Difference between revisions)
Jump to: navigation, search
 
Line 115: Line 115:
 
It is important to realize that software timing is used for the bit timing. Any interrupt that occurs during SERIN or SEROUT will delay the transmission. Disable interrupts while you use SERIN or SEROUT.
 
It is important to realize that software timing is used for the bit timing. Any interrupt that occurs during SERIN or SEROUT will delay the transmission. Disable interrupts while you use SERIN or SEROUT.
  
<span style="font-family: Arial;">&nbsp;</span>
 
  
[[File:Notice.jpg|notice]]The SEROUT will use the pin in Hi-Z open collector mode. It does require a pull up resistor. In Open Collector mode you can connect several AVR chip pin and poll the ‘bus’ with the SERIN statement.&nbsp;<br/>When you want to use the pins in PORT OUTPUT mode, the pins can not be tied together. Just define a constant named SEROUT_EXTPULL with a value of 0.
 
  
The mode you chose is for all pins used with SEROUT. So you can not mix open collector and port mode.
+
[[File:Notice.jpg|notice]]SEROUT can be used in PORT and open collector TRI-state mode.
 
+
<span style="font-family: Arial;">&nbsp;</span>
+
 
+
<span style="font-size: 10pt;">[[File:Notice.jpg]]SEROUT can be used in PORT and open collector TRI-state mode.</span>
+
  
 
By default PORT mode will be used. PORT mode means, that the defined PORT PIN will be set to output mode, and that the pin output will be swithed between 0 and 1.
 
By default PORT mode will be used. PORT mode means, that the defined PORT PIN will be set to output mode, and that the pin output will be swithed between 0 and 1.
Line 131: Line 125:
 
&nbsp;
 
&nbsp;
  
In Open Collector mode you can connect several AVR chip pin and poll the ‘bus’ with the [http://avrhelp.mcselec.com/serin.htm SERIN] statement.<br/>When you want to use the pins in PORT OUTPUT mode, the pins can not be tied together.
+
In Open Collector mode you can connect several AVR chip pin and poll the ‘bus’ with the [[SERIN]] statement.<br/>When you want to use the pins in PORT OUTPUT mode, the pins can not be tied together.
  
 
Define a constant named <span style="font-weight: bold;">SEROUT_EXTPULL</span> with a value of 1 for the TRI-STATE open collector mode.
 
Define a constant named <span style="font-weight: bold;">SEROUT_EXTPULL</span> with a value of 1 for the TRI-STATE open collector mode.
  
 
Define a constant named <span style="font-weight: bold;">SEROUT_EXTPULL</span> with a value of 0, or omit the constant <span style="font-weight: bold;">SEROUT_EXTPULL</span> from your code in order to work in PORT mode.
 
Define a constant named <span style="font-weight: bold;">SEROUT_EXTPULL</span> with a value of 0, or omit the constant <span style="font-weight: bold;">SEROUT_EXTPULL</span> from your code in order to work in PORT mode.
 +
 +
 +
 +
[[File:Notice.jpg|notice]]The mode you chose is fixed and global for all SEROUT statements. You can not switch between SEROUT_EXTPULL value in your code dynamically.
  
 
= <span class="f_Header">ASM</span> =
 
= <span class="f_Header">ASM</span> =

Latest revision as of 21:24, 17 January 2017

Contents

Action

Sends serial data through a dynamic software UART.

 

 

Syntax

SEROUT var , bts , port , pin, baud , parity , dbits , sbits

 

 

Remarks

While the OPEN and CLOSE statements can be used for software UARTS, they do not permit to use the same pin for input and output. The settings used when opened the communication channel can also not be changed at run time.

 

The SERIN and SEROUT statements are dynamic software UART routines to perform input and output. You can use them on the same pin for example send some data with SEROUT and get back an answer using SERIN.

 

Since the SERIN and SEROUT routines can use any pin and can use different parameter values, the code size of these routines is larger.

 

Parameter

Description

Var

A variable which content is send through the UART. A constant can NOT be used.

Bts

The number of bytes to receive. String variables will wait for a return (ASCII 13). There is no check if the variable you assign is big enough to hold the result.

Port

The name of the port to use. This must be a letter like A for portA.

Pin

The pin number you want to use of the port. This must be in the range from 0-7.

Baud

The baud rate you want to use. For example 19200.

Parity

A number that codes the parity. 0= NONE, 1 = EVEN, 2 = ODD

Dbits

The number of data bits. Use 7 or 8.

Sbits

The number of stop bits. 1 to 2.

 

The use of SEROUT will create an internal variable named ___SER_BAUD. This is a LONG variable. It is important that you specify the correct crystal value with $CRYSTAL so the correct calculation can be made for the specified baud rate.

 

Note that ___SER_BAUD will not hold the passed baud rate but will hold the bit delay used internal.

 

Since the SW UART is dynamic you can change all the parameters at run time. For example you can store the baud rate in a variable and pass this variable to the SEROUT routine.

 

Your code could change the baud rate under user control this way.

 

It is important to realize that software timing is used for the bit timing. Any interrupt that occurs during SERIN or SEROUT will delay the transmission. Disable interrupts while you use SERIN or SEROUT.


noticeSEROUT can be used in PORT and open collector TRI-state mode.

By default PORT mode will be used. PORT mode means, that the defined PORT PIN will be set to output mode, and that the pin output will be swithed between 0 and 1.

The optional TRI state mode means that the PORT PIN will be in input mode with the PORT PIN set to 0. Switching of the output pin is accomplished by switching the Data Direction Register (DDR). This mode requires an external PULL UP register to VCC.

 

In Open Collector mode you can connect several AVR chip pin and poll the ‘bus’ with the SERIN statement.
When you want to use the pins in PORT OUTPUT mode, the pins can not be tied together.

Define a constant named SEROUT_EXTPULL with a value of 1 for the TRI-STATE open collector mode.

Define a constant named SEROUT_EXTPULL with a value of 0, or omit the constant SEROUT_EXTPULL from your code in order to work in PORT mode.


noticeThe mode you chose is fixed and global for all SEROUT statements. You can not switch between SEROUT_EXTPULL value in your code dynamically.

ASM

The routine called is named _serout and is stored in mcs.lib

For the baud rate calculation, _calc_baud is called.

 

 

See also

SERIN

 

 

Example


'-----------------------------------------------------------------------------------------
'name : serin_out.bas
'copyright : (c) 1995-2005, MCS Electronics
'purpose : demonstration of DYNAMIC software UART
'micro : AT90S2313
'suited for demo : yes
'commercial addon needed : no
'-----------------------------------------------------------------------------------------
 
$regfile = "2313def.dat" ' specify the used micro
$crystal = 4000000 ' used crystal frequency
$baud = 19200 ' use baud rate
$hwstack = 32 ' default use 32 for the hardware stack
$swstack = 10 ' default use 10 for the SW stack
$framesize = 40 ' default use 40 for the frame space
 
'tip : Also look at OPEN and CLOSE
 
'some variables we will use
Dim S As String * 10
Dim Mybaud As Long
'when you pass the baud rate with a variable, make sure you dimesion it as a LONG
 
Mybaud = 19200
Do
'first get some data
Serin S , 0 , D , 0 , Mybaud , 0 , 8 , 1
'now send it
Serout S , 0 , D , 1 , Mybaud , 0 , 8 , 1
' ^ 1 stop bit
' ^---- 8 data bits
' ^------ even parity (0=N, 1 = E, 2=O)
' ^-------------- baud rate
' ^-------------------- pin number
' ^----------------------- port so PORTA.0 and PORTA.1 are used
' ^--------------------------- for strings pass 0
' ^-------------------------------- variable
Wait 1
Loop
End
 
'because the baud rate is passed with a variable in this example, you could change it under user control
'for example check some DIP switches and change the variable mybaud

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language