Anda di halaman 1dari 88

ROBOT .

HEAD to TOE
Product Users Manual PTK40A

PTK40A
Training Kit

User Manual
V2.0
August 2014

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Index
CHAPTER
1. Packing List
2. Introduction
2.1
Introduction
2.2
Board Layout and On board components
3. Getting Started
3.1
MPLAB IDE Introductions
3.2
MPLAB IDE Installations
3.3
Using MPLAB IDE
3.4
PICkit2 Introduction
3.5
PICkit2 Installation
3.6
Using PICkit2 Programmer
4. Digital Input and Digital Output
4.1
Digital Input
4.2
External Digital Input
4.3
Digital Output
4.4
Blinking LED
4.5
Controlling a buzzer
4.6
Activate relay
4.7
Hardware Configuration
5. 7 Segment Display
5.1
Introduction
5.2
How to program
6. LCD and Keypad
6.1
Introduction
6.2
Controlling LCD
6.3
Input signal by keypad
7. DC Brush Motor
7.1
Introduction
7.2
Controlling DC brush motor
7.3
Hardware configuration
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

PAGE
4
5
5
6
11
11
11
11
23
34
27
31
31
32
33
34
35
35
36
37
37
38
40
40
40
42
44
44
44
46
2

ROBOT . HEAD to TOE


Product Users Manual PTK40A

8. Stepper Motor
8.1
Introduction
8.2
Controlling Stepper motor
8.3
How to program
8.4
Hardware configuration
9. Servo Motor
9.1
Introduction
9.2
Controlling servo motor
9.3
Hardware configuration
10. Analog Input
10.1 Introduction
10.2 How to program
10.3 Hardware configuration
11. Rotary Encoder
11.1 Introduction
11.2 Display Encoder Reading
11.3 How to program
12. Real Time Clock
12.1 Introduction
12.2 Timer1 as Real Time Clock
12.3 I2C with External RTC chip, DS1307
12.4 Hardware configuration
13. SPI with EEPROM
13.1 Introduction
13.2 How to program
13.3 Hardware configuration

47
47
47
48
50
52
52
52
54
55
55
56
57
58
58
58
59
60
60
60
62
66
67
67
67
69

14. UART
14.1 Introduction
14.2 How to program
14.3 Hardware configuration

70
70
71

15. Universal Serial Bus (USB)


15.1 Introduction
15.2 How to program
15.3 Hardware configuration

74
74
74
76

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

70

ROBOT . HEAD to TOE


Product Users Manual PTK40A

16. Warranty

77

Chapter 1
Packing List
By September 2014, PTK40A is shipped with PIC18F4520 as the default PIC. Anyway it
include PIC16F877A and PIC18F4550 in the kit. PTK40A comes in standard casing with its
accessories. User is advice to check the parts and components according to the packing list. If
there is any part missing, please contact us at sales@cytron.com.my immediately.
1. AC to DC power adaptor, 12VDC, UK type plug, 100VAC - 240VAC,
50/60Hz
2. Training kit Main Board (PTK40A) with it components soldered and mounted
3. Prototyping board/Bread Board
4. USB mini cable for loading program
5. 1 pack of female to male jumper wire (10 pcs)
6. 1 pack of female to female jumper wire (10 pcs)
7. PIC16F877A, preloaded with test code
8. PIC18F4550, preloaded with test code + HID Bootloader
9. PIC18F4520, preloaded with test code
10. CD or DVD which contain the necessary software and documents
Optional/Add-on items: (not included)
1. SKPS, PS2 Controller Starter Kit
2. SKPSW, Wireless PS2 Starter Kit
3. Wired PS2 Controller
4. Wireless PS2 Controller
5. SKXBee, XBee Starter Kit
6. 80mm Magnetic Door Lock
7. MD10C, Enhanced 10A Motor Driver
8. MD30C, Enhanced 30A Motor Driver
9. SD02B, 2A Stepper Driver
10. UC00A, USB to UART converter
11. IR01A, Medium Range Infrared Sensor
12. GP2Y0A21, Infrared Distance Sensor
13. LV-EZ1, Ultrasonic Range Finder
14. XBee Series 1
15. XBee Series 2
16. XBee WiFi
17. BlueBee
18. USB Bluetooth Dongle

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 2
Introduction
2.1 Introduction
PTK40A Training Board is developed based on Microchip 8-bit 40 pins PIC MCU
(MicroController Unit). It is compatible with PIC16F and PIC18F PIC MCU. PTK40A can
be used to cover programming, interfacing, and applications of microcontroller and
mechanical hardware to help users in learning and developing their project with PIC MCU.
The systems hardware is well configured so that it can be programmed by any compatible
programming language such as assembly, BASIC and C (for PIC MCU). MPLAB X IDE
from Microchip Technology .Inc is included in this kit which has assembler that enable user
to develop the program with assembly language. However, the explanation and sample
programs provided in the users manual are based on C language due to its relatively
flexibility and easiness in understand and developing program. The test code and examples
are based on HI-TECH compiler (v9.83) for PIC16F877A and MPLAB XC8 Compiler for
PIC18F4520 and PIC18F4550.
In this kit, the USB ICSP PIC Programmer, UIC00B is being integrated to be utilized for
downloading program (hex code) into PIC MCU. Using UIC00B offers a convenient
programming method that allow program to be loaded or updated while the PIC MCU stays
on the board.
With PIC18F4550, users is able to load hex code into PIC using USB B type cable via USB
Bootloader (Optional).
PTK40A provides several basic modules or projects for user to explore PIC MCU peripherals
and applications such as matrix keypad, parallel character LCD, DC brush motor & encoder,
Stepper motor, UART, relay, and more. These basic modules are mainly the fundamental
control and monitor of Mechatronic applications. The detailed explanation and sample
programs for these basic modules are provided in CD/DVD.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

2.2 Board Layout and On Board Components

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

DC adaptor socket

25

I2C connector

ON/OFF slide switch

26

DS1307 RTC IC

Power Indicator LED

27

3V cell battery

IC-CD4511

28

USB B Type Socket

7 Segment Display (Common


Cathode)

29

SPI EEPROM

RC Servo motor

30

Pin selector

Relay

31

Expansion Ports

Relay status indicator LED

32

Reset button

Terminal block for external device

33

4x4 Keypad

10

External Stepper
driver

34

Programmable switches

11

L293B Full Bridge Driver

35

IC MCP602

12

Stepper/DC Motor selector

36

DC motor

13

Unipolar/Bipolar selector

37

Press for threshold

14

Stepper motor

38

IR sensor

15

16x2 LCD Display

39

Status indicator LED for motor


direction

16

Preset for LCD contrast

40

Status indicator LED for encoder

17

Zip socket for PIC microcontroller

41

Buzzer

18

20Mhz Crystal

42

Voltage selector for digital input

19

32.768KHz crystal oscillator

43

External digital input connector

