CONFIG COMx 2084 ENHANCED

Top  Previous  Next

Action

Configures the UART of AVR chips that have an extended UART like the M2560.

 

 

Syntax

CONFIG COMx = baud , synchrone=0|1,parity=none|disabled|even|odd,stopbits=1|2,databits=4|6|7|8|9,clockpol=0|1

 

 

Syntax Xmega

CONFIG COMx = baud , Mode=mode, Parity=parity, Stopbits=stopbits, Databits=databits

 

 

Syntax Xtiny

CONFIG COMx = baud , Mode=mode, Parity=parity, Stopbits=stopbits, Databits=databits , Baud_Offset=baud_ofs , TXPIN=tx

 

 

 

Remarks

COMx

The COM port to configure. Value in range from 1-4

baud

Baud rate to use.

synchrone

0 for asynchrone operation (default) and 1 for synchrone operation.

Parity

None, disabled, even or odd

Stopbits

The number of stop bits : 1 or 2

Databits

The number of data bits : 4,5,7,8 or 9.

Clockpol

Clock polarity. 0 or 1.

 

 

noticeNote that not all AVR chips have the extended UART. Most AVR chips have a UART with fixed communication parameters. These are : No parity, 1 stopbit, 8 data bits.

The Mega2560 does support 4 UART's.

 

 

 

Remarks Xmega

COMx

The COM port to configure. Value in range from 1-8

baud

Baud rate to use. If the baud rate can be generated accurately depends on the system clock.

mode

The USART mode, this can be :

- ASYNCHRONEOUS or 0 (default) for asynchronous operation.

- SYNCHRONEOUS or 1 , for synchronous operation.

- IRDA or IRCOM for IRDA operation

- SPI or MSPI for operation as SPI controller

 

Parity

None, disabled, even or odd

Stopbits

The number of stop bits : 1 or 2

Databits

The number of data bits : 5,6,7,8 or 9.

 

In the Xmega the registers have a fixed offset. This allows to use dynamic UARTS : you can change settings at run time by using a variable. This will use some more code when using just one UART but will save code when using multiple UARTS because you need only one copy of the code.

 

In the Xmega you MUST use CONFIG COM before you can use the UART. The CONFIG commands makes a call to _INIT_XMEGA_UART where the various parameters are passed to setup the UART. You also need to specify the baud rate. Do not use $BAUD.

 

The CLOCKPOL for the SPI mode has been removed, it will be added to a configuration command for the SPI.

 

The CONFIG COM will set the TX pin to output mode. This are the following pins :

 

UART

TX pin

RX pin

BAUD

COM1 - UART_C0

PORTC.3

PORTC.2

BAUD

COM2 - UART_C1

PORTC.7

PORTC.6

BAUD1

COM3 - UART_D0

PORTD.3

PORTD.2

BAUD2

COM4 - UART_D1

PORTD.7

PORTD.6

BAUD3

COM5 - UART_E0

PORTE.3

PORTE.2

BAUD4

COM6 - UART_E1

PORTE.7

PORTE.6

BAUD5

COM7 - UART_F0

PORTF.3

PORTF.2

BAUD6

COM8 - UART_F1

PORTF.7

PORTF.6

BAUD7

 

In IRDA mode, depending on the module you use, it might be necessary to invert the logic level of the TX pin with CONFIG XPIN. For example when COM1 is used for the IRDA module, you would use : CONFIG XPIN=PORTC.3, INVERTIO=ENABLED

 

 

 

Remarks XTINY/MEGAX

COMx

The COM port to configure. Value in range from 1-4

baud

Baud rate to use. If the baud rate can be generated accurately depends on the system clock.

mode

The USART mode, this can be :

- ASYNCHRONEOUS  : (default) for asynchronous operation.

- SYNCHRONEOUS  : for synchronous operation.

- IRCOM  : for IRDA operation

- SPI  : for operation as SPI controller

Parity

None, disabled, even or odd

Stopbits

The number of stop bits : 1 or 2

Databits

The number of data bits : 5,6,7,8 or 9.

Baud_offset

The Xtiny has an internal oscillator that runs at 16 or 20 MHz. The center frequency can be off depending on temperature and voltage.

The Xtiny has 4 calibrated offset values for the oscillator which can be used to correct the BAUD.

 

The options are :

- NONE : default, BAUD is calculated but no offset is used.

All of the other values will read the signature row and will call code from xtiny.lib to compensate the BAUD value.

 

- OSC16_3V3 : OSC runs at 16 MHz and at 3V3

- OSC16_5V : OSC runs at 16 MHz and at 5V

- OSC20_3V3 : OSC runs at 20 MHz and at 3V3

- OSC20_5V : OSC runs at 20 MHz and at 5V

It is up to the user to apply the selected voltage.

It is up to the user to use the specified oscillator value. (change fuse bits). By default the 20 MHz internal osc is selected.

So this setting does not change oscillator values, it only tells the compiler which sigrow must be loaded and used.

TXPIN

This options selects the pins used for the UART.

The XTINY/MEGAX has a port multiplexer. This multi plexer allows to chose which pins are used for hardware connected to the port pins.

So this allows to chose one or more alternative pin locations for hardware such as USART,SPI, TWI and TIMER output.

You can use CONFIG PORT_MUX when you use alternative pin positions. But CONFIG PORT_MUX does not set port direction.

For the USART it is required to set the TX pin into output mode.

That is why this option exists : you can chose the alternative pin location and the compiler will set the port pin into output mode and will set the proper port multiplexer register bit.

You can chose between the default location starting with DEF_ and the alternative location starting with ALTx_ Some processors also allow to disconnect the pins totally and they have a NONE option.

 

Please note that you only should use this option when you use the alternative pin location.

 

It is preferred to use CONFIG COM instead of using $BAUD.

 

 

noticeIt is important that you specify all parameters of CONFIG COM. Do not omit one. The only optional parameter is TXPIN for the alternative TX pin.

 

 

See Also

CONFIG COM1 , CONFIG COM2

 

 

Example

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

'name                     :

'copyright                : (c) 1995-2021, MCS Electronics

'purpose                  : test for M2560 support

'micro                    : Mega2560

'suited for demo          : yes

'commercial addon needed  : no

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

 

$regfile = "m2560def.dat"                                   ' specify the used micro

$crystal = 8000000                                          ' used crystal frequency

$hwstack = 40                                               ' default use 32 for the hardware stack

$swstack = 40                                               ' default use 10 for the SW stack

$framesize = 40                                             ' default use 40 for the frame space

 

'The M128 has an extended UART.

'when CO'NFIG COMx is not used, the default N,8,1 will be used

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

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

Config Com3 = 19200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

Config Com4 = 19200 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

 

 

'Open all UARTS

Open "com2:" For Binary As #1

Open "Com3:" For Binary As #2

Open "Com4:" For Binary As #3

 

 

Print "Hello"                                               'first uart

Dim B As Byte

Dim Tel As Word

 

Do

  Incr Tel

  Print Tel ; " test serial port 1"

  Print #, Tel ; " test serial port 2"

  Print #, Tel ; " test serial port 3"

  Print #, Tel ; " test serial port 4"

 

  B = Inkey(#3)

  If B <> 0 Then

     Print #, B ; " from port 4"

  End If

  Waitms 500

Loop

 

Close #1

Close #2

Close #3

End