SOME ASSEMBLY REQUIRED

Similar documents
Contents. Preface. Introduction. Introduction to C Programming

BASIC INTERFACING CONCEPTS

Fredrick M. Cady. Assembly and С Programming forthefreescalehcs12 Microcontroller. шт.

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Digital Design. Verilo. and. Fundamentals. fit HDL. Joseph Cavanagh. CRC Press Taylor & Francis Group Boca Raton London New York

Introduction to C. Why C? Difference between Python and C C compiler stages Basic syntax in C

Computer Organisation CS303

Computer Organization CS 206 T Lec# 2: Instruction Sets

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

UPY14602-DIGITAL ELECTRONICS AND MICROPROCESSORS Lesson Plan

Practical C++ Programming

Preface... xxi Chapter One: Digital Signals and Systems... 1 Chapter Two: Numbering Systems... 17

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

CONTENTS CHAPTER 1: NUMBER SYSTEM. Foreword...(vii) Preface... (ix) Acknowledgement... (xi) About the Author...(xxiii)

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

AVR ISA & AVR Programming (I)

Instruction Sets: Characteristics and Functions Addressing Modes

Computer Arithmetic andveriloghdl Fundamentals

SYLLABUS UNIT - I 8086/8088 ARCHITECTURE AND INSTRUCTION SET

COMPUTER ORGANIZATION & ARCHITECTURE

Intel Architecture Software Developer s Manual

The Waite Group's. New. Primer Plus. Second Edition. Mitchell Waite and Stephen Prata SAMS

COS 140: Foundations of Computer Science

EMBEDDED Systems. Functions. MODULE- 1 C programming with data Structure Introduction to C. Array and String. Control Flow Statements In C

DEPARTMENT OF ECE QUESTION BANK SUBJECT: MICROPROCESSOR AND MICROCONTROLLER UNIT-1 PART-A (2 MARKS)

MICROPROCESSOR BASED SYSTEM DESIGN

THE 8051 MICROCONTROLLER


Programming Fundamentals - A Modular Structured Approach using C++ By: Kenneth Leroy Busbee

Run time environment of a MIPS program

acret Ameya Centre for Robotics & Embedded Technology Syllabus for Diploma in Embedded Systems (Total Eight Modules-4 Months -320 Hrs.

Honorary Professor Supercomputer Education and Research Centre Indian Institute of Science, Bangalore

CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY COMPUTER ARCHITECURE- III YEAR EEE-6 TH SEMESTER 16 MARKS QUESTION BANK UNIT-1

Automation Engineers AB Pvt Ltd, NOIDA Job-Oriented Course on Embedded Microcontrollers & Related Software Stack

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Automating with STEP 7 in STL

Lecture 4: Instruction Set Architecture

MICROPROCESSOR Architecture, Programming and Interfacing SUNIL MATHUR. Assistant Professor Maharaja Agrasen Institute of Technology Delhi

8051 Overview and Instruction Set

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

COMP3221: Microprocessors and. and Embedded Systems. Instruction Set Architecture (ISA) What makes an ISA? #1: Memory Models. What makes an ISA?

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

Instruction Set Architecture

SYLLABUS. osmania university CHAPTER - 1 : REGISTER TRANSFER LANGUAGE AND MICRO OPERATION CHAPTER - 2 : BASIC COMPUTER

Contents. 1 Introduction to Computers, the Internet and the World Wide Web 1. 2 Introduction to C Programming 26

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

C Programming for Electronic Engineers

AVR Microcontrollers Architecture

Modesto Junior College Course Outline of Record CMPSC 241

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

Design of Embedded Systems Using 68HC12/11 Microcontrollers

Architecture & Instruction set of 8085 Microprocessor and 8051 Micro Controller

Chapter 7 Central Processor Unit (S08CPUV2)


Midterm 1 topics (in one slide) Bits and bitwise operations. Outline. Unsigned and signed integers. Floating point numbers. Number representation

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Computer Organization and Technology Processor and System Structures

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

Contents. 2 Introduction to C++ Programming,

Course Description: This course includes concepts of instruction set architecture,

Assembly Language Programming of 8085

Microcontrollers. Microcontroller

Diploma in Embedded Systems

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

AVR MICROCONTROLLER ARCHITECTURTE

EMBEDDED SYSTEMS: Jonathan W. Valvano INTRODUCTION TO THE MSP432 MICROCONTROLLER. Volume 1 First Edition June 2015

Numeric Encodings Prof. James L. Frankel Harvard University

CS2214 COMPUTER ARCHITECTURE & ORGANIZATION SPRING 2014

THE 8051 MICROCONTROLLER

Language Reference Manual simplicity

Acknowledgments About the Author

William Paterson University of New Jersey Department of Computer Science College of Science and Health Course Outline

Introduction. Assessment Test. Part I The Programmer s Exam 1

This course is designed for web developers that want to learn HTML5, CSS3, JavaScript and jquery.

DHANALAKSHMI COLLEGE OF ENGINEERING DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING YEAR : III SEM : VI

ii) Do the following conversions: output is. (a) (101.10) 10 = (?) 2 i) Define X-NOR gate. (b) (10101) 2 = (?) Gray (2) /030832/31034