20

UIC00B USB ICSP Programmer

44

LEDs

21

Cytron Starter Kit

45

Analog selector

22

Status indicator LED for Rx and Tx

46

LM35 Temperature sensor

23

UART VCC

47

Potentiometer

24

UART connector

48

External Analog Input connector

and DC motor

Power Supply
Power supply part is main power for PTK40A. User may supply power either using DC
adaptor or battery. PTK40A should comes with AC to DC adapter, use it to power up this
training kit.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

7 Segment LED Display


7-segment is a component that consists seven segments of LEDs that may display the number
from 0 to 9.
RC Servo
RC servo motor is a type of motor that is generally used in hobby RC (Radio Control) toy.
Due to its special feature that offers open loop position control, it has been employed in
building robot arm and biped robot.
Relay
Relay is an electrical device that is used as a switch in electrical system with high current or
voltage.
UIC00B USB ICSP Programmer
On board programmer (UIC00B) is utilize to download program into PIC MCU. UIC00B
offers a convenient programming method that allow program to be loaded or updated while
the PIC MCU stays on the board. It is compatible with PICkit2 and able to be integrated into
MPLAB X IDE.
UART
There are 2 types of UART terminal on PTK40A which are UC00A and Cytron Starter Kit.
UC00A used to communicate between PTK40A and Computer Desktop/Laptop. The test
code shows SKPS communicate with PTK40A to read analog value from joystick and display
it on LCD.
Microcontroller
PTK40A comes with PIC18F4520 on it. It also include two extra PIC, PIC16F877A and
PIC18F4550. The PIC18F4520 is being placed on the 40 pin ZIF (Zero Insert Force) socket
and it is preloaded with test code. PTK40A support 40-pin PDIP PIC microcontroller, 5V
compatible, PIC16F and PIC18F.
16x2 Characters, parallel interface Liquid Crystal Display
The 16x2 characters LCD offers character display for embedded system. It can be used to
display numerical information, text message and also special symbol.
Stepper Motor
Stepper motor is a very important actuator in automation system due to its feature which is
able to rotate in digital steps. There are many types of stepper motor available in the market
such as unipolar type, bipolar type, single-phase type, multi-phase type etc. PTK40A comes
with hybrid type of stepper which can be configured as unipolar or bipolar.
Motor Driver
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

ROBOT . HEAD to TOE


Product Users Manual PTK40A

L293D IC which is motor driver for stepper and DC motor.


External Driver Connectors
Besides using L293D to drive stepper and DC motor, user may use external driver such as
SD02B to drive stepper motor and MD10C to drive DC motor. This are additional header
pins for PTK40A to interface to external driver.
LEDs
The LEDs act as general purpose indicator of digital output of PIC MCU on PTK40A. Each
LED is labelled with the corresponding output pin of PIC MCU, all LEDs in this section are
active high configuration.
DC Motor & Encoder
The DC brush motor included in PTK40A is a general purpose DC motor with 6V operating
voltage. The slotted encoder disc is a 1/16" thick plastic, with 1.4" outer diameter. The slotted
encoder disc has 8 spokes that enable it to provide up to 16 transitions or 8 pulses per
rotation.
Buzzer
Buzzer is one of the digital output devices. It is in active high configuration, controllable by
PIC MCU.
External Digital Input
Provide header pins for external digital sensor such as limit switch, IR01A, infrared sensor
switch, PIR sensor module, etc . The voltage for sensor can be selected through jumper, either
5V or 12V.
Analog Input
There are 3 analog input options on PTK40A. Variable resistor or potentiometer act as
voltage divider, generating analog voltage when it is turned. LM35 temperature sensor
generates analog voltage base on surrounding temperature. There is also an external header
pin for user to connect any suitable analog sensor. Possible analog sensor would be
Ultrasonic Range Finder, Infrared distance sensor, 3 axis accelerometer, Gyro and more.
Selection of analog input is done through the jumper. The analog input is AN0/RA0 on PIC
MCU.
4x4 Keypad and Push Buttons
The 4 x 4 keypad is mounted on PTK40A to ease user pressing numbers.
Pin Selector
Pin selector for PTK40A Training Kit. PTK40A shared several I/O pins for different devices
and functions. For example JP10, buzzer pin is shared with PWM pin. To choose buzzer, user
need to move mini jumper JP10 and place it at BUZZER. Same applied for SPI, I 2C, USB,
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

10

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Relay and Servo.


I/O Expansion Ports
Expansion ports or JP4 is provided as optional expansion for user to develop other
experiments.
I2C
PTK40A uses external RTC chip, DS1307 to demonstrate I2C protocol. DS1307 serial realtime clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56
bytes of NV SRAM chip. Via I2C, PIC MCU is able to set the time, date and day into
DS1307, DS1307 will start counting and keep the data even when the main power is off (it
uses the coin cell battery) and PIC may obtain the real time any time.
SPI
SPI (Serial Peripheral Interface) is again a serial communication protocol shown on PTK40A.
PTK40A uses external EEPROM IC to reveal the SPI communication, data can be written
and read to/from EEPROM.
USB B Type Socket
The USB type B receptor offers option for user to develop USB application using PIC18F
family of PIC that support USB peripheral, one example is PIC18F4550.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

11

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 3
Getting Started
3.1 MPLAB X IDE Introduction
MPLAB X IDE is a software program that runs on a PC (Windows, Mac OS, Linux)
to develop applications for Microchip microcontrollers and digital signal controllers. It is
called an Integrated Development Environment (IDE), because it provides a single integrated
"environment" to develop code for embedded microcontrollers.
MPLAB X Integrated Development Environment brings many changes to the PIC
microcontroller development tool chain. Unlike previous versions of the MPLAB IDE
which were developed completely in-house, MPLAB X IDE is based on the open source
NetBeans IDE from Oracle. Taking this path has allowed us to add many frequently requested
features very quickly and easily, while also providing us with a much more extensible
architecture to bring you even more new features in the future.
MPLAB X IDE provides a good platform for other compiler language tools to be integrated.
MPLAB XC8, XC16 and XC32 from microchip provide fully integrated, optimized code.
Along with compilers from HI-TECH, IAR, microEngineering Labs, CSC and Byte Craft,
they are invoked by MPLAB X IDE project manager to compile code. However, project
created with MPLAB IDE is not compatible in MPLAB X IDE.
The initial use of MPLAB X IDE is covered here. This section shows the steps to install
MPLAB X IDE. It is followed by a simple tutorial to create project. Those who are
unfamiliar with MPLAB X IDE will get a basic understanding of using the system to develop
an application. No previous knowledge is assumed, and comprehensive technical details of
MPLAB X IDE and its components are omitted in order to present the basic framework for
using MPLAB X IDE.
3.2 MPLAB X IDE Installation
To install MPLAB IDE, simply put the PTK40A CD-ROM into CD drive of PC. Browse
Applications folder and further into MPLAB X IDE vX.XX folder. User can also
download MPLAB X IDE from the Microchip website (www.microchip.com), locate the
download (.zip) file, select the file and save it to the PC. Unzip the file and execute the
resulting file to install. We cannot ensure the compatibility of the example code in different
version of MPLAB X IDE. Therefore, it is recommend to select the same version as in the
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

