C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands



Similar documents
KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

8051 MICROCONTROLLER COURSE

Embedded C Programming, Linux, and Vxworks. Synopsis

Sources: On the Web: Slides will be available on:

El Dorado Union High School District Educational Services

The C Programming Language course syllabus associate level

El Dorado Union High School District Educational Services

Keil C51 Cross Compiler

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

The programming language C. sws1 1

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

Lecture N -1- PHYS Microcontrollers

SECTION C [short essay] [Not to exceed 120 words, Answer any SIX questions. Each question carries FOUR marks] 6 x 4=24 marks

Digitale Signalverarbeitung mit FPGA (DSF) Soft Core Prozessor NIOS II Stand Mai Jens Onno Krah

Freescale Semiconductor, I

I PUC - Computer Science. Practical s Syllabus. Contents

Chapter 13. PIC Family Microcontroller

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

3. Programming the STM32F4-Discovery

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

Microtronics technologies Mobile:

KS3 Computing Group 1 Programme of Study hours per week

The Answer to the 14 Most Frequently Asked Modbus Questions

Programmable Logic Controllers Definition. Programmable Logic Controllers History

Soft processors for microcontroller programming education

S7 for Windows S7-300/400

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Pre-tested System-on-Chip Design. Accelerates PLD Development

A Practical Approach to Education of Embedded Systems Engineering

EXERCISE 3: String Variables and ASCII Code

Computer Automation Techniques. Arthur Carroll

B.Sc.(Computer Science) and. B.Sc.(IT) Effective From July 2011

Java (12 Weeks) Introduction to Java Programming Language

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Tutorial for MPLAB Starter Kit for PIC18F

isppac-powr1220at8 I 2 C Hardware Verification Utility User s Guide

Von der Hardware zur Software in FPGAs mit Embedded Prozessoren. Alexander Hahn Senior Field Application Engineer Lattice Semiconductor

Dear Reader. The latest tutorial will be available from now on from the website of my consulting company Argenox Technologies:

Fondamenti su strumenti di sviluppo per microcontrollori PIC

Microprocessor & Assembly Language

Object Oriented Software Design

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

Course Name: ADVANCE COURSE IN SOFTWARE DEVELOPMENT (Specialization:.Net Technologies)

Measuring Resistance Using Digital I/O

(Cat. No L3) Product Data

Informatica e Sistemi in Tempo Reale

Lab Experiment 1: The LPC 2148 Education Board

How to design and implement firmware for embedded systems

EasyPIC4 User s Manual

Microcontroller Based Low Cost Portable PC Mouse and Keyboard Tester

2011, The McGraw-Hill Companies, Inc. Chapter 3

2.0 Command and Data Handling Subsystem

Course Title: Software Development

Embedded Software development Process and Tools: Lesson-4 Linking and Locating Software

How to read this guide

Top 10 Bug-Killing Coding Standard Rules

To perform Ethernet setup and communication verification, first perform RS232 setup and communication verification:

An Introduction to MPLAB Integrated Development Environment

Windows PowerShell Essentials

ELEG3924 Microprocessor Ch.7 Programming In C

MICROPROCESSOR AND MICROCOMPUTER BASICS

Object Oriented Software Design

In-System Programmer USER MANUAL RN-ISP-UM RN-WIFLYCR-UM

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

E-Blocks Easy RFID Bundle

About The Tutorial. Audience. Prerequisites. Copyright & Disclaimer

Lecture 7: Programming for the Arduino

Embedded Software Development: Spottbillige Hardware + OSS = Zum Spielen zu Schade!

Computer Performance. Topic 3. Contents. Prerequisite knowledge Before studying this topic you should be able to:

VB.NET Programming Fundamentals

PROGRAMMABLE LOGIC CONTROLLERS Unit code: A/601/1625 QCF level: 4 Credit value: 15 TUTORIAL OUTCOME 2 Part 1

AN10866 LPC1700 secondary USB bootloader

Computer Programming Tutorial

So far we have considered only numeric processing, i.e. processing of numeric data represented

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

Computer Programming I & II*

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

Microcontrollers in Practice

Objective-C Tutorial

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

BE635 User Manual. Rev. V Bolymin, Inc. All Rights Reserved.

Introduction to Java

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

User s Manual of Board Microcontroller ET-MEGA2560-ADK ET-MEGA2560-ADK

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Software based Finite State Machine (FSM) with general purpose processors

CGI-based applications for distributed embedded systems for monitoring temperature and humidity

by NetMedia, Inc. All rights reserved. Basic Express, BasicX, BX-01, BX-24 and BX-35 are trademarks of NetMedia, Inc. 2.

MACHINE ARCHITECTURE & LANGUAGE

