0% found this document useful (0 votes)
63 views

Project 1 Spring 19

This document introduces the lab environment and tools for an electrical engineering project at the University of Washington. It will cover the C programming language, Arduino microcontrollers, and embedded software development. Students will complete multiple projects using the Arduino ATmega 2560 and Arduino UNO microcontrollers. The document provides background on pointers in C, the ATmega development environment, and features of the ATmega 2560 microcontroller that will be used. Understanding documentation and thorough design is emphasized for success in embedded systems work.

Uploaded by

Julius Cansino
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Project 1 Spring 19

This document introduces the lab environment and tools for an electrical engineering project at the University of Washington. It will cover the C programming language, Arduino microcontrollers, and embedded software development. Students will complete multiple projects using the Arduino ATmega 2560 and Arduino UNO microcontrollers. The document provides background on pointers in C, the ATmega development environment, and features of the ATmega 2560 microcontroller that will be used. Understanding documentation and thorough design is emphasized for success in embedded systems work.

Uploaded by

Julius Cansino
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

EE 474 Project 1

Spring 2019
Introducing the Lab Environment
University of Washington - Department of Electrical Engineering
Blake Hannaford, Shwetak Patel, Allan Ecker, Brad Emerson, Anh Nguyen, Greg Lee, Jered Aasheim, Tom Cornelius, James K. Peckol

Introduction:
This project has three main purposes. The first is to introduce the C language, the
CodeBlocks C compiler, explore some of the important aspects of the C language including
pointers and multiple file programs, then practice simple debugging. The second is to
introduce the Arduino ATMega 2560 microcomputer, the Arduino subset of the C language,
the associated development environment, and the world of embedded applications. The third
is to lay the foundation for the embedded system that we will develop over the course of the
quarter which will include the second Arduino processor, the UNO. Wow, so, when is the
final project due? Like a typical embedded system, we have a hardware piece and a software
piece. This term, our major focus will be on the software piece.
For each of the projects, you are strongly encouraged to read through the entire project
specification before trying to start designing, writing code, or exploring. The best way to
become familiar with any new piece of hardware is to take a tour of its features; the same
holds true for learning a new piece of software or programming language. This is the
approach that we will take.
We will take our first practical steps with the C language by starting with a working C
program. We will make several modifications to the program then compile using our GCC
compiler and run our versions. Next, we will work with several programs that compile and
appear to work properly and yet (intentionally) contain several common errors. Using our
skills and knowledge, we will try to identify and correct those errors. Then, we will build our
project, compile it, download it, and run it on the target platform.
We will take the same approach as we move on to more complex projects in future
assignments. As we explore the processors, we’ll begin with the documentation and discover
some of the key features of each device. This is exactly how we will do it in future when we
start to work with a new microprocessor for our company or in our graduate research.….then,
on the second day…

Prerequisites:
Familiarity with data types, data structures, as well as standard program design, development,
and debugging techniques. No beer until the project is completed. I really mean it.

Background
For us, the ATMega 2560 is a new microprocessor and development environment. Learning
new components and tools is exciting and challenging. This is one of the fun parts of
engineering. At the same time, sometimes it can be frustrating when we can’t find any good
documentation or the hardware or the software doesn’t behave the way we want it to, or