12

ROBOT . HEAD to TOE


Product Users Manual PTK40A

CD. Please refer to MPLAB X IDE Installation.pdf to install it into your computer.
3.3 Using MPLAB X IDE
To start MPLAB X IDE, double click on the icon installed on the desktop after installation or
select Start>Programs>Microchip>MPLAB X IDE>MPLAB X IDE vX.XX. A screen will
display the MPLAB X IDE logo followed by the MPLAB X IDE desktop as in figure below.

In order to create code that is executable by the target PICmicro MCU, source files need to be
in a project. The code can then be built into executable code using selected language tools
(assemblers, compilers, linkers, etc.). Since this example is using MPLAB XC8 Compiler, do
remember to install the XC8 Compiler too. Please refer to MPLAB XC8 C Compiler
Installation.pdf to install the compiler.
All projects will have these basic steps:
Create New Project
MPLAB X Project Wizard will be used to create new project.
Select Language Tools
In Create New Project Wizard the language tools will be selected. For this tutorial, the
built-in assembler and linker will be used. For other projects one of the Microchip
compilers or other third party tools might be selected.
Add Files into Project
Files will be added into the project depending on the project requirement. To ease the
development, please ensure all the necessary files are in a folder as the project folder.
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

13

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Create Code
Start with template file will be easier.
Build Project
The project will be built causing the source files to be assembled and linked into
machine code that can run on the selected PICmicro MCU.
Verify Code
Finally, the code will be tested by loading it to PTK40A and run the program.
The MPLAB X IDE create new project steps will guide us through most of the process. For
PTK40A, the device should be PIC18F4550 or PIC18F4520 or PIC16F877A, depending
which PIC model is being placed on the ZIF socket. Anyway, the default PIC on the PTK40A
is PIC18F4520 (from October 2014 onwards).
The next step is to create a project using Project Wizard. A project defines how all related
files organized to be compiled and assembled. We will create project for simple application
by a single C file and a header file.
In MPLAB X IDE windows, goto File -> New Project . New Project window will appears,
please choose Microchip Embedded for Categories and Standalone Project for Projects.
Click on Next to proceed to Step 2.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

14

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Step 2: Select Device. Under Family, choose Advanced 8-bit MCUs (PIC18) if you are
creating project for PIC18F4550 or PIC18F4520. Choose Mid-Range 8-bit MCUs
(PIC12/16/MCP) if you are creating project for PIC16F877A. For Device, type is the exact
model of PIC, example PIC18F4550, or you can find the model from the drop down menu.

Step 3: Select Tool. Please connect the USB mini B cable to UIC00B on PTK40A. Under
Hardware Tools, please click PICkit2. Click Next to proceed.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

15

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

16

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Step 6: Select Compiler. The wizard will jump step 6 to select compiler. For PTK40A, it is
currently using XC8 C Compiler to develop example code for PIC18F and HI-TECH C
compiler for PIC16F877A. Users Manual will show the selection of XC8 C compiler for
PIC18F. PTK40A PIC18F codes have been developed from XC8 C Compiler version 1.12,
newer version should be fine. Click Next to proceed.

Last but not least, step 7: Select Project Name and Folder. MPLAB X project will
automatically create a folder with project name with the extension of .x. The last step is to
decide the project and where it is located in the computer drive. Put any project name you
like and browse to the directory where you want the project to be.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

17

ROBOT . HEAD to TOE


Product Users Manual PTK40A

After pressing the Finish icon, MPLAB X IDE will appears with new set of windows.

Compiling project is not possible at this stage as there is no source file/s in the project. A
project must have minimum 1 source file. For the case of PTK40A, it require 2 files, a
source file and a header file.
Please copy these 2 files from CD/DVD, under Getting Started folder:
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

18

ROBOT . HEAD to TOE


Product Users Manual PTK40A

PTK40A_PIC18F_Template.c
system.h
into the project folder, in this example is PTK40A Template.X

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

19

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Now, please add these files into the project in MPLAB X IDE. Goto Projects Window (on top
left corner), right click (mouse) on Source Files and select Add Existing Item

A browse window appears to allow selection of file. Browse to the folder of PTK40A
Template.X which we created earlier and copy & paste two files. Select
PTK40A_PIC18F_Template.c and click Select.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

20

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

21

ROBOT . HEAD to TOE


Product Users Manual PTK40A

The file will automatically add under Source Files and you can open the file by double
click (left) the file.

Follows the same methods, add system.h into Header Files under the projects window.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

22

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Now, compilation/build of the project is possible. Please click the Clean and Build Project
to generate hex code. Or goto Run -> Clean and Build Project under the tools bar.

If there is not error, Loading Completed message will appeared under the output Window.
If there is error, try search error message/s and double click on the error message, MPLAB X
IDE will highlight the suspected error syntax.
The easiest way to compile and load program together is to connect the Mini B USB cable to
PTK40A, power up PTK40A and click Make and Program Device, or goto Run -> Make
and Program Device under the tools bar.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

23

ROBOT . HEAD to TOE


Product Users Manual PTK40A

If the MPLAB X IDE successfully compile the project and load into PTK40A, message of
Running Target will appear at the bottom of Output window. The hex code of this project
has been loaded into the PIC on PTK40A and it is running, user may check the outcome of
the program on PTK40A.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

24

ROBOT . HEAD to TOE


Product Users Manual PTK40A

If the template is being compiled and loaded, LED RD7 will blink with 200ms ON and
500ms OFF. Feel free to modify the template file.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

25

ROBOT . HEAD to TOE


Product Users Manual PTK40A

To open existing project from MPLAB X IDE would be easy. Just click on Open Project
icon, or goto File -> Open Project...

A browse window will appear and user may browse the existing project to open.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

26

ROBOT . HEAD to TOE


Product Users Manual PTK40A

3.4 PICkit 2 Introduction


The on board UIC00B is compatible with Microchip PICkit 2 Programmer. Therefore, if
other development tools besides MPLAB IDE or MPLAB X IDE is being used, user can still
load the hex code into PIC MCU on PTK40A by using PICkit 2 Programmer GUI. This
section illustrates the installation of PICkit 2 Programmer and method to utilize it. The PICkit
2 Programmer application allows user to program all supported devices. The programming
interface appears as shown in figure below.

Device Family
Select a device family to search for a connected device in that family. Selecting the device
family of the current part will clear all device data. Some families which cannot be autodetected (such as Baseline) will bring up a drop down box from which supported devices may
be selected.
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