UPS PIco. to be used with. Raspberry Pi B+, A+, B, and A. HAT Compliant. Raspberry Pi is a trademark of the Raspberry Pi Foundation

Fundamentals of Java Programming

Transcription:

C Programming for Embedded Microcontrollers Warwick A. Smith Elektor International Media BV Postbus 11 6114ZG Susteren The Netherlands 3

the Table of Contents Introduction 11 Target Audience 11 What is an Embedded System? 11 Why use the C Programming Language? 13 Prerequisites 13 The Approach Taken in this Book 19 Some Good Advice 19 1 Your First C Program 20 1.1 About the Programming Tools 20 1.2 Downloading the DJGPP Compiler 22 1.3 Installing the DJGPP Compiler 23 1.4 Setting Up the DJGPP Compiler 25 1.5 Installing the Compiler from the Elektor Website 28 1.6 How C Programs are Created 29 1.7 Start Programming 30 1.8 Compiling the Program 31 1.9 About Your First C Program 35 1.10 Analysing the Program 35 1.10.1 Characters and Strings 35 1.10.2 Functions 36 1.10.3 Program Statements 36 1.10.4 Preprocessor Directives 37 1.10.5 Whitespace Characters 37 1.11 Exercises 37 1.11.1 Solutions 38 1.12 Summary 38 2 C Basics 40 2.1 Setting up Programmer's Notepad 40 2.1.1 Some General Settings 40 2.1.2 A New Program to Compile 43 2.1.3 Setting up Programmer's Notepad to Run the DJGPP Compiler 45 2.1.4 A Keyboard Shortcut for the Compiler 47 2.1.5 Setting Up Programmer's Notepad to Run Programs 48 2.2 How the sticky.c Program Works 50 2.3 Input, Output and Variables 51 2.3.1 Variables 53-2.3.2 Getting Input from the User scanf () Function 55 2.4 Variable Types 57 2.4.1 Floating Point Variables 57 2.4.2 Character Variables 58 2.5 Arithmetic Operators 59 2.6 Field Width Specifiers 61 5

2.7 Compiling and Linking 61 2.7.1 Some Compile and Link Experiments 63 2.8 Errors and Warnings 64 2.8.1 Compile Errors 64 2.9 Link Errors 66 2.10 Exercises 67 2.10.1 Solutions 68 2.11 Summary 69 3 Comparative Operators and Decisions 71 3.1 Comparative Operators 71 3.1.1 True and False 71 3.2 Decisions 72 3.2.1 Using if to Make a Decision 73 3.2.2 Using else in Conjunction with if 73 3.2.3 The Operator and the = == Operator 75 3.2.4 Using else if in Conjunction with if 77 3.3 Exercises 79 3.3.1 Solutions 79 3.4 Summary 80 4 The while Loop 82 4.1 The while Loop 82 4.2 Using if Inside the while Loop 84 4.3 The Guess My Number Game 85 4.4 Back to the Temperature Controller Example 86 4.5 Commenting Programs 88 4.6 Programming Style 89 4.7 Exercises 91 4.7.1 Solutions 91 4.8 Summary 93 5 Functions 94 5.1 Your Second Function 95 5.2 Passing Data to a Function 96 5.3 Passing More Than One Value to a Function 97 5.4 Passing a Variable to a Function 98 5.5 Getting a Value Back from a Function 99 5.6 Passing Values to a Function and Receiving a Value Back 100 5.7 Flashing LED Simulation Program 101 5.8 Pre-processor Directives 102 5.9 Functions Calling Functions 106 5.10 Using Multiple Source Files 107 5.11 Header Files 108 5.12 The make Program and Make File 109 5.12.1 How the Make File Works 112 6

5.13 How Functions Relate to Linking and Library Files 112 5.14 Exercises 113 5.14.1 Solutions 113 5.15 Summary 115 6 Number Systems 117 6.1 Binary Basics 117 6.2 The Need for Binary Numbers 118 6.3 Numbering Systems 119 6.3.1 A Quick Look at Decimal Numbers 119 6.3.2 Binary Numbers 120 6.3.3 Hexadecimal Numbers 124 6.4 Working with Hexadecimal Numbers in C 129 6.4.1 Field Width Specifiers Revisited 131 6.5 The ASCII Alphanumeric Code 133 6.6 Exercises 137 6.6.1 Solutions 137 6.7 Summary 138 7 Memory and Microcontrollers 139 7.1 Memory Basics 139 7.2 A Look at a Memory Chip 140 7.3 How Microprocessors Access Memory and Peripherals 144 7.4 Pointers 145 7.5 More on C Data Types 148 7.6 Choosing a Microcontroller and Embedded System 152 7.7 Summary 155 8 Your First Embedded C Program 157 8.1 How Embedded Programming Differs from PC Programming 157 8.2 The Embedded C Programming Tools 160 8.3 The YAGARTO Toolchain 160 8.3.1 Installing YAGARTO 161 8.3.2 Testing the YAGARTO Installation 163 8.3.3 Running Eclipse for the First Time 163 8.4 Writing Your First Embedded Program 164 8.4.1 The while (1) Loop 164 8.4.2 About the Source Code 165 8.4.3 Opening the Program in Eclipse 165 8.4.4 Modifying the Program to Run on Your Embedded System 169 8.4.5 Modifying the Make File 174 8.4.6 Loading the Program to Flash Memory 174 8.4.7 A Brief Explanation of the Project Files 178 8.5 Exercises 179 8.6 Summary 180 7

