0% found this document useful (0 votes)
58 views6 pages

PICExperimentsLabBookwithPIC18F2431andXC8new TOCandpreface

Uploaded by

alvaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views6 pages

PICExperimentsLabBookwithPIC18F2431andXC8new TOCandpreface

Uploaded by

alvaro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/344394888

PIC Experiments Lab Book with PIC18F2431 and XC8

Book · September 2020

CITATIONS READS

0 1,234

1 author:

Innocent Ejiro Okoloko


Embedded Systems Services
22 PUBLICATIONS 42 CITATIONS

SEE PROFILE

Some of the authors of this publication are also working on these related projects:

Robot soccer project View project

All content following this page was uploaded by Innocent Ejiro Okoloko on 27 September 2020.

The user has requested enhancement of the downloaded file.


PIC Experiments Lab Book with
PIC18F2431 and XC8
Innocent Okoloko

September 1, 2020
PIC Experiments Lab Book
with
PIC18F2431 and XC8
(Codes in MPLAB X colour)

Innocent Okoloko

September 2020
Contents
Preface ............................................................................................................................................................................. 3
1 Downloading and installing MPLAB X..................................................................................................................... 4
2 Downloading and installing compilers for MPLAB X ............................................................................................ 5
3 ICSP connection for PIC18F2431 using PICkit3 & PICkit4................................................................................... 6
4 Power supply.............................................................................................................................................................. 10
5 Equipment and supplies........................................................................................................................................... 13
6 Creating a new project in MPLAB X ....................................................................................................................... 14
7 Experiment 1. LED Flash ......................................................................................................................................... 20
8 Experiment 2. Blink LEDS....................................................................................................................................... 22
9 Experiment 3. Count button presses without debouncing .................................................................................. 24
10 Experiment 4. Count button presses with debouncing ...................................................................................... 26
11 Experiment 5. Count button presses using interrupts........................................................................................ 28
12 Experiment 6. 4x4 Matrix keypad......................................................................................................................... 31
13 Experiment 7. 2x20 LCD ........................................................................................................................................ 36
14 Experiment 8. 4x4 Keypad + 2x20 LCD .............................................................................................................. 45
15 Experiment 9. 4X4 Keypad + 2x20 LCD with interrupt .................................................................................... 50
16 Experiment 10. 7 Segment LED ............................................................................................................................ 55
17 Experiment 11. 7 Segment LED direct connection .............................................................................................. 58
18 Experiment 12. Keypad with 7 segment LED ...................................................................................................... 61
19 Experiment 13. 7 Segment LEDs in series ........................................................................................................... 66
20 Experiment 14. Analog-to-digital conversion (ADC) ......................................................................................... 69
21 Experiment 15. ADC digital voltmeter.................................................................................................................. 71
22 Experiment 16. ADC capture compare PWM (CCP) .......................................................................................... 74
23 Experiment 17. Pulse width modulation (PWM) using CCP module .............................................................. 77
24 Experiment 18. ADC multichannel....................................................................................................................... 79
25 Experiment 19. POT DC motor speed control..................................................................................................... 82
26 Experiment 20. H-Bridge with discrete components ........................................................................................ 85
27 Experiment 21. L293D H-Bridge DC motor control .......................................................................................... 89
28 Experiment 22. L293D DC motor direction and speed control ....................................................................... 91
29 Experiment 23. SyRen 10 DC motor controller with PWM .............................................................................. 94
30 Experiment 24. Stepper motor: bipolar full-step 1 phase on ........................................................................... 97
31 Experiment 25. Stepper motor: bipolar full-step 2 phase on .......................................................................... 102
32 Experiment 26. Stepper motor: bipolar half-step ............................................................................................ 105
33 Experiment 27. DC servo motor control with PWM ........................................................................................ 108
34 Experiment 28. DC servo motor control with PWM and POT ....................................................................... 111
1
35 Experiment 29. Brushless DC motor control with ESC and PWM ................................................................ 113
36 Experiment 30. LDR light sensor with ADC and PWM .................................................................................. 116
37 Experiment 31. Infrared remote control ............................................................................................................ 119
38 Experiment 32. Relay to switch AC electric light ............................................................................................. 122
39 Experiment 33. PIC-to-PIC parallel communication ...................................................................................... 124
40 Experiment 34. UART RS232 FTDI232 transmit a character ...................................................................... 127
41 Experiment 35. UART RS232 MAX232 transmit a character ....................................................................... 129
42 Experiment 36. UART RS232 to receive a character and light LEDs ........................................................... 131
43 Experiment 37. UART RS232 to transmit a string .......................................................................................... 136
44 Experiment 38. UART RS232 to receive characters and write to LCD ........................................................ 138
45 Experiment 39. UART RS232 keypad character to terminal ......................................................................... 140
46 Experiment 40. UART RS232 ADC data logging ............................................................................................. 144
47 Experiment 41. UART PIC to PIC serial communication ................................................................................ 147
48 Experiment 42. Bluetooth UART to transmit a character .............................................................................. 151
49 Experiment 43. Bluetooth UART to receive character and light LEDs ........................................................ 153
50 Experiment 44. Bluetooth UART to transmit a string .................................................................................... 156
51 Experiment 45. Bluetooth UART to receive character write to LCD ............................................................. 158
52 Experiment 46. Bluetooth UART write keypad character to terminal ......................................................... 161
53 Experiment 47. Bluetooth UART ADC data logging ........................................................................................ 165
54 Experiment 48. Temperature sensor data using Bluetooth UART ................................................................ 168
55 Experiment 49. Serial peripheral interface (SPI) basic ................................................................................... 170
56 Experiment 50. Serial peripheral interface (SPI) bidirectional ..................................................................... 174
57 Experiment 51. SONAR HC-SR04 data to LCD ................................................................................................ 179
58 Experiment 52. SONAR HC-SR04 data to LCD using interrupt.................................................................... 182
59 Experiment 53. Radio transmitter frequency and period with CCP .............................................................. 185
60 Experiment 54. Radio transmitter pulse width with CCP............................................................................... 189
61 Experiment 55. Temperature controlled electric fan ....................................................................................... 192
62 Experiment 56. Light seeking robot ................................................................................................................... 195