- 1 of 18 -
when, unfortunately, they behave exactly the way that we have told them to, or when we
can’t immediately find the answer to our questions and problems.
At this stage in learning about engineering and the engineering process, playing with the toys
often seems to be the most exciting part…the documentation, formal design, and so on, is,
well, often seen as rather boring. Why do I have to go through all this…why can’t I just go to
the Internet and find something like the design and make a few modifications and be done?
In the real-world, the documentation and formal design are absolutely critical parts of all
phases of the engineering development process. Doing it and doing it right can mean the
difference between success or failure of a project, the malfunction of the system following
delivery to the customer, or the possible loss of life while it is being used.
It’s also very important to recognize and to remember that the answers to most interesting
real-world engineering problems originate in our brains, discovered as we use our
imaginations and knowledge to creatively apply the underlying theory and tools; they are
reached through our persistent hard work and diligence. The solutions to challenging
problems are not sitting, ready, and waiting for us on the Internet. The Vikings didn’t
discover North America by searching some ancient version of the Internet…they took
risks…they explored and challenged. We didn’t find the solution for making the first
successful airplane flight, to putting someone on the moon, to making the first soft landing
on Mars, to designing and programming the first microprocessors, or the discovery of the
Higgs boson on the Internet and we won’t find the answers to many of today’s problems or
the projects this quarter there either. We challenge you to explore, to think, and to make
those discoveries.
Sometimes your instructor or TAs will have the answers and sometimes we won’t. As we
said in the opening, bear in mind that we’re (starting to work with) learning the environment
and tools too. This platform and development environment are complex and are new to all of
us (but we’re learning). If we all work together, to identify and solve problems as they occur,
everyone benefits and we help to build useful material and for the next classes. The answers
are there somewhere. Let’s all work to find them.
Regardless of the specific platform/environment used, embedded systems development
requires at least the following:
1. An understanding of the problem that we are trying to solve. This is the key point.
2. A target platform on which to develop the application.
3. A mechanism for programming the target platform.
Target platform is a term used loosely in industry to mean the actual piece of hardware that
performs the computation and/or control– the place where our application (the software) will
ultimately reside and run.
A target platform can be a simple a single chip (i.e. a PIC), or as complicated as a feature-
rich single board computer (i.e. a pocket sized Pentium). Despite the differences in physical
characteristics, the target platform’s purpose is the same: to execute the software written by
the developer.
A target platform can be as simple as a single chip microprocessor (i.e. a PIC or an Arduino),
or as complicated as a feature-rich single board computer (build around a multicore high
performance processor, several special purpose processors, and possibly several
programmable logic devices). Despite the differences in physical characteristics, the target

- 2 of 18 -
platform’s purpose is the same: to execute the software written by the developer. Here, and
for the typical microprocessor based application, the software will be written in the C
language. On many occasions, the C++ language or, on the opposite extreme, the processor’s
assembly language, may be used; for what are called hard real-time systems, but Java is
rarely used. On a programmable logic device like an FPGA, it may be a mixture of C and
Verilog.
The term to program here has two meanings. The first is the more traditional embedded
sense and simply means writing software to control a given piece of hardware (in this case
the target platform and any peripheral devices that may be connected to it – see above). In
the embedded world, programming the target also means storing the executable into memory
on the target. As the characteristics of the target platform can vary greatly, so too can the
mechanism used to program the target platform.
Some development environments allow one to develop directly on the target platform (much
like developing code on a PC), while others require that code be developed on another host
computer and then transferred to the target platform. We call this transfer downloading to
the target platform. Apart from the actual technique(s) used, every embedded system has to
provide a mechanism for programming it.
One of the more difficult concepts to learn in C is the notion of pointers. Once you begin to
see what they are and how they work, you’ll find that they’re actually rather straightforward.
In reality, pointers are no more complicated than a variable type whose value is interpreted
as the address of something in memory. This is exactly the same as interpreting a collection
of bits (they’re just bits) as an integer, a character, a floating point number, an interesting
picture that you downloaded from one of those special websites on the Internet (don’t
download stuff here using BitTorrent – they have no sense of humor and will shut the
university’s network down), or treating all the data on your hard drive as a really really big
integer. Weird, huh?
There is a very good explanation of pointers with accompanying drawings in your text. Once
again, take the time to read through this material.
ATMega Development Environment
The laboratory and development environment will be a combination of the traditional PC, the
CodeBlocks IDE, and the Arduino ATMega2560 and UNO (based upon the ATMega328)
microcontrollers and peripheral devices, Arduino integrated development environment (IDE).
We will work with two feature-rich single board microcomputers: the Arduino ATmega2560
and the UNO as we design and develop our project. We also have a collection of peripheral
devices that we will work with over the course of the quarter.
Some of the ATMega2560 features include:
 Clock
o 16 MHz
o 4KB EEPROM
o 8KB SRAM
 Memory
o 256 KB Flash
o 4KB EEPROM

- 3 of 18 -
o 8KB SRAM
 I/O
o 54 Digital I/O pins
o 16 Analog Input
o 4 Serial I/O
o 4 SPI
o 2 TWI
o Ethernet interface
 Debug Support
 Internal Peripherals
o 13 LED
o 15 8 bit PWM
o 16 Channel 10-bit A/D

