HEAD to TOE
Product Users Manual PTK40A
PTK40A
Training Kit
User Manual
V2.0
August 2014
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
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
74
74
74
76
70
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
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.
DC adaptor socket
25
I2C connector
26
DS1307 RTC IC
27
3V cell battery
IC-CD4511
28
29
SPI EEPROM
RC Servo motor
30
Pin selector
Relay
31
Expansion Ports
32
Reset button
33
4x4 Keypad
10
External Stepper
driver
34
Programmable switches
11
35
IC MCP602
12
36
DC motor
13
Unipolar/Bipolar selector
37
14
Stepper motor
38
IR sensor
15
39
16
40
17
41
Buzzer
18
20Mhz Crystal
42
19
43
20
44
LEDs
21
45
Analog selector
22
46
23
UART VCC
47
Potentiometer
24
UART connector
48
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.
10
11
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
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
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.
14
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.
15
16
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.
17
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
PTK40A_PIC18F_Template.c
system.h
into the project folder, in this example is PTK40A Template.X
19
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.
20
21
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.
22
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.
23
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.
24
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.
25
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.
26
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
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.
28
5. The following window concerns the installation folder. Click Browse if you
want to change the default destination. Assuming change, click on Next.
29
30
31
32
33
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
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.
35
36
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.
37
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.
38
39
40
41
and interesting.
42
43
44
Chapter 5
7-Segment Display
5.1 Introduction
45
46
47
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.
48
49
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
50
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.
51
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.
52
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.
53
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.
54
55
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.
56
57
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.
58
59
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.
60
61
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.
62
63
64
65
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.
66
67
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.
68
Chapter 11
Rotary Encoder
11.1 Introduction
69
70
71
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.
72
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
74
75
76
Hardware configuration
For hardware configuration, move mini jumper an JP3 and JP5 to I2C shown as figure below.
77
78
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.
Serial
Serial
Serial
Slave
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.
79
80
81
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.
82
83
84
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
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.
86
87
16. WARRANTY
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