ATMEL AVR1500 User Manual

http://www.BDTIC.com/ATMEL
AVR1500: Xplain training - XMEGA Basics

Prerequisites

Required knowledge
- Basic knowledge of microcontrollers and the C programming language
Software prerequisites
- WinAVR/GCC 20100110 or later
Hardware prerequisites
- Xplain evaluation board
- JTAGICE mkII
Estimated completion time:
- 2 hours

1 Introduction

This Application Note covers Atmel AVR XMEGATM basics, using the IO ports to show various concepts in four tasks. The goal of this training is to get you started with small code fragments, utilizing the XMEGA header files and some of the XMEGA features for more efficient and compact code.
®
AVR® Studio® 4.18 or later
8-bit Microcontrollers
Application Note
Rev. 8308A-AVR-06/10

2 Writing C-code for XMEGA

http://www.BDTIC.com/ATMEL
The pressure to reduce development time and at the same time ensure high quality of electronic products has made high-level programming languages a requirement. It is easier to maintain and reuse and gives better portability and readability.
The choice of programming language alone does not ensure high readability and reusability; good coding style does. Therefore the Atmel XMEGA peripherals, header files and drivers are designed with this in mind.
The following sub-sections will give a brief overview of some of the programming style that is new to XMEGA. A more detailed description is given in the application note “AVR1000: Getting Started Writing C-code for XMEGA”.

2.1 Bit Masks and Bit Group Masks

Register bits can be manipulated using pre-defined masks, or alternatively bit positions (which are not recommended). The pre-defined bit masks are either related to individual bits, called a bit mask, or a bit group. The pre-defined bit group masks are called group mask for short.
A bit mask is used both when setting and clearing individual bits. A bit group mask is mainly used when clearing multiple bits in a bit group.
If you are used to the ATmega or ATtiny AVR microcontrollers you would typically set the Event delay (EVDLY) bit similar to Example 1.
Example 1. Bit position usage in standard Atmel AVR microcontroller
TCD0.CTRLD |= (1<< EVDLY );
This is because the header files for those microcontrollers specify the bit position, and with the shift operation (<<) you create a bit mask. With the XMEGA header files this is more readable because both the bit positions and the bit masks are already defined.
Example 2. Bit mask usage in XMEGA
TCD0.CTRLD |= TC0_EVDLY_bm; // with bit mask specifier
Example 3. Bit position usage in XMEGA
TCD0.CTRLD |= (1 << TC0_EVDLY_bp); // with bit position specifier
Using the format in example 2 is recommended, but both examples achieve setting bit 4 to the value 1, that is, register value will be ORed with the binary value 0001 0000.
Many configurations are controlled by a group of bits. For example; in Timer/Counter CTRLD register (see Figure 1) the EVACT[2:0] and the EVSEL[3:0] bits bits. The value of the bits in a group selects a specific configuration. The group mask uses the same name as the bits in the bit group and is suffixed “ position of the bit group is suffixed “_gp”.
are grouped
_gm”, while the
2
AVR1500
8308A-AVR-06/10
http://www.BDTIC.com/ATMEL
Figure 1. Timer Control D register as depicted in the Atmel XMEGA A Manual
AVR1500
The bit group mask is primarily intended for clearing old configuration of a bit group before writing a new value. The bit group position is useful when setting numerical factors, for example, multiplication factors for PLL.
Example 4. Group mask usage
TCD0.CTLD &= ~(TC0_EVACT_gm); // Clear group bits with group mask
By looking at the mask and bit relation as they are in the XMEGA header files, we see what the above does:
#define TC0_EVACT_gm 0xE0 /* Event Action group mask. */ #define TC0_EVACT_gp 5 /* Event Action group position. */
#define TC0_EVACT0_bm (1<<5) /* Event Action bit 0 mask. */ #define TC0_EVACT0_bp 5 /* Event Action bit 0 position. */ #define TC0_EVACT1_bm (1<<6) /* Event Action bit 1 mask. */ #define TC0_EVACT1_bp 6 /* Event Action bit 1 position. */ #define TC0_EVACT2_bm (1<<7) /* Event Action bit 2 mask. */ #define TC0_EVACT2_bp 7 /* Event Action bit 2 position. */
As you understand the TC0_EVACT_gm will have a binary value of 1110 0000, which is very useful for clearing a group of bits.

