What is microcontroller a case study on PIC Microcontroller 16F877
1.
Introduction
This tutorial is to introduce the microcontroller technolgy the capabilities and the specifications of a commonly used Microcontroller Microchip PIC Microcontroller 16F877 and describe the experiments conducted using the Development board Flash PIC Microcontroller development board which accomodates this microcontroller.
1.1.
What is a microcontroller?
A microcontroller is a compact standalone computer, optimized for control applications. Entire processor, memory and the I/O interfaces are located on a single piece of silicon so, it takes less time to read and write to extrernal devices.
1.2.
Why are microcontrollers used?
Following are the reasons why microcontrollers are incorporated in control systems:
- Cost: Microcontrollers with the supplemantary circuit components are much cheaper than a computer with an analog and digital I/O
- Size and Weight: Microcontrollers are compact and light compared to computers
- Simple applications: If the application requires very few number of I/O and the code is relatively small, which do not require extended amount of memory and a simple LCD display is sufficient as a user interface, a microcontroller would be suitable for this application.
- Reliability: Since the architecture is much simpler than a computer it is less likely to fail.
- Speed: All the components on the microcontroller are located on a singe piece of silicon. Hence, the applications run much faster than it does on a computer.
2.
Getting Started
2.1.
Parts list:
The parts list and the vendor list is given in Table-2.1.1
Description
|
Vendor
|
Part No.
|
Price
|
Qty.
|
FlashPIC Microcontroller-Development Board |
PRLLC |
FlashPIC Microcontroller-Dev |
45.00$ |
1 |
CCS ICD-S debugger/ programmer |
PRLLC |
ICD-S |
75.00$ |
1 |
Embedded C Programming and the Microchip PIC Microcontroller Richard H. Barnett, Larry D. O’Cull, Sarah A. Cox ISBN: 1401837484 |
Amazon.com |
N/A |
57.70$ |
1 |
12VDC adapter |
Radioshack |
273-1776 |
16.99$ |
1 |
8 position dip switch |
Jameco |
38842 |
0.89$ |
1 |
Multipurpose PC Board |
276-150 |
$1.69 |
1 |
|
LM35CZ Linear Series |
Jameco |
107107 |
$5.79 |
1 |
Plastic Sphere |
Bubblegum machine |
|
|
|
DAC-0832 8 bit DAC |
Jameco |
128186 |
3.95 |
1 |
LF353 Op-Amp |
Jameco |
22939 |
$.39 |
1 |
10Kohm Resistor |
Radioshack |
271-1335 |
$.99 |
1 |
20Kohm Resistor |
Radioshack |
271-0265 |
$.99 |
2 |
10ohm 10W Resistor |
Radioshack |
271-132 |
$1.69 |
1 |
TIP31 Power Transistor |
Jameco |
33048 |
$.49 |
1 |
0.01uF Capacitor |
Radioshack |
272-1051 |
$1.19 |
1 |
1N4004 Diode |
Radioshack |
276-1103 |
$.79 |
1 |
100 ohm resistor |
Radioshack |
271-1311 |
$.99 |
1 |
Technie Toyz 12V PC Fan |
Compusa |
MD-TTF-8025A-2B |
$10.99 |
1 |
9V battery |
Radioshack |
23-875 |
$3.29 |
1 |
6-Ft. Serial RS-232C Cable |
Radio Shack |
26-117 |
$13.49 |
1 |
1x40 pin header |
Jameco |
103270 |
0.75$ |
1 |
2.2.
Connecting
the equipment
a. Powering up the PIC Microcontroller Development Board
The PIC Microcontroller 16F877 chip has to be powered at all times when its being programmed and running an application.
- Cut the connector off the DC end of the 12VDC adapter and strip the wire ends.
- Determine the + and – wire leads using a voltmeter.
- Apply the 12VDC to the board by connecting the 12VDC adapter to + and – pins of J2 of Flash PIC Microcontroller Development Board. See Figure2.2-1
Caution: Make sure you wire the – and + ends of the adapter of the to the right pins of Jumper 2. Do not plug the adapter to 110V/AC outlet before you wire the power to the board. In order to avoid cheking the leads with a voltmeter everytime, its always a good practice to label the leads for future reference.
Figure-2.2-1 12VDC
Power connection J2
- Plug the adapter to the 110V/AC outlet.
- Observe the green LED D9 illuminating and rest of the LEDs flashing in order.
b.Connecting the Flash PIC Microcontroller
Development Board to a PC through ICD-S:
After we put power on the FlashPIC Microcontroller development board, now we are ready to program the PIC Microcontroller 16F877 chip.
1. Connect the ICD-S to the computer using the RS-232 cable, which came with the ICD-S.
2. Conncect the ICD-S to the developmet board using the phone jack. (See Figure2.2-2)
Figure2.2-2 Connecting the ICD-S, PC, and the FlashPIC Microcontroller-Development Board
c. Coding,Compiling and Transfering the .hex files to the chip
Now that we have our hardware setup, we are ready to write the code and transfer the .hex files to the PIC Microcontroller 16F877.
- Install the CCS compiler demo version, which came with the reference book ”Embedded C Programming and the Microchip PIC Microcontroller” to your computer
- Insall the ICD software which came with the ICD-S unit.
- Start the CCS compiler.
- Go to menu path: Options>Debugger/Programmer
- Change the to the path of Icd.exe. (See Figure2.2-3)This would enable starting the ICD-S software via toolbar button.
Figure-2.2-3 Device Debugger/Programmer Options Menu
- The software setup is complete. We can type the source code and compile it and transfer the file to the PIC Microcontroller chip as illustrated in Figure2.0-3. After single clicking the “compile” command button on the toolbar and generating the .hex file, we can single click the “program chip” command button. These coomand buttons are illustrated in Figure 2.2-4.
Figure2.2-4 The CCS Compiler
- After the “program chip” command button is pressed the ICD V2.7 program is initiated. First we check the RS-232 communications, the ICD-S and the PIC Microcontroller 16F877 by using the following command buttons respectively: “Check COMM”, “Test ICD”, “Test Target”. If all of them check to be OK, now we can browse and transfer the .hex file to the chip by using “Download to target Command button”. After the .hex file is transferred, now we can run the software and test its functionality by clicking on “Run Program” command button. (Figure2.2-4)
- Since the software is tested now the ICD-S can be disconnected and the Development board can be powered up and the .hex file can be run without being connected to the computer.
Figure2.2-5 ICD Control Program
3.
PIC Microcontroller 16F877
Microcontroller
In this section, properties of PIC Microcontroller 16F877 microcontroller,CCS compiler, ICD-S, the reference book “Embedded C programming and the microchip PIC Microcontroller” are briefly explained to give a general idea; it may seem confusing for a first time reader who is not familiar to microcontrollers technology or C programming. However, as the funtionality of the components such as timers, A/D converters, I/O Ports are explained in detail in Section 3 as they are being used in the experiments, the fundemental concepts would be better understood and, the reader can flashback to this section to view the schematics and the specifications.
PIC Microcontroller 16F877 is one of the most commonly used microcontroller especially in automotive, industrial, appliances and consumer applications. In Figure –1, the block diagram of the PIC Microcontroller 16F877 is illustrated.
The core features of PIC Microcontroller 16F877 are:
• High performance RISC CPU
• Only 35 single word instructions to learn
• All single cycle instructions except for program
branches which are two cycle
• Operating speed: DC - 20 MHz clock input
DC - 200 ns instruction cycle
• Up to 8K x 14 words of FLASH Program Memory,
Up to 368 x 8 bytes of Data Memory (RAM)
Up to 256 x 8 bytes of EEPROM Data Memory
• Pinout compatible to the PIC Microcontroller16C73B/74B/76/77
• Interrupt capability (up to 14 sources)
• Eight level deep hardware stack
• Direct, indirect and relative addressing modes
• Power-on Reset (POR)
• Power-up Timer (PWRT) and
Oscillator Start-up Timer (OST)
• Watchdog Timer (WDT) with its own on-chip RC
oscillator for reliable operation
• Programmable code protection
• Power saving SLEEP mode
• Selectable oscillator options
• Low power, high speed CMOS FLASH/EEPROM
technology
• Fully static design
• In-Circuit Serial Programming. (ICSP) via two
pins
• Single 5V In-Circuit Serial Programming capability
• In-Circuit Debugging via two pins
• Processor read/write access to program memory
• Wide operating voltage range: 2.0V to 5.5V
• High Sink/Source Current: 25 mA
• Commercial, Industrial and Extended temperature
ranges
• Low-power consumption:
- < 0.6 mA tyPIC Microcontrolleral @ 3V, 4 MHz
- 20 μA tyPIC Microcontrolleral @ 3V, 32 kHz
- < 1 μA tyPIC Microcontrolleral standby current
The peripheral features of the PIC Microcontroller 16F877 are:
• Timer0: 8-bit timer/counter with 8-bit prescaler
• Timer1: 16-bit timer/counter with prescaler,
can be incremented during SLEEP via external
crystal/clock
• Timer2: 8-bit timer/counter with 8-bit period
register, prescaler and postscaler
• Two Capture, Compare, PWM modules
Capture is 16-bit, max. resolution is 12.5 ns
- Compare is 16-bit, max. resolution is 200 ns
- PWM max. resolution is 10-bit
• 10-bit multi-channel Analog-to-Digital converter
• Synchronous Serial Port (SSP) with SPI. (Master
mode) and I2C. (Master/Slave)
• Universal Synchronous Asynchronous Receiver
Transmitter (USART/SCI) with 9-bit address
detection
• Parallel Slave Port (PSP) 8-bits wide, with
external RD, WR and CS controls (40/44-pin only)
• Brown-out detection circuitry for
Brown-out Reset (BOR)
The Pinouts of the PIC Microcontroller 16F877 is given in Figure 3.0-2
Figure-3.0-1 PIC Microcontroller 16F877 Block Diagram
Figure 3.0-2 Pin diagram of PIC Microcontroller 16F877
3.1.
Flash PIC Microcontroller development board
Usually, a microcontroller by itself is not sufficient to perform the intended tasks. For instance, an oscillator chip is necessary to time the programmed instructions. In order to investigate the capabilities or to test a given microcontroller, obviously it is vital to build the proper circuitary. Example: potentiometer and a power supply to simulate analog inputs or LEDs to simulate the digital outputs. Hence, some hardware and sofware vendors provide the microcontroller with the supplemantary circuit elements on the same breadboard. These boards are called Development Boards. One can also build a development board himself/herself if he/she is willing to go through the painsaking process of building the circuit.
Microcontroller. Microcontroller Pakistan,Microcontroller based projects,Microcontroller. What is Microcontroller
Figure-3.1-1 FlashPIC Microcontroller Development Board
The development board used in the series of experiments is Flash PIC Microcontroller development board. (Figure3.1-1) It has the following features:
- RS232 through a 9-Pin D-Shell as well as screw terminals and a jumper header.
- Up to 32K words of In-System Programmable FLASH memory with up to 256
bytes of EEPROM and up to 1.5K of Internal RAM (depending on processor
selection).
- Up to 8, 10 bit, Analog Inputs, using either internal or user supplied reference.
- 9 I/O controlled LEDs, 8 of which are jumper selectable.
- 32KHz “watch” crystal for on-board Real-Time operations.
- A universal clock socket allows for “canned oscillators”, as well as a variety of
crystals, ceramic resonators, and passive terminations.
- 0.1” centered headers provide for simple connection to the processor special
function pins and I/O.
- A 6-pin, ICD connection is provided for in-system programming and debugging.
This connection is directly compatible with the Microchip ICD, ICD2 and CCS
ICD-S programming hardware. Flash PIC Microcontrollers can also be programmed through
RS232 using an appropriate boot loader application.
- On-board regulation allows for power inputs from 8-38VDC with an LED power
indicator.
- Termination is provided for 5VDC output at 250ma
3.2.
CCS PIC Microcontroller-C compiler
The CCS PCW compiler is specially designed to meet the special needs of the PIC Microcontrollermicro MCU controllers. These tools allow developers to quickly design application software for these controllers in a highly readable, high-level language.
The compilers has some limitations when compared to a more traditional C
compiler. The hardware limitations make many traditional C compilers
ineffective. As an example of the limitations, the compilers will not permit
pointers to constant arrays. This is due to the separate code/data segments in
the PIC Microcontrollermicro MCU hardware and the inability to treat ROM areas as data. On
the other hand, the compilers have knowledge about the hardware limitations
and do the work of deciding how to best implement your algorithms. The
compilers can efficiently implement normal C constructs, input/output operations
and bit twiddling operations.
The compiler can output 8 bit hex, 16 bit hex, and binary files. Two listing formats
are available. Standard format resembles the Microchip tools and may be
required by some third-party tools. The simple format is easier to read. The
debug file may either be a Microchip .COD file or Advanced Transdata .MAP file.
All file formats and extensions are selected via the Options|File Formats menu
option in the Windows IDE.
The usage of the copiler is explained in Section 2.0 Getting started. The reference book “Embedded C Programming and the Microchip PIC Microcontroller” comes with a demo version of the compiler.
3.3.
ICD-S Debugger/Programmer
The ICD-S programmer is the hardware/firmware interface to burn the .hex files to the Microchip PIC Microcontroller. The ICD unit works with CCS's PCW debugger or CCS's stand-alone ICD control software. CCS's PCW debugger is a very robust debugger integrated with PCW, and provides very detailed debugging information at the C level. The stand-alone control software allows you to quickly program target chips using ICD's ICSP. The control software also lets you update the ICD unit's firmware without having to remove the chip from the ICD unit. (Using these software tools requires you to have loaded the CCS-ICD firmware onto the ICD unit, which is loaded by default.
Microcontroller. Microcontroller Pakistan,Microcontroller based projects,Microcontroller
Figure-3.3-1 FlashPIC Microcontroller Development Board
3.4.
The Reference Book: Embedded C Programming and the
Microchip PIC Microcontroller by Barnett, Cox and O’Cull
This book is a good guide for introducing the microcontroller technology. First chapter is dedicated to teaching basic C programming however, this book shouldn’t be considered a C programming handbook. One should always have a book like: Teach yourself C by Zhang ISBN 0-672-31861 as a C programming reference guide for beginners.
This book is
designed to teach C language programming as it applies to embedded
microcontrollers and to fuel knowledge in the application of the Microchip family
of PIC Microcontroller microcontrollers. Coverage begins with a step-by-step exploration of the
C language showing readers how to create C language programs to solve problems.
PIC Microcontroller processors are then studied, from basic architecture to all of the standard
peripheral devices included in the microcontrollers. Numerous worked-out
example programs demonstrate common uses for each of the peripherals. Readers
are subsequently introduced to the built-in functions available in C, to speed
their programming and problem solving. Finally, readers are taken through use
of the C Compiler, and to help custom learn to efficiently develop projects.
Figure-3.4-1
Embedded C Programming and the Microchip PIC Microcontroller
Included with the book is a CDROM containing samples all of the example programs from the book as well as an evaluation version of the CCS-PIC MicrocontrollerC Compiler.
Author`s note 01/22/04 : In the first examples and projects in chapter 1, functions like “scanf” and “printf” are used that require prior knowledge to interface the board through RS-232 which is introduced in the late chapters of the book. It might be discouraging for the student not being able to do the first project of the book, hands-on. Although I was able to find answers to most of my questions about using the C compiler and the hardware however, I had to do a lot of skipping between chapters and Appendixes to find these answers which was time consuming. In general it is a nice and descriptive text book.
4.
Experiments
The most quick and effective way of learning a new toPIC Microcontroller is, having hands on experience. In this section we will conduct some experiments using the FlashPIC Microcontroller Development board to which would give us a jump start on in PIC Microcontroller usage.
Where to Find in Pakistan:
Shops Electronic 2000, IC Shop, New Dynamic, Hall Road Lahore.
Saniyo Electronics, Hussain Agahi Multan.