9 Embedded I/O & Memory Maps 181 9.1 Loading a Program into SRAM 182 9.2 Writing to More than One LED 183 9.2.1 The Program's New C Language Elements 188 9.3 Reading the Switches and Writing to the LEDs (I/O) 189 9.4 The AT91SAM7S Memory Map 194 9.5 A Closer Look at the PIO Controller 195 9.6 Exercises 199 9.6.1 Solutions 199 9.7 Summary 199 10 The DBGU Serial Port 201 10.1 Hardware Requirements for PC to uc Serial Communications 201 10.1.1 Serial Cable 201 10.1.2 USB to RS-232 Serial Adapter 203 10.2 Programming the Serial Port 203 10.2.1 About the DBGU Serial Port Hardware 209 10.2.2 How the serial_tx Program Works 211 10.3 Using the sprintf () Function 213 10.4 Receiving Data on the Serial Port 215 10.5 Exercises 10.5.1 Solutions 10.6 Summary 218 217 217 11 Previous C Topics Revisited 219 11.1 Serial Port Driver 11.2 Format Specifiers 220 11.3 Escape Sequences 221 11.4 Loops 224 11.4.1 A while Loop that uses break and continue 224 11.4.2 The do while Loop 225 11.4.3 The for Loop 226 11.5 Nested Loops and Decisions 227 11.6 Decision Making with the sw it ch Statement 229 11.7 The Conditional Operator 231 11.8 Functions and Pointers 233 11.8.1 Returning More Than One Value from a Function 235 11.9 Variables and Scope 236 11.9.1 Global Variables 11.10 Static Variables 11.11 Floating Point Data Types 238 11.12 Casts 11.13 Exercises 11.13.1 Solutions 11.14 Summary 240 219 236 237 238 239 239 8

12 Arrays and Strings 242 12.1 Arrays 242 12.2 Strings 246 12.2.1 Writing to a String 248 12.2.2 Initialising a String 250 12.2.3 C Library String Functions 251 12.3 Arrays and Addresses 252 12.3.1 Passing an Array to a Function 254 12.4 Strings as Pointers 256 12.5 A Look at the DBGUTxMsg () Function 256 12.6 Multidimensional Arrays 257 12.7 Exercises 260 12.7.1 Solutions 261 12.8 Summary 261 13 Bit Manipulation and Logical Operators 263 13.1 Bit Manipulation with Bitwise Operators 263 13.1.1 Why Do We Need Bitwise Operators? 266 13.1.2 The Left and Right Shift Operators 269 13.1.3 The C Assignment Operators 271 13.2 Logical Operators 272 13.3 Operator Precedence 275 13.4 Exercises 276 13.4.1 Solutions 276 13.5 Summary 277 14 More Hardware Programming 278 14.1 The AT91SAM7S Timer Counter 278 14.2 The Analogue to Digital Converter (ADC) 281 14.3 Using the Timer and Interrupt 283 14.4 The Watchdog Timer 286 14.5 Summary 288 15 Wrapping Up 290 15.1 Structures 290 15.1.1 Pointers to Structures 293 15.2 Unions 294 15.3 Enumerated Type 296 15.4 The typedef Declarator 299 15.5 Storage Class Specifiers 300 15.6 Type Qualifiers 301 15.7 The goto Statement 301 15.8 A List of All C Keywords 302 15.9 More Preprocessor Directives 302 15.10 Debugging 303 15.11 Some Final Example Programs 303 15.11.1 Voltmeter 303 9

15.11.2 Voltmeter with ADC Filtering 305 15.11.3 Stopwatch 305 15.12 Summary 306 Appendix A: The ASCII Table 308 Appendix B: Source Code and Software 310 PC programs Chapter 1 to Chapter 7 310 Embedded Programs Chapter 8 to Chapter 15 313 Software Programs 316 Index 317 10