Some of the UNO (ATMega328) features include:


 Microcontroller ATmega328
 Clock
o 16 MHz
 Memory
o 256 KB Flash
o 1 KB EEPROM
o 2 KB SRAM
 I/O
o 14 Digital I/O pins
o 6 Analog Input
 Internal Peripherals
o Test LED
o Rx/Tx LED
We have run a series of tests with the new environment and believe that all is functioning as
it should be. Yet, it is still easy to miss some things. Please let us know if you encounter
something that is not working (other than possibly your code).
In this class, we will use many of these hardware features as we design and develop the
various design projects; the details regarding each of the components will be given as
needed.
The ATMega2560 does have some limitations that we deem important to know and
understand. To address the problem, we will also be working with the CodeBlocks IDE and
the C language outside of the ATMega2560.
To program the ATmega2560 target platform, a host PC is required. The design can be
developed on the ATmega2560 using the Arduino language which is a subset of C/C++ and
compiled and run on the device. Alternately, the code can also be written off the board then
uploaded to the device, compiled, and run. The same process holds for the Uno.

- 4 of 18 -
Project Objectives
 Introduce the C language, C programs, and the PC development environment: the
basic C/C++ preprocessor, program structure, multiple file programs, pointers,
passing and returning variables by value and by reference to and from subroutines,
designing, compiling, and debugging on the target platform.
 Introduce the working environment: the embedded development environment,
debugging tools, the target platform, host PC, equipment, etc.
 Learn how to confirm that the target platform is functioning properly.
 Learn a bit about the Arduino ATmega2560 processor.
Every member of your team should do each of these exercises….not just watch. Your team
can submit one final report.
Learning CodeBlocks and the C Language – The First Steps:
We will start this lab project by taking the first steps in the formal engineering design and
development process. We will begin with the C language and the CodeBlocks development
environment on a PC.
To download and install the open source CodeBlocks development environment (IDE) on
your computer go to the CodeBlocks home page then find and run the file codeblocks-
12.11mingw-setup.exe from the binary release download page to bring in and install the
GCC compiler and GDB debugger on your computer. This IDE works well and is
straightforward to use.
We will now explore the development environment. To do so, we will begin with a known
good C program, then, we will work through the first steps of the development cycle with a
program that we write.

Working with C in the PC Environment


Building a Program
1. Using CodeBlocks on your PC, we will create a project then add the C file project1a-
2019.c which you can find in the Lab1 folder on the class web page to that project..
To start, select

- 5 of 18 -
 File →New Project

Figure 3 Code Blocks Splash Screen

2. In the pop-up window select:


 Next

Figure 4 Console Application

- 6 of 18 -
3. In the pop-up window select:
 Console Application

Figure 5 Project Type Selection

4. In the pop-up window select:


 C then Next

Figure 6 Implementation Language Selection

- 7 of 18 -
5. In the pop-up window
 Give the project a Title, Location, and filename.
 Select Next then Finish.
 Bring down the C file project1a-2019.c from the project 1 folder and enter it
into the project directory you just created.

Figure 7 Project Directory Selection

In the in the left hand pane of the workspace window that pops up
 Select main.c
Using the right mouse button, select
 Remove this file from project

Figure 8 CodeBlocks IDE

- 8 of 18 -
6. Using the right mouse button, select MyProject
 Select Add Files
7. In the window that pops up, browse to where you have saved project1a-2019.c.
 Select the file and select Open to add it to the project.
8. In the window that pops up,
 Select OK

Figure 9 Build Type

- 9 of 18 -
9. If you double click the file project1a-2019.c, you should now have something like…

Figure 9 Code Blocks IDE

10. Now compile and execute the program.


 Select Build → Build or press Ctrl-F9
There should be no errors – if there are, then bad copying.
To run the program, press Ctrl F10
The program should print the first two lines and halt waiting for input from the user.
The results printed for the value of z may be different for you project.
Here the number 23.4 is entered and the remaining lines are then printed.

the sum of x and y is 15


please enter a value
23.4
the data is 23.40
Press any key to continue