27

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Read Device Reads program memory, data EEPROM memory, ID locations and
Configuration bits.
Write Device Writes program memory, data EEPROM memory, ID locations and
Configuration bits.
Verify Verifies program memory, data EEPROM memory, ID locations and Configuration
bits read from the target MCU against the code stored in the programming application.
Erase Performs a Bulk Erase of the target MCU. OSCAL and band gap values are
preserved on parts with these features.
Blank Check Performs a Blank Check of program memory, data EEPROM memory, ID
locations and Configuration bits.
3.5 PICkit 2 Installation
1. To install PICkit 2 Programmer, simply put the PTK40A CD/DVD into CD
drive of PC. Browse the contain of CD/DVD.
2. Goto Applications folder further into UIC00B folder, there is UIC00B
setup file.
3. Click setup.exe to start install PICkit 2 software.

4. PICkit 2 will display welcome setup. Click next to continue installation.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

28

ROBOT . HEAD to TOE


Product Users Manual PTK40A

5. The following window concerns the installation folder. Click Browse if you
want to change the default destination. Assuming change, click on Next.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

29

ROBOT . HEAD to TOE


Product Users Manual PTK40A

6. Click next to start the installation of the PICkit 2 programming software.

7. The following license agreement window will appears. In order to proceed


with the installation, read the conditions, select the option I Agree and click on Next.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

30

ROBOT . HEAD to TOE


Product Users Manual PTK40A

8. Wait for a while. PICkit 2 is being installed to PC.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

31

ROBOT . HEAD to TOE


Product Users Manual PTK40A

9. After complete installation, click Close to exit.

3.6 Using PICkit 2 Programmer


To load program into PIC MCU:
Connect DC adaptor to DC barrel socket.
Connect USB mini cable to UIC00B (left top corner) on PTK40A,

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

32

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Switch on power supply for PTK40A.


Launch
PICkit
2
Programmer
by
selecting
Start>Programs>Microchip>PICkit 2
When the PICkit 2 programmer application is first opened, it will attempt to
identify the connected device by the device ID and display it in the Configuration
window as shown in figure below.

To connect to a device once the application is already running, select


Tools>Check Communication.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

33

ROBOT . HEAD to TOE


Product Users Manual PTK40A

To import hex file to be programmed into the PTK40A, select File>Import


HEX as shown in figure below.

Browse for the hex file and click Open. The code is displayed in the Program
Memory and EEPROM Data windows. The name of the hex file is displayed in the
Source Block under Program Memory. Hex file generated from MPLAB X IDE will
be named according to project name, not C file name. It is normally being placed
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

34

ROBOT . HEAD to TOE


Product Users Manual PTK40A

under the project.X folder\dist\default\production\project_name.hex


After the device family has been selected and a hex file has been imported, the
target device can be programmed by clicking Write. The device will be erased and
programmed with the new imported hex code.

The status of the write operation is displayed in the status bar located under
the device Configuration window. If the write is successful, the status bar turns green
and displays Programming Successful as shown in figure below.

For more information about PICkit 2 Programmer, please refer microchip


website.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

35

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

36

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 4
Digital Input and Digital Output
4.1 Digital Input
Switch is a commonly used component to change digital status. PTK40A offers 4 commonly
used push buttons; one is designated for Reset purpose and other three as programmable
digital input switch. It is being connected in pulled high configuration; the input signal is
initially at 5V (high). When switch is pressed, the input signal becomes 0V (low). PIC
microcontroller can be programmed to read the status of input pin and determine action.
Besides switches, digital sensors can also provide digital signal to microcontroller which
serve the same function as switches.

Figure 4.1 Connection of push buttons to PIC microcontroller


From figure 4.1, SW1, SW2 and SW3 are sharing input pins with KC1, KC2 and KC3 of
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

37

ROBOT . HEAD to TOE


Product Users Manual PTK40A

keypad respectively. Please do not press any key on the keypad while using the push button
switches. Switches are being pull-up to 5V through resistor, thus if pressed, the corresponding
pin is pulled to Gnd. In program, please check for logic low (0V) if a press is to be expected.
4.2 External Digital Input
Besides the on board push button switches, PTK40A also offers External Digital Input. It is
designed for attaching additional digital input devices to the board such as limit switch or
digital sensor. The pin for external digital input is being shared with KC4 pin of keypad.
Please do not press any key on the keypad while using the external digital input. Also, do not
connect any digital sensor when keypad is needed. JP15 is header pin to select preferred
voltage for digital input device, it can either be 5V or 12V. JP13 is header pins for external
digital input device connection.

Figure 4.2: Connection for external digital input

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

38

ROBOT . HEAD to TOE


Product Users Manual PTK40A

4.3 Digital Output


LED is the most basic and commonly used output device in electronic circuit. It is an
indicator that we can use to display the logic status (High or Low) of a specific pin. As like
other output devices, LED can be active-high or active-low as shown in figure below. In
PTK40A, most of output pin of PIC is being connected to an LED as indicator, it is being
connected in active high configuration.

(a) Active-high connection

(b) Active-low connection


Figure 4.3: Active high and active low connection

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

39

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 4.4: Connection of LEDs in PTK40A


Besides LED as single and straight forward digital output, PTK40A also offer simple audio
output, which is the buzzer. However, because of pin constrain the buzzer is being shared
with PWM output. JP10 is pin header to select Buzzer or PWM for motor driver. Use mini
jumper on JP10 selects the appropriate connection. Either Buzzer or PWM can be used in the
same time.

Figure 4.5 Connection of buzzer to PIC microcontroller

A relay is an electrically operated switch. Many relays use an electromagnet to mechanically


operate a switch, but other operating principles are also used, such as solid-state relays.
Relays are used where it is necessary to control a circuit by a low-power signal (with
complete electrical isolation between control and controlled circuits), or where several
circuits must be controlled by one signal. The first relays were used in long distance telegraph
circuits as amplifiers: they repeated the signal coming in from one circuit and re-transmitted
it on another circuit. Relays were used extensively in telephone exchanges and early

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

40

ROBOT . HEAD to TOE


Product Users Manual PTK40A

computers to perform logical operations.


With direct connection, a microcontroller is unable to provide or resist large current and
voltage. However, using Relay, microcontroller is able to control circuit with high current and
voltage. From the schematic shown, there are three output from the relay which are NO
(Normally Open), COM (Common) and NC (Normally Close). NC terminal is connected to
COM terminal when relay is not activated; while NO terminal is the other way around. When
the relays core is activated, NO will be connected to COM while NC will be disconnected
from COM. PTK40A comes with 2 LEDs as indicator to the status of relay. When relay is
activated, VIN LED will light to indicate the relay is activate and GND LED will light when
the relay is deactivate.
Do take note that the NO terminal of relay on PTK40A is connected to Vin which is 12V
input from DC adapter.

Figure 4.6: Connection of relay and PIC Microcontroller

4.4 Blinking LED


