You are on page 1of 34

CoreChart / Robot628

1 Day Workshop

INNOVATIVE CONTROL SOLUTIONS

Office: 51 Byron Place, Adelaide, South Australia 5000


Email: shortcourse@elabtronics.com Web: http://www.elabtronics.com
Telephone: +61 8 8231 5966 Fax: +61 8 8231 5266

bitset Icon Assembler was selected as a Winner of World Congress on IT WCIT2002


Secrets of IT Innovation Competition
Preface

Upon completion of this Microcontroller Development Course, the user should have an
understanding in the basics of how a microcontroller works. It will also introduce
elabtronics’ CoreChart, an icon based assembler program designed to simplify the
programming of microcontrollers for beginners, while still offering all the functionality
of text-based programming for more experienced users.

The course is structured in way that an aspect of the microcontroller is explained, and
then a similarly themed practical is undertaken for the user to become more familiar
with the concepts presented. This will hopefully avoid making beginners feel
overwhelmed by having to consume large amount of information at one time.

Role of Microcontrollers in our life


Quote from Dataquest Industrial Analyst (USA): “In 1999 the embedded market used
approximately 5.2 billion microcontrollers a year, compared to 346 million
microprocessors for PC, or 15 times as many microcontrollers as microprocessors”.

You might ask: What do I need to know about Microcontroller thingy’s? The answer is
quite simple – nothing. These machines will go about their tasks silently and without
complaint. They never tire, get hungry, distracted, or disgruntled and never forget what
they are supposed to do (unlike most of us on occasion).

These are your remotes, watches, calculators, CD players, Gameboys, sensor lights,
alarm systems, stereos, VCR’s, Digital cameras, washing machines, auto doors, GPS’s,
depth sounder’s, bar code readers, pace makers, and millions of other devices that we
take for granted.

3 million cows in Australia will be tagged with microcontrollers (similar to the one used
in the ledfun), which will be used to gather important health information of the cows.
Millions of smart cards are made from these chips.

In the world of big business the emphasis is on who can produce the most powerful chip
for the smallest amount of money.

The result has been shift in industry over the last 40 years that has been unequalled
since the development of the internal combustion engine. Computers the size of your
desk (1980) used to boast of 2000KB (2,000,000b) of memory, today it is an easy task
to find single chips that exceed that by far. The PIC16F628 has 2KB and is about as
long (size) as its name “PIC16F628” (Times “12”) if not a little smaller.

Consumers should be a part of this revolution. There is no reason to be waiting for the
latest ‘product from overseas’ when the average person could be talented enough to
produce the next Gameboy, MP3, Furby or Poo Chi dog. Even better, the nature of the
technology is such that one person or a team working away in the corner of a room can
produce results that are the equal of the industrial giants.

2
Microcontrollers, or “computers on a single chip”, play a crucial role in most industries.
The ICT industry is one that is experiencing extreme growth. Microcontrollers have
been used to build web servers the size of five cent coins. These web servers are being
used for Internet Connectivity in equipment and products, such as air-conditioners that
can be turned on with a phone call or refrigerators that order you fresh milk when the
expiry date is past it’s “Use by” date. These omnipresent microcontrollers are found in
practically every industry, such as telecommunication, food processing, bio-electronics,
ICT and automotive.

What is a Microcontroller?

Like most computers, microcontrollers (MCs) are simply general-purpose instruction


executors. Like the computer, the effectiveness of an MC depends amongst other things
upon the program of instructions provided to it and hence the skill of the programmer.
A program is a series of instructions that step the computer through a sequence of
simple data manipulations to accomplish useful tasks.

In all cases MCs are small stand-alone computer systems designed for the purpose of
control whereas a personal computer is primarily designed to process large amounts of
data quickly. An MC has all of the components on a single chip, such as CPU, RAM,
ROM, inputs and outputs. A microprocessor is simply a CPU and needs the RAM,
ROM, inputs and outputs to be added to the system.

A typical Microcontroller system A simplified Personal Computer system

Keyboard

mouse

3.5 Floppy

Figure 1. Above shows the typical structure of a microcontroller and personal computer
system. Note the similarities.

Why elab628?

Apart from being fun to use, the elab628 kit and concept is particularly suited to
educational activities as it is useful and versatile, while still remaining inexpensive.

