You are on page 1of 30

AVR MICROCONTROLLERS AND ATMEL STUDIO FOR

C PROGRAMMING WITH ARDUINO ● WARWICK A. SMITH


C
AVR MICROCONTROLLERS AND ATMEL STUDIO FOR

Warwick A. Smith
C PROGRAMMING
WITH ARDUINO
PROGRAMMING
WITH ARDUINO
Warwick A. Smith lives in Technology is constantly changing. New microcontrollers
South Africa and works become available every year. The one thing that has stayed
as an Electronics Engineer the same is the C programming language used to program
and Embedded System these microcontrollers. If you would like to learn this standard
Programmer. He is a language to program microcontrollers, then this book is for you!
bestselling author of the Arduino is the hardware platform used to teach the C
books C Programming for programming language as Arduino boards are available
Embedded Microcontrol- worldwide and contain the popular AVR microcontrollers from
lers, ARM Microcontroller Atmel.
Interfacing and Open Atmel Studio is used as the development environment for
Source Electronics on writing C programs for AVR microcontrollers. It is a full-featured
Linux. integrated development environment (IDE) that uses the GCC C
software tools for AVR microcontrollers and is free to download.

• Start learning to program from the very first chapter

• No programming experience is necessary

• Learn by doing - type and run the example programs

• A fun way to learn the C programming language

• Ideal for electronic hobbyists, students and engineers


wanting to learn the C programming language in an
embedded environment on AVR microcontrollers

• Use the free full-featured Atmel Studio IDE software for


Windows

• Write C programs for 8-bit AVR microcontrollers as found


ISBN 978-1-907920-46-2
on the Arduino Uno and MEGA boards

• Example code runs on Arduino Uno and Arduino MEGA


2560 boards and can be adapted to run on other AVR LEARN
microcontrollers or boards

Elektor International Media BV • Use the AVR Dragon programmer / debugger in conjunction
DESIGN

with Atmel Studio to debug C programs


www.elektor.com
Warwick A. Smith
LEARN DESIGN SHARE

LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● S
SHARE

● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE
GN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ●
LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ●
● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE ● LEARN ● DESIGN ● SHARE
C Programming with Arduino

Warwick Smith

an Elektor Publication
LEARN DESIGN SHARE
● This is an Elektor Publication. Elektor is the media brand of
Elektor International Media B.V.
78 York Street
London W1H 1DP, UK
Phone: (+44) (0)20 7692 8344
© Elektor International Media BV 2016
First published in the United Kingdom 2016

159020-1/EN

● All rights reserved. No part of this book may be reproduced in any material form, including photocopying,
or storing in any medium by electronic means and whether or not transiently or incidentally to some other
use of this publication, without the written permission of the copyright holder except in accordance with
the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the
Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1P 9HE. Applications for the
copyright holder’s written permission to reproduce any part of this publication should be addressed to the
publishers. The publishers have used their best efforts in ensuring the correctness of the information contained
in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused
by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any
other cause.

● British Library Cataloguing in Publication Data


Catalogue record for this book is available from the British Library

● ISBN 978-1-907920-46-2
EISBN 978-3-89576-352-6
EPUB 978-3-89576-353-3

Prepress production: DMC ¦ daverid.com


Printed in the Netherlands by Wilco

Elektor is part of EIM, the world’s leading source of essential technical information and electronics products for pro engineers,
electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers
high-quality content - via a variety of media channels (e.g., magazines, video, digital media, and social media) in several
languages - relating to electronics design and DIY electronics. www.elektor.com
LEARN DESIGN SHARE
Table of Contents

Table of Contents

Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Learning C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Target Audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

AVR Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

The AVR Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Embedded Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

Choosing an Embedded System to Learn C On . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

Why use the C Programming Language? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Prerequisites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Hardware Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Arduino Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

Programmer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

The Approach Taken in this Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Some Good Advice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Accompanying Files and Support Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Chapter 1    Your First C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

1.1   Download and Install Atmel Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

1.1.1   Downloading Atmel Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

1.1.2   Installing Atmel Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.2   Download and Install a Terminal Emulator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.3   Install Template Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.4   Connecting the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1.5   How C Programs are Created . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1.6   Start Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1.6.1   Creating a New Project from the Template Files . . . . . . . . . . . . . . . . . . . . . 27

1.6.2   Building the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

1.6.3   Build Problems and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

1.6.4   Loading and Running the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

1.6.5   Program Loading and Running – Problems and Solutions . . . . . . . . . . . . . . 33

1.7   About Your First C Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

●5
C Programming with Arduino
1.8   Analysing the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

1.8.1   Characters and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