Controlling LED blinking is the fundamental part for this module. On PTK40A, the LEDs are
connected in active high. Thus, the LEDs will be ON if PIC MCU produces 5V (logic High)
at designated pin, and OFF when the pin is 0V. By repeating this sequence, LED will Blink.
In order for the LED to blink, we should switch ON the LED and delay for a moment before
it is switched OFF.
We can also add a switch into our program to control the blinking of LED. LED will only
blink when we press the switch and it will stop when the switch is pressed for the second
time. By integrating together with the switch, the blinking of LED becomes more controllable
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

41

ROBOT . HEAD to TOE


Product Users Manual PTK40A

and interesting.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

42

ROBOT . HEAD to TOE


Product Users Manual PTK40A

4.5 Controlling a Buzzer


Same as the LED, buzzer is a simple output component that can be used as sound indicator
when there is an emergency. The buzzer will buzz continuously when power is provided (5V)
and will silent when the power is being cut off (0V). In other word, this is an active high
configuration. Refer to Figure 4.4 for the connection of buzzer with PIC Microcontroller.
4.6 Activate relay
A magnetic lock is a simple locking device that consists of an electromagnet and armature
plate. If an additional magnetic door lock is required, connect the red wire (positive) to COM
terminal of JP19 terminal block and the black wire (negative) to GND of JP19 terminal block.
After connection is completed, the magnetic lock will release when the relay is activated and
lock when relay is deactivated.

Figure 4.7 Connection of magnetic lock to JP19

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

43

ROBOT . HEAD to TOE


Product Users Manual PTK40A

4.7 Hardware Configuration


For hardware configuration, move mini jumper on JP7 for relay and JP10 for buzzer.

Figure 4.8 Hardware configurations for relay and buzzer

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

44

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 5
7-Segment Display
5.1 Introduction

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

45

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 5.1: Connection of 7-segment with PIC Microcontroller


7-segment is a component that consists seven segments of LEDs which would display the
number from 0 to 9. For normal application, it can be connected as shown at figure 5.1
PTK40A uses common cathode 7-segment. Microcontroller can display any number (0-9) by
activating the correct segment by providing 5V to the PIC pin, like what we had done to the
LED in previous section; of course resistor is needed in between. For example,
microcontroller can switch on the segment a, b, c, d, g and switch off segment f, e to display
the digit 3. However, to save the usage of the I/O pins of MCU and to simplify the control
commands, PTK40A utilize 7-segment decoder.
On PTK40A, two CD4511 are used to decode 2 units of 7-segment. With that we are able to
control 2 units of 7-segment with just 6 digital output pins from microcontroller.

5.2 How to Program


In normal setup, after connecting a 7-segment to CD4511, number can be displayed by
providing the input signal as shown in table below. For example, by setting input signal (D3,
D2, D1 and D0) to low (0) will display 0 on 7 segments.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

46

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Table 5.1: The input signal to control 7-segment

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

47

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 6
LCD and Keypad
6.1 Introduction
The 2x16 character, parallel interface LCD is a famous device used in embedded system to
display information. It can be used to display numerical information, text message and also
special symbol. We can control LCD using either 8 pins (8-bit interface) or 4 pins (4-bit
interface), depending on the I/O pins that we have. For learning purposes, we would
recommend 8-bit interface which is relatively easy. Figure 6.1 shows the schematic of LCD
interface in PTK40A.

Figure 6.1: Connection of a 2x16 character LCD


6.2 Controlling LCD
Before using LCD for display purpose, LCD has to be initialized either by the internal reset
circuit or sending commands to initialize the LCD. Given below is a flowchart that describes
the steps to initialize LCD. After initialize LCD, we can easily display any character by
sending character in ASCII format to the data pins which are located at pin 7 to pin 14.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

48

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

49

ROBOT . HEAD to TOE


Product Users Manual PTK40A

6.3 Input Signal by Keypad


Keypad is an array of switches. There are 2 terminal pins connected each time a button is
pressed. For example; when button 1 is pressed, pin COL1 and ROW1 is connected.
Initially, there is no connection between rows and columns. The button connects it. The
keypads pins need to be pulled up or pulled down to avoid floating case. Pull up normally
connect to 5V and pull down is connect to ground. 4x4 keypad pin can directly connect to
microcontroller or keypad decoder IC.
In this section, we are trying to scan the button being pressed on keypad. The 4x4 keypad is a
16-way XY-Matrix hexadecimal keypad. It has eight connections where four pins for row
(ROW1-ROW4) and four pins for column (COL1-COL4). The internal connection for the
keypad is shown in Figure 6.2.

Figure 6.2: Internal Connection of a 4x4 Keypad

From the schematic in Figure 6.3, the row pins are connected directly to PIC, and being
configured as controllable pins (Output from PIC). Column pins are being pulled high (via
resistor) to 5V, and being configured as readable pins (Input to PIC). KC1, KC2 and KC3 of

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

50

ROBOT . HEAD to TOE


Product Users Manual PTK40A

keypad are being shared with push button SW1, SW2 and SW3 respectively. Thus, user may
either use keypad or push button on PTK40A.
To check which button is pressed, users need to scan a column at a time and row by row.
Make rows as output and columns as input as explained earlier. For example; set (5V, high
logic) all the rows by default. 1st scan, clear (logic low) row 1 and scan column 1 until
column 4 (1, 2, 3 and A) for low logic , this will determine which button is pressed in
row 1. If one of those buttons is pressed, record it and jump out from the scanning loop and
continue with the action required. If none button is pressed in row 1, set it back to default
(logic high) and clear (logic low) row 2. Scan column 1 to column 4 again and this will scan
button press of 4, 5, 6 and B. And this process should be repeated until all four rows
are being scanned.

Figure 6.3: Connection 4x4 keypad to microcontroller


Once we understand the working concept of keypad, displaying the characters on LCD by
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

51

ROBOT . HEAD to TOE


Product Users Manual PTK40A

using keypad becomes an easy job. A button itself doesnt bring any meaning to our system.
We need to assign the exact characters to the buttons respectively so that our system knows
which character is being pressed.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

52

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 7
DC Brush Motor
7.1 Introduction
DC (Direct Current) brush motor is the most common and easy-to-begin actuator which is
usually used in many types of machines and automation systems. The main advantage of DC
motor is that it can be operated by DC current, normally from batteries. In many applications
such as cars, ships, remote controlled racing cars or mobile robots where AC power supply is
not available, DC operated components are preferred. In normal application, a DC brush
motor is equipped with a set of gear to reduce the output speed of the motor and increase the
torque at the same time.
7.2 Controlling DC Brush Motor
Most of the DC motors can be controlled easily by providing the necessary voltage to it. To
change the rotating direction of DC motor, simply reverse the polarity of the DC input. This
changeover process can be achieved via a simple changeover switch (relay) or by using a
suitable motor driver. In this training kit, motor driver L293D is employed to control the DC
brush motor.
Another advantage of DC motors is speed control of motor can be easily achieved by
providing variable voltage to it. There are many methods to offer more precise control and
maximum efficiency in controlling the speed. PWM (pulse width modulation) is among the
popular alternative in DC motor speed control. Figure below shows the connection between
DC motor and PIC.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