This provides the opportunity to implement all sorts of programming and design
possibilities, in electronics, robotics and control technology.

3
World class elab628 industrial controllers and data loggers have been implemented by
students and professional engineers for various industries. The elab628 users are using
the development board in conjunction with the latest cutting edge technology in
hardware and software engineering.

Features of the elab628 microcontroller development board

• It is a simple and stand alone concept.


• Easily constructed by users who have basic soldering skills.
• Supplied with all construction information making it easy to assemble and test.
• Kit supplied with a single sided circuit board and all components including a
PIC16F628 microcontroller (MCU)
• No specialized parts, except the actual MCU.
• Uses an MCU which can be electrically programmed and erased over 100,000
times. (No expensive ‘windowed’ development parts.)
• The MCU is not removed from its socket during the programming or running of
an application thus preventing damage to fragile IC pins.
• The programmer and control circuitry are integrated onto the one circuit board.
• No external 15V supply required for programming purposes.
• The arrangement of input and output ports with screw block terminals makes
connection to applications easy.
• Eight lines wired for the input and output of digital signals (PORTA).
• Eight lines available, as inputs or outputs, for LED indicators or for interfacing
to relays (PORTB).
• Various software programs for compiling, simulating and downloading to the
MCU are available as shareware. Free support from www.microchip.com
• Low Cost

Hardware Options

Let us take a closer look at the elab628 Board. The elab628 has been designed to be
as flexible as possible and incorporates many hardware options.

It is important to make yourself familiar with the PIC16F628 microcontroller as much


as possible. Don’t worry if you don’t understand everything the first time you read it
through. Most of the detail will become clear to you as you work through these
tutorials. The full technical data sheet for the PIC16F628 is available on the elab628
CD, along with many others.

Secondly, the hardware on the elab628 Programmer has options. The controller has 2
Ports, Port A (8 bits) and Port B (8 bits). These correspond to the I/O port numbers on
the PIC16F628 and can individually programmed to be either inputs or outputs.

Note: PIC stands for Peripheral Interface Controller

4
Serial port

Port B
Port A B0-B7
A0-A7 Shown as
TS0-TS7

LED’s

Port A has a set of Resistive Dividers with a pulldown resistor going to 0V. These can
be used to set a trip voltage to trip an input either high or low. For example you need to
check if an input is above 12V or under 12V – resistors can divide the 12V down to the
1V - 1.5V PIC16F628 trip voltage (voltage divider). This will then trip the pin when it
is above 12V. This can then be used to control another external component for
regulation. Port A may be configured for both analog and digital input via internal
settings in the PIC.

Port B has been provided with special options if used as an output. It can directly drive
LEDs, or Transistors can be used instead to control relays or similar devices connected
to the Port B Connectors. Relays may be used directly on the board although you must
utilize a protection diode to prevent voltage spikes caused when relay contacts break
(back emf).

The other major option is the use of the Serial Port for your own PC interface. A
program can be written to control the RB6 and RB7 bits of Port B on the chip to send
information to or from the PC. Software would need to be developed on both the
elab628 and the PC.
We will look at the hardware further later, for now let’s take a look at how to use the
software.

Example Software and Samples


The “sample programs” folder created by your teacher will have examples of previously
created programs. The programs are provided with extensive comments for your own
modification and use.

elab628 Software
This course is based on the CoreChart icon assembler program that is found on your
desktop. Double click the icon to open the program.

elab628 Hardware
This course is based on the elab628 development kit by elabtronics. For the
purpose of your learning you will be provided with a elab628 training board set up
to operate the learning area you are working in.

5
Practical 1a
Programming the elab628
Now it is time to install an example BST file ( which is found on the hard-drive of this
computer) into the microchip. A “BST file” is one that contains instructions in a format
suitable for programming PIC chips. This will test if the hardware and software are all
working properly. Following the steps and diagrams below will serve as a diagnostic
procedure for your project, as well as showing you how to load a program into the chip.

Step 1: Connect elab628 to PC

Connect the supplied DB9 Serial Cable from the COM port connector on the
elab628 Programmer to an available COM port on your computer. You will need
to know which COM port it is (COM1 or COM2) so that the software can be
configured correctly.

Step 2: Open programming software

1. Make sure the CoreChart Program is installed and run CoreChart.