Mechatronics and Microcomputers. Stipendium Hungaricum 2018/2019 Autumn Semester Szilárd Aradi, PhD

С and the Programming for Multitasking. Thomas W. Schultz. Purdue University West Lafayette, Indiana

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Development Tools. 8-Bit Development Tools. Development Tools. AVR Development Tools

Appendices E through H are PDF documents posted online at the book s Companion Website (located at

Arduino Uno. Power & Interface. Arduino Part 1. Introductory Medical Device Prototyping. Digital I/O Pins. Reset Button. USB Interface.

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

C# Programming: From Problem Analysis to Program Design. Fourth Edition

ECE232: Hardware Organization and Design

Digital Fundamentals

Introduction to Computers - Chapter 4

UNIT 2 PROCESSORS ORGANIZATION CONT.

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI

Chapter 2A Instructions: Language of the Computer

Introduction to Assembly language

Quick Reference Guide

Number Systems for Computers. Outline of Introduction. Binary, Octal and Hexadecimal numbers. Issues for Binary Representation of Numbers

CN310 Microprocessor Systems Design

Assembly Language Design

When an instruction is initially read from memory it goes to the Instruction register.

CPU: SOFTWARE ARCHITECTURE INSTRUCTION SET (PART

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Transcription:

SOME ASSEMBLY REQUIRED Assembly Language Programming with the AVR Microcontroller TIMOTHY S. MARGUSH CRC Press Taylor & Francis Group CRC Press is an imprint of the Taylor & Francis Croup an Informa business A CHAPMAN & HALL BOOK

Contents Acknowledgments, xxi Author, xxiii INTRODUCTION, XXV Chapter 1 Computer Systems 1 BASIC PROCESSOR ARCHITECTURE 1 COMPONENTS OF A COMPUTER SYSTEM 2 Central Processing Unit 2 Storage 3 Input and Output 4 CLASSIFICATION OF PROCESSORS 5 CISC versus RISC 5 Functionality 5 Architecture 7 NUMERATION SYSTEMS 7 The Nature of Data 8 Binary, Octal, Hexadecimal, Decimal... 10 Conversions 12 Repeated Division Algorithm 14 Polynomial Evaluation Algorithm 14 Horner's Algorithm 14 BOOLEAN DATA 16 Boolean Operations 17 vii

viii Contents Applications of Boolean Operations 18 Mask Out, Clear, Zero 20 Set and Union 20 Toggle 21 Shifting and Rotating Bits 22 EXERCISES 23 Chapter 2 The Atmel AVR Microcontroller Family 27 THE AVR CORE 27 Instructions 28 Registers 28 Clock 29 MACHINE LANGUAGE FOR THE AVR MICROCONTROLLER 30 One Plus One Equals? 30 Load Immediate 31 Add 34 Expanding Opcodes 36 Execution Trace 37 Jump 39 AVR STUDIO 42 New Project 42 Editor 43 Assembling 43 Debugger 44 Mnemonics 48 AVR DEVELOPMENT PLATFORMS 50 STK-500 Development Kit 50 The ATAVRXPLAIN Demonstration Kit 52 INTRODUCTION TO AVR ASSEMBLY LANGUAGE S3 Assembly Language Source Files 53 Line Format 54 Additional Features 54

Contents ix SAMPLE AVR ASSEMBLY LANGUAGE PROGRAM 55 A Counting Program 56 Explanation of the Statements 57 Assembling the Program 58 Obtaining a Listing File 58 The Map File 59 Simulating Downloading Execution 59 to the AVR Processor 60 Adjusting the Clock Speed 64 EXERCISES 66 PROGRAMMING EXERCISES 68 DEMONSTRATION KIT 69 Program 2.2a: Counter 69 Chapter 3 Assembly Language 73 DIRECTIVES 75 The Assembler's Location Counters 75 EXPRESSIONS 79 Symbols and Literals 80 DATA DEFINITION DIRECTIVES 82 Operators Used in Expressions 83 Functions Used in Expressions 86 INSTRUCTIONS 87 Zero-Operand One-Operand Two-Operand Instructions 89 Instructions 90 Instructions 90 THE TOGGLER PROGRAM: SAMPLE I/O 90 AVR Digital I/O Ports and STK-500/XPLAIN LEDs 92 AVR Digital I/O Ports and STK-500/XPLAIN Pushbuttons 94 The Toggler Program 95 EXERCISES - 99

x Contents PROGRAMMING EXERCISES 102 DEMONSTRATION KIT 103 Program 3.1a: Toggler 103 Program Notes 106 Chapter 4 Integer Data Representation 109 UNSIGNED INTEGER DATA 109 CHARACTER DATA 115 SIGNED INTEGER DATA 116 Two's Complement 116 One's Complement 121 Sign and Magnitude 121 Excess-N 122 BINARY CODED DECIMAL 123 DATA VALUES IN PROGRAMS 124 Define Byte, Word, and Others 124 Data Encoding 125 ASSIGNMENT STATEMENTS 128 Simple Variables 128 Assignment 130 Register-Based Variables 132 Evaluating Expressions 134 BRANCH INSTRUCTIONS 135 EXERCISES 137 PROGRAMMING EXERCISES 142 Chapter 5 AYR Core: Startup, Reset and Memories 145 AVR RESET SIGNAL 147 Interrupt Jump Table 148 BASIC SYSTEM ORGANIZATION 150 Program Memory 150

Contents xi General Purpose Registers 150 I/O Registers and Data Space 150 Stack 151 EEPROM Storage 154 PROGRAM MEMORY 154 Program Indirect Addressing 154 DATA SPACE 158 Data Direct Addressing 158 I/O Direct Addressing 160 Data Indirect Addressing 160 Data Indirect Addressing with Postincrement or Predecrement 161 Data Indirect Addressing with Displacement 162 EEPROM 163 The Standard Include File 166 EEPROM Read 167 Defining EEPROM Data 167 Using EEPROM Data in the AVR Simulator 168 Downloading EEPROM Data to the Microcontroller 171 Writing Data to EEPROM 172 EXERCISES 173 PROGRAMMING EXERCISES 176 DEMONSTRATION KIT 177 Program 5.1a: LED Patterns 177 The Standard Include File 180 EEPROM Read 181 Defining EEPROM Data 182 Using EEPROM Data in the AVR Simulator 2 183 Downloading EEPROM Data 184 Writing Data to EEPROM 185

xii Contents Chapter 6 The Stack and Function Calls 189 STACK 189 Stack Pointer 190 Push and Pop 190 FUNCTIONS 191 Defining a Function 192 Function Call Instructions 193 Return Address and RET Instructions 195 ARGUMENTS, PARAMETERS, AND RETURN VALUES 196 Return Values 196 Arguments and Parameters: Call by Value/Call by Reference 199 Memory Allocation for Parameters 200 Register Parameters 201 Stack Parameters 204 Inline Parameters 207 LOCAL STORAGE 211 Parameter and Local Storage Access in Stack Frames 215 Fibonacci Numbers Example 215 Stack Frame Naming Conventions 218 Function Fibonacci Continued 219 Stack Cleanup: Preprocessor Automatic Parameter Removal 221 Macros 222 AUXILIARY DATA STACK (SOFTWARE STACK) 224 Macros for the Data Stack 226 Fibonacci with Data Stack 228 CALL BY REFERENCE EXAMPLE 230 EXERCISES 233 PROGRAMMING EXERCISES 238

' Contents xiii Chapter 7 Serial Communications Using the Universal Synchronous and Asynchronous Serial Receiver and Transmitter 241 RS-232 241 SERIAL COMMUNICATION BASICS 243 Mark and Space 244 Serial Data Frame 244 AVR USART CONFIGURATION 246 Baud Rate 246 Data, Parity, and Stop Bits 248 Physical Connections 251 Terminal Programs 253 XPLAIN Programming Notes 254 AVR USART TRANSMIT AND RECEIVE 255 Enabling the Transmitter and Receiver 255 Transmitting Data 256 Receiving Data 259 Disabling the Transmitter or Receiver 259 SERIAL TRANSMISSION ERRORS 260 Detecting Errors 260 POLLED I/O 262 SERIAL COMMUNICATIONS EXAMPLE 262 Transmit Time 266 EXERCISES 266 PROGRAMMING EXERCISES 268 DEMONSTRATION KIT 270 Program 7.1a: Serial Echo 270 Chapter 8 Logical Operations 275 BITWISE LOGICAL OPERATIONS Status Flags 275 276

xiv Contents USES OF LOGICAL INSTRUCTIONS 278 Clearing, Setting, and Toggling 280 Masks 281 SHIFT AND ROTATE 282 Arithmetic Shifting 284 Other Applications of Shift and Rotate 285 SPECIAL BITWISE OPERATIONS 287 AVR TIMER/COUNTER 288 Timer/Counter 1 Configuration: ATMEGA16A 289 Timer/Counter 1 Compare Match (ATMEGA16A) 290 Timer/Counter Configuration: ATxmegal28Al 292 Timer/Counter Period: ATxmegal28Al 293 TIMER/COUNTER EXAMPLE: LED BLINKER 295 EXERCISES 297 PROGRAMMING EXERCISES 299 DEMONSTRATION KIT 301 Program 8.1a: LED Blinker 301 Chapter 9 Control Structures 305 ALTERING THE FLOW OF CONTROL 306 Jump 306 Call and Return 307 Conditional Branch 307 JUMP AND CONDITIONAL BRANCH INSTRUCTIONS 308 Comparisons 310 Conditional Skip Instructions 311 SELECTION 311 If Else 312 Compound Conditions 312 Multiway Selection Structure 318 Computed Goto 319

Contents xv LOOPING 320 While, For, and Do Until 321 PSEUDOCODE DEVELOPMENT: A CHECKSUM PROGRAM 326 EXERCISES 338 PROGRAMMING EXERCISES 342 DEMONSTRATION KIT 344 Chapter 10 Interrupts 355 INTERRUPT PROCESSING 355 Interrup t Ve ctor Table 357 Interrupt Signals 358 Global Interrupt Flag 360 Interrupts and the SREG 361 GENERAL EXTERNAL INTERRUPTS 362 External Interrupt Behavior 363 External Interrupt Control Registers (ATMEGA16A) 364 External Interrupt Control Registers (XMEGA Series) 366 Other External Interrupts 367 INTERRUPT-CONTROLLED UP/DOWN COUNTER 368 TIMER COUNTER INTERRUPTS 372 AVR Timer/Counters Interrupts 373 Prescaler Units 376 1NTERRUPT-BASED SWITCH BOUNCE ELIMINATION 377 USART INTERRUPTS 385 EXERCISES 392 PROGRAMMING EXERCISES 395 DEMONSTRATION KIT 398

xvi Contents Chapter 11 Arithmetic Operations 411 ADDITION AND SUBTRACTION 41? Half-Adder 411 Full-Adder 412 Ripple Carry Adder 412 Example: An 8-Bit Ripple Carry Addition 414 Status Flags 416 Addition 416 Subtraction 417 Subtraction Using Addition 418 AVR ADDITION AND SUBTRACTION 418 16-Bit Addition and Subtraction 419 Immediate Data 421 Comparisons 423 16-Bit Support 424 Increment and Decrement 425 Negation 428 Widening and Narrowing Conversions 429 BCD ADDITION AND SUBTRACTION 429 BINARY/BCD CONVERSION 431 MULTIPLICATION AND DIVISION 433 8-Bit Multiplication 433 Extended Precision Multiplication 434 Multiplication Algorithm 436 Division Algorithm 439 NUMERIC I/O AND TYPE CONVERSIONS 443 Unsigned to ASCII 443 ASCII to Unsigned 445 SIEVE OF ERATOSTHENES 446 EXERCISES 459 PROGRAMMING EXERCISES 462

Contents xvii DEMONSTRATION KIT 464 Chapter 12 Arrays 477 DECLARING AND USING ARRAYS 477 Element Access 478 Array Traversals 479 MULTIDIMENSIONAL ARRAYS 480 Array Sizes 483 STRINGS 485 Fixed-Length Strings 485 Variable Length Strings 485 STORAGE ALLOCATION 488 Dynamic Memory Allocation 488 String Constants and String Initialization 489 String Operations 491 DYNAMIC MEMORY ALLOCATION 492 A Heap for Strings 493 STRUCTURES 503 Linked Lists 506 A BETTER HEAP 508 EXERCISES 511 PROGRAMMING EXERCISES 515 Chapter 13 Real Numbers 51 7 IEEE FLOATING POINT REPRESENTATION 517 Range and Precision 518 Decoding IEEE Single Precision Codes 519 Encoding IEEE Single Precision Codes 520 Denormalized Form 522 Infinity and NaN 523 Single Precision Range and Precision 524

xviii Contents ADDITION AND SUBTRACTION OF IEEE FLOATING POINT NUMBERS 525 Addition 525 Subtraction 526 Comparison of Floating Point Numbers 527 MULTIPLICATION AND DIVISION OF FLOATING POINT NUMBERS 528 Multiplication 529 Division 530 FIXED POINT ARITHMETIC 530 Encoding and Decoding Fixed Point Representations 531 Fixed Point Addition and Subtraction 531 Fixed Point Multiplication and Division 532 Fixed Point Notation 533 AVR FMUL, FMULS, and FMULSU Instructions 534 SUMMARY 535 EXERCISES 535 PROGRAMMING EXERCISES 537 Chapter 14 Programming the AVR in C 539 OBJECT FILES AND LINKERS 540 Standard Functions 541 Executables 542 ANATOMY OF A C PROGRAM 542 Main Function 542 Sample AVR GCC Project 543 FUNDAMENTALS OF C PROGRAMMING 55T Comments 551 Include Files 551 Functions 553 Variables 554 Allocation Classes 554

Contents xix Scope 555 Special Types 558 GCC Compiler and Variable Allocation 558 Arrays and Pointers 564 Typedef and Struct 566 Literals, Characters, and Strings 567 String Functions 567 Control Structures 568 Arguments and Parameters 569 AVR-Specific Syntax 571 Function Conventions 573 Program Memory 574 Delay Library 576 Math Library 576 ASSEMBLY LANGUAGE AND C 577 EXERCISES 580 PROGRAMMING EXERCISES 583 APPENDIX A: GETTING STARTED WITH THE XPLAIN EVALUATION KIT, 585 INDEX, 593