53

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 7.1: Connection of motor driver L293D with PIC Microcontroller

Again, because of pin constraints of 40-pin PIC, motor driver is being share to drive either
DC brush motor or stepper motor. From figure 7.1, JP10 is pin selection to control either
buzzer or PWM. User need to move mini jumper to PWM in DC motor exercises. JP20 and
JP21 are to select either stepper motor or DC brush motor.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

54

ROBOT . HEAD to TOE


Product Users Manual PTK40A

7.3 Hardware Configuration


For hardware configuration, move mini jumper on JP20 and JP21 to select DC motor and
mini jumper on JP10 to select PWM.

Figure 7.2: Hardware configuration for DC motor and stepper motor

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

55

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 7.3: Hardware configuration to select PWM

Chapter 8
Stepper Motor
8.1 Introduction
Stepper motor is another useful actuator in robotics application due to its feature of step to
step rotation. This is an important requirement for conveyor system in automation station.
There are many types of stepper motor available in the market which include unipolar,
bipolar, single-phase, multi-phase, etc. For learning purposes, we would recommend the
unipolar type stepper motor which is relatively easy for beginner to get started.

8.2 Controlling Stepper Motor

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

56

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 8.1: Internal circuit of step motor


Before we go into the interface part, we should know about the operation principle of
unipolar type stepper motor. The stepper motor model which has 4-poles is shown in Figure
8.1 (normally a stepper would have more than 4-poles). A permanent magnet is used for rotor
and coils are put on stator. In the case of this motor, step angle of the rotor is 90 degrees. As
for four poles, the top and the bottom and either side are a pair coil, respectively. For
example, the coil are being winded to the upper and lower pole. The coil are winded up in the
direction to get opposite magnetic pole when electric current is applied the coil. To make the
motor shaft turn, first, one pair of coil is given power, which magnetically attracts the gear's
teeth. When the gear's teeth are aligned to the first pair of pole, they are slightly offset from
the next pair of pole. So when the next pole is turned on and the first is turned off, the gear
rotates slightly to align with the next one, and from there the process is repeated. Each of
those rotations is called a "step", with an integer number of steps making a full rotation. In
that way, the motor can be turned by a precise angle.
8.3 How to Program
The turn of the motor is controlled by the electric current which pours into certain terminal of
wires in which will cause certain pole being magnetized. While the rotor rotational speed can
be controlled by the frequency of changing the current to particular poles, table below shows
the sequence of pulses to control the stepper motor.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

57

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Table 8.1 (a): Clockwise Control


Control

Table 8.1(b): Counterclockwise

Note that, the step angle for a typical stepper motor is 1.8 or smaller, thus the signal
sequence should be repeated for 200 times to complete a 360. In PTK40A, we use a motor
driver L293D to drive the stepper motor. Figure 8.2 shows the schematic diagram of stepper
motor to motor driver and to PIC MCU.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

58

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 8.2: Circuit connection for stepper motor

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

59

ROBOT . HEAD to TOE


Product Users Manual PTK40A

The reason we need a motor driver instead of controlling directly from microcontroller is
because a microcontroller is unable to provide high current to drive a stepper motor. Another
reason is, a microcontroller only provides +5V but stepper motors may need different voltage
depending on the product specification.
8.4 Hardware Configuration
For hardware configuration, move mini jumper to STEPPER at JP20 and JP21. Then place
mini jumper at JP24 and JP23 to select BIPOLAR or UNIPOLAR. Another mini jumper
used on JP10 to select PWM.

Figure 8.3: Hardware configuration for unipolar and bipolar

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

60

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 8.4: Hardware configuration for DC motor and stepper motor

Figure 8.5: Hardware configuration to select PWM

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

61

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 9
Servo Motor
9.1 Introduction
Servo motor generally refers to a motor that is equipped with encoder which is able to
feedback its current location to the control system. This feature makes the servo motor
extremely useful in robotic arm development. The positioning of the joints is the main issue
for robotic arm where servo motor can be employed to solve this problem.
For educational purposes, we usually employ hobby servo instead of industry servo motor.
Hobby servo performs the similar function that allows users to control its location of
movement. Typically, hobby servo is used in Radio controlled racing car, remote controlled
helicopter and remote controlled airplane as steering due to its ability in positioning the
steering.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

62

ROBOT . HEAD to TOE


Product Users Manual PTK40A

9.2 Controlling Servo Motor


Generally, the turning angle of a hobby servo is 180 and the input voltage is between 4.8V
and 6.0V. A hobby servo come with three wires, which are signal, +V and ground. How do
we rotate or activate a hobby servo? It is definitely different from controlling a DC brush
motor.
From the schematic diagram in Figure 9.1, we know that the signal, +V and ground pin is
connected to PIC output pin, +5V and ground respectively. The signal wire is used to control
the turning angle and position of servo. The angle is determined by the duration of the pulses
that is applied to the signal wire. This is called Pulse Coded Modulation or sometime Pulse
Position Modulation. The servo expects to have a pulse in every 20 milliseconds. The length
of the pulse will determine angle of the motor to turn to. For example, to turn the shaft to 90
degree, we should ON the signal pin for 1.5 milliseconds and OFF it for 18.5 milliseconds.
The total length of the pulse should be equal to 20 milliseconds. To maintain this location, the
pulse should be generated continuously until we want to change its position.

Figure 9.1: Connection of servo motor with PIC Microcontroller


If the duration of ON(high) signal is shorter than 1.5 ms, the turning angle will be closer to
0 degree. On the contrary, the turning angle will be closer to 180 degrees if it is larger than
1.5ms

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

63

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 9.2: Input signal and the position respectively

9.3 Hardware Configuration


For hardware configuration, move mini jumper at JP9 to SERVO to control servo motor.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

64

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 9.3 Hardware configuration to select servo motor

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

65

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 10
Analog Input
10.1 Introduction
Variable resistor or so called potential meter is the typical device which being used to
represent analog device, turning the knob will generates different analog voltage. If a pin is
configured as digital input, only 5V (high) or 0V (low) is readable. With digital input,
microcontroller is NOT able to recognize if 3.2V, 1.6V or any other value of signal is
presented. However, with analog input, microcontroller is able to read analog value from 0V
to 5V and convert it in register. ADC of PIC microcontroller is 10-bit resolution. This means
that when analog voltage is being measured and converted, it is being store in 10-bit binary
value (in decimal is 0-1023). In other word, if the reference voltage is 5V, with 10-bit
resolution, the smallest voltage change detectable by ADC module is:
5V / 1024 ~= 4.88mV
Thus the sensitivity of this ADC is 4.88mV. Every 4.88mV change in analog voltage will
result in change of one bit in the ADC result. Most of new PIC MCU has eight or more
channels of ADC on chip. However this section will only show example of using 1 channel of
ADC. Though PIC18F4520 has > 8 channels of ADC, PTK40A comes with only three
different ADC source. All ADC inputs are connected to AN0 (RA0 or pin 2) via a mini
jumper. Only 1 ADC input can be used at a time. Use mini jumper to select ADC input at
JP14. To demonstrate ADC input, PTK40A comes with potential meter, temperature sensor
and external analog input. Following section explains the method to read analog voltage from
ADC.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