2. Make sure the elab628 is in the Programming Mode (PROG) by switching the
program/run switch towards the PROG on the board.

LD

Run Program

Push

6
3. Make sure that CoreChart is set to 628 mode – it should say CoreChart628 on
the top. If this is not set, then go to ‘Options’ / Select chip model

4. A dialog box will know appear – Select P16F628- then click OK.

Note: Other varieties of Chip


can be programmed with
Bitset just by changing models

7
5. Make sure your COM Port setting is set correctly to the port you plugged the
serial cable into. The setting is changed at: ‘Options’ / ‘Hardware Settings’. In most
cases your computer will be configured correctly.

Note:
If your computer displays the machine code
when you click “Send to PIC” then the setting
in configuration “Show Full IC Prog” must be
changed.

Just click on “Show Full IC Prog” until the


“Tick” disappears

A dialog box will appear, the correct setting is: JDM Programmer, your COM PORT
(look if you don’t know) and ‘Direct I/O’ is for Windows 95 or 98 (Use Windows API
on other versions of Windows )

Step 3: Load sample file

8
On the left hand top menu select “File”. Position the pointer onto Open and left
click the mouse.

Ask your teacher where the samples are stored and select the “Sample Program for
628” folder. You should see a file, “Program1.BST”.

#1

#2

9
Step 4: Program chip

Go to “Options” and click “Send To Pic”. The Program will now transfer the
Sample program “Program1.bst” to your hardware and verify it by reading the
software back off the PIC.

Lots of little dialog boxes will


appear and disappear.

Don’t worry

Unless you see this.

10
Trouble shooting
If the programmer fails to take a program it is unlikely that the unit is “Defective”. It is
more probable that there is minor error with your setup.

Check the following:


Cables - Make sure that the DB9 serial cable is connected securely to the Com port on
the eLab628 and the correct (1 or 2) Com port on the computer.

Switches – Check that Program/Run switch is in the program position

Settings – Ensure that the settings are as shone in Step 2 #5 – You may try adjusting the
I/O delay to improve performance.

Reset Button - In some circumstances this button has “stuck” ,just check it.

If you assembled your own elab628 you might want to check your soldering.
If the Programmer still fails to accept a “known” working program refer the problem to
your teacher.

Step 5: Run program

Test your elab628 Programmer. Switch the elab628 Programmer into Run Mode
by switching the program/run switch towards the ‘RUN’ label on the board. Make
sure power is connected to the board or the board is plugged into the serial port.
Press the Push Button and the LEDs should light up in order and the buzzer should
beep when the light reaches the end. When the button is released and the LDR
( Light Dependant Resistor ) is covered, the LEDs will light up in reverse order.

LDR

Run

Push Button

11
If you have any problems, check the hardware and check each step carefully.
Otherwise you are ready to go on to Practical 1b.

Note: During certain operations which place a large load on the serial port power supply
the supply voltage will drop below the required amount and it will fail (brown out). This
is because the port can only supply a very limited amount of power (about 150mA). If
the board is fitted with transistors, relays and LED’s then it will be very easy to do.
Connect an external power supply to prevent a “brown out” condition.