1.8.2   Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

1.8.3   Program Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

1.8.4   Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

1.8.5   Whitespace Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

1.8.6   Ending the Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

1.9   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

1.9.1   Three Text Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

1.9.2   Find the Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

1.10   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

1.10.1   Solution to 1.9.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

1.10.2   Solution to 1.9.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

1.11   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

Chapter 2    C Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

2.1   Input, Output and Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

2.1.1   Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

2.1.2   Getting Input from the User Using the scanf() Function . . . . . . . . . . . . . . . 44

2.2   Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

2.2.1   Floating Point Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

2.2.2   Character Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

2.3   Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

2.4   Field Width Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

2.5   Compiling and Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

2.6   Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

2.6.1   Compile Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

2.6.2   Link Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

2.7   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

2.7.1   Ohm’s Law Calculations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

2.7.2   Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

2.8   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

2.8.1   Solution to 2.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

●6
Table of Contents
2.8.2   Solution to 2.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

2.9   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58

Chapter 3    Comparative Operators and Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.1   Comparative Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.1.1   True and False . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.1.2   C Comparative Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

3.2   Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

3.2.1   Using if to Make a Decision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

3.2.2   Using else in Conjunction with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

3.2.3   The = Operator and the == Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

3.2.4   Using else – if in Conjunction with if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

3.3   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.3.1   Variable Compare . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.3.2   Variable Compare 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.4   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.4.1   Solution to 3.3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

3.4.2   Solution to 3.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

3.5   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

Chapter 4    The while Loop and Commenting Code . . . . . . . . . . . . . . . . . . . . . . . . . . 73

4.1   The while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73

4.2   Using if Inside the while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

4.3   The Guess My Number Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

4.4   Back to the Temperature Controller Example . . . . . . . . . . . . . . . . . . . . . . . . . . 78

4.5   Commenting Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.6   Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.6.1   Tab Settings in Atmel Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.6.2   Choosing Tab Width . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.6.3   Code Indenting Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

4.6.4   Other Aspects of Programming Style . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

4.7   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

4.7.1   Subtract . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

4.7.2   Program Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

●7
C Programming with Arduino
4.8   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

4.8.1   Solution to 4.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

4.8.2   Solution to 4.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

4.9   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

Chapter 5    Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

5.1   Your Second Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89

5.2   Passing Data to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

5.3   Passing More Than One Value to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

5.4   Passing a Variable to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

5.5   Getting a Value Back from a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95

5.6   Passing Values to a Function and Receiving a Value Back . . . . . . . . . . . . . . . . . . 96

5.7   Flashing LED Simulation Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

5.7.1   The while(1) Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

5.7.2   The long Data Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

5.8   Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

5.9   Functions Calling Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

5.10   Using Multiple Source Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

5.10.1   Adding a New C Source File to an Atmel Studio Project . . . . . . . . . . . . . . 104

5.10.2   Project with Two C Source Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . 104

5.11   Header Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106

5.12   How Functions Relate to Linking and Library Files . . . . . . . . . . . . . . . . . . . . . 107

5.13   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

5.13.1   Parallel Resistor Calculation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

5.13.2   Resistor Formula C File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

5.14   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

5.14.1   Solution to 5.13.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108

5.14.2   Solution to 5.13.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109

5.15   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

Chapter 6    Number Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

6.1   Binary Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

6.2   The Need for Binary Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

6.3   Numbering Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

●8
Table of Contents
6.3.1   A Quick Look at Decimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

6.3.2   Binary Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

6.3.3   Hexadecimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

6.4   Working with Hexadecimal Numbers in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

6.5   The ASCII Alphanumeric Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

6.6   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.6.1   Hexadecimal to Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.6.2   Print Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.7   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.7.1   Solution to 6.6.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

6.7.2   Solution to 6.6.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

6.8   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

Chapter 7    Memory and Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7.1   Memory Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7.1.1   ROM (Read Only Memory) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7.1.2   RAM (Random Access Memory) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7.1.3   Data Stored in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7.2   A Look at a Memory Chip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

7.3   How Microprocessors Access Memory and Peripherals . . . . . . . . . . . . . . . . . . . 133

7.4   Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

7.5   More on C Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

7.6   Microcontroller Memory Sizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142

7.7   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143

Chapter 8    Accessing AVR Ports in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

8.1   AVR Pins and Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

8.2   Switching the On-board LED On and Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

8.3   Controlling LEDs interfaced to a Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

8.3.1   Connecting the Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

8.3.2   Starting a New Atmel Studio Project from Scratch . . . . . . . . . . . . . . . . . . 154

8.3.3   LED Port Control Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

8.4   Increment and Decrement Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

8.4.1   Pre-increment and Pre-decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

