Thursday, 8 August 2013

Getting down with PIC16F87X Microcontroller

Introduction
Embedded microcontrollers are everywhere in today's world, as you might know microcontroller and microprocessor  have substantially change our electronics lives . In an average household you will find them in cell phones, TV, calculators, remote control, air conditioner, microwave oven and MP3 players etc. Hardly  will any new appliance gets home without at least one controller. For example, in an  MP3 player, one controller could be for button and display, another for converting the music from digital format to something we can hear, etc. Nowadays, electronics device and circuit are mostly design as software running within microcontrollers, and these little devices can do so much beyond our imaginations. So electronics today are blend of hardware and software.
Microcontroller are simple to use, you don't need to be an expert in electronics before you start using them, all you need to get started is the basic understanding of electronics and digital circuits. Once you get your grips on it, that's it.
This write-up is  focused on  the overview of the PIC microcontroller internal architecture, describing the features of the 16F87X specifically.

What is a microcontroller?
Microcontrollers and microprocessors are integrated circuits, but they differ fundamentally from other ICs. The designers of microcontrollers have not made them to do a particular job. As such when
you buy them, you can not specify what function it will do, until you get home and configure it as you desire. Thus a microprocessor or microcontroller can be configured to check the status of a button, and then turn a motor ON or OFF. While the same IC can be configured later, to read the status of an infra-red sensor, decode the signal and turn another device ON or OFF. If these two types of circuits were to be made using conventional digital ICs, it would have required a large number of components. Moreover any change in the specification, like change of Infra-Red codes would result in total change in design.
The ability to configure microcontroller is called programming. A program is nothing but a series of instructions, in a correct and logical manner to instruct the microprocessor respond to various inputs. Microcontrollers are programmed to do a specific job, and these jobs could vary from changing TV channels to controlling industrial processes.

PIC Microcontroller
A company named Microchip® made its first simple microcontroller, which they called PIC. Originally this was developed as a supporting device for PDP computers to control its peripheral devices, and therefore named as PIC, Peripheral Interface Controller. Thus all the chips developed by Microchip® have been named as a class by themselves and called PIC.
The PIC16F87X is a family member of PIC CMOS FLASH-based 8-bit microcontrollers. It's upward compatible with the PIC16C5x, PIC12Cxxx and PIC16C7x devices. It features 200 ns instruction execution, 256 bytes of EEPROM data memory, self programming, an ICD, 2 Comparators, 8 channels of 10-bit Analog-to-Digital (A/D) converter, 2 capture/compare/PWM functions, a synchronous serial port that can be configured as either 3-wire SPI or 2-wire I2C bus, a USART, and a Parallel Slave Port.


Organisation of  PIC Microcontroller
The Figure above shows the pin out details of a very popular 40-pin PIC microcontroller, PIC16F87X, as you can see that each pin has been assigned a number of functions. Sometimes two and sometimes three. This situation is very common in microcontrollers, as there is always more which your microcontroller can offer, yet the number of pins on a given package is limited. In a given application a pin is usually tied to a specific job, and all functionality of a pin is usually not required, however you can use the specific pin your own way. The specific function of a pin is selected by configuring various bits of internal registers. The number and names of these special function registers (SFRs) vary from device to device as some devices have limited functionality while others have more. The selection and settings of these SFR’s is the key to successful programming. It is of best interest to go through the data sheets of the device before starting programming.

Features


High-Performance RISC CPU

Ø   Lead-free; RoHS-compliant
Ø   Operating speed: 20 MHz, 200 ns instruction cycle
Ø   Operating voltage: 4.0-5.5V
Ø   Industrial temperature range (-40° to +85°C)
Ø   15 Interrupt Sources
Ø   35 single-word instructions

Ø   All single-cycle instructions except for program branches (two-cycle)

Special Microcontroller Features

Ø   Flash Memory: 14.3 Kbytes (8192 words)
Ø   Data SRAM: 368 bytes
Ø   Data EEPROM: 256 bytes
Ø   Self-reprogrammable under software control
Ø   In-Circuit Serial Programming via two pins (5V)
Ø   Watchdog Timer with on-chip RC oscillator
Ø   Programmable code protection
Ø   Power-saving Sleep mode
Ø   Selectable oscillator options
Ø   In-Circuit Debug via two pins

Peripheral Features

Ø   33 I/O pins; 5 I/O ports
Ø   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
Ø   16-bit Capture input; max resolution 12.5 ns
Ø   16-bit Compare; max resolution 200 ns
Ø   10-bit PWM
Ø   Synchronous Serial Port with two modes:
Ø   SPI Master
Ø   I2C Master and Slave
Ø   USART/SCI with 9-bit address detection
Ø   Parallel Slave Port (PSP)
Ø   8 bits wide with external RD, WR and CS controls
Ø   Brown-out detection circuitry for Brown-Out Reset
Ø   Analog Features
Ø   10-bit, 8-channel A/D Converter
Ø   Brown-Out Reset

PROGRAM MEMORY (FLASH): This is used for storing a written program. Since memory made in FLASH technology can be programmed and cleared more than once, it makes this microcontroller suitable for device development. 

EEPROM - This is a non-volatile data memory that are used in computer and other computerized system and which needs to be saved when there is no power supply. It is usually used for storing important data that must not be lost if power supply suddenly stops. For instance, one such data is an assigned temperature in temperature regulators. If during a loss of power supply this data was lost, we would have to make the adjustment once again upon return of supply. Thus our device looses on self-reliance. 


