Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

•Projects

Microcontroller
BootCamp (1)
Arduino and Bascom

By Burkhard Kainka
(Germany)

In the Editorial Office we receive a fair number of


requests from electronics enthusiasts who are looking
for an easy way to get started with microcontrollers. It’s
been a good while since we last ran a large series of articles
on this subject in Elektor, and the associated hardware is vintage,
so it’s high time to run a new series. This series is aimed at our readers
who already have some experience with analog electronics and now want to start
using microcontrollers in their own circuits.

You might ask why you should use a microcon- For comparison: the NE555 timer IC
troller when it’s possible to do so much with First of all there’s the output. When you look
ordinary analog electronics. At first glance, this at the internal block diagram of the NE555 on
looks like a good question. New microcontrollers the data sheet (Figure 1), you can see that it
with more features, higher performance, higher has a push-pull output stage that can actively
clock rates and even more memory are appear- switch high and low. Microcontrollers have exactly
ing all the time, but the first demo program for the same kind of outputs. They are called ports,
every one of them invariably makes a LED blink. where the name “port” can stand for a set of
This leads to the justified criticism that you could outputs or for pins that can be configured either
get the same result by simply taking an NE555 as inputs or as outputs. The circuitry connected
timer IC and adding a couple of resistors and a to the output for a LED blinker application is also
capacitor. That’s absolutely right, and the com- the same: a LED with a series resistor, connected
parison is better than you might think because either to ground (GND) or to the supply voltage
a lot of the elements of an NE555 timer IC can (Vcc). The NE555 also has a second output driven
also be found in a microcontroller. by an open-collector transistor, which can only

30 | April 2014 | www.elektor-magazine.com


Microcontroller Bootcamp

switch something to ground. Many microcontrol- the other hand, an analog electronics solution is
lers can also emulate this function. Actually the a better and more economical choice for quite a
only difference is that microcontrollers usually few simple tasks.
have several outputs but the NE555 has only one,
since the push-pull output and the open-collector Anyone with a bit of experience in microcon-
output are not independent. troller development can also mention another
advantage of microcontrollers: once the circuit
Next we have the inputs of the NE555, which is complete, you don’t need to touch the solder-
consist two inputs to a comparator that controls ing iron again. From that point on, all you do is
an internal flip-flop. A typical application for this write and test code. Changes to device functions
is an astable multivibrator, which hobbyists often can be implemented and tested very quickly.
call a blinker circuit. You can put all this together Microcontrollers are general-purpose and versatile
in almost no time. You just plug the numbers computation workhorses. The learning curve is
into a couple of formulas to determine the right worth the effort, since you ultimately save time.
component values, and then the NE555 does Your first exposure to a microcontroller data
exactly what you want. Determining the compo- sheet may put you off, since it can easily amount
nent values for an NE555 circuit and program- to 300 pages or more. Fortunately, there are
ming a microcontroller are actually comparable
tasks. There’s another thing that’s very similar:
both devices (NE555 and microcontroller) have
VCC
a Reset input that you can use to set everything
RESET
back to the starting point. And with both devices 4 8
VCC
the Reset input is usually high in the quiescent
state and must be actively pulled to ground.
5k

DIS-
THRESHOLD 6 INHIBIT/ 7
Figure 2 shows a simple square-wave genera- R
RESET CHARGE
CONTROL 5
tor in the form typically used as an LED blinker. VOLTAGE

The NE555 data sheet also shows several other Q


5k

basic circuits, including a monostable and a pulse- FLIP-


FLOP
TRIGGER 2
width modulator—all of which are typical tasks S
3 OUT
for microcontrollers. If you further consider the
countless NE555 applications you can find some-
NE555
5k

where on the Internet, you certainly have to


agree that in many cases all you need is a 555 1
GND
timer IC and a microcontroller is overkill. For Figure 1.
everything from light curtains to servo controls Block diagram of the NE555.
or processing analog sensor signals, there are
many things that can be done with this IC and
similar devices. And you can be sure that there
are still lots of potential applications that haven’t
been worked out yet.

