Download: AVR001 Conditional Assembly and portability macros

AVR001 Conditional Assembly and portability macros Features • Increased portability • Easier Code Writing • Simplified I/O Register Access • Improved Assembly Status Feedback 1 Introduction This application note describes the Conditional Assembly feature present in the AVR Assembler version 1.74 and later. The AVR assembler is downloaded together with AVR Studio from the AVR section of the Atmel web site. Examples of how to use Conditional Assembly are presented. One of the examples is a set of macros that will enable the software writer to write a generic code that will assemble to any AVR wi...
Author: Tucker Shared: 8/19/19
Downloads: 1534 Views: 3348

Content

AVR001 Conditional Assembly and portability

macros

Features

• Increased portability • Easier Code Writing • Simplified I/O Register Access • Improved Assembly Status Feedback 1 Introduction This application note describes the Conditional Assembly feature present in the AVR Assembler version 1.74 and later. The AVR assembler is downloaded together with AVR Studio from the AVR section of the Atmel web site. Examples of how to use Conditional Assembly are presented. One of the examples is a set of macros that will enable the software writer to write a generic code that will assemble to any AVR without other modifications than changing the device definition file. 8-bit

Microcontrollers Application Note

, 2 Theory of operation Conditional Assembly (CA) was introduced in the AVR assembler in version 1.74. CA is based on a series of directives similar to the preprocessor directives available in C. Two debugging directives were introduced in AVR assembler version 1.74; a message directive and an error directive. Please see AVR Studio help on Assembler for a complete list of directives. 2.1 Conditional Assembly The assembler evaluates a CA directive expression at compile-time and determines if the code enclosed by the CA directive is to be included or not. A list of Conditional Assembly directives is presented in Table 2-1. Table 2-1. Conditional Assembly Directives Directive Description Includes the code present between the .ifdef and the corresponding .else or .endif if the symbol is defined. Only symbols declared by .EQU or .SET are evaluated .ifdef by .ifdef; symbols defined by the .def directives are treated differently by the assembler and cannot be used with .ifdef. Includes the code present between the .ifndef and the corresponding .else or .endif if the symbol is not defined. Only symbols declared by .EQU or .SET are .ifndef evaluated by .ifndef; symbols defined by the .def directives are treated differently by the assembler and cannot be used with .ifndef. Includes the code present between the .if and the .if corresponding .else, .elif, or .endif if the expression evaluates to a value different from zero (true). A .elif must be preceded by .if. It will include the code present between the .elif and the corresponding .else, .elif .elif, or .endif if the expression evaluates to a value different from zero (true). A .else must be preceded by a .if or .elif. Will include the code present between the .else and the .else corresponding .endif if the expression specified for the corresponding .if or .elif evaluates to zero (false). A .endif must be preceded by a .ifdef, .ifndef, .if, .else or .elif. The .endif defines the end of scope for the .endif corresponding .ifdef, .ifndef, .if, .else or .elif. The directives can be nested in up to 5 levels. CA directives can be combined with the .MACRO directive to make macros assemble differently depending on the enclosed CA directives. This is used in the example code for this application note. 2.2 Debugging Directives When developing code it is useful with different types of feedback at assembly-time. For this purpose two new directives have been added: one that outputs a message to 2 AVR001,

AVR001

