Está en la página 1de 113

Embedded Designing & Programming with Microcontrollers (16F84A)

Roland Fernandez

What is this course about?


Application (Netscape) Operating Compiler System Assembler (Windows XP)
Processor Memory I/O system Datapath & Control Digital Design Circuit Design
transistors

Software Hardware

Instruction Set Architecture

Coordination of many levels of abstraction

Programming Levels of Representation


temp = v[k]; High Level Language Program (e.g., C) Compiler Assembly Language Program (e.g. ARM) Assembler Machine Language Program (ARM) Machine Interpretation Control Signal Specification
1110 1110 1110 1110

v[k] = v[k+1];

v[k+1] = temp;

ldr ldr str str


0101 0101 0101 0101

r0 , [r2, #0] r1 , [r2, #4] r1 , [r2, #0] r0 , [r2, #4]


0010 0010 0010 0010 0000 0000 0001 0001 0000 0000 0000 0000 0000 0000 0000 0000 0000 0100 0000 0100

1001 1001 1000 1000

ALUOP[0:3] <= InstReg[9:11] & MASK

An Overview

The Aims:
Basics of Microprocessor-based systems Programmers view of Computer Architecture Interaction between hardware and software, i.e. Interfacing.

PIC MICROCONTROLLER

PIC microcontrollers
The microcontrollers played revolutionary role in embedded industry after the invention of Intel 8051. The steady and progressive research in this field gave the industry more efficient, high-performance and lowpower consumption microcontrollers.

PIC microcontrollers

PIC stands for Peripheral Interface Controller. The General Instruments used the acronyms Programmable Interface Controller and Programmable Intelligent Computer for the initial PICs (PIC1640 and PIC1650).

PIC microcontrollers

The AVR, PIC and ARM are the prime examples. The new age microcontrollers are getting smarter and richer by including latest communication protocols like USB, I2C, SPI, Ethernet, CAN etc.

History
The PIC microcontroller was developed by General Instruments in 1975. PIC was developed when Microelectronics Division of General Instruments was testing its 16-bit CPU CP1600. Although the CP1600 was a good CPU but it had low I/O performance.

History
The PIC controller was used to offload the I/O the tasks from CPU to improve the overall performance of the system. In 1985, General Instruments converted their Microelectronics Division to Microchip Technology.

History

In 1993, Microchip Technology launched the 8-bit PIC16C84 with EEPROM which could be programmed using serial programming method. The improved version of PIC16C84 with flash memory (PIC18F84 and PIC18F84A) hit the market in 1998.

Development:
Since 1998, Microchip Technology continuously developed new high performance microcontrollers with new complex architecture and enhanced in-built peripherals. At present PIC microcontrollers are widely used for industrial purpose due to its high performance ability at low power consumption.

Development:

It is also very famous among hobbyists due to moderate cost and easy availability of its supporting software and hardware tools like compilers, simulators, debuggers etc.

Categories of 8 bit PIC


The 8-bit PIC microcontroller is divided into following four categories on the basis of internal architecture: 1. Base Line PIC 2. Mid-Range PIC 3. Enhanced Mid-Range PIC 4. PIC18

Base Line PIC


are the least complex PIC microcontrollers. works on 12-bit instruction architecture which means that the word size of instruction sets are of 12 bits for these controllers. are smallest and cheapest PICs, available with 6 to 40 pin packaging.

The small size and low cost of Base Line PIC replaced the traditional ICs like 555, logic gates etc. in industries.

Mid-Range PIC
are based on 14-bit instruction architecture and are able to work up to 20 MHz speed. These controllers are available with 8 to 64 pin packaging. These microcontrollers are available with different peripherals like ADC, PWM, Op-Amps and different communication protocols like USART, SPI, I2C (TWI), etc.

Enhanced Mid-Range PIC


These controllers are enhanced version of Mid-Range core. This range of controllers provides additional performance, greater flash memory and high speed at very low power consumption. This range of PIC also includes multiple peripherals and supports protocols like USART, SPI, I2C and so on.

PIC18

PIC18 range is based on 16-bit instruction architecture incorporating advanced RISC architecture which makes it highest performer among the all 8-bit PIC families.

PIC18
The PIC18 range is integrated with new age communication protocols like USB, CAN, LIN, Ethernet (TCP/IP protocol) to communicate with local and/or internet based networks. This range also supports the connectivity of Human Interface Devices like touch panels etc.

Market Share

Microcontroller Market Share - Units


No.
1 2 3 4 5 6 7 8 9

1990 Rank
Motorola Mitsubishi NEC Intel Hitachi Philips Matsushita National Siemens

1991 Rank
Motorola Mitsubishi NEC Intel Hitachi Philips Matsushita
SGS-Thomson

1992 Rank
Motorola Mitsubishi Intel NEC Philips Hitachi Matsushita
SGS-Thomson

1993 Rank
Motorola Mitsubishi NEC Hitachi Philips Intel SGSThomson

1994 Rank
Motorola Mitsubishi NEC Philips Intel

1995-96 Rank
Motorola Mitsubishi SGS-Thomson NEC

97-PRESENT
Motorola

Microchip
ST-Micro NEC Mitsubishi Philips Siemens Toshiba Hitachi

Microchip
Philips Zilog Hitachi Fujitsu

Microchip
Zilog
SGS-Thomson

Microchip
Matsushita

Siemens

National

Matsushita

10
11 12 13 14 15 16 17 18

TI
Sharp Oki Toshiba
SGS-Thomson

TI
National Toshiba Sony Sharp Oki Zilog Microchip
Matra MHS

TI
Zilog Toshiba Siemens Microchip Sharp Sanyo
Matra MHS

Toshiba
National Zilog TI Siemens Sharp Oki Sony Sanyo

Hitachi
Toshiba National TI Ricoh Fujitsu Siemens Sharp Oki

Intel
Siemens Toshiba Matsushita TI National Temic Sanyo Ricoh

Zilog
Fujitsu Atmel National Sanyo Matsushita TI Sony Intel

Zilog
Matra MHS

Sony Fujitsu

Sony

19

AMD

Fujitsu

Oki

Fujitsu

Sony

Oki

LG Semicon

PIC MCU Product Migration Path

The PIC Microcontroller

Microchip Technology Inc.


The Embedded Control Solutions Company
Precision Voltage Reference High Voltage I/O's

Amplifiers

Filters

A/D

AC Power Battery

Powe r M anage me nt - Re gulators - Supe rv isory

PICmicro Microcontroller

LCD Drivers Serial NV Memory Interface Products Digital Peripherals

SRAM

LED Drivers

Complete Compatibility

Microcontroller vs Microprocessor

Microcontroller has the Memory and IO onchip while as opposed the microprocessor.
Microcontrollers are microprocessors with an onchip Memory and IO

Microprocessor are intended for high performance applications where COST and SIZE is not the most critical selection criteria Microcontrollers on the other hand are designed to minimize chip count and cost with application specialization at the expense of flexibility

Microcontroller vs Microprocessor

Microprocessors are the most flexible Microcontrollers are the most compact

Harvard vs. Von Neumann


Microprocessors are usually Von Neumann machines with a single memory for program and data to allow maximum flexibility for allocation of memory Microcontrollers are mostly Harvard, having separate memories for programs and data

PICmicro Architecture
Harvard Architecture
Von Neumann

CPU

8-Bits Harvard Program and Data Memory

Fetches instructions and data from one memory. Limits Operating Bandwidth

CPU

Two separate memory spaces for instructions and data


8-Bits

Increases throughput
Different program and data bus widths are possible

12/14/16 Bits Program Memory Data Memory

Advantages
Fewer chips are required since most functions are already present on the processor chip Lower cost and simpler size result from a simpler design Overall reliability is higher, since there are fewer components and interconnections

Advantages
Lower

power requirements because on-chip power requirements are smaller than external loads More pins are available for user IO (no pin is used for the bus) Fewer external connections are required

Disadvantages
Reduced flexibility, since you cant easily change the functions designed into the chip Expansion of memory or IO is limited or impossible Limited data transfer rates due to practical size and limits for a single chip Lower performance IO because of design compromises IO to fit everything in one chip

Bus Oriented Microcontrollers


Real World
Peripheral Devices

CPU

Memory

IO

Others

uP Functions
Microcontroller Functions

APPLICATIONS

HUNDREDS OF APPLICATIONS
CONSUMER
TV/VCR EQUIPMENT STEREO RECEIVER

AUTOMOTIVE
AUTO SECURITY SYSTEM KEYLESS ENTRY

OFFICE AUTOMATION
COMPUTER MOUSE LAPTOP TRACKBALL

TELECOM
CELLULAR PHONE CORDLESS TELEPHONE

INDUSTRIAL
MOTOR CONTROL THERMOSTAT

CD PLAYER
REMOTE CONTROLS CABLE TV CONVERTER VIDEO GAMES CAMERA GARAGE OPENER MICROWAVE OVEN WASHER/DRYER KITCHEN APPLIANCES

RADAR DETECTOR
CRUISE CONTROL ANTI-LOCK BRAKING SPEEDOMETER CLIMATE CONTROL TURN SIGNALS ACTIVE SUSPENSION FUEL PUMP CONTROL FUEL INJECTION

HANHELD SCANNER
COMPUTER KEYBOARD LASER PRINTER INTERFACE BOARD PC LAN SYSTEM X/Y PLOTTER COPIER BAR CODE READER DISK DRIVE TAPE BACK-UP UNIT

ANSWERING MACHINE
PAY PHONE PAGER MODEM CREDIT CARD VERIFICATION

SECURITY SYSTEM
POSTAGE METRE UTILITY METER ROBOTICS PROCESS CONTROL GAS PUMP SMOKE DETECTOR CARBON MONOXIDE DETECTOR X/Y TABLE

CORDLESS TOOLS
VACCUM CLEANER ELECTRIC BLANKET

SUN ROOF CONTROL


AIR BAG SENSOR POWER SEATS

SERIAL BUS
FACSIMILE MACHINE

PIC16F84
SCOREBOARDS QUEUING SYSTEM

MOVING SIGNS

TIMER

INDUSTRIAL CONTROLLER

PIC16F84
TIMER

MOVING SIGNS

TACHOMETER

PIC12C508

PAROL SEQUENCER

PEDDLERS

PNEUMATIC VALVES FOR PNEUMATIC ACTUATORS

PIC12C671
REF-MATE

TRAFFIC LIGHT

LIGHT DIMMER

Automotive Applications

PIC 16F84A

The PIC 16F84 Microcontroller


The PIC16C84 and 16F84 are EEPROM devices, which means that they can be rapidly reprogrammed as often as you wish, without the need for ultra-violet light The 16F84 contains a Flash ROM

The PIC 16F84 Features


High performance RISC CPU Features: Only 35 single word instructions to learn All instructions single cycle except for program branches which are two-cycle Operating speed: DC = 10 Mhz, 400ns Ins. Cycle 14-bit wide instructions 8-bit wide data path

The PIC 16F84 Features


15 Special Function Hardware registers Eight-level deep hardware stack Direct, indirect and relative addressing modes Four interrupt sources:
External RB0/INT pin TMR0 timer overflow PORTB<7:4> interrupt-on-change Data EEPROM write complete

Peripheral Features
13 I/O pins with individual direction control High current sink source for LED drive 8-bit counter with 8-bit programmable prescaler

Special Features
10,000 erase/write cycles Enhanced FLASH Program memory typical 10,000,000 typical erase/write cycles EEPROM Data memory typical EEPROM Data Retention > 40 years In-Circuit Serial Programming (ICSP) via two pins

Special Features

Power-on Reset (POR), Power-up Timer (PWRT), Oscillator Start-up Timer (OST) Watchdog Timer (WDT) with its own OnChip RC Oscillator for reliable operation Code protection Power saving SLEEP mode Selectable oscillator options

CMOS/Flash EEPROM Tech.


Low power, High speed Fully static design Wide operating voltage range:

2.0 V to 6.0 V Commercial 2.0 V to 6.0 V Industrial

Low Power Consumption


<2mA typical @ 5V, 4MHz <1uA typical standby current at 2A

CMOS/Flash EEPROM Tech.

Low power, High speed Fully static design Wide operating voltage range:
2.0 V to 6.0 V Commercial 2.0 V to 6.0 V Industrial

Low Power Consumption


<2mA typical @ 5V, 4MHz <1uA typical standby current at 2A

PIC 16x84x Pin-outs

PIC 16x84x Pin-outs

Basic Circuit

PIC 16F84 Pinout Descrition

MEMORY ORGANIZATION
There are two memory blocks in the PIC16F84A. These are the program memory and the data memory. Each block has its own bus, so that access to each block can occur during the same oscillator cycle.

Program Memory Organization The PIC16FXX has a 13-bit program counter capable of addressing an 8K x 14 program memory space. For the PIC16F84A, the first 1K x 14 (0000h03FFh) are physically implemented (Figure 2-1).

The RESET vector is at 0000h and the interrupt vector is at 0004h.

MEMORY ORGANIZATION (Cont.)


Data Memory Organization The data memory is partitioned into two areas.

1) Special Function Registers (SFR) area. The SFRs control the operation of the device.