Reducing development time 4 8


7 RES VCC
The similarities between the NE555 and a micro-
100k

DIS
controller are summarized in Table 1. You could
3
OUT
formulate the result of a fair comparison as fol- 9V 6 NE555
THR
lows: The microcontroller has a bit more of 2 TR
1k

CV GND
everything and is therefore generally the better
5 1
choice for complex tasks. For example, a single LED

microcontroller could probably handle a task that


22u
would take ten NE555s. Above a certain level of
Figure 2.
complexity, the microcontroller solution is also Blinker circuit with the
smaller and cheaper than the analog solution. On NE555

www.elektor-magazine.com | April 2014 | 31


•Projects

you don’t entirely understand how it works. The


Table 1. Comparison of NE555 and microcontroller.
main thing is to have a couple of positive expe-
NE555 chip ATmega microcontroller
riences at the beginning, and after that it just
Switching output Port outputs
goes automatically. That’s doubtless the reason
Inputs Port inputs why the first demo task for every microcontroller
Timing control Internal timer is a LED blinker. Here as well we remain true to
by RC networks driven by crystal-controlled clock this tradition, if only to maintain the comparison
Reset input Reset input with the NE555.
Comparator inputs Comparator; analog inputs
Arduino and Bascom
PWM function PWM outputs
Microcontrollers actually come in all sorts and
Analog signal processing Analog to digital converter
sizes. At the start of a series such as this we are
Flip-flop Memory cells faced with the choice of which system to use,
and there are many different options. We spent
a long time talking about which microcontroller,
approaches to the world of microcontrollers that which circuit board (existing or new), and which
do not require you to understand everything right programming language we could specifically rec-
up front. In many programming languages the ommend for beginners. Our discussions led to
first steps are very easy. You just need to have the following proposal: hardware: Arduino Uno;
enough confidence to try something even when software: Bascom.

Arduino has become the most popular system


in the hobby environment. The programs are
developed on a PC using a simple programming
language, and they can be downloaded directly
to the microcontroller over a USB connection.
A large variety of boards and extension boards
(Arduino shields) are also available at amaz-
ingly low prices. The entire system is based on
the open-source concept, so the software and
the hardware are fully documented. The low-
cost Arduino Uno board [1] is a good choice for
Figure 3. our course because it is equipped with a widely
The Arduino Uno board. used microcontroller. The ATmega328 is an AVR
microcontroller made by Atmel. Programming this
device is quick and quite easy. The microcontrol-
Table 2. Arduino Uno at a glance.
ler has enough memory to allow relatively large
Microcontroller ATmega328 programs to be executed later on (see Table 2).
Supply voltage 5 V There is a dedicated development environment
USB port 5 V supply and device programming available for the Arduino. A development envi-
External power supply 7–12 V ronment in this context, also called an integrated
development environment (IDE), is a PC pro-
Digital I/O pins 14 (of which 6 can be used for PWM output)
gram that is used to develop programs for a
PWM channels 6
microcontroller. In the development environment,
Analog inputs 6
the microcontroller programs are typed in using
Current per I/O pin 40 mA (max.) an editor and then converted into bytes by the
3.3-V output 50 mA (max.) compiler, after which they are downloaded to the
Flash memory 32 KB (with 0.5 KB occupied by the boot loader) microcontroller.
SRAM 2 KB (ATmega328)
To ensure that the compiler understands what
EEPROM 1 KB (ATmega328)
the microcontroller is supposed to do, you must
Clock speed 16 MHz
adhere to the syntax rules of a particular pro-
Price (for Elektor Members) $39.70 / £24.75 (check  [1]) gramming language when you write the pro-

32 | April 2014 | www.elektor-magazine.com


Microcontroller Bootcamp

gram. The Arduino IDE uses a simple version of


+5V
the C programming language, which is the lan-
guage used by most professional programmers.
It also has several special commands that make
program development easier. Many users have 100n

become familiar with this programming language 20 7 21