●9
C Programming with Arduino
8.4.2   Post-increment and Post-decrement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

8.5   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

Chapter 9    I/O and Memory Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

9.1   Input Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

9.1.1   Connecting a Switch to a Port Pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

9.1.2   Reading the State of a Switch in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

9.2   Input and Output Pins on the Same Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

9.2.1   Connecting a Switch and LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

9.2.2   Programming a Switch and LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

9.2.3   The AND Bitwise Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

9.3   AVR Memory Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

9.3.1   Memory Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

9.3.2   Flash Program Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

9.3.3   Data Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

9.4   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

Chapter 10    Previous C Topics Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

10.1   Format Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

10.2   Field Width Specifiers Revisited . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

10.3   Escape Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176

10.4   Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

10.4.1   A while Loop that uses break and continue . . . . . . . . . . . . . . . . . . . . . . 178

10.4.2   The do – while Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

10.4.3   The for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

10.5   The Problem with printf() in Embedded Systems . . . . . . . . . . . . . . . . . . . . . . 182

10.5.1   Programming the Serial Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

10.5.2   Writing Serial Port Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

10.6   Nested Loops and Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

10.6.1   Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188

10.6.2   Nesting Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189

10.7   Decision Making with the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . 190

10.8   The Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

10.9   Functions and Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

● 10
Table of Contents
10.9.1   Passing an Address to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

10.9.2   Returning More Than One Value from a Function . . . . . . . . . . . . . . . . . . 195

10.10   Variables and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

10.10.1   Local Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

10.10.2   Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

10.11   Static Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

10.12   Floating Point Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

10.13   Casts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

10.14   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

10.14.1   Count Down for Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

10.14.2   LED Control using a switch Construct . . . . . . . . . . . . . . . . . . . . . . . . . 201

10.15   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

10.15.1   Solution to 10.14.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

10.15.2   Solution to 10.14.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

10.16   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

Chapter 11    Arrays and Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

11.1   Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205

11.2   Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

11.2.1   Writing to a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

11.2.2   Initialising a String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

11.2.3   C Library String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213

11.3   Arrays and Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

11.3.1   Arrays and Array Element Addresses . . . . . . . . . . . . . . . . . . . . . . . . . . 214

11.3.2   Passing an Array to a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

11.4   Strings as Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218

11.5   Writing to a String using sprintf() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

11.6   Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221

11.7   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

11.7.1   Button Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

11.7.2   Button Wait . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

11.8   Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

11.8.1   Solution to 11.7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

● 11
C Programming with Arduino
11.8.2   Solution to 11.7.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227

11.9   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228

Chapter 12    Bit Manipulation and Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . 229

12.1   Bit Manipulation with Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

12.1.1   Why Do We Need Bitwise Operators? . . . . . . . . . . . . . . . . . . . . . . . . . . 231

12.1.2   Using the Bitwise AND and OR Operators . . . . . . . . . . . . . . . . . . . . . . . 232

12.1.3   Using the Bitwise NOT Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234

12.1.4   Using the Bitwise XOR Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

12.1.5   The Left and Right Shift Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

12.1.6   The C Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

12.2   Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238

12.3   Operator Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241

12.4   Using Pre-defined Bit Names with Shift Operators . . . . . . . . . . . . . . . . . . . . . 243

12.4.1   Pre-defined Bit Names in C Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243

12.4.2   The AVR _BV() Macro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

12.5   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

12.5.1   Print Binary Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

12.6   Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

12.6.1   Solution to 12.5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

12.7   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

Chapter 13    Programming Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

13.1   Timer Counter Hardware . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251

13.1.1   Polled Timer Time Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252

13.1.2   Hardware Timer Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

13.2   The Analogue to Digital Converter (ADC) . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

13.3   The Watchdog Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

13.4   I/O Ports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

13.4.1   I/O Port Register Functionality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

13.4.2   Register Pin Toggle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

13.4.3   Enabling Pull-up Resistors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

13.5   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265

Chapter 14    Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

● 12
Table of Contents
14.1   Configuring Arduino Boards for Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . 267

14.2   Debugging Example Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267

14.3   Switching Compiler Optimisation Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

14.4   Enabling debugWIRE on the AVR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

14.5   Using the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

14.5.1   Starting the Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

14.5.2   Single Stepping and Adding Watches . . . . . . . . . . . . . . . . . . . . . . . . . . 271

14.5.3   Viewing Hardware Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271

14.5.4   Viewing and Modifying Variable and Register Values . . . . . . . . . . . . . . . . 273

14.5.5   Viewing Variables in Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

14.5.6   Stopping Debugging and Setting Breakpoints . . . . . . . . . . . . . . . . . . . . 274