66

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 10.1: Circuit connection for Analog Input

10.2 How to Program


PTK40A offers the hardware for analog input, thus user may start with program right away.
Since I/O of PIC is multiplex with other function, user must make correct initialization and
configuration before particular pin can be used as analog input.
First, is to configure ADC pin as input which is controlled by TRISX register, in this case is
TRISA. Follow by ADCON0 and ADCON1. ADCON0 register controls the operation of
ADC module. ADCON1, and ADCON2 registers configures the function of port pins (digital
or analog is configured in this register).
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

67

ROBOT . HEAD to TOE


Product Users Manual PTK40A

After all the configuration and initialization have been completed, PIC may start to read
analog voltage. Bit 1 of ADCON0 control the analog voltage conversion to begin and to
indicate the conversion is completed. Thus to start ADC, user must set this bit and wait for
this bit to be cleared before reading the analog value. ADC value is stored in ADRESH and
ADRESL, two 8-bit registers. Because the analog result is 10-bit, it cannot be stored in 1 byte
register.
10.3 Hardware configuration
For hardware configuration, move mini jumper at JP14 to use potentiometer, temperature
sensor or analog input sensor. Only an analog input can be used at a time.

Figure 10.2: Hardware configuration for analog input

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

68

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 11
Rotary Encoder
11.1 Introduction

Figure 11.1: Optical sensors and slotted discs


Rotary encoder is a sensor or transducer used to convert the data of rotary motion into a
series of electronic pulse that is readable by some sort of device. The hardware of rotary
encoder consists of a piece of slotted disc and a unit of optical sensor as shown in the figure
above. The slotted disc has a 1.4 outside diameter with 8 spokes that provides 16 transitions.
The optical sensor is then used to sense the 16 transitions of the slotted disc. With these
transitions, the PIC is able to recognize the rotary angle of the disc. By this concept, a rotary
encoder can be employed in a DC motor so that the motor have the ability to know its
current position.

11.2 Display Encoder Reading


In PTK40A, the rotary encoder is mounted at the shaft of DC brush motor. The rotary
encoder will decode the rotation of DC brush motor. The IR sensor requires +5V and GND to
power up, and provide it will produce 0 to 5V pulses output when the rotary encoder rotates.
It provides a +5V output when its beam is unblocked, and a 0V output when the beam is
blocked. This IR sensor can handle a pulse train up to 1 kHz (1000 ticks/sec). ENC LED
will light up when the beam is unblocked. From the 0 and 1 signals, the PIC is able to
know number of rotation. For visualization purposes, we can display the number of rotation
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

69

ROBOT . HEAD to TOE


Product Users Manual PTK40A

that was taken by the slotted disc on LCD.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

70

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 11.2: Connection of encoder to PIC Microcontroller

11.3 How to program


Timer0 may be used to count the pulses from rotary encoder. To utilize Timer0 for this
purpose, as usual, it needs to be initialized in prior. The detailed configuration of TIMER0
can be referred to the datasheet of PIC18F4520.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

71

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 12
Real Time Clock
12.1 Introduction
In this chapter, we will learn how to generate real time clock through timer1 and displaying it
on the LCD. PIC itself does not know about time, thus an oscillator is needed to generate
time. Although there is a 20MHz oscillator for PIC microcontroller, this oscillator is unable
to generate real time for the microcontroller because the 20MHz oscillator will generate
pulses which are too huge for timer buffer to count 1 second. It requires slower clock source.
12.2 Timer 1 as Real Time Clock (RTC)
This section will explore the method using Timer1 in PIC Microcontroller as Real Time
Clock (RTC).
First is to configure 16 bit timer and external clock. Timer1 is a 16-bit timer/counter
consisting of two 8-bit registers (TMR1H and TMR1L). Timer1 can be enabled/disabled by
setting/clearing control bit (TMR1ON). T1CON in control register for timer1. Configure this
register to configure 16 bit timer and external clock. Other registers need to be configured are
PIR1, PIE1 and INTCON. PIR1 register contains the individual flag bits for the peripheral
interrupts. PIE1 register contains the individual enable bits for peripheral interrupts.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

72

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 12.1: Connection for timer

TMR1L and TMR1H are separate register files located in SFR (Special Function Register).
These registers pair is incremented when pulses coming in and Timer 1 is enabled via the
T1CON register.
Incrementing TMR1L will rolls over and increments TMR1H on overflow.
TMR1IF is set when TMR1H overflow.
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

73

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Each register can be read or written to during program.


Each of these registers is 8 bits wide so it takes 2 write operations to load this
register pair.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

74

ROBOT . HEAD to TOE


Product Users Manual PTK40A

12.3 I2C with External RTC Chip, DS1307


DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD)
clock/calendar plus 56 bytes of NV SRAM chip. Address and data are transferred serially
through an I2C, bidirectional bus. The clock/calendar provides second, minute, hour, day,
date, month, and year information. The end of the month date is automatically adjusted for
months with fewer than 31 days, including corrections for leap year. The clock operates in
either 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in powersense circuit that detects power failures and automatically switches to the backup supply.
Timekeeping operation continues while the part operates from the backup supply.
I2C pin is being shared with SPI, thus either one of this protocol can be used in a time. Like
timer, I2C need to configure before it is being used. Two pins used for data transfer is Serial
clock (SCL) and Serial data (SDA). These pins must configure as input. They are six register
for I2C operation. SSPCON, SSPCON1, SSPCON2 and SSPSTAT are the control and status
registers in I2C mode operation. SSPSR is the shift register used for shifting data in or out.
SSPBUF is the buffer register to which data bytes are written to or read from. SSPADD
register holds the slave device address when the SSP is configured in I2C Slave mode. When
the SSP is configured in Master mode, the lower seven bits of SSPADD act as the baud rate
generator reload value.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

75

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

76

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Below the schematic of DS1307 interface.

Figure 12.2: Connection for I2C RTC


12.4

Hardware configuration

For hardware configuration, move mini jumper an JP3 and JP5 to I2C shown as figure below.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

77

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 12.3: Hardware configuration for I2C

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