- 10 of 18 -
Building Your Own Applications
Now that you have successfully run an existing application on the target platform, this
next exercise will have you write your own program and then make a series of changes to
it to gain practice in some of the techniques that we will have to use in our more complex
applications.
Application 1
Write a program that will display the letters: A B C D console and flash them
together at approximately a one-second rate.
Application 2
Modify the program in Application 1 to print then erase the letters A then B then
C then D at approximately a one-second rate.
Application 3
Modify the program in Application 1 to flash the letters A and C at a one-second
rate and the letters B and D at a two-second rate.
Application 4
Modify the program to parameterize the two delay() function calls in the two for
loops so that they will support different user specified delays rather than the
single hard coded value as they are now. Where will those values have to be
specified?
Application 5
Modify the program so that each of the two respective for loops is replaced by the
following functions.
void f1Data(unsigned long delay1);
void f2Clear(unsigned long delay2);

Application 6
Modify the program so that parameter, delay, is passed into the two functions in
part 4 above by reference rather than by value.
Application 7
Modify the program so that the two functions in part 5 above are replaced by a
single function. The function should be able to be called with the character to be
displayed and the value of the delay.
Application 8
Modify the program so that the function you wrote in part 6 is in a separate file.
Your program will now be composed of two files.

- 11 of 18 -
Troubleshooting
1. Find the file project1b-2019.c in the Project 1 folder and upload it to your project
directory in the CodeBlocks environment.
2. The program will compile and execute apparently correctly, however, there is a
problem with it.
3. Identify what the problem is and indicate how you found it. Correct the problem
and demonstrate that you have, indeed, fixed the problem.
4. Find the file project1c-2019.c in the Lab1 folder and upload it to your project
directory in the CodeBlocks environment. The program will compile, however, it
has a problem during execution.
Identify what the problem is and indicate how you found it. Correct the problem
and demonstrate that you have, indeed, fixed the problem.

Learning the ATMega and Uno Environment and Tools – The First Steps
As our next step, we will now move to the ATMEGA 2560 and the UNO, explore the
development tools, build, and run an existing simple program on the board. The pinout
for the ATMega board are given in Figure 10 and the UNO in Figure 11.
The port used to connect to a computer are on the upper left hand side of the board.

Figure 10 Arduino ATMega 2560 Figure 11 Arduino Uno

The source code for several of the programs we will be using is in the same
directory as the project assignment; they will be the files ending with the .ino
suffix.

Working with the Development Environments and Target Platforms


Working with C in the ATMega Environment
When we move to the Arduino ATMega 2860 and UNO microcontrollers, we will work
with the Arduino development environment, IDE, and implementation of C.
To download and install the open source development environment IDE, go to the
Arduino home page:
https://www.arduino.cc/en/Main/Software

- 12 of 18 -
Then find and select the appropriate installer for your environment and run the file. This
will bring in the IDE, and the GCC compiler. This IDE works well and is straightforward
to use.
Our next step is to connect the ATMega to the PC. Now is as good of a time as any to do
that. Now, select and open the Arduino IDE amongst the installed programs on your PC.
You should now see the IDE workspace.

Figure 12 Arduino IDE Screen

Now, select
 Tools
then
o Board: “Arduino/Genunio Mega or Mega 2560”
 “Arduino/Genunio Mega or Mega 2560”
We’re off to a good start. Cool.
When you connected your Arduino board to the PC, the Arduino IDE created a virtual
com port (communications port). This is a connection that functions like a serial port on
your PC and it is how your PC talks with the ATMega and vice versa.
Now, select
 Tools
o Port:
 COM5 (Arduino/Genunio Mega or Mega 2560)

- 13 of 18 -
Your com port number will probably be different. In any case, select that one, not COM1
or COM2 if they appear.

Getting to Work
Now, the ultimate task for all electrical engineers: Getting an LED to flash…this will be
our first Arduino program…
Select
 File
o Examples
 01.Basics
 Blink

Figure 13 The Blink Program

A new window gets created and opened. The window that opened contains all of the code
for the Blink program. In Arduino jargon, such a program is called a sketch.
Start by identifying the major functional blocks…you should be able to identify 3.The
program is well annotated, so read through and understand what each line of code does.
Don’t get too excited, though. This is an example program…all your programming
assignments will not be this easy nor will they be hidden here.
An important thing to notice here is that, unlike the traditional C program, an Arduino
sketch does not have a top-level main() function. Rather, it has a top-level loop()
function (delimited by the two {} that we call curly brackets….ahhhh, that’s because they
are curly).