AVCC VCC AREF
and have no trouble working with it. Neverthe- 1
RES
less, for this course we chose the Basic program-
23 14
ming language. There are many reasons for this C0 B0
24 15
C1 B1
choice. One is the Bascom Basic compiler for 25 16
C2 B2
AVR microcontrollers, which is widely used and 26
C3 B3
17
27 18
popular. The learning curve is especially easy, in 28
C4 B4
19
C5 B5
part because Bascom includes many special com-
ATmega328p
mands for sending characters from a microcon- 2
D0 D7
13
3 12
troller to a PC, for showing letters and numbers D1 D6

1k
4 11
D2 D5
on a display, and much more. However, perhaps 5
D3 D4
6

the most important reason for using Bascom is GND X1 X2 GND


that it makes you fit for all AVR controllers. The 8 9 10 22
LED
selected Arduino board (Figure 3) is used here
as a learning platform, but in the end you can 16MHz

use any desired AVR microcontroller on other 22p 22p


Figure 4.
commercially available boards or on your own linker circuit with a
boards. You also don’t have to limit yourself to microcontroller.
ATmega devices. For example, you may be able
to manage with the compact ATtiny13, which a piece of prototyping board. This means that
has only eight pins. if you wish, you can follow the entire course
without the Arduino board. However, it’s more
A special feature of the Arduino board is the USB convenient and easier for beginners to use the
port. In addition to downloading programs to the Arduino Uno board.
microcontroller as described below, you can use Listing 1 shows the Bascom program for the LED
it to send characters back and forth between the blinker. It starts off with two directives for the
PC and the ATmega328. Among other things, compiler, which define what is called the envi-
you can use this to control the microcontroller
remotely from a PC program or to send mea-
surement values captured from sensors by the Listing 1. LED blinker
microcontroller to the PC and display them on '-----------------------------------
the PC. Another thing is that the board can be 'Uno_LED1.BAS
powered over USB if you so wish. All you need
is a USB cable, and you’re ready to go.
'-----------------------------------
$regfile = "m328pdef.dat" 'ATmega328p
Your first program
One option for putting together a blinker circuit $crystal = 16000000 '16 MHz
with an ATmega328 is shown in Figure 4. To '-----------------------------------
make it easier to see what matters here, the cir-
cuit diagram shows the microcontroller without Config Portb = Output
all the peripheral circuitry of the Arduino board.
The LED is already present on the Arduino board, Do
so you don’t have to put anything together. Portb.5 = 1 'LED on
Similar simplified circuit diagrams are also used Waitms 500 '500 ms
for the other example applications described later Portb.5 = 0 'LED off
on. The idea behind this is that you can also try
Waitms 500 '500 ms
out all of these examples using a bare micro-
Loop
controller, for example on a breadboard or on

www.elektor-magazine.com | April 2014 | 33


•Projects

ronment. This is necessary because the Bascom Portb.5 = 1


compiler needs to know the target microcontroller Waitms 500
for compiling the program. The Arduino Uno is Portb.5 = 0
equipped with an ATmega328P, so the directive is: Waitms 500

$regfile = “m328pdef.dat” The individual line to the LED is switched to the


“high” voltage level (close to the supply volt-
You also have to tell the compiler what clock age of the microcontroller) by the instruction
speed the microcontroller runs at. The higher Portb.5 = 1. This causes a current to flow through
the clock speed, the faster the microcontroller the LED. The port pin is switched back to the
executes the instructions. This can be important “low” voltage level (close to the ground level) by
when dealing with things that require exact tim- the instruction Portb.5 = 0. There is also a wait
ing. An example is sending characters to the PC instruction to delay program execution. Waitms
at a particular data rate. The board has a 16-MHz 500 is self-explanatory; it causes a time delay
crystal that sets the clock speed of the controller. of 500 ms. All of this is built into a loop struc-
ture. Everything between “Do” and “Loop” will
be repeated indefinitely. The only way to stop
the program is to switch off the supply voltage
or press the Reset button.

Software: the compiler