14.5.7   Viewing Pin State Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

14.5.8   Viewing Variable Value Changes & Setting Conditional Breakpoints . . . . . 276

14.5.9   Stepping into a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

14.6   Enabling ISP on the AVR / Disabling debugWIRE . . . . . . . . . . . . . . . . . . . . . . 277

14.7   Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

14.8   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

Chapter 15    Interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

15.1   Handling Interrupts in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

15.2   Using the Timer Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

15.3   One Millisecond Tick Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

15.4   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

Chapter 16    Wrapping Up the C Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

16.1   Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

16.1.1   Using Structures in C Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

16.1.2   Initialising Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287

16.1.3   Pointers to Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

16.2   Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288

16.3   Enumerated Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

16.4   The typedef Declarator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293

16.5   Storage Class Specifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

16.6   Type Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

● 13
C Programming with Arduino
16.7   The goto Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295

16.8   A List of All C Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

16.9   More Preprocessor Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

16.10   C99 Variable Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

16.11   Alternative Continuous Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298

16.12   Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299

Chapter 17    C Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

17.1   Serial Port Driver Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

17.1.1   Serial Port Driver Design Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301

17.1.2   Serial Port Driver Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

17.2   Converting Numbers to Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

17.2.1   Using AVR C Library Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

17.2.2   Writing Number to String Conversion Functions . . . . . . . . . . . . . . . . . . . 309

17.3   Voltmeter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

17.3.1   Voltmeter Project Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

17.3.2   Voltmeter Project Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314

17.3.3   Enabling the printf Family of Functions to Work . . . . . . . . . . . . . . . . . . . 317

17.3.4   Principle of Operation of the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

17.3.5   Examining the Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

17.3.6   Code Timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

17.4   Stopwatch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

17.4.1   Stopwatch Project Hardware and Code . . . . . . . . . . . . . . . . . . . . . . . . . 320

17.4.2   Stopwatch Project Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

17.4.3   Stopwatch Project Code Explanation . . . . . . . . . . . . . . . . . . . . . . . . . . . 323

17.5   Where to from here? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

Appendix A   The ASCII Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

Appendix B   Arduino – AVR Port Pin Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331

B.1   Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331

B.2   Arduino MEGA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

Appendix C   Standard Arduino I/O Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

C.1   Arduino Uno . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

C.2   Arduino MEGA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

● 14
Table of Contents
Appendix D   References and Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

D.1   Book Website and Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

D.2   Datasheets and Application Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

D.3   AVR Libc Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

D.4   AVR Forum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

D.5   Additional Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

● 15
C Programming with Arduino

● 16
Introduction

Introduction

This book will teach you the C programming language using AVR microcontroller based
Arduino boards, such as the Uno and MEGA, as a teaching platform. Atmel Studio for
Windows operating systems is the IDE and software tool used in this book for writing,
compiling and loading C programs to the Arduino boards.
Atmel Studio and the standard C language are used as an alternative to the Arduino
IDE which was designed to help those with less technical knowledge of electronics and
microcontrollers to get started with microcontroller projects. Standard C is taught by
Universities as part of electronic engineering courses and is used throughout industry for
programming microcontrollers. C programming can be used on any microcontroller for
which C programming tools are available and is not restricted to Arduino. Arduino is used
with this book for teaching C as it is very convenient to use and readily available.

Learning C

Learning a new programming language and learning how to program embedded systems
covers a lot of ground as there are many things to learn: the C programming language
itself, some knowledge of microcontroller architecture, some electronics, microcontroller
peripherals, number systems and logic. There is also the software programming toolchain
and IDE to learn, which in the case of this book is the Atmel Studio IDE that uses the
GNU toolchain for AVR microcontrollers. It may seem to be a daunting task to learn about
all these aspects of an embedded system and programming language, but taking it one
step at a time as this book does makes it easy, enjoyable and a lot of fun.

Technical Terms

IDE – Integrated Development Environment. An IDE is software that provides a complete


development environment for writing and editing programs, compiling, loading programs
to the target board and debugging.
Toolchain – the C toolchain is a set of software programs that are used to convert a C
program into a file that can be loaded to a microcontroller.
Atmel Studio – an IDE that runs on Windows operating systems and is used to program
Atmel AVR and other Atmel microcontrollers. Atmel Studio can be downloaded for free
from the Atmel website.

Target Audience

This book has been written for the hobbyist, student and engineer wanting to learn the C
programming language in an embedded environment using microcontrollers.

AVR Microcontrollers

AVR microcontrollers, the microcontrollers found on Arduino boards such as the Uno and
MEGA are used in this book. The C language applies equally to other microcontrollers, but
when learning to program, a particular microcontroller architecture must be chosen as
hardware differences between microcontrollers will change how they are programmed.