the message window if encountered and one that issues an error (accompanied with an error message). The debugging directives are described in Table 2-2. Table 2-2. Debugging Directives Directive Description If the .message directive is encountered when code is .message "string” assembled the "string" is written to the message window. If the .error directive is encountered when code is .error "string” assembled an assembly error is issued and the "string" is written to the message window. These directives can be combined with (e.g. AC) directives to provide information about the assembling status. The use of the .message directives is included in the code example for this application note. Please note that AVR Assembler 1.77.1 stops futher assembly when encountering the .error directive. 2.3 Example 1: General usage of CA The reuse of code modules from one project to the next can often be a great time saver. Reusage of code modules will, without pre-processor directives or in this case CA directives, easily result in several different versions of the code module as it needs to be tailored for the different targets used. Using CA one version can be used for several target devices. As an example, consider the initialization of the I/0 pins used for UART communication: .EQU ATmega128=1 ;Declares the symbol ATmega128 ;EQU ATmega16=1 ;Declares the symbol ATmega16 .EQU UART =0 ;UART0 or UART1 .ifdef ATmega128 .message "UART Module assembled for ATmega128." .if UART == 0 sbi DDRE, PE1 ;Configure TxD as output .elif UART == 1 sbi DDRD, PD3 ;Configure TxD as output .else .error "UART number not specified" .endif .elif ATmega16 .message "UART Module assembled for ATmega128." .if UART == 0 sbi DDRD, PD1 ;Configure TxD as output .else .error "UART number not specified" .endif .endif, As seen from the code example, a device specific initialization of (e.g. a UART) can be contained in one file. This allows for better control of the code modules reused. 2.4 Example 2: Conditional Assembly in Macros The code example for this application note requires the AVR Assembler version 1.77 or later to assembled correctly. The AVR assembler is included with AVR Studio, which can be downloaded from the AVR section on the Atmel web site. The file "macros.h" includes a number of macros to ease bit and byte access in the I/0 and data space. The macros are listed with comments in Table 2-3. Table 2-3. Macros Defined in the File “macros.inc” Macro name Description "Set Bit" - Set a bit in any location in the I/0 SETB [Address, Bit Mask, Register] space. Registers that can be used are R16-R31. "Clear Bit" - Clear a bit in any location in the I/0 CLRB [Address, Bit Mask, Register] space. Registers that can be used are R16-R31. "Skip if Bit Set" - skip the instruction following the SKBS [Address, Bit Mask, Register] macro if the bit specified by Bit Mask in any location in the I/0 space is set. "Skip if Bit Cleared" - skip the instruction following SKBC [Address, Bit Mask, Register] the macro if the bit specified by Bit Mask in any location in the I/0 space is cleared. "Store register" - Stores the contents of a register STORE [Address, Register] in a location in any location in the I/0 space. "Load register" - Load a register with the contents LOAD [Register, Address] from any location in the I/0 space. The reason for using these macros to access I/0 space (and extended I/0 space) is that the code writer need not consider where in the I/0 space the accessed registers are located. This would be required if the macros where not used as not all instructions reach all addresses in the I/0 space. The advantages are therefore numerous: • The author doesn't need to know the I/0 map, just the names of the registers. • The standard definition files for register and bit names can be used. • The most code size efficient instructions are used to access a register. • The assembly code can be ported to any device without modifying the code. 2.4.1 The Set-Bit Macro As all the macros are similar in nature only the SETB are described in details. Three arguments are specified for the SETB macro: a destination address, a bit mask and a register. The register is only used if the address is higher than 0x001F, but it is recommended to specify it anyway to ensure correct assembly and best portability opportunities. The range of the Bit mask is verified to be between 0 and 7, if this condition is violated an error is issued using the .error directive. 4 AVR001,

AVR001

If the address is below 0x1F the SBI instruction is used to set the bit. If the Address is between 0x1F and 0x3F IN and OUT instruction is used to access the address. Finally, if the address is above 0x3F the LDS and STS instructions are used. Figure 2-1 shows how the assembler handles the SETB macro using CA. Figure 2-1. Assembling flow for CA inside SETB macro. SETB macro Yes Msg: "Only Arg 1 > 7? values 0-7 allowed..." No Yes lds ... Arg 0 >0x3F? sbr ... sts ... No Yes in ... Arg 0 >0x1F? sbr ... out ... No sbi ... End macro 2.5 Improvements At the expense of one of the registers Y or Z the LOAD and STORE macros could be improved to execute faster and be more code compact: If one of these registers are reserved for indirect access the STS and LDS instructions could be replaced by STD and LDD. As this is a constraint to the code writer this has not been added in the present implementation.,

Disclaimer Atmel Corporation Atmel Operations

2325 Orchard Parkway Memory RF/Automotive San Jose, CA 95131, USA 2325 Orchard Parkway Theresienstrasse 2 Tel: 1(408) 441-0311 San Jose, CA 95131, USA Postfach 3535 Fax: 1(408) 487-2600 Tel: 1(408) 441-0311 74025 Heilbronn, Germany Fax: 1(408) 436-4314 Tel: (49) 71-31-67-0

Regional Headquarters Microcontrollers Fax: (49) 71-31-67-2340 Europe 2325 Orchard Parkway 1150 East Cheyenne Mtn. Blvd.

Atmel Sarl San Jose, CA 95131, USA Colorado Springs, CO 80906, USA Route des Arsenaux 41 Tel: 1(408) 441-0311 Tel: 1(719) 576-3300 Case Postale 80 Fax: 1(408) 436-4314 Fax: 1(719) 540-1759 CH-1705 Fribourg Switzerland La Chantrerie Biometrics/Imaging/Hi-Rel MPU/ Tel: (41) 26-426-5555 BP 70602 High Speed Converters/RF Datacom Fax: (41) 26-426-5500 44306 Nantes Cedex 3, France Avenue de Rochepleine Tel: (33) 2-40-18-18-18 BP 123