So far, so good. But how is the program con-
verted into an executable form, and how do you
get it into the microcontroller? The Bascom Basic
compiler was developed by Mark Alberts for the
8051 family and for AVR microcontrollers. First
you have to get a copy of this PC software. It is
available from the website of MSC Electronics
[2]. You can opt for the paid full version or the
free demo version. The demo version is fully
adequate for trying out the software and for get-
ting starting with programming. It is limited to
programs that occupy up to 4 KB (4,096 bytes)
Figure 5. The corresponding directive for the compiler is: after compilation. By comparison, the Arduino
The first program in the has room for up to 32 KB. However, 4 KB is not
Bascom editor. $crystal = 16000000 peanuts; it takes a fair amount of programming
effort to fill it up. Most of the examples in this
Before you get to the actual program, there’s series will be much smaller.
another important configuration issue: the port Installing Bascom is very easy. After you launch
pins of the microcontroller can be used as inputs the program, the first thing you see is an empty
or as outputs. All port pins are initially config- Editor window where you can type in your own
ured as inputs after a restart. Here we need an program. Of course, you can also import an exist-
output, so the corresponding port must be con- ing program into the Editor (see Figure 5). These
figured accordingly: program files (with the suffix .bas) are plain text
files that can also be viewed with Windows Note-
Config Portb = Output pad or another editor program. This is called
“source code” because the compiler uses the con-
This instruction configures all six pins belonging tents of these text files as the source for compila-
to port B as outputs. The program actually only tion. The compiled program is called “hex code”
uses port pin PB5; all of the other pins remain because the bytes are often shown in hexadecimal
unused. Why did we choose PB5 in particular? notation, always with two hex characters in the
Because a yellow LED is already connected to range 0–9 and A–F per byte. Numbers in the form
this pin on the Arduino Uno board. of bits and bytes and the various notations used

34 | April 2014 | www.elektor-magazine.com


Microcontroller Bootcamp

for them will be a frequent topic in this series on doesn’t work in practice, and likewise a micro-
microcontroller programming. controller with nothing in its program memory
cannot program itself. However, this is possible if
You can type in the program in Listing 1 yourself a boot loader is already present in memory, and
or download it from the Elektor page. The file the Arduino comes with a built-in boot loader.
UNO_LED1.bas is located in the zip folder [3]. The development environment on the PC also has
Compiling the program is very easy: simply click to be able to download program code using the
Program/Compile, click the corresponding icon Arduino boot loader. Fortunately, Bascom devel-
on the toolbar (a black IC), or press the F7 key. oper Mark Alberts already guessed that some-
If there is any sort of error in the source text, one would want to program an Arduino board in
an error message will be displayed. If there are Bascom at some point in time, so this capabil-
no problems, a pop-up window shows what per- ity is already incorporated. After you configure
centage of the memory is occupied. In this case the right settings, everything is quite easy. Here
it is less than 1%, which is rounded down to 0%. we describe how this works with the demo ver-
What matters is the resulting hex file UNO_LED1. sion, since there is a small difference with the
hex or the binary file UNO_LED1.bin, which are full version.
two different file formats with the same con-
tent. They contain the executable code for the
microcontroller. Now you have to load this code
into the microcontroller’s flash memory. There
are many ways to do this, and for now we only
describe the simplest way. Other options will be
described in subsequent instalments.

The simplest way: use the boot loader