The AVR Architecture

AVR microcontrollers from Atmel (www.atmel.com) are RISC microcontrollers available

● 17
C Programming with Arduino

in 8-bit and 32-bit series. These microcontrollers contain a “AVR core” that is interfaced
to memory and peripheral devices and packaged in a single chip. Only 8-bit AVR
microcontrollers are presented in this text.

Technical Terms

RISC – RISC stands for Reduced Instruction Set Computer and is a processor that is
designed with a simplified instruction set in order to produce higher performance. The
AVR architecture has been designed this way.

Atmel AVR Microcontrollers

Embedded Systems

An embedded system is an electronic circuit board that contains a microprocessor or


microcontroller and other electronic parts, running a software program and used for a
specific purpose rather than a general purpose as a Personal Computer (PC) would be
used.

Technical Terms

Microprocessor – A microprocessor is a silicon chip, also known as an Integrated Circuit


or I.C., containing a Central Processing Unit (CPU) that will fetch program instructions
from memory and execute or run them. The memory required by the microprocessor is
external to the microprocessor chip – it must be put on the circuit board by the hardware
engineer designing the system, whether it is to be used as an embedded system or
general purpose computer. There are a number of microprocessor architectures such as
Intel’s x86 architecture, ARM architecture, AVR from Atmel and many others.
Microcontroller – A microcontroller contains a microprocessor and a number of
peripheral devices such as timers, serial ports, general purpose input/output (I/O) pins,
counters, analogue inputs, etc. all inside a single silicon chip. A microcontroller will usually
have memory on the chip as well. Some microcontrollers will be able to support external
memory and others have enough memory on-chip and do not have the ability to support
external memory. Microcontrollers are designed to be used in embedded systems as their
on-chip peripherals and memory enable an embedded system designer to save circuit
board space by not having to add these items as external devices on the circuit board.

● 18
Introduction

An example of an embedded system is a calculator. It is specifically made to perform


mathematical calculations input by a user on the keypad – it is a dedicated device. A PC
on the other hand is a general purpose computer that can be used for any number of
purposes such as word processing, playing games, a file server, web server, etc. (i.e. not
an embedded system).
Other examples of embedded systems are cell phones and computer keyboards as
they contain embedded microcontrollers that give these devices their functionality.
Smartphones, however, have become more like PCs than dedicated embedded systems,
even running their own operating systems. A washing machine may contain an embedded
system that responds to button presses on its front panel and controls washing cycles.
Although Arduino boards are general purpose boards that can be used for many different
tasks, they are still embedded systems as their end use is for a specific task, such as a
temperature monitoring system, animated robot, chocolate dispensing machine, or any
other project that you can think of.

Abbreviations

Microprocessor – the word microprocessor is often abbreviated to μP.


Microcontroller – the word microcontroller is often abbreviated to μC or MCU.
μ – is the symbol for “micro” and is also used in electronic or other engineering
measurements such as microfarad μF for capacitor values.

Choosing an Embedded System to Learn C On

Arduino has been chosen as the embedded system for this book, and specifically Arduino
boards that have 8-bit AVR microcontrollers on-board and can be fitted with the standard
add-on boards called shields.
Arduino has been chosen because it is so widely available, is an open source project
and has many second-source compatible boards available. Before Arduino became
popular, a specific proprietary board would have to be recommended for any embedded
programming book. With Arduino we now have standard off-the-shelf hardware that is
open source, allowing anyone to view and modify the circuit diagram and board.
Here are some of the reasons that Arduino was chosen:
• Powered by USB – no external power supply needed
• Easy to connect hardware using an electronic breadboard and jumper wires
• USB port on-board that is configured as a virtual COM port on the PC for serial
communications
• Widely available with alternative sources and clone boards / derivative boards
• Has ICSP header for programming and debugging
In the pages that follow, you will learn to write programs that run on an Arduino board
or embedded system and switch LEDs on and off, read switches to see if they are being
pressed or released, send messages out of the serial port to be displayed on a PC and
more.

● 19
C Programming with Arduino

Arduino MEGA 2560 (left) and Arduino Uno (right)

Why use the C Programming Language?