2
Preface
This book is a sequel to my first experiments lab book, PIC EXPERIMENTS LAB BOOK USING PIC16F877A
and XC8. Like the first book, this one is also intended for use as a fast-paced laboratory guide for practical
experimental work using Microchip® PIC microcontrollers. In order for students, engineers, hobbyists and
enthusiasts to understand the inner workings of processors and embedded systems they need to be familiar
with a real barebone microcontroller, where you connect the peripherals from scratch and you write the
libraries from scratch.
There are new features in this book which are improvements on the previous book. First, why did I choose a
PIC18F2431? I attempted to replicate the experiments in the previous book, using a smaller more advanced
processor that will consume less power and can be programmed faster. This approach is more efficient and
economical from the engineering point of view. With the facilities available in the PIC18F2431, I believe that
with more literature and examples on how to use it, it can become very popular with users.
The previous book contained 29 Experiments; this book contains 56 Experiments. I observed that a required
LCD header file “CHARACTER_MAP.h” was omitted by error in the previous book. This book includes not
only the “CHARACTER_MAP.h” but also a complete LCD library header file “SUNPLUSLCD.h” which uses
the “CHARACTER_MAP.h”. Moreover, a new USART library file “UART.h” has been included. All the
experiments implementing USART with RS232 have been replicated using Bluetooth and even more
experiments on Bluetooth are added. This is because it is more convenient and economical to implement
serial communication using Bluetooth than RS232 (as long as the environment is not too noisy). Other new
experiments are: FTDI232, SPI, SONAR, temperature sensor, temperature controlled fan, relay, signal
processing using drone radio transmitter and receiver, multichannel ADC, brushless DC motor (BLDC) ESC,
bipolar stepper full-step (1 phase and 2 phase), bipolar half-step, and a light seeking robot.
In addition, all codes are printed with the full MPLAB X colour for readability and understanding. The
diagrams have been redrawn and posted as high quality svg images in full colour. Two new chapters, “Power
supply” and “Equipment and tools” have been included. A section on troubleshooting has also been included
after every similar experiment.
To make the codes easier to understand, references to the datasheets for components used in an experiment
have been placed in the introductory comment section of each program. An attempt is made to direct the
reader to the section of the appropriate datasheet whenever a register or concept is used from a datasheet in
parts of the code. This will direct readers to which page in the datasheet clearly explains what was done in
that section of code.
This book as it is, is still an experiments lab book. Therefore, it is assumed that the reader has a knowledge
of basic electrical circuits or basic electronics, and C programming.
The book is a collection of experiments using a single advanced 8-bit microcontroller from Microchip® - the
PIC18F2431. The language used is XC8, free from Microchip®, and there is no theoretical burden. The
programming environment used is MPLAB X, also free from Microchip®. The book is intended for use in
companion with a theoretical reading/course on embedded systems (or similar course), along with the
PIC18F2431 datasheet (Microchip document DS39616D), and all other datasheets that are included in each
experiment, which should be used as reference guides. With the datasheet of any other processor different
from the PIC18F2431 the book can also be used with that PIC microcontroller. All one needs to do is to look
for the similar pinouts and ports in the datasheet of the other microcontroller and follow the examples in this
book. So, the knowledge gained here can be applied to other PIC microcontrollers with a little more effort.
Future editions will include more experiments and projects.
For those who want to practically learn the theory of embedded systems using PIC and have the time for
reading a voluminous textbook, I intend to write a practical introductory textbook in the near future, based
on the experiments in this book and more experiments to come.

View publication stats

You might also like