2.2 Bit Group Configuration Masks

It is often required to consult the datasheet to investigate what bit pattern needs to be used when setting a bit group to a desired configuration. This also applies when reading or debugging a code. To increase the readability and to minimize the likeliness of setting bits in bit groups incorrectly, a number of group configuration masks are made available. The name of a group configuration has the suffix “_gc”.
Figure 2. Group configuration name composition
From Figure 2 one can see that the group configuration is used for the Receive
e
compl configuration selects a high (HI) interrupt level.
To change a bit group to a new configuration, the bit group configuration is typically used in conjunction with the bit group mask, to ensure that the old configuration is erased first.
te interrupt level (RXCINTLVL) bits in a USART module. This specific group
8308A-AVR-06/10
3
Example 5.
http://www.BDTIC.com/ATMEL
USARTC0.CTRLA = (USARTC0.CTRLA & ~USART_RXCINTLVL_gm) | USART_RXCINTLVL_MED_gc;
The example above shows group mask and configuration mask used together. The first part of the example ( RCXINTLVL bits in the USARTC0 register in a similar way to what is shown in example 4. The last part ( medium interrupt level.
This code is used to reconfigure USARTC0 Receive Complete Interrupt level to medium, without affecting the other bits in the register. You will see code similar to this in a lot of the drivers that comes with the different application notes.

2.3 Module registers and addresses

The IO map in the Atmel XMEGA is organized so that all registers for a given peripheral module are placed in one continuous memory block. This makes it possible to organize all peripheral modules in C structs, where the address of the struct defines the base address of the module. All registers belonging to a module are elements in the module struct.
The following code shows how the different registers for the Programmable Multilevel Interrupt Controller (PMIC) is defined in the XMEGA header files.
USARTC0.CTRLA & ~USART_RXCINTLVL_gm) clears the
| USART_RXCINTLVL_MED_gc) sets the new value to get a
Example. Definition from XMEGA header file (iox128a1.h):
typedef struct PMIC_struct { register8_t STATUS; /* Status Register */ register8_t INTPRI; /* Interrupt Priority */ register8_t CTRL; /* Control Register */ } PMIC_t
This code defines the registers available in the PMIC module. The struct is used to define the PMIC at a specific memory address.
Example. Peripheral module definition:
#define PMIC (*(PMIC_t *) 0x00A0)
The example above shows how the module instance definition uses a de-referenced pointer to the absolute address in the memory, coinciding with the module instance base address. The module pointers are pre-defined in the XMEGA header files, it is therefore not necessary to add these definitions in the source code.
If the examples above do not make sense to you, don’t worry.
What you need to know is how to use these definitions. With the above definitions, which are part of the XMEGA header files, you can access any registers within a module with the “.” (dot) syntax, as shown in the following example:
4
AVR1500
8308A-AVR-06/10

3 Overview

http://www.BDTIC.com/ATMEL
Example usage
Unsigned char temp; Temp = PMIC.STATUS; // Read status register into temp PMIC.CTRL |= PMIC_PMRRPE_bm; // Set PMRRPE bit in control register
The main advantage of using the Module registers and the Module addresses shown above is the ability to create drivers that are independent of the actual peripheral (for example works for both USART 1 and USART 4) and between different parts of the Atmel XMEGA family.
Here is a short overview of the tasks in this training:
This training covers XMEGA basics, using the IO ports to show various concepts in four tasks. The goal of this training is to get you started with small code fragments, utilizing the XMEGA header files and some of the XMEGA features for more efficient and compact code.
Task 1. Basic LED Control
This task shows how to use #defines and module names from the XMEGA header files to create portable code and how to manipulate IO ports.
Task 2. Generic Drivers
AVR1500
This task shows how to use pointers to peripheral module to make generic driver code and how to read switches and output to LEDs.
Task 3. Output and Pull Configuration
This task shows how to use the XMEGA header files with its group mask and group configuration values to efficiently modify bit fields within registers.
Task 4. Multi Configuration
This task shows how to use the multi configuration register to configure more than one pin at a time.
Good luck!
8308A-AVR-06/10
5
Loading...
+ 11 hidden pages