You are on page 1of 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

‘Interfacing’. .An Overview  The Aims: ◦ Basics of Microprocessor-based systems ◦ Programmer’s view of Computer Architecture ◦ Interaction between hardware and software. i.e.

PIC MICROCONTROLLER .

 The steady and progressive research in this field gave the industry more efficient.PIC microcontrollers The microcontrollers played revolutionary role in embedded industry after the invention of Intel 8051.  . 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).

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

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

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

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

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

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

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

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. in industries.  are smallest and cheapest PICs.   The small size and low cost of Base Line PIC replaced the traditional ICs like 555. logic gates etc. available with 6 to 40 pin packaging. .

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

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

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. .

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

Market Share .

Units No.Microcontroller Market Share . 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 .

Re gulators .Supe rv isory PICmicro Microcontroller LCD Drivers Serial NV Memory Interface Products Digital Peripherals SRAM LED Drivers .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 .

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 .

having separate memories for programs and data  . 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.Harvard vs.

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 can’t 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 typical erase/write cycles EEPROM  Data memory typical  EEPROM Data Retention > 40 years  In-Circuit Serial Programming™ (ICSP™) via two pins  .000 erase/write cycles Enhanced FLASH  Program memory typical  10.000.

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 .

High speed  Fully static design  Wide operating voltage range:   2.0 V to 6. 4MHz  <1uA typical standby current at 2A .CMOS/Flash EEPROM Tech.0 V to 6. Low power.0 V – Commercial  2.0 V – Industrial  Low Power Consumption  <2mA typical @ 5V.

0 V – Industrial  Low Power Consumption  <2mA typical @ 5V.CMOS/Flash EEPROM Tech.    Low power.0 V to 6.0 V – Commercial  2. 4MHz  <1uA typical standby current at 2A .0 V to 6. High speed Fully static design Wide operating voltage range:  2.

PIC 16x84x Pin-outs .

PIC 16x84x Pin-outs .

Basic Circuit .

PIC 16F84 Pinout Descrition .

. These are the program memory and the data memory. For the PIC16F84A. The RESET vector is at 0000h and the interrupt vector is at 0004h. the first 1K x 14 (0000h03FFh) are physically implemented (Figure 2-1).MEMORY ORGANIZATION There are two memory blocks in the PIC16F84A. Each block has its own bus. Program Memory Organization The PIC16FXX has a 13-bit program counter capable of addressing an 8K x 14 program memory space. so that access to each block can occur during the same oscillator cycle.

) Data Memory Organization The data memory is partitioned into two areas. . 2) General Purpose Registers (GPR) area. The SFRs control the operation of the device. is 8-bits wide and is accessed either directly or indirectly through the FSR.MEMORY ORGANIZATION (Cont. 1) Special Function Registers (SFR) area.

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

PORTA and TRISA Registers .

The corresponding data direction register is TRISB. bi-directional port. . Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input.) PORTB and TRISB Registers PORTB is an 8-bit wide.I/O PORTS (Cont. 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  .

XT. HS modes .LP.

Crystal operation Capacitor selection for crystal operation: .

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

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 .

 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.Reset circuit Reset is used to put the microcontroller into a known state.  .

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 .

) LITERAL AND CONTROL OPERATIONS .Instruction Set Descriptions (Cont.

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS .

Instruction Set Descriptions (Cont.) LITERAL AND CONTROL OPERATIONS .

) 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.

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

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

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

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

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

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

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

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

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

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

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

Exercise 1 BASIC OUTPUT OPERATION .

Diagram 1: .

Delay_ms(100). } } . PORTB = 0. while(1) { PORTB = 0xff. PORTB = 0x00. Delay_ms(100).Source Code 1-a: void main() { TRISB = 0.

while(1) { PORTB = 0x00. } } .Source Code 1-b: (Same Diagram) void main() { TRISB = 0. PORTB = 0.PORTB++) Delay_ms(100). for(.PORTB<=255. Delay_ms(100).

Activity # 1: .

Activity # 2: .

Exercise II BASIC INPUT OPERATION .

Diagram 2-a: .

while(1) { if(PORTA==0x01) { } else if(PORTA==0x02) { } else if(PORTA==0x04) { } PORTB = 0xf0. Delay_ms(100). Delay_ms(100). } PORTB = 0xff. } . Delay_ms(100). } PORTB = 0x0f. TRISA = 0x1f. Delay_ms(100). else if(PORTA==0x08) { PORTB = 0x99. } else { PORTB = 0x00.Source Code 2-a: void main() { TRISB = 0. PORTB = 0. Delay_ms(100). } else if(PORTA==0x10) { PORTB = 0x66. 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: .

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

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". column } // Clear display // Turn cursor off // Print text to LCD. Lcd_Out(1. 1. 2nd row. PORTB // PORTB is output // Initialize LCD connected to Lcd_Cmd(Lcd_CLEAR). Lcd_Init(&PORTB). 1st . Lcd_Cmd(Lcd_CURSOR_OFF). text). void main() { TRISB = 0.

Lcd_Cmd(Lcd_CLEAR). // Clear display Lcd_Out(2. 4. Delay_ms(500). // Initialize LCD connected to PORTB // Clear display Lcd_Cmd(Lcd_CURSOR_OFF). Lcd_Out(3. TRISB = 0x00. . // PORTB is output Lcd_Init(&PORTB). void main() { TRISA = 0x1f. Lcd_Cmd(Lcd_CLEAR). "kEYPRESSED: ").Source Code 4-b: char *text = "PIC TRAINING". 8. text). // Turn cursor off Lcd_Out(2. 5. "******").

while(1) { if(PORTA==0x10) { Lcd_Chr(2. Delay_ms(50). } } //END OF WHILE //0 //# //* } . } else if(PORTA==0x19) //7 //8 { Lcd_Chr(2. } else if(PORTA==0x1A) { Lcd_Chr(2.'1').'0').'*'). } else if(PORTA==0x12) { Lcd_Chr(2. } else if(PORTA==0x14) { Lcd_Chr(2.'7'). Delay_ms(50).16.16.'2').'4'). Delay_ms(50).16.'#').16. } //6 else if(PORTA==0x18) { Lcd_Chr(2. Delay_ms(50).'8'). Delay_ms(50). Delay_ms(50).'5'). Delay_ms(50). } else if(PORTA==0x11) { Lcd_Chr(2. Delay_ms(50).16.16.'3'). } //5 //4 //3 //2 //1 else if(PORTA==0x16) { Lcd_Chr(2. Delay_ms(50).'6').16.16. } else if(PORTA==0x1d) { Lcd_Chr(2.16. } else if(PORTA==0x1E) { Lcd_Chr(2. Delay_ms(50).16. Delay_ms(50).'9'). Delay_ms(50).16. } else if(PORTA==0x15) { Lcd_Chr(2.16. //9 } else if(PORTA==0x1C) { Lcd_Chr(2.