2) General Purpose Registers (GPR) area. is 8-bits wide and is accessed either directly or indirectly through the FSR.

I/O PORTS
Are external registers that sre used to communicate to external devices. Some pins for these I/O ports are multiplexed with an alternate function for the peripheral features on the device. In general, when a peripheral is enabled, that pin may not be used as a general purpose I/O pin. PORTA and TRISA Registers
PORTA is a 5-bit wide, bi-directional port. The corresponding data direction register is TRISA. Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input. Clearing a TRISA bit (= 0) will make the corresponding PORTA pin an output.

PORTA and TRISA Registers

I/O PORTS (Cont.)


PORTB and TRISB Registers PORTB is an 8-bit wide, bi-directional port. The corresponding data direction register is TRISB. Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input. Clearing a TRISB bit (= 0) will make the corresponding PORTB pin an output.

PORTB and TRISB Registers

PIC oscillator circuits


LP Low-power crystal XT Crystal/resonator HS High-speed crystal/resonator RC resistor capacitor

LP, XT, HS modes

Crystal operation
Capacitor selection for crystal operation:

Resonator operation
Resonators are available from 4 to about 8 MHz. They are not as accurate as crystal-based oscillators. Resonators are usually 3-pin devices and the two pins at either sides are connected to OSC1 and OSC2 inputs of the microcontroller. The middle pin is connected to the ground.