RAM- Data memory used by program during its execution. RAM stores all inter results or     temporary data during run-time. Ports are physical connections between the microcontroller and the outside world. PIC16F87X has five I/O ports and 33 pins in all 5 ports.


FREE-RUNTIMER  is an 8-bit register inside a microcontroller that works independently of the program. On every fourth clock of the oscillator it increments its value until it reaches the maximum (255), and then it starts counting over again from zero. As we know the exact timing between each two increments of the timer contents, timer can be used for measuring time which is very useful with some devices.

CENTRAL PROCESSING UNIT has a role of connective element between other blocks in the microcontroller. It coordinates the work of other blocks and executes the user program.

CISC, RISC
This term is related to computerised devices, and it needs to be explained here in more detail. Harvard architecture is a newer concept than von-Neumann's. It rose out of the need to speed up the work of a microcontroller. In Harvard architecture, data bus and address bus are separate. Thus a greater flow of data is possible through the central processing unit, and of course, a greater speed of work. Separating a program from data memory makes it further possible for instructions not to have to be 8-bit words. PIC16F87X uses 14 bits for instructions which allows for all instructions to be one word instructions. It is also typical for Harvard architecture to have fewer instructions than von-Neumann's, and to have instructions usually executed in one cycle. Microcontrollers with Harvard architecture are also called "RISC microcontrollers". RISC stands for Reduced Instruction Set Computer. Microcontrollers with von-Neumann's architecture are called 'CISC microcontrollers'. Title CISC stands for Complex Instruction Set Computer. Since PIC16F87X is a RISC microcontroller, that means that it has a reduced set of instructions, more precisely 35 instructions. (Ex. Intel's and Motorola's microcontrollers have over hundred instructions) All of these instructions are executed in one cycle except for jump and branch instructions. According to what its maker says, PIC16F87X usually reaches results of 2:1 in code compression and 4:1 in speed in relation to other 8-bit microcontrollers in its class.

REGISTERS
Special Function Registers
The Special Function Registers are registers used by the CPU and peripheral modules for controlling the desired operation of the device. These registers are implemented as static RAM. The Special Function Registers can be classified into two sets: core (CPU) and peripheral.

Status Registers
These registers contains the arithmetic status of the ALU, the RESET status and the bank select bits for data memory.The STATUS register can be the destination for any instruction, as with any other register. 

 USART
The Universal Synchronous Asynchronous Receiver Transmitter (USART) module is one of the two serial I/O modules. (USART is also known as a Serial Communications Interface or SCI.) The USART can be configured as a full duplex asynchronous system that can communicate with peripheral devices such as CRT terminals and personal computers, or it can be configured as a half duplex synchronous system that can communicate with peripheral devices such as A/D or 40 D/A integrated circuits, serial EEPROMs etc. The USART can be configured in the following modes:
• Asynchronous (full duplex)
• Synchronous - Master (half duplex)
• Synchronous - Slave (half duplex)

ANALOG-TO-DIGITAL CONVERTER (A/D) MODULE
The Analog-to-Digital (A/D) Converter module has five inputs for the 28-pin devices and eight for the other devices. The A/D conversion of the analog input signal results in a corresponding 10-bit digital number. The A/D converter has a unique feature of being able to operate while the device is in SLEEP mode. To operate in SLEEP, the A/D clock must be derived from the A/D‟s internal RC oscillator. The A/D module has four registers. These registers are:
• A/D Result High Register (ADRESH)
• A/D Result Low Register (ADRESL)
• A/D Control Register0 (ADCON0)
• A/D Control Register1 (ADCON1)

INTERRUPTS

The PIC16F87X family has up to 14 sources of interrupt. The interrupt control register (INTCON) records individual interrupt requests in flag bits. It also has individual and global interrupt enable bits. A global interrupt enable bit, GIE (INTCON<7>) enables (if set) all unmasked interrupts, or disables (if cleared) all interrupts. When bit GIE is enabled, and an interrupt‟s flag bit and mask bit are set, the interrupt will vector immediately. Individual interrupts can be disabled through their corresponding enable bits in various registers. Individual interrupt bits are set, regardless of the status of the GIE bit.

INSTRUCTION 

Each PIC16F87X instruction is a 14-bit word, divided into an OPCODE which specifies the instruction type and one or more operands which further specify the operation of the instruction. The PIC16F87X instruction set summary in byte-oriented, bit-oriented, and literal and control operations.
All instructions are executed within one single instruction cycle, unless a conditional test is true or the program counter is changed as a result of an instruction. In this case, the execution takes two instruction cycles with the second cycle executed as a NOP. One instruction cycle consists of four oscillator periods. Thus, for an oscillator frequency of 4 MHz, the normal instruction execution time is 1 μs. If a conditional test is true, or the program counter is changed as a result of an instruction, the instruction execution time is 2μs.

APPLICATIONS
PIC16F87X find it's place in most simple and complex microcontroller projects, some of these can be LCD, Access control and surveillance, Home automation, Digital display, Robotic and industrial instrumentation control projects.


1 comment:

  1. This mcu can do so much. I am currently learning mcu programming in PIC BASIC PRO language and I need a programmer PIC KIT 2 to write my codes into the mcu. Where can I get a suitable one?

    ReplyDelete