If I think of all the technology changes that have taken place in the world of
microelectronics since the start of my career as an embedded programmer, the one
thing that has stayed the same is the C language. Processor architectures and systems
have come and gone, but the common thing between all of them is that they were
programmed in C.
The reason for the popularity and use of C is that C is a standard language and ideally
suited to embedded programming where access to the hardware of a system is required.
C programming tools produce small and fast programs – a requirement when you
have limited amounts of memory available on an embedded system. Whenever a new
processor architecture comes on the market, you can be pretty sure that there will be C
programming tools available for it.
C programming is necessary to move beyond Arduino in order to program other
embedded systems that are not supported by the Arduino IDE, as well as gain more
control over Arduino boards.
You may have heard that C is cryptic and difficult to learn, but this is nonsense. C is a
simple language with very few keywords and is easy to learn. As with any new language,
you need to become familiar with the syntax of the language. This book will explain
everything to you, starting with simple and easy programs. When I leaned to program in
C, I thoroughly enjoyed it and I hope that you will too.

Prerequisites

In order to use this book you will need to be able to operate a computer running Windows
and know how to download and install software on a Windows system.
Any knowledge of electronics will help, for example you should know what an LED is, a
switch, I.C. and a resistor. I will refer to basic electronic components such as these in
the text. If you don’t know what these items are, there are plenty of books available that
teach basic electronics. A good online resource for basic electronics, breadboard circuits
and an introduction to Arduino is: startingelectronics.org/beginners/start-electronics-now/
Any knowledge of programming in any language will help, but is not required.
Basic mathematics is also assumed. i.e. addition, subtraction, multiplication, division,
counting and a little algebra. If you are not good at mathematics, that is not a problem as
the mathematics is very basic and the programs that you will write will actually help you
to understand mathematics better.

● 20
Introduction

You will need to have an Internet connection in order to download software programs and
example programs used with this book (all software used is free).

Hardware Requirements

A standard USB cable will be needed for powering the Arduino board and communicating
between the Arduino and PC. An external power supply can be used to power the Arduino
board, but a USB cable will still be needed for serial communications with the PC.
Electronic components such as LEDs, resistors and switches will be needed and are listed
in each section of the book when they are required.
When using the Atmel Studio IDE, a programming device is needed to load programs to
the microcontroller on the Arduino board. The programmer is connected to the PC using a
USB cable and to the 6-pin header on the Arduino board labelled ICSP. More details of the
Arduino board and programmer required can be found below.

Arduino Board

An Arduino Uno with an Atmega328P microcontroller or Arduino MEGA 2560 will be


needed to follow the example programs in this book. An Arduino Uno R3 and Arduino
MEGA 2560 R3 were used when developing the code examples.
If you have not yet bought an Arduino board, I would recommend getting the Arduino
MEGA 2560 as it has more memory and input / output pins than the Uno.

Atmel AVRISP mkII Programmer and Arduino Uno

● 21
C Programming with Arduino

Programmer

An AVRISP mkII programmer (part number ATAVRISP2) or AVR Dragon programmer /


debugger (part number ATAVRDRAGON) is required for loading programs to the Arduino
board. Both of these programmers are Atmel devices and are fully compatible with Atmel
Studio.
I would recommend getting the AVR Dragon as it has debugging capabilities as well
as the ability to do HV programming that can be used to unbrick a DIP packaged AVR
microcontroller. The AVRISP mkII can only program AVR microcontrollers and has no
debugging capabilities. The AVR Dragon does not come with any cables, you will need a
standard USB cable as well as a 6-way ribbon cable with female 6-way (2 by 3) headers
on each end.
There are other AVR programmers on the market made by various manufacturers that
may be cheaper than the programmers from Atmel, but these programmers will need
external software in order to use them. They will also require extra set-up steps in order
to use them in Atmel Studio and will most likely not have any debugging abilities.

Atmel AVR Dragon Programmer / Debugger and Arduino Uno

Technical Terms

Debugging – Debugging refers to finding and fixing program errors. An embedded


programmer or software engineer will write a program and then test it to see if it works
as intended. If there is a problem with the program, e.g. “Why does the LED not switch
on when it is supposed to?”, the embedded programmer will debug the program.
AVR microcontrollers on the Arduino boards used in this book can be debugged using
the AVR Dragon attached to the ICSP header on the board. When a software program
called a debugger is run on the PC from within Atmel Studio, it accesses the embedded
system through the ICSP header via the AVR Dragon and enables the programmer to step
through the program and examine the contents of the microcontroller’s memory. This
helps the programmer to find the bug (program error) and fix it.

● 22
Introduction

The Approach Taken in this Book

This book uses free or open source software only. You will start learning to program right
from chapter 1 after installing and setting up programming tools.
The first 7 chapters of the book teach the basics of the C language, concentrating on the
language by sending output from C programs running on an Arduino to the PC for display
in a terminal window, and receiving data from the terminal window typed in by a user.
Chapter 8 and beyond builds on the C basics already learned in the first chapters and
starts to look at programs that use the microcontroller’s peripherals for more practical
embedded applications.