RC oscillator

For applications where the timing accuracy is not important we can connect an external resistor and a capacitor to the OSC1 input of the microcontroller

Reset circuit
Reset is used to put the microcontroller into a known state. Normally when a PIC microcontroller is reset execution starts from address 0 of the program memory. This is where the first executable user program resides.

PIC microcontrollers can be reset when one of the ff conditions occur:


Reset during power on (Power On Reset) Reset by lowering MCLR input to logic 0 Reset when the watchdog overflows.

power on reset Using an external reset button

Assembly Language Instructions

Instruction Set Descriptions


LITERAL AND CONTROL OPERATIONS

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BYTE - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BIT - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BIT - ORIENTED FILE REGISTER OPERATIONS

Instruction Set Descriptions (Cont.) BIT - ORIENTED FILE REGISTER OPERATIONS

Exercise 1

BASIC OUTPUT OPERATION

Diagram 1:

Source Code 1-a:


void main() { TRISB = 0; PORTB = 0; while(1) { PORTB = 0xff; Delay_ms(100); PORTB = 0x00; Delay_ms(100); } }

Source Code 1-b: (Same Diagram)


void main() { TRISB = 0; PORTB = 0; while(1) { PORTB = 0x00; Delay_ms(100); for(;PORTB<=255;PORTB++) Delay_ms(100); } }