Asia Fax: (33) 2-40-18-19-60 38521 Saint-Egreve Cedex, France

Room 1219 Chinachem Golden Plaza ASIC/ASSP/Smart Cards Tel: (33) 4-76-58-30-00 Fax: (33) 4-76-58-34-80 77 Mody Road Tsimshatsui Zone Industrielle East Kowloon 13106 Rousset Cedex, France Hong Kong Tel: (33) 4-42-53-60-00 Tel: (852) 2721-9778 Fax: (33) 4-42-53-60-01 Fax: (852) 2722-1369 1150 East Cheyenne Mtn. Blvd.

Japan Colorado Springs, CO 80906, USA

9F, Tonetsu Shinkawa Bldg. Tel: 1(719) 576-3300 1-24-8 Shinkawa Fax: 1(719) 540-1759 Chuo-ku, Tokyo 104-0033 Japan Scottish Enterprise Technology Park Tel: (81) 3-3523-3551 Maxwell Building Fax: (81) 3-3523-7581 East Kilbride G75 0QR, Scotland Tel: (44) 1355-803-000 Fax: (44) 1355-242-743

Literature Requests

www.atmel.com/literature Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN ATMEL’S TERMS AND CONDITIONS OF SALE LOCATED ON ATMEL’S WEB SITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, CONSEQUENTIAL, PUNITIVE, SPECIAL OR INCIDENTAL DAMAGES (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, BUSINESS INTERRUPTION, OR LOSS OF INFORMATION) ARISING OUT OF THE USE OR INABILITY TO USE THIS DOCUMENT, EVEN IF ATMEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. Atmel makes no representations or warranties with respect to the accuracy or completeness of the contents of this document and reserves the right to make changes to specifications and product descriptions at any time without notice. Atmel does not make any commitment to update the information contained herein. Atmel’s products are not intended, authorized, or warranted for use as components in applications intended to support or sustain life. © Atmel Corporation 2005. All rights reserved. Atmel®, logo and combinations thereof, AVR®, and AVR Studio® are registered trademarks, and Everywhere You AreSM are the trademarks of Atmel Corporation or its subsidiaries. Other terms and product names may be trademarks of others.]
15

Similar documents

8-bit Microcontroller Application Note
8-bit Microcontroller Application Note Rev. 2527A–AVR–9/02 AVR223: Digital Filters with AVR Features • Implementations of Simple Digital Filters • Coefficient and Data Scaling • Fast Implementation of 2nd Order FIR Filter • Compact Implementation Of 8th Order FIR Filter • Fast Implementation of 2nd
8-bit Microcontroller Application Note AVR040: EMC Design Considerations Scope
8-bit Microcontroller Application Note Rev. 1619C–AVR–01/04 AVR040: EMC Design Considerations Scope This application note covers the most common EMC problems designers encounter when using microcontrollers. It will briefly discuss the various phenomena. The refer- ence literature covers EMC design i
8-bit RISC Microcontroller Application Note
8-bit RISC Microcontroller Application Note Rev. 2508B–AVR–01/04 AVR182: Zero Cross Detector Features • Interrupt Driven • Modular C Source Code • Size Efficient Code • Accurate and Fast Detection • A Minimum of External Components Introduction One of the many issues with developing modern applicati
AVR242: 8-bit Microcontroller Multiplexing LED Drive anda4x4Keypad Multiplexing Features reduced to fifteen with a bit of ingenuity,
AVR242: 8-bit Microcontroller Multiplexing LED Drive anda4x4Keypad Multiplexing Features reduced to fifteen with a bit of ingenuity, allowing the smaller 20-pin AVR to be LED Drive and a • 16 Key Pushbutton Pad in4x4Matrix • 4 Digit Multiplexed LED Display with used. The circuit diagram is shown in
8-bit Microcontroller Application Note AVR180: External Brown-out Protection
8-bit Microcontroller Application Note Rev. 1051B–AVR–05/02 AVR180: External Brown-out Protection Features • Low-voltage Detector • Prevent Register and EEPROM Corruption • Two Discrete Solutions • Integrated IC Solution • Extreme Low-cost Solution • Extreme Low-power Solution • Formulas for Compone
8-bit Microcontroller Application Note
8-bit Microcontroller Application Note Rev. 1259D–AVR–04/05 AVR134: Real Time Clock (RTC) using the Asynchronous Timer Features • Real Time Clock with Very Low Power Consumption (4 µA @ 3.3V) • Very Low Cost Solution • Adjustable Prescaler to Adjust Precision • Counts Time, Date, Month, and Year wit
  Wiring Diagram