- 14 of 18 -
Once the loop() function is entered, the program will run forever. Well, not if you turn
power off, of course, or jump up and down on your Arduino board. It’s pretty cool, but,
it ain’t that cool.
Our next step is to compile the program into a form that the ATMega will understand. It’s
trilingual – it understands Italian, English, and C assembly. we’re going to go for C
assembly for the moment.
Select
 √ in the second toolbar
This will appear as Verify in the toolbar. Selecting the symbol will compile the program
for you. If there are no errors, you will have a notification similar to that shown in the
lower pane of the window.

Figure 14 Compiling the Sketch

- 15 of 18 -
If there is an error, the bottom pane will contain a message such as shown. If there are
errors, we….strike that, you will have to fix them before proceeding. If there aren’t,
then, we are ready for the next step. Downloading the code into the microcontroller.

Figure 15 A Compile Error

Select → in the toolbar

Figure 16 Downloading the Compiled Code

- 16 of 18 -
then, we are ready for the next step. Downloading the code into the microcontroller.
Despite the fact that the symbol indicates Upload, we are really Downloading to the
microcontroller. If you upload, the 1’s and 0’s have to get a running start to get up the
wire to your processor – this slows them down. On the other hand, by downloading, they
are going downhill and will get to your processor much sooner. Just make sure that they
don’t get going too fast, or they might over run the processor and spill out onto the floor
as shown. We don’t want them all over the lab floor. If you do spill a bunch, get a bit
bucket from stores and broom to clean them up.

111001
0 10 1
10

When the download is complete, the program will start running on the target processor –
the ATMega 2560. In this case, an LED on the board will start blinking. Wahoo!!!! An
engineer’s initiation and success…cool.

Building, Editing, and Running a Program


Let’s now modify an existing project.
1. Install the TFT display onto your ATMega board. See Figure 23 in the User
Manual on the class webpage under Documentation /TFT-Display….
2.8inch touch screen user manual(Arduino-EN)V1.00.2017.03.22.pdf
2. Find the file project1d-2019.ino in the Project1 folder, bring it down and open the
file on the ATMega,
3. Annotate each line of the main program to identify its purpose in the program and
what it does (these really are different).
4. When you are finished, save the program.
5. Compile your program and download to the ATMega.
6. Execute your program. There should be no errors – if there are, then bad copying.
The program should count in decimal according to the following pattern. Note that
the following is actually 10 iterations through the program and the program’s output
will appear on only one line on the console display….at time t0, the value 9 is
displayed…at time t1, the values 9 8 are displayed and so forth.

The value of i is:

t0: 9

t1: 98

t2: 987

- 17 of 18 -
t3: 9876

t4: 98765

t5: 987654

t6: 9876543

t7: 98765432

t8: 987654321

t9: 9876543210

7. Port Applications 4-8 that you developed for CodeBlocks to the ATMega 2560.

Working with C in the ATMega and UNO Environments


We now bring the Arduino ATMega 2560 and UNO microcontrollers together. We will
continue work with the Arduino IDE.
Create a second instance of the IDE. Connect one instance to the ATMega and the other
to the UNO. As we did with the ATMega earlier, now configure one of the IDEs for the
ATMega and one for the UNO. Find the sketch SerialTest0-mega.ino in the project folder
and download it to the ATMega. Find the sketch SerialTest0-uno.ino in the project folder
and download it to the UNO. Under the tools menu for each of the IDEs, start the tool
Serial Monitor. Connect pin 18 (TX1) on the ATMega to pin 0 (RX) on the UNO and
pin 19 (RX1) on the ATMega to pin 1 (TX) on the UNO.
The characters A B C should be displayed on the UNO Serial Monitor.
You are now communicating between the two microcontrollers.

Deliverables
A project report containing
1. The annotated source code for all CodeBlocks and the ATMega Processor
applications.
2. Representative screen shots showing the results of executing the applications on the
PC screen. Representative screen shots means that you need to visually document
that you got the programs to compile and run on your system and that your design
was able to meet the project requirements and specifications.

- 18 of 18 -

You might also like