This program must be located in the microcon-
troller’s flash ROM in order to run on the micro-
controller. Flash ROM is a special type of memory,
Figure 6.
similar to EEPROM, in which electrical charges
The settings for the Arduino
ensure that the memory contents are retained
boot loader.
reliably for many decades. Flash ROM can be
rewritten repeatedly (“flash” refers to very fast
writing), so programs stored in flash memory can First you have to install the original Arduino soft-
always be altered at a later date. Special pro- ware available from [4], which includes the USB
gramming devices are available for programming driver for the Arduino Uno board. Then you con-
flash memory. They are connected to specific pins nect the Arduino Uno over a USB cable. The driver
of the microcontroller, and the program bytes are is loaded automatically, after which the Arduino
received from the development environment on Uno should be visible in the Windows Device Man-
the PC via the USB link. ager window. There you can see which COM port
However, this can also be done without a pro- number (e.g. COM2 or COM3) has been assigned
gramming device. When the Arduino board has to the Arduino Uno. If you wish you can change
a USB connection to the PC as mentioned above, the COM port number in Device Manager, but this
the program bytes can be sent to the micro- is usually not necessary. However, you should
controller over the USB link as long as there is note or write down the COM number. If you wish,
a small program running in the microcontroller you can also open the Arduino IDE and try out a
that receives the data and writes it to the micro- couple of sample programs. However, here we
controller’s flash memory. want continue straightaway with Bascom.
This small program is called a boot loader, which
is related to the expression “booting up” for start- In Bascom you can choose from a large variety
ing up a computer. This term originates from the of programming devices and boot loaders under
word “bootstrap” in the saying “pull yourself up the menu item Options/Programmer. The right
by your bootstraps”. Of course, pulling yourself setting is “ARDUINO” (not “Arduino STK500/2”),
out of the mud by tugging on your bootstraps as shown in Figure 6. It’s also important to con-

www.elektor-magazine.com | April 2014 | 35


•Projects

program is stuck. Incidentally, ESC also works


with the full version.
Now you’re done, and it’s time for action. As pre-
viously mentioned, press F7 to compile the source
code. Then press F4 to start the programmer.
Alternatively you can click the small green PCB
symbol “Program Chip”; the result is the same.
In any case, a new Programmer window opens.
There the compiled program (UNO_LED1.bin) is
already nicely loaded and displayed in the form
of hexadecimal numbers (Figure 7).

Good job: it works!


After this you can sit back and relax; the rest is
automatic. However, you can also do everything
yourself. If you opt for this, you must be very
careful because there is a function here that can
completely erase the microcontroller memory,
which means that the Arduino boot loader is also
gone. Stay well clear of anything with the word
“erase” in it. The only right option is “Chip/Write
Buffer into Chip”.
Now you will see a long chain of messages in
the programmer window, with the lovely word
“Started” at the end. During the programming
Figure 7. figure the right COM port (in this example COM2), process you can see from the activity of the Tx
The compiled program in and it’s essential to configure the right baud rate and Rx LEDs on the Uno board that a lot of data
the form of hex numbers. (115,200). This is because both parties must traffic is going on. At the end the window closes
agree on the data transmission rate in bits per again.
second (baud). We’ll come back to this subject
later on in the course when we talk about send- The downloaded program starts running imme-
ing messages and measurement data to the PC. diately after the end of programming, and the
If you only go by the Bascom help and the exam- yellow LED blinks. It may be a tiny program,
ples included with Bascom, some of your settings but it’s a big step for you as the programmer. If
will be wrong here because the Arduino Uno is you’re not quite sure about all this, try making
relatively new and the Arduino camp has only some small changes to the program. For example,
recently changed to the highest standard trans- you can change the blinking rate. For really fast
mission rate of 115,200 baud. All in the interest blinking, change Waitms 500 to Waitms 100, or
of fast data transfer and correspondingly super- for very slow blinking change it to Waitms 2000.
fast device programming. Time is scarce in our After editing the code, recompile it and repro-
fast-paced era. gram the microcontroller, and then check the
result. If the LED does exactly what you pro-
In Figure 6 you can see that the option “Auto grammed it to do, there’s no room for doubt: it
Flash” has also been ticked. This saves a mouse really works! In the next instalment we turn our
click later on, and it reduces the risk of doing attention to inputs.
something wrong. The “Terminal Emulator” option (120574-I)
is also very helpful if you want to have programs
Web Links
send messages and data to the PC later on. Once
the right settings have been selected, close the [1] www.elektor.com/arduino
window with ESC. That’s the previously mentioned [2] www.mcselec.com
difference between the demo version and the full [3] www.elektor-magazine.com/120574
version, which has an “OK” button. If you don’t
[4] http://arduino.cc/en/Main/Software
know about using the ESC key, it looks like the

36 | April 2014 | www.elektor-magazine.com

You might also like