Wiring Diagram Samsung Electronics 6-1
8-bit Microcntroller Application Note
8-bit Microcntroller Application Note Rev. 2532A–AVR–01/03 AVR243: Matrix Keyboard Decoder Features • 64-key Push-button Keyboard in8x8Matrix • No External Components Required • Wakes Up from Sleep Mode on Keypress • Easily Implemented into Other Applications • Low Power Consumption • Software Conta
  Alignment and Adjustments
Alignment and Adjustments 1. Tuner FM THD Adjustment FMOutput Antenna GND SETSSG FREQ. 98 MHz Terminal Oscilloscope Adjustment FM S.S.G Input point FM DETECTOR COIL Speaker (FM DET) Terminal output Input Output 60 dB Distortion Meter Minumum Distortion (0.4% below) (Figure 1-1) Figure1-1 IF CENTER
8-bit Microcontroller Application Note
8-bit Microcontroller Application Note Rev. 2540A–AVR–07/03 AVR104: Buffered Interrupt Controlled EEPROM Writes Features • Flexible Multi-byte EEPROM Buffer • Power Efficient EEPROM Access • Access Control on Buffers • EEPROM Buffer Rewrite Introduction Many applications use the built-in EEPROM of t
  Schematic Diagram - This Document can not be used without Samsung’s authorization -
Schematic Diagram - This Document can not be used without Samsung’s authorization - 7-1 MAIN 7-1 Samsung Electronics 7-2 FRONT - This Document can not be used without Samsung’s authorization - Samsung Electronics 7-2 7-3 DSP - This Document can not be used without Samsung’s authorization - 7-3 Sam
  TroubleShooting
TroubleShooting 9-1 Main 9-1 Samsung Electronics Samsung Electronics 9-2 9-3 Samsung Electronics Samsung Electronics 9-4 9-2 DVD Servo parts 9-5 Samsung Electronics Samsung Electronics 9-6 9-7 Samsung Electronics Samsung Electronics 9-8 9-9 Samsung Electronics Samsung Electronics 9-10 9-11 Samsung
AVR054: Run-time calibration of the internal RC oscillator
AVR054: Run-time calibration of the internal RC oscillator Features • Calibration of internal RC oscillator via UART • LIN 2.0 compatible synchronization/calibration to within +/-2% of target frequency • Alternate run-time synchronization/calibration to within +/-1% of target frequency • Support for
SERVICE Manual
DVD RECEIVER AMP HT-DB120 SERVICE Manual DVD RECEIVER AMP SYSTEM CONTENTS 1. Alignment and Adjustments 2. Exploded Views and Parts List 3. Electrical Parts List 4. Block Diagrams 5. PCB Diagrams 6. Wiring Diagram 7. Schematic Diagrams 8. IC block Diagrams 9.Troubleshooting - Confidential - ELECTRONI
8-bit Microcontroller Application Note AVR131: Using the AVR’s High-speed PWM
8-bit Microcontroller Application Note Rev. 2542A–AVR–09/03 AVR131: Using the AVR’s High-speed PWM Features • Analog Waveform Generation using PWM • High-speed Prescalable PWM Clock Introduction This application note is an introduction to the use of the high-speed Pulse Width Mod- ulator (PWM) avail
8-bit RISC Microcontroller Application Note AVR042: AVR® Hardware Design Considerations
8-bit RISC Microcontroller Application Note Rev. 2521B–AVR–01/04 AVR042: AVR® Hardware Design Considerations Features • Providing Robust Supply Voltage, Digital and Analog • Connecting the RESET Line • SPI Interface for In-System Programming • Using External Crystal or Ceramic Resonator Oscillators
8-bit Microcontroller Application Note AVR240: 4x4Keypad – Wake-up on Keypress Features Introduction
8-bit Microcontroller Application Note AVR240: 4x4Keypad – Wake-up on Keypress Features • 16 Key Pushbutton Pad in4x4Matrix • Very Low Power Consumption • AVR in Sleep Mode and Wakes Up on Keypress • Minimum External Components • ESD Protection Included if Necessary • Efficient Code • Complete Progr
Microcontrollers for Fluorescent and High Intensity Discharge Lamp Ballasts SMARTER, MORE FLEXIBLE LIGHTING SOLUTIONS
LIGHTINGMICROCONTROLLERSMicrocontrollers for Fluorescent and High Intensity Discharge Lamp Ballasts SMARTER, MORE FLEXIBLE LIGHTING SOLUTIONS Developed together with the industry leading lamp ballast manufac- turers, Atmel® microcontrollers are optimized for Linear and Dimmable Fluorescent tubes, as
AVR121: Enhancing ADC resolution by oversampling
AVR121: Enhancing ADC resolution by oversampling Features • Increasing the resolution by oversampling • Averaging and decimation • Noise reduction by averaging samples 1 Introduction Atmel’s AVR controller offers an Analog to Digital Converter with 10-bit resolution. In most cases 10-bit resolution
  IC Block Diagrams 8-1 Main