Activity # 1:

Activity # 2:

Exercise II

BASIC INPUT OPERATION

Diagram 2-a:

Source Code 2-a:


void main() { TRISB = 0; TRISA = 0x1f; PORTB = 0; while(1) { if(PORTA==0x01) { } else if(PORTA==0x02) { } else if(PORTA==0x04) { } PORTB = 0xf0; Delay_ms(100); } PORTB = 0x0f; Delay_ms(100); } PORTB = 0xff; Delay_ms(100); } else if(PORTA==0x10) { PORTB = 0x66; Delay_ms(100); else if(PORTA==0x08) { PORTB = 0x99;

Delay_ms(100);

}
else { PORTB = 0x00; Delay_ms(100);

Diagram 2-b:

Source Code 2-b:


unsigned int counter1=0; unsigned int counter2=0; void main() { TRISB = 0; TRISA = 0x1f; while(1) { if(PORTA.F0==0 || PORTA.F1==0) { while(PORTA.F0==0){} counter1++; } if(counter1<=9) PORTB = (counter2*16)+counter1; } } else { counter1=0; counter2++; PORTB = (counter2*16)+counter1; } if(counter1==0 && counter2==10) { counter2=0; PORTB = (counter2*16)+counter1; } Delay_ms(100);

Exercise III

KEYPAD OPERATION

Diagram 3:

Keypad key equivalent:

Source Code 3:
void main() { TRISB = 0; TRISA = 0x1f; PORTB = 0; while(1) { if(PORTA==0x10) PORTB = 0x06; else if(PORTA==0x11) PORTB = 0x5b; else if(PORTA==0x12) PORTB = 0x4f; else if(PORTA==0x14) PORTB = 0x66; else if(PORTA==0x15) PORTB = 0x6d; //5 //4 //3 } } //2 //1 else if(PORTA==0x16) PORTB = 0x7d; else if(PORTA==0x18) PORTB = 0x07; else if(PORTA==0x19) PORTB = 0x7f; else if(PORTA==0x1A) PORTB = 0x6f; else if(PORTA==0x1C) PORTB = 0x77; else if(PORTA==0x1E) PORTB = 0x58; else if(PORTA==0x1d) PORTB = 0x3f; //6 //7 //8 //9 //* //#

MM74C922 / MM74C923
16-Key Encoder / 20-Key Encoder

MM74C922 / MM74C923
16-Key Encoder / 20-Key Encoder

Connection Diagrams

Block Diagram

Exercise IV

LCD OPERATION

LCD

LCD Routines
Lcd_Config Lcd_Init Lcd_Out Lcd_Out_Cp Lcd_Chr Lcd_Chr_Cp Lcd_Cmd

Lcd_Cmd Options

Diagram 4:

Source Code 4-a:


char *text = "mikroElektronika";

void main() {
TRISB = 0; Lcd_Init(&PORTB); PORTB // PORTB is output // Initialize LCD connected to

Lcd_Cmd(Lcd_CLEAR);
Lcd_Cmd(Lcd_CURSOR_OFF); Lcd_Out(1, 1, text); column }

// Clear display
// Turn cursor off // Print text to LCD, 2nd row, 1st

Source Code 4-b:


char *text = "PIC TRAINING"; void main() { TRISA = 0x1f; TRISB = 0x00; // PORTB is output

Lcd_Init(&PORTB);
Lcd_Cmd(Lcd_CLEAR);

// Initialize LCD connected to PORTB


// Clear display

Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off Lcd_Out(2, 5, text); Lcd_Out(3, 8, "******"); Delay_ms(500); Lcd_Cmd(Lcd_CLEAR); // Clear display Lcd_Out(2, 4, "kEYPRESSED: ");

while(1) { if(PORTA==0x10) { Lcd_Chr(2,16,'1'); Delay_ms(50); } else if(PORTA==0x11) { Lcd_Chr(2,16,'2'); Delay_ms(50); } else if(PORTA==0x12) { Lcd_Chr(2,16,'3'); Delay_ms(50); } else if(PORTA==0x14) { Lcd_Chr(2,16,'4'); Delay_ms(50); } else if(PORTA==0x15) { Lcd_Chr(2,16,'5'); Delay_ms(50); } //5 //4 //3 //2 //1

else if(PORTA==0x16) { Lcd_Chr(2,16,'6'); Delay_ms(50); }

//6

else if(PORTA==0x18) { Lcd_Chr(2,16,'7'); Delay_ms(50); } else if(PORTA==0x19)

//7

//8

{
Lcd_Chr(2,16,'8'); Delay_ms(50); } else if(PORTA==0x1A) { Lcd_Chr(2,16,'9'); Delay_ms(50); //9

}
else if(PORTA==0x1C) { Lcd_Chr(2,16,'*'); Delay_ms(50); } else if(PORTA==0x1E) { Lcd_Chr(2,16,'#'); Delay_ms(50); } else if(PORTA==0x1d) { Lcd_Chr(2,16,'0'); Delay_ms(50); } } //END OF WHILE //0 //# //*

También podría gustarte