Some Good Advice

Take your time to learn and understand how the programs work before moving to the
next chapter. Experiment with the code by changing it to see what happens and try to
write your own programs, or modified versions of the example programs to aid in learning
as you progress through the book.
If you can’t type, learn to type while learning C. This is what I did when learning to
program and have never regretted it. I always entered the example programs by typing
them in rather than just running the downloaded example programs from disk.
Learning C and embedded systems is great fun and a big adventure, so it is my wish that
you will thoroughly enjoy using this book and learning new things.

Accompanying Files and Support Website

An accompanying zipped file is available for download from Elektor that contains all of
the C source code for the example programs in this book. Download the accompanying
files from the Elektor website at www.elektor.com by browsing for the book from the books
menu or using the search box to find the book’s page.
An accompanying website can be found at wspublishing.net/avr-c that contains extra
articles and information related to this book and will be updated with any errata found.

● 23
C Programming with Arduino

● 24
Chapter 1 • Your First C Program

Chapter 1 •  Your First C Program

What you will do in this chapter:

• Download and install Atmel Studio


• Download and install a terminal emulator program
• Install template files to make creating new projects easier
• Connect the Arduino board and programmer to a PC
• Write your first C program
• Compile, load and run your first program

1.1 • Download and Install Atmel Studio

Downloading and installing Atmel Studio is straightforward and should not pose any
problems. Brief download and installation instructions are presented below. More detailed
instructions with screen captures can be found on the supporting website at:
wspublishing.net/avr-c.

1.1.1 • Downloading Atmel Studio

Atmel Studio can be downloaded by visiting the Atmel Studio web page and scrolling
down the page to find a link to the Atmel Studio installer.
Atmel Studio web page: www.atmel.com/tools/atmelstudio.aspx
Two downloads of Atmel Studio are available – a web installer version and an offline
installer version. I would suggest downloading the offline installer version which will allow
Atmel Studio to be installed to any PC whether it is connected to the Internet or not.

 Figure 1-1  Download Atmel Studio


Click the image of the disk to the left of the chosen version of Atmel Studio (Figure 1-1)
and you will be taken to a new web page. You can now either create an Atmel account,
sign in with an existing Atmel account, or download the file as a guest. To download
Atmel Studio as a guest, enter your details and a valid email address. A link to the file to
download will be emailed to you, which you can click to open a page that contains a link
to the Atmel Studio installation file. Click the link to start the download.
The advantage of creating an Atmel account is that after Atmel Studio is installed,
updates and addons for Atmel Studio can be installed which require signing into an Atmel

● 25
C Programming with Arduino

account. If you do the download as a guest, you can always create an Atmel account
later.
When the download has finished, a file with a name such as as-installer-7.0.582-full.exe
will be found in the download folder. The exact file name may be different, depending on
whether the downloaded file is a newer version or not.

1.1.2 • Installing Atmel Studio

To install Atmel Studio, simply double-click the downloaded file to run it, which will start
the installation. You will need to agree with the license to be able to continue with the
installation. Do a standard full installation and leave the installation folder names at their
defaults.

1.2 • Download and Install a Terminal Emulator

A terminal emulator program called Tera Term is used for program input / output between
a PC and Arduino board using a USB cable. Tera Term can be downloaded from:
en.osdn.jp/projects/ttssh2/releases/
Download the zipped Tera Term file, which was at version 4.88 at the time of writing, and
named teraterm-4.88.zip containing a folder called teraterm-4.88.
It is not necessary to install Tera Term, simply copy the teraterm-4.88 folder from the
zipped file to any convenient location on the PC, e.g. to the desktop. Double-click the file
called ttermpro.exe in the teraterm-4.88 folder to run Tera Term.
It is best to enable Windows to show file extensions in the file manager to more easily
identify file types. Enabling file extensions to be visible will show the .exe extension of
ttermpro.exe as well as the .c extension of C program files.

Displaying File Name Extensions in Windows 7 File Manager

To display file name extensions in file manager, first open file manager and then click
Organize → Folder and search options to pop up the Folder Options dialog box. In
the dialog box, click the View tab. In the Advanced settings box, uncheck the Hide
extensions for known file types box. Click the OK button in the dialog box to save the
changes.

1.3 • Install Template Files