IC Block Diagrams 8-1 Main 8-1-1. FAN8082D 8-1 Samsung Electronics 8-1-2. LC87F66C8A Samsung Electronics 8-2 8-3 Samsung Electronics Samsung Electronics 8-4 8-5 Samsung Electronics 8-1-3. M62446AFP Samsung Electronics 8-6 8-7 Samsung Electronics 8-1-4. M66010FP 8-1-5. SI-8050SE 8-1-6. SI-8090JFE S
AVR053: Calibration of the internal RC oscillator Features Introduction
AVR053: Calibration of the internal RC oscillator Features • Calibration using STK500, AVRISP, JTAGICE or JTAGICE mkII • Calibration using 3rd party programmers • Adjustable RC frequency with +/-1% accuracy • Tune RC oscillator at any operating voltage and temperature • Tune RC oscillator to any fre
8-bit Microcontroller Application Note AVR230: DES Bootloader Features
8-bit Microcontroller Application Note Rev. 2541D–AVR–04/05 AVR230: DES Bootloader Features • Fits All AVR Microcontrollers with Bootloader Capabilities • Enables Secure Transfer of Compiled Software or Sensitive Data to Any AVR with Bootloader Capabilities • Includes Easy To Use, Configurable Examp
AVR106: C functions for reading and writing to Flash memory
AVR106: C functions for reading and writing to Flash memory Features • C functions for accessing Flash memory - Byte read - Page read - Byte write - Page write • Optional recovery on power failure • Functions can be used with any device having Self programming Program memory • Example project for us
8-bit Microcontroller Application Note AVR201: Using the AVR® Hardware Multiplier
8-bit Microcontroller Application Note Rev. 1631C–AVR–06/02 AVR201: Using the AVR® Hardware Multiplier Features • 8- and 16-bit Implementations • Signed and Unsigned Routines • Fractional Signed and Unsigned Multiply • Executable Example Programs Introduction The megaAVR is a series of new devices i
  Printed Circuit Board Diagram
Printed Circuit Board Diagram 5-1 MAIN 5-1 Samsung Electronics 5-2 FRONT Samsung Electronics 5-2 5-3 DSP 5-3 Samsung Electronics 5-4 JACK * RCA JACK * SCART JACK Samsung Electronics 5-4 5-5 DVD PACK * TOP VIEW * BOTTOM VIEW 5-5 Samsung Electronics
8-bit Microcontroller Application Note AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory
8-bit Microcontroller Application Note Rev. 2546A–AVR–09/03 AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory Features • Fast Storage of Parameters • High Endurance Flash Storage – 350K Write Cycles • Power Efficient Parameter Storage • Arbitrary Size of Parameters • Semi-redu
8-bit RISC Microcontoller Application Note AVR130: Setup and Use the AVR® Timers Features
8-bit RISC Microcontoller Application Note Rev. 2505A–AVR–02/02 AVR130: Setup and Use the AVR® Timers Features • Description of Timer/Counter Events • Timer/Counter Event Notification • Clock Options • Example Code for Timer0 – Overflow Interrupt • Example Code for Timer1 – Input Capture Interrupt •
8-bit RISC Microcontroller Application Note AVR151: Setup And Use of The SPI Features Introduction
8-bit RISC Microcontroller Application Note Rev. 2585A–AVR–11/04 AVR151: Setup And Use of The SPI Features • SPI Pin Functionality • Multi Slave Systems • SPI Timing • SPI Transmission Conflicts • Emulating the SPI • Code examples for Polled operation • Code examples for Interrupt Controlled operati
AVR241: Direct driving of LCD display using general IO
AVR241: Direct driving of LCD display using general IO Features • Software driver for displays with one common line • Suitable for parts without on-chip hardware for LCD driving • Control up to 15 segments using 16 IO lines • Fully interrupt driven operation Introduction As a low power alternative t
8-bit Microcontroller Application Note
8-bit Microcontroller Application Note Rev. 0938B–AVR–01/03 AVR204: BCD Arithmetics Features • Conversion 16 Bits ↔ 5 Digits, 8 Bits ↔ 2 Digits • 2-digit Addition and Subtraction • Superb Speed and Code Density • Runable Example Program Introduction This application note lists routines for BCD arith