Practical 1b
Editing and Renaming a PIC Program using CoreChart Software
In this practical you will learn how to edit an existing CoreChart program. The file
you will modify is “program1.bst”. After editing this program you will rename the
modified program to a name and file of your choice (e.g. "bob’sfast.bst”).

Example

Bob’s fast.bst

As in Practical 1a, this new program file will be programmed onto the PIC16F628
chip (on the elab628 board), and tested. You will see the results with your own eyes!

Step 1: Opening the program1.bst program

Run the CoreChart program. Go to “File” and Open the folder: “Sample
Program for 628”. Load the program: “Program1.bst” (same procedure as
Practical 1a)

12
Step 2: Editing and Renaming the program1.bst program

You should now have the icons for the program1.bst showing on the screen.

• Select (Click on) the “Assignment” icon block on the right hand side of the
window

• Set the icon variable to “w”


• Set the value of the assignment to “10”
• Move the cursor just above icon 14(Delay), a small box will appear and click
• The icon should now read (14) “w=10”

13
• Select another “Assignment” icon and set it to read “y = w” place it on top of the
‘call delay’ icon (15)

• Save the program in your folder as “Program 1b”


• Re-program(same procedure as Practical 1a) the PIC16F628 microchip, move
the mouse pointer to the menu bar and select Options Æ Send to PIC (if you
haven’t saved the program yet it will ask you to do this first).

If time permits, modify the delay number used in this program (w=10) by changing the
number 10 to 50, and test the results.

Was there any change?

Can you figure out why?

How could we create a larger delay than can be provided from a single
delay?

14
SECTION 2

(For advanced students)


The previous section described the role of MCs and how they affect us.
This section will detail the elab628 development board specifically, and
describe the hardware further.
Figure 2 below shows the track layout and component positions on the
PCB.

Figure 2: elab628 PCB layout


Figure 3 below shows a simplified block diagram of a microcontroller.

15
elab628 Hardware Description

Power

The PIC16F628 requires a 5V DC supply; actually, any voltage from 4.0 to 6.0 Volts
will do fine, so you can run it from three or four 1.5V cells. The PIC chip consumes
only 1 mA - even less, at low clock speeds - but the power supply must also provide the
current flowing through LEDs or other high-current devices that the PIC chip might be
driving.

The circuit relies on a 0.1µF capacitor from V+ (pin 14) to ground (pin 5), mounted
close to the PIC chip, to protect the chip and adjacent components from electrical noise.
This capacitor should always be present, no matter how constant the DC supply is. This
is capacitor C3 on the elab628 Board.

Reset

The MCLR pin (4) is normally connected to V+ (pin 14) through a 10K resister.
Grounding the pin momentarily will clear the RAM and reset the PIC. If your power
supply voltage rises slowly, the MC may start up in a confused state. A normally open
reset button, between MCLR and ground, is added to prevent this from occurring and
will also provide a circuit reset switch.

Clock

The clock is a small oscillator used to generate a signal voltage for stepping the MCU
sequentially through a program. Clock frequencies may vary from the DC level up to a
number of megahertz (MHz). At DC the MCU will be stalled with no operations taking
place.
OSC1/4

18 17 16 15 14 13 12 11 10
VDD
OSC1 OSC2

V
a PIC 16F84
PIC16F628
OSC 1

OSC 2

OSC 1 1 2 3 4 5 6 7 8 9
1

C
Values fo F OSC / 4

Figure 4: RC oscillator

The PIC16F628 chip has the input clock signal divided by 4. For almost all operations,
each instruction in a program will take one of these clock cycles to be executed. For
example, a clock crystal frequency of 4MHz is internally divided to 1MHz. This means
that each instruction will take 1 millionth of a second to execute (1µs.)

16
The ability of the PIC chip to take one internal clock cycle for each instruction is
particularly useful when calculating specific time intervals in real time applications.

The PIC16F628 offers a choice of clock configurations. Internal circuitry is provided


for both crystal and RC (Resistor-Capacitor) operation. The elab628 uses the RC
option, as shown in figure 4, running at approximately 4Mhz. This is internally divided
to 1Mhz. It is worth noting that the clock frequency can be changed easily by varying
the R and C constants. Using a lower clock frequency can be useful when teaching
students the programming of longer time intervals as the number of ‘time wasting’
loops is decreased.

Digital Electronics / Binary numbers

The word “digital” refers to electronics that use “digits” for processing information.
“Digits”, or numbers, could be 3 or 6 or even 2. In fact we normally use digits
anywhere between 0 to 9.
Electronics that are not digital are referred to as “analogue”. An example of an
“analogue” signal is say a voltage: The voltage at any time may vary between 0 to 5
Volts. This is an “analogue” signal.
For electronic computers such as the PIC chip on the elab628, it is easier to have just 2
digits, such as 0 and 1. The circuit can then be either on or off, or maybe 0V or 5V.
This system of using just 2 digits is called the binary number system. The digits are
called bits (binary digits).
Just like decimal numbers, binary numbers are made larger by putting more digits
together. Below is a table of decimal number up to ten and their binary equivalents.

Decimal Binary
0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010

Outputs

Output devices are used to communicate information. Personal computers use a video
display and printer. Microcontrollers use simpler devices such as lamps, light emitting
diodes (LEDs), opto couplers, relays, and switching transistors.

Relays, switching transistors and opto couplers are in turn used to interface to higher
power applications such as heaters, electric motors and solenoid devices.

17
This section for advanced classes
The output from an MCU is in the form of a digital signal (logic 0 or 1) placed on the
required port connection (I/O line). Sometimes the output of an MCU may need to be
translated into a corresponding analogue voltage level, in which case a digital to
analogue converter (DAC) is used.

+5v +9v

18
17
16
15
14
13
12
11
10
NC

relay
NO

PB1
PB2
PB3
PIC16F84 C
PIC16F628 390R B transistor
BC54
BC549
R1
E
1
2
3
4
5
6
7
8
9
LED

Figure 5: Possible PIC output

In figure 5, one pin of the output interfacing system used on the elab628 has been
illustrated (there are 8 similar pins). The elab628 has been designed with a dual
purpose. By soldering in the LEDs as provided in the basic kit, each I/O line of PORTB
is connected to a LED via a 390 Ohm resistor. This configuration allows the status of
PORTB pins to be displayed (i.e. an LED which is on indicates a logic 1). Each pin of
the PIC chip is able to source 20mA, which is ample for each LED. If the LEDs are
omitted, and the switching transistors are inserted instead, then each I/O line of PORTB
can be used to switch relays via the collector of the transistors as shown above. If you
wish to maintain the LED (light) while still operating a transistor then you must solder
an additional 390 ohm parallel with the corresponding resistor.

Assembly Language

The PIC microcomputer on the elab628 spends its time reading binary instructions
from the program memory, one after another, and doing whatever those instructions say.
Each instruction consists of 14 bits. If you could see the bits as binary ones and zeroes,
a program might look like this:

11000000000000
00000001100110
11000000000001
00000010000110
10100000000100

The earliest computers were programmed by skilled technicians writing binary codes
just like this. As you can see, though, binary codes are very hard for human beings to
comprehend as they do not represent the “normal” way that we communicate.

Another reason binary codes are hard to write is that many of them refer to locations in
memory. For instance, a "go to" instruction will have to say what memory address to
jump to. Programming would be much easier if you could label a location in the
program and have the computer figure out its address.

18
For both of those reasons, assembly language was invented over forty years ago. Or, to
be more precise, many assembly languages have been invented, one for each type of
CPU. What assembly languages have in common is that the instructions are abbreviated
by readable codes (mnemonics) such as GOTO, and locations can be represented by
programmer-assigned labels

End advanced section

All students proceed to Practical 2a

19
Practical 2a
Starting your own program

Step 1: Open program1.bst

Startup the CoreChart program, open the “program1.bst” file by clicking on File-
>Open.>drive>your folder “program1.bst”

Step 2: Prepare to make new program

Delete all icons between the “Start” icon and the “End” icon. The reason we are reusing
the file is because it contains the subroutine “delay”, which we need to use. Note that
you cannot delete the “label” icons until the “goto” icons have been deleted. Delete
these “goto” icons first, then the “label” icons, and lastly delete the other icons.

When completed
the screen should
look like this.

Note:
You can select
multiple icons by
holding the shift
key

20
Step 3: Save new file

You should now be looking at a blank area with just two icons “Start” and “End”.
Select a Text icon and a box will appear below, enter into it the program name
“;ON LED PROGRAM”. The description is “;To turn on a LED or relay”.
(Remember: each comment line should be preceded with a semicolon (;)). Save the
modified program as “on.bst” (File->Save As…).

Note

You must proceed each line of


comments with a semi colon (;)

If you don’t it will cause a program


failure

21
Step 4: Insert new instructions

Now we basically have a clear program. To add code to this program it is simply a
matter of putting code icons on the page. Start by inserting the following icons in
the order shown below.

Set / Reset Icon

Label Icon

Timing Icon

Goto Icon

Procedure
# 1 Select a Set/Reset Icon, in the Icon Properties box (look below icons)

Set – Click on down arrow and select Port B


Bit – Click on down arrow and select 0
Set to On
Place above End icon

# 2 Select a Label Icon, in the Icon Properties box

In the “Label Name” box enter – “Here”


Place above End icon

# 3 Select a Timing icon, in the properties box

Set Timing drop down box – Clear Watchdog


Place above End icon

# 4 Select a Goto Icon, in the properties box

22
Set the Goto drop down box to – Here
Place above end icon

Your screen
should look like
this

Step 5: Test the program

Save the program into your own folder and run it. Do this by selecting Options-
>Send to Pic or hit the <F9> shortcut key. When the program is run, the elab628
LED 0 should light up.

Step 6: Extend program

Change the program in Step 4 by changing to LED number 1. Do this by changing


icon 3, “set”. Click on the icon and the properties box will appear.

Set PORTB 1 ON

Save and recompile (Send to Pic), then run this and the next LED (1) should turn
on.

23
Step 7: Modify program

Add another icon under icon 3 (set PORTB 0 on).

Set PORTB 0 ON

Save and recompile (Send to Pic), then run this and the LED’s 0 and 1 should turn on.

Now one at a time change the program by adding another port ie. B2, B3….

At what point do the lights flash off and on uncontrolled?

Can you guess at why?

After successfully completing the above go on to Practical 2b

Practical 2b
Loops and Delays

Step 1: Prepare for a new program

Startup the CoreChart program, open the “on.bst” and save the program as
“onoff.bst” (File, Save as, Select your folder) and clear the icons like you did as
per Practical 1b (all icons between start and stop deleted).

Step2: Insert description

Select the Text icon and add to the comments to include your name and the program
name “;ON /OFF LED FLASHER”. The description is “;To flash a LED or
relay”. Save the modified program

(Note: Each line in the Paste Text icon’s comments must begin with a semicolon “;”
otherwise an error will occur when you try to send it to the PIC chip).

New Icon

Assignment Icon

Call Icon

24
Step 3: Add new instructions

Change the program to the following icons...

ICON PROPERTIES
Label here
Set PORTB 0 ON
Assignment w = 10
Call delay
Set PORTB 0 OFF
Assignment w = 10
Call delay
Goto here

Your program
should now
look like this

Step 4: Run the program

Save the program and send to the PIC chip as per Practical 1b. It should flash LED
0.

Step 5: Change length of delay

Change the program in step 3 by changing the value of W in the assignment icon to
a larger number, up to 255. Resave and send to the PIC chip. The LED should
flash slower since there is now a longer delay between changes to the on and off
states.

25
Extension Questions (required)

How does entering different values for “W” in each delay effect the timing?

How does adding another delay directly after the existing ones affect the result?

Step 6: Turn on other ports

Also try making PORTB,2 or PORTB,3 flash and see what happens.
After successfully completing the above, go on to Practical 2c.

Practical 2c
Chasing LEDs

Step 1: Prepare for new program

Startup the CoreChart program, open the “onoff.bst” and save the program as
“chase.bst” (file-save as).

Step 2: Insert description

Change the comments in the Paste Text icon to include your name and the program
name “;LED CHASER”. The description is “;To make the LEDs chase across”.
Save the program.

Step 3: Add new instructions

Modify

ICON PROPERTIES
Label here
Set PORTB 0 ON
Assignment w = 10
Call delay
Set PORTB 0 OFF
Assignment w = 10
Call delay
Set PORTB 1 ON
Assignment w = 10
Call delay
Set PORTB 1 OFF
Assignment w = 10
Call delay
Goto here

26
Your screen
should now
look like this

Step 4: Run the program

Save the program, send it to the PIC and run it. It should flash LED number 0 and
LED number 1 alternately.

Step 5: Make LED 2 flash

Change the program in Step 3 by adding a third section with LED Number 2 also
turned on then off. Resaving and sending to the PIC should cause the LEDs to
chase from 0 to 1 then to 2.

Step 6: Make the LEDs chase each other

Add in the rest of the LEDs and see if you can make them all chase each other.

27
More of what's Inside a PIC…the Memory

The figure below shows the most important parts inside the PIC16F628 device. As we
have seen, the PIC chip is a tiny but complete computer. It has a central processing unit
(CPU), program memory (PROM), working memory (RAM), and input-output ports.

Figure 6: Block diagram of the PIC16F628 chip

As you can see from figure 6, the device is essentially a one-chip computer

The CPU is, of course, the ‘brain’ of the computer. It reads and executes instructions
from the PROM. As instructions are interpreted, it can store and retrieve data in RAM.
Some CPUs make a distinction between registers located within the CPU and RAM
located outside it; but the PIC chip does not. The general-purpose working RAM is also
known as ‘file registers’. On the PIC16F628, there are 224 bytes of general-purpose
RAM.

These file registers are defined automatically by CoreChart, and they configure the
assembler by putting it into PIC16F628 mode and defining variables. In CoreChart
variables can be added simply from the menu Variables and will automatically be
assigned the next free register.

Besides the general-purpose memory, there is a special ‘working register’, or ‘w


register’, where the CPU holds the data that it's working on. There are also several
special-function registers, each of which control the operation of the PIC chip in some
way.

The program memory of the PIC16F628 consists of flash EPROM; it can be recorded
and erased electrically, and it retains its contents when powered off. Many other PIC
chips require ultraviolet light for erasure, and some are not erasable. The PIC16F628,
however, is erasable and reprogrammable over 100,000 times.

28
Inputs and Outputs

There are two input-output ports, port A and port B, and each pin of each port can be set
individually as an input or an output. The bits of each port are numbered, starting at
zero. Figure 7 below shows PORTA, labelled RA0-RA7, and PORTB, labelled RB0-
RB7.

Figure 7: Pin diagram of PIC16F8X chips

Input devices, known as sensors, supply information to the MC system from the outside
world. With a desktop PC system this is usually a keyboard and a mouse. In a
microcontroller system the inputs may be a number of different devices such as
switches, keypads or even a light sensor.

The input signal to the MC from a sensor is usually in digital form. More advanced
devices, such as the PIC16F877 chip on the elab877 development board, have the
ability to accept analogue signals since an analogue to digital converter is built into the
chip. Usually digital signals are at voltage levels near to that of the power supply rails.
In the case of the PIC16F628, a logic 0 is represented by 0 volts and a logic 1 by 5
volts.

Because MCU devices are CMOS in nature and have electrons as their major current
carrier, zero volts is referred to as the source (VSS) and the positive connection as the
drain (VDD).

+5v
18
17
16
15
14
13
12
11
10

RA1 RA0

R1
PIC16F84
PIC16F628
390R
PA2
PA3

R2 RA2 RA3 RA4


PORTA 10k
1
2
3
4
5
6
7
8
9

CONNECTION

Figure 8: Interfacing to the PIC chip with PORTA

Figure 8 shows the circuitry for interfacing to PORTA of the elab628. By using this
arrangement, the I/O lines RA0 to RA7 can be used to either output or input a logic
signal. Resistor R2 has the effect of holding pin 1 (RA2) low (logic 0) to prevent the

29
voltage on it from floating. It also provides a load across which the output signal is
developed.

Alternatively, a logic signal can be input to the MCU by pulling pin 1 high. This
happens when the switch is pressed. Resistors R1 and R2 form a voltage divider
providing a voltage high enough on pin 1 to be read as logic 1. It is important that input
logic signals be kept within the device specifications i.e. 0 to 5 Volts.

By using this arrangement, the PORTA I/O lines on the elab628 have been set up for
the dual purposes of input and output.

Figure 9 below shows a similar process for setting PORTB to be used as both an input
and output. PORTB operates slightly differently to PORTA because the external circuit
is already on the elab628 board. This external circuit was shown previously in figure
5, part 2.

+5v +9v
18
17
16
15
14
13
12
11
10

NC
relay
NO
PB1
PB2
PB3

PIC16F84
PIC16F628 390R
transistor
BC54
R1
BC549

LED
1
2
3
4
5
6
7
8
9

Figure 9: Interfacing to PIC chip with PORTB

Defining Inputs and Outputs

Let us look more closely at how inputs and outputs are set on the ports.

Micro-controller Configuration

When you double click the “Start” icon in CoreChart, a window containing all the PIC
micro-controller configurations will be displayed (see figure 10).

30
Figure 10: PIC chip configuration window

By default, all the pins of PORTA (RA0 to RA7) are set as inputs.

All the pins of PORTB (RB0 to RB7) are set as outputs.

In the CONFIGURATION Word, Power-up timer bit (PWRTE) is enabled whereas


the Code Protection (CP) and Watchdog Timer (WDTE) are disabled. The default
oscillator is set as RC oscillator.

In the OPTION Register, PORTB pull-ups (RBPU) are disabled and the prescaler rate
select bits (PS2 to PS0) are cleared (i.e. 0).

(For more information, please refer to the PIC16F628 Technical Manual).

Change Default Settings

To change one of the settings in the CONFIG Word or OPTION Register, move the
mouse icon over one of the boxes or radio buttons provided and left click the mouse. A
tick or a dot indicates that the particular bit is selected.
To deselect the bit, move the mouse icon over one of the selected boxes or radio buttons
you want to deselect and left click the mouse .
You can also configure the input/output pins of PORTA and PORTB with the similar
procedure described in the previous paragraphs. (Note: An arrow pointing towards the
PIC microchip stands for an input, whereas an arrow pointing away from the
microchip indicates an output).

31
Practical 3a

Testing for inputs

Step 1: Prepare for new program

Startup the CoreChart program, open the “program1.bst” and delete all the icons
between start and end, then save as “press.bst” (use the procedure from Practical
1b).

Step 2: Insert description

Insert a paste text icon and include your name and the program name “PRESS ON
LED” the description is “To turn on a LED or relay by pressing a button”.
Save the modified program.

Step 3: Insert new instructions

Insert the following icons.

ICON PROPERTIES
Assignment w = 7
Assignment CMCON = w
Label Here
Set PORTB 0 OFF
If PORTA 5 OFF
Set PORTB 0 ON
Call Delay
Goto Here

Step 4: Run the program

Save the program and send it to the PIC chip, it should now light up LED 0 when
the button is being pressed.

Step 5: Make LED flash

Change the program in step 4 by inserting “call delay” before the IF icon. Resaving
and sending to the PIC should now cause the LED to flash when the button is
pressed. Can you work out why?
(Answer: It is because there is now the LED is turned off for a short delay period,
instead of being turned on again instantly as was the case before).

Step 6: Implement buzzer

Try changing the program so it switches on and off the Peizo buzzer on PORTA, 3
when the button is pressed. Make a note of what happens. (Hint: Change the bit
that gets turned on and off in the last program).

32
Practical 4a

Making the robot walk

Step 1: Prepare for new program

Startup the CoreChart program, open the “program1.bst” and delete all the icons
between start and end, then save as “walk.bst” (use the procedure from Practical
1b).

Step 2: Insert description

Insert a paste text icon and include your name and the program name “ROBOT
WALK” the description is “Making the robot walk by pressing a button. Using
binary numbers in the program”. Save the modified program.

Step 3: Insert new instructions

Insert the following icons.

ICON PROPERTIES
Set PORTB 0 ON
Timer Clear Watchdog
If PORTA 5 ON
Goto Start
Assignment w = b’11010100’
Assignment PORTB = w

LED 0 should be lit and wait for the push button to be pushed for the walk to
start. The eyes of the robot also should be lit.

How PIC makes Decisions.

At this point it is a good idea to look at the full set of PIC Instructions. The advantage
of the PIC16F series is that there are only 35 instructions. Every instruction that does
not involve a jump executes in just one clock cycle. Thus, the PIC qualifies as a form of
RISC processor (reduced instruction set computer). By contrast, the Pentium has
hundreds of instructions, each of which takes several clock cycles to execute. This
makes the Pentium a CISC processor (complex instruction set computer).

In order to simplify the PIC instruction set, and limit instructions to one clock cycle,
there is no “if” statement. Instead, the PIC has several instructions that test for a
condition and skip the next instruction if it is true. An example of how this works is
demonstrated below.

33
ICON PROPERTIES
IF PORTA Bit 0 OFF
Goto m1
Assignment w = 10

The icons translate to

if PORTA bit 0 is on then goto m1


else w = 10

By interleaving skips and gotos in this way, the series of instructions:

ICON PROPERTIES
IF PORTA Bit 0 OFF
Goto m1
Calculate PORTB =>> PORTB
Goto m2
LABEL m1
Calculate PORTB =<< PORTB
LABEL m2

rotate the bits of PORTA to the left if A0=1 and to the right if A0=0.

Checking the Results

When a calculation is made, the STATUS register records if the result was zero (Z) or it
overflowed (C) past the 8 bits of one register. The carry bit (C) can also indicate if a
result was positive or negative. The STATUS register is 8 bits long, and is used to
select register banks, as well as check on whether there has been a power-down or time-
out.

Technical Help…
The Microchip Technical Data Sheet…
see the “Technical Data” directory on the CD
elabtronics: www.elabtronics.com email: tech@elabtronics.com
Microchip website: www.microchip.com

End of Document

34

You might also like