Included with the accompanying download from Elektor are two template files used to
start new projects in Atmel Studio called template_mega_dragon.zip and template_
uno_dragon.zip found in the Templates folder of the download file. To install these
template files, open your Documents folder and then Atmel Studio → 7.0 → Templates
→ ProjectTemplates and copy the files into the ProjectTemplates sub-folder.
If you are using a newer version of Atmel Studio, substitute the new version number in
place of 7.0 in the above path.
The full path to the ProjectTemplates folder is:
C:\Users\<your user name>\Documents\Atmel Studio\7.0\Templates\
ProjectTemplates

● 26
Chapter 1 • Your First C Program

1.4 • Connecting the Hardware

Use a standard USB cable to connect the AVRISP programmer or AVR Dragon
programmer / debugger to the PC that has Atmel Studio loaded on it. Drivers for the
programming device will automatically be installed.
Connect the 6-way ribbon cable from the programming device (AVRISP or Dragon) to
the 6-pin header labelled ICSP on the Arduino board. Don't connect the programmer to
the 6-pin header that is near the USB connector on the Arduino. On the AVR Dragon, the
6-way ribbon cable is connected to the 6-pin header labelled ISP. Pin 1 of the ISP header
on the Dragon is marked with a 1. Pin 1 of the ICSP header on the Arduino board is
marked with a small dot.
Connect the Arduino board to a second USB port on the PC using a second standard USB
cable. On a Windows 7 system the Arduino drivers will need to be installed if the Arduino
has never been programmed using the Arduino IDE software on the PC. The drivers will
enable the Arduino to be seen as a virtual COM port on the PC. A Windows 10 system
should automatically configure the Arduino as a virtual COM port. For more detailed
hardware connection instructions, see the supporting website at: wspublishing.net/avr-c

1.5 • How C Programs are Created

C programs are written using a text editor and saved to a file with a .c file extension e.g.
myprog.c. When the C compiler is run it reads the C text file, known as the C source file,
and creates an executable file that can be loaded to and run on the embedded system.
The text editor in Atmel Studio has "syntax highlighting" which will highlight C language
keywords making the program a lot easier to read.

1.6 • Start Programming

With the software tools installed and hardware connected, you are now ready to write
your first C program. Start by creating a new sub-folder in which to store the C programs
that you will write when following the examples in this book. The sub-folder can be
created in Documents → Atmel Studio or Documents → Atmel Studio → 7.0 assuming
that the version of Atmel Studio that you are using is version 7.0. A sub-folder called c_
book in Documents → Atmel Studio → 7.0 will be used in the text when describing how
to create new Atmel Studio projects.
You may want to install the code examples for the book that are included with the
accompanying files downloaded from Elektor, so that you can check your code against the
examples. Copy the Arduino_C folder from the accompanying zipped file to Documents
→ Atmel Studio.

1.6.1 • Creating a New Project from the Template Files

When writing a new C program using Atmel Studio, a new project must be created that
will contain the C code as well as other files that store the project settings, such as the
part number of the target microcontroller, which programming device is being used and
various settings for the C toolchain.
Start Atmel Studio, which can easily be found by clicking the Windows start button and
typing "Atmel", or by double-clicking the Atmel Studio icon on the desktop.
A new project can be created by clicking the New Project... item on the Start Page in
Atmel Studio. If the Start Page is not visible, use the top menu to open it by clicking

● 27
C Programming with Arduino

View → Start Page. Alternatively click the New Project icon on the top left toolbar or use
File → New → Project... from the top menu.
In the New Project dialog box that appears (Figure 1-2), select template_mega_dragon
if you are using the Arduino MEGA or template_uno_dragon if you are using the Arduino
Uno. If the template files do not appear in the dialog box, then refer to section 1.3 on
page 26 which explains how to install the templates.

 Figure 1-2  The New Project Dialog Box


The name of the first project is hello, so remove the text in the Name: field of the dialog
box and type hello in its place. Use the Browse... button to navigate to the sub-folder
that you created earlier to store your new projects in. Figure 1-3 shows the changes
made to the bottom of the dialog box for the first project.

 Figure 1-3  Naming the Project and Selecting the Destination Folder


Click the OK button when the project name and destination have been changed. A new
project will be created in its own folder inside the selected destination folder.
In the right panel of Atmel Studio, click the tab called Solution Explorer to display the

● 28
Chapter 1 • Your First C Program

project's files as shown in Figure 1-4.

 Figure 1-4  Select the Solution Explorer Tab


Atmel Studio should now have the main.c file open with some skeleton code displayed as
shown in Figure 1-5. If the main.c file is not open, double-click it in the Solution Explorer
pane to open it for editing.

 Figure 1-5  The hello Project Created from the Template File

● 29

You might also like