78

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 13
SPI with EEPROM
13.1 Introduction
SPI (Serial Peripheral Interface) is one of the serial communications. The Serial Peripheral
Interface is a synchronous serial data link standard named by Motorola that operates in full
duplex mode. Devices communicate in master/slave mode where the master device initiates
the data frame. Multiple slave devices are allowed with individual slave select lines. For
learning purpose, PTK40A uses SPI Serial EEPROM as a slave device.

13.2 How to Program


The SPI allow 8 bits of data to be synchronously transmitted and receive simultaneously. To
accomplish communication, typically three pins are used:

Serial
Serial
Serial
Slave

Data Out (SDO) RC5/SDO


Data In (SDI) RC4/SDI/SDA
Clock (SCK) RC3/SCK/SCL
Select RA5/AN4 (may be used when in a slave mode operation)

Again, SPI registers are need to be configured. SSPCON1 and SSPSTAT are the control and
status registers in SPI mode operation. The SSPCON1 register is readable and writeable.
After all the configuration and initialization have been completed, user may start to use it.
The schematic of SPI EEPROM is shown below.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

79

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 13.1: External EEPROM interfaced through SPI

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

80

ROBOT . HEAD to TOE


Product Users Manual PTK40A

13.3 Hardware configuration


For hardware configuration, move mini jumper at JP3, JP5, JP6 and JP7 to SPI in order to
utilize SPI communication shown as figure below.

Figure 13.2 Hardware configurations for SPI

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

81

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 14
UART
14.1 Introduction
A Universal Asynchronous Receiver and Transmitter is a type of "asynchronous
receiver/transmitter", a piece of computer hardware that translates data between parallel and
serial forms. A UART is usually an individual (or part of) integrated circuit used for serial
communications over a computer or peripheral device serial port. UART is now commonly
included in microcontrollers.
UART communication can be done between PTK40A and computer or Cytron starter kit like
SKPS. To communicate between PTK40A and computer, UC00A replace RS232 serial port
as serial communication. In this section, sample test code will show you how PTK40A send
command to SKPS to read value of joystick.
14.2 How to Program
RCSTA and TXSTA are status and control register for UART. Initialize this register to
transmit and receive data from or to computer. Baud rate is the most important element in
transmit and receive data. Correct baudrate is necessary in UART. Example of common baud
rate used are 9600bps, 57600bps and 115200bps.
To transmit a character, program need to check and ensure TXIF bit is set. Transmitter
Interrupt Flag (TXIF) is set when the transmit buffer is empty, thus program may load data to
it. The transmit engine will automatically shift the data out when there is data in TXREG.
TXIF will automatically be cleared when there is a write operation to TXREG.
To receive a character, program need to at least check the RCIF bit is set. Receiver Interrupt
Flag (RCIF) is set when the receive buffer (RCREG) is full, thus the program may copy the
received data for process. RCIF will automatically clear once a read operation is perform on
RCREG.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

82

ROBOT . HEAD to TOE


Product Users Manual PTK40A

14.3 Hardware configuration


For hardware configuration, move mini jumper at JP9 to SK-RST to used Cytron starter kit
shown as figure below.

Figure 14.1: Hardware connection between PTK40A and SKPS

Figure 14.2: Hardware configurations for UART

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

83

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Figure 14.3: Hardware connection between PTK40A and PC

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

84

ROBOT . HEAD to TOE


Product Users Manual PTK40A

Chapter 15
Universal Serial Bus (USB)
15.1 Introduction
Universal Serial Bus (a.k.a USB) is commonly being used on computer and electronic
devices such as mouse, keyboard, pendrive, camera, etc. It is an industry standard developed
in the mid of 1990s that defines the cables, connectors and communications protocols used in
a bus for connection, communication, and also power supply. Since, it has become common
standard for other devices, such as smartphones, PDAs and video game consoles. USB has
effectively replaced a variety of earlier interfaces, such as serial COM port and parallel ports
on computer. Now, PIC microcontroller offers USB device peripheral in certain model,
example is PIC18F4550. With USB device, a PIC18F4550 can be programmed to become a
USB mouse, keyboard, joystick, etc.
PTK40A comes with the USB B type socket and interface ready for USB development.
However, the PIC MCU must be changed to PIC18F4550 to support USB interface. With
USB connection, user can even load program via USB cable without using programmer, we
call this USB bootloader.
15.2 How to Program
USB peripheral is quite complex to be explained in few pages of document, particularly it is
on an embedded platform where no ready driver is available. Knowing the difficulties and
challenges, Microchip offers a comprehensive getting starting guide. Besides the MPLAB X
IDE, Microchip have developed plenty of USB library which include:
Audio - Microphone
Audio - MIDI
Audio - Speaker
Bootloader - HID and Custom
CCID - Smart Card Reader
CDC - Virtual Serial Com Port
HID - Joystick
HID - Keyboard
HID - Mouse
more.
Download the latest Microchip Application Libraries, install and study example under the
USB folder.
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

85

ROBOT . HEAD to TOE


Product Users Manual PTK40A

PIC18F4550 that comes in PTK40A is pre-programmed with USB HID bootloader. The
firmware of HID bootloader is being modified from Microchip HID bootloader, user are
welcome to explore and study the code. The firmware is located under PTK40A CD or
DVD\Applications\Bootloader firmware\PTK_C18_USB_HID_Boot.X
The PTK40A USB HID bootloader is being compiled under MPLAB X IDE v1.70 with
MPLAB C18 C compiler v3.43.

Figure 15.1: PTK40A USB HID firmware open under MPLAB X IDE
Note: USB HID firmware is provided as it is, Cytron Technologies does not take any
responsible for further improvement nor customization.
To utilize the HID bootloader, please refer to the PTK40A with USB Bootloader.

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

86

ROBOT . HEAD to TOE


Product Users Manual PTK40A

15.3 Hardware configuration


If USB is needed on PTK40A, there are a few requirements:
PIC must be replaced with PIC18F4550
USB B type cable is needed, and it should be connected to USB B type
connector, left bottom corner of PTK40A

Figure 15.2: USB B type cable is connected to B type socket on PTK40A


3 x Jumper needed to be move to USB

Figure 15.3: Three jumpers are moved to USB

Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

87

ROBOT . HEAD to TOE


Product Users Manual PTK40A

16. WARRANTY

Product warranty is valid for 12 months.


Warranty only applies to manufacturing defect.
Damaged caused by misuse is not covered under warranty
Warranty does not cover freight cost for both ways.

Prepared by:
Cytron Technologies Sdn. Bhd.
No. 16, Jalan Industri Ringan Permatang Tinggi 2,
Kawasan Industri Ringan Permatang Tinggi,
14100 Simpang Ampat,
Penang, Malaysia.
Tel: +604-504 1878
Fax: +604-504 0138
URL: www.cytron.com.my
Email: support@cytron.com.my
sales@cytron.com.my
Created by Cytron Technologies Sdn. Bhd. All Rights Reserved

88

Anda mungkin juga menyukai