PIC16F877A
MICROPROCESSOR
CPU
RAM
ROM
ADC
• Multiple chip need
external RAM,
ROM, ADC,etc
• High cost for
simple task
• Easy to upgrade
• Application:
- Desktop
- Laptop
• Intel Pentium
• Motorola 68K
• Raspberry Pi
MICROCONTROLLER
RAM
CPU
ROM
TIMER
ADC
Etc..
• Single chip intergrated with
RAM, ROM, ADC, etc.
• Low cost for simple task
• Difficult to upgrade
• Application:
- Control Unit
- Roboticx
• Microchip PIC
• Atmel
• Arduino
Why Microchip PIC is popular?
Peripheral Interface Controller (PIC) popular due to:
• Low cost
• Wide availability
• Large user base
• Extensive collection of application notes
• Availability of low cost or free development tools
• Serial programming capability
MICROCHIP FAMILY
1.Baseline core devices
-PIC10, PIC12 and PIC16
2.Mid-range core devices
-PIC12 and PIC16
3.High End core devices
-PIC24 and dsPIC
Microchip 16F877/877A
*The pin having notation R can be used as digital I/O
*The pin that are labeled as AN can be used as analog inputs
SPECIFICATION
1. CPU 8-bit PIC
2. Operating Voltage (V) - 2 to 5.5 V
3. Number of I/O pins – 33
4. ADC Module - 8ch, 10-bit
5. Timer Module - 8-bit(2), 16-bit(1)
6. External Oscillator - Up to 20Mhz
7. Data EEPROM - 256 bytes
Elements of a digital controller
The microcontroller contains all these elements in one chip
PIC 16F877 MCU Block diagram
Flash
ROM
Program
Memory
8192
x 14 bits
0000 – 1FFF
Instruction Register
MCU
control
lines
Program Counter
(13 bits)
Stack
13 bits
x 8 levels
RAM
File
Registers
368
X 8 bits
000-1FF
Data Bus
(8 bits)
File Select
Register
Working (W)
Register
File Address
Literal
Op-
code
Instructions
Address
Arithmetic & Logic
Unit
Ports, Timers
ADC, Serial I/O
Status bits
Timing control
EEPROM
256 bytes
Clock Reset
Port A B C D E
Program address
Instruction
Decode &
CPU control
Status (Flag)
Register
Shows the main parts of the chip in simplified form
Bank 0 (000 – 07F) Bank 1 (080 – 0FF) Bank 2 (100-180) Bank 3(180-1FF)
Address Register Address Register Address Register Address Register
000h Indirect 080h Indirect 100h Indirect 180h Indirect
001h Timer0 081h Option 101h Timer0 181h Option
002h PC Low 082h PC Low 102h PC Low 182h PC Low
003h Status Reg 083h Status Reg 103h Status Reg 183h Status Reg
004h File Select 084h File Select 104h File Select 184h File Select
005h Port A data 085h PortA direction 105h - 185h -
006h Port B data 086h PortB direction 106h Port B data 186h PortB direction
007h Port C data 087h PortC direction 107h - 187h -
008h Port D data 088h PortD direction 108h - 188h -
009h Port E data 089h PortE direction 109h - 189h -
00Ah PC High 08Ah PC High 10Ah PC High 18Ah PC High
00Bh Interrupt Control 08Bh Interrupt Control 10Bh Interrupt Control 18Bh Interrupt Control
00Ch
to
01Fh
20 Peripheral
Control
Registers
08Ch
to
09Fh
20 Peripheral
Control
Registers
10Ch
to
10Fh
4 Peripheral
Control
Registers
18Ch
to
18Fh
4 Peripheral
Control
Registers
110h
to
16Fh
96 General
Purpose
Registers
190h
to
1EFh
96 General
Purpose
Registers
020h
to
06Fh
80 General
Purpose
Registers
0A0h
to
0EFh
80 General
Purpose
Registers
070h
to
07Fh
16 Common
Access GPRs
0F0h
to
0FFh
Accesses
70h – 7Fh
170h
to
17Fh
Accesses
70h – 7Fh
1F0h
to
1FFh
Accesses
70h – 7Fh
PIC16F877 simplified
file register map
PIC microcontroller types
MCU Pins
Data
word
(bits)
Program
memory
(bytes)
Typical
Instruction
Set
Speed
MIPS Comment
10FXXX
= 6 8 <= 512 33 x 12 bits <= 2
Low pin count, small form factor, cheap
No EEPROM, none low power,
assembler program
12FXXX
= 8 8 <= 2 KB 12 / 14 bits <= 5
Low pin count, small form factor, cheap
EEPROM, 10-bit ADC, some low power,
assembler
16FXXX
<= 64 8 <= 14 KB 35 x 14 bits <= 5
Mid-range, UART, I2C, SPI
many low power, C or assembler
program
18FXXXX
<= 100 8 <= 128 KB 75 x 16 bits <= 16 High range, CAN, USB
J series 3V supply, C program
24FXXXX
<= 100 16 <= 128 KB
76 x 24 bits
= 16 Power range, 3V supply, no EEPROM,
data RAM < 8 KB, C program
I/O pin operation
The pin can be set for input or output data transfer
CPU Data Bus
Output
Current
Driver
Output
Data
Latch
Data
Direction
Latch
Tri-state
Output
Enable
Input
Data
Latch
Write data bit
Read data bit
Write TRIS bit
Analogue input
multiplexer
General Timer Operation
A binary counter is used as a timer when driven from the clock
Binary Counter
Pre-scaler
(clock
divide)
Post-scaler
(output
divide)
Timer
Overflow/
Timeout
(Interrupt)
Flag
Clock
Source
Select
Instruction Clock
External Pulse
Capture register
Compare register
Capture signal
Match flag
ADC operation
The ADC converts an analog input into a binary code
ANx
Analogue
to Digital
Converter
Vref+
Input volts 0-Vf
Reference volts, Vf
8-bit or 16-bit
integer result
Setup ADC
Read ADC
Comparator operation
Vc+
Vc-
Compartor
status bit
Vc+ > Vc-
The comparator simply sets a bit if one input is higher than the other
Parallel Slave Port operation
The PSP allows an external data bus to be connected to the MCU
Parallel
Slave
Port
Chip select
Read
Write
EXTERNAL
Data x 8
Interrupt
INTERNAL
Data x 8
Interrupts sources in the PIC 16F877
Interrupt Source Interrupt trigger event CCS C Interrupt label
TIMERS
Timer 0 Timer 0 register overflow INT_TIMER0
Timer 1 Timer 1 register overflow INT_TIMER1
CCP 1 Timer 1 capture or compare detected INT_CCP1
Timer 2 Timer 2 register overflow INT_TIMER2
CCP2 Timer 2 capture or compare detected INT_CCP2
PORTS
RB0/INT pin Change on single pin RB0 INT_EXT
Port B pins Change on any of four pins RB4 – RB7 INT_RB
Parallel Slave Port Data received at PSP (write input active) INT_PSP
Analog Converter A/D conversion completed INT_AD
Analog Comparator Voltage compare true INT_COMP
SERIAL
UART Serial Port Received data available INT_RDA
UART Serial Port Transmit data buffer empty INT_TBE
SPI Serial Port Data transfer completed (read or write) INT_SSP
I2C Serial Port Interface activity detected INT_SSP
I2C Serial Port Bus collision detected INT_BUSCOL
MEMORY
EEPROM Non-volatile data memory write complete INT_EEPROM
Program Execution
Program Execution
1
Start counter
statement
2
Run
Counter
until
overflow
5
Time-out
Process
(Interrupt
Service
Routine)
7
Continue
3
Timeout
Interrupt
6
Return
from
Interrupt
4
Jump to
ISR
Timer Interrupt Process
Time-out forces the program to be suspended and the ISR
executed
PIC MCU
TX1 Transmit
RX1 Receive
Ground
HOST PC
RX2
TX2
COM PORT
Ground
Line
Driver
Interface
USART RS232 Signal
Line drivers convert the signal to a bipolar, higher voltage
+/- 12V
Typical USART RS232 signal
Bit
2
Bit
3
Idle Start
Bit
Bit
0
Bit
1
Bit
4
Bit
5
Bit
6
Bit
7
Stop
Bit
Time
1
0
Bit period
The data bits are timed from the falling edge of the start bit
Master
Serial Data Out, SDO
Serial Data In, SDI
Serial Clock, SCK
Slave Select SS1
Outputs SS2
SS3
Slave 1
SDO
SDI
SCK
!SS
Slave 2
SDO
SDI
SCK
!SS
SPI Connections
SPI uses hardware slave selection and separate clock
SPI Signals
7 6 5 4 3 2 1 0 Data bits
SDO/SDI
SCK Clock
Each data bit is transferred on the falling edge of the clock
Master Slave1 Slave2
+5V
SDA
SCL
etc
I2C Connections
Slave selection uses addresses issued by the Master
7 6 5 4 3 2 1 0
SDA
SCL
Acknowledge
Address / Data bits
Start
I2C Signals
Data is strobed in using the master clock, and reception
is acknowledged by the slave by taking the data line low
A simple C program
/*
OUTBYTE.C MPB 2-1-07 V1.0
*/
#include "16F877A.h" // MCU select
void main() // Main block
{
output_D(255); // Switch on outputs
}
This minimal program outputs a binary code to Port D
Program hex file
:1000000000308A0004280000840183131F30830518
:1000100083161F149F141F159F1107309C00880121
:08002000FF3083128800630029
:02400E00733FFE
:00000001FF
;PIC16F877A
The machine code is downloaded as a binary file to the chip
Basic circuit
Microchip
PIC
RESET
*CLOCK
POWER
• 5V
• GND
*Optional – Certain microcontroller have built-in oscillator to replace external clock
Basic Circuit (Schematics)
RESET
CLOCK
REGULATOR
5V
GND
LOAD
Basic I/O Programming
Microchip 16F877/877A
*The pin having notation R can be used as digital I/O
*The pin that are labeled as AN can be used as analog inputs
Pin Direction
How to set pin as input or output
Microchip
PIC
Set_tris_c(0x0F)
Set_tris_c(0b00001111)
RC7 RC0
• Use ‘0x’ for hex format or ‘0b’ for binary format
• Use ‘1’ for input or ‘0’ for output
Input Command
Microchip
PIC
data_s=input(PIN_C1)
Data_p=portc
1
0V
5V
3 or 0b00000011
• Output for data_s: 0 or 1 (bit)
• Output for data_p: 0 or 1,2,…. , 255 (byte)
How to read data?
Output Command
Microchip
PIC
0V
5V
• Single output: output_high/low
• Parallel output: output_x (* x is port name)
How to write data?
output_high(PIN_C4)
output_high(PIN_C5)
output_low(PIN_C6)
output_low(PIN_C7)
OR
output_c(0b00110000)
RC7 RC0
IO Command Summary
Pins
Single Parallel
MODE
INPUT data = input(PIN_C0)
0 or 1
data = portc
0 or 1,2,…,255
OUTPUT output_high(PIN_C5)
output_low(PIN_C6)
output_c(value)
Flowchart
Processing Input/Output
Decision
Connector Terminator
Flowchart : Structures
Statement
Sequence Selection Loop
Statement
IF
(Test)
THEN ELSE
DO
WHILE
LOOP
DO WHILE
YES
NO
YES NO
Example: Sequence
Exercise : Sequence
Example: Loop
Conditional Loop
Exercise: Loop
Example: Selection
IF Statement
LCD Programming
Interface LCD
LCD Code
LCD Command (1)
LCD Command (2)
ADC
Analog to Digital Converter
Data Conversion
Quantizing
ADC Pins
ADC Code
Hardware Interfacing
Sensor & DC Motor
Temperature Sensor
Relay
Relays allow one circuit (control circuit) to switch a second circuit (main
circuit) which can be completely separate from the first. For example a low
voltage battery circuit can use a relay to switch a 230VAC mains circuit.
MOSFET
L293D
www.pahangskills.gov.my

MICROCONTROLLER.pptx

  • 1.
  • 2.
    MICROPROCESSOR CPU RAM ROM ADC • Multiple chipneed external RAM, ROM, ADC,etc • High cost for simple task • Easy to upgrade • Application: - Desktop - Laptop • Intel Pentium • Motorola 68K • Raspberry Pi
  • 3.
    MICROCONTROLLER RAM CPU ROM TIMER ADC Etc.. • Single chipintergrated with RAM, ROM, ADC, etc. • Low cost for simple task • Difficult to upgrade • Application: - Control Unit - Roboticx • Microchip PIC • Atmel • Arduino
  • 4.
    Why Microchip PICis popular? Peripheral Interface Controller (PIC) popular due to: • Low cost • Wide availability • Large user base • Extensive collection of application notes • Availability of low cost or free development tools • Serial programming capability
  • 5.
    MICROCHIP FAMILY 1.Baseline coredevices -PIC10, PIC12 and PIC16 2.Mid-range core devices -PIC12 and PIC16 3.High End core devices -PIC24 and dsPIC
  • 6.
    Microchip 16F877/877A *The pinhaving notation R can be used as digital I/O *The pin that are labeled as AN can be used as analog inputs
  • 7.
    SPECIFICATION 1. CPU 8-bitPIC 2. Operating Voltage (V) - 2 to 5.5 V 3. Number of I/O pins – 33 4. ADC Module - 8ch, 10-bit 5. Timer Module - 8-bit(2), 16-bit(1) 6. External Oscillator - Up to 20Mhz 7. Data EEPROM - 256 bytes
  • 8.
    Elements of adigital controller The microcontroller contains all these elements in one chip
  • 9.
    PIC 16F877 MCUBlock diagram Flash ROM Program Memory 8192 x 14 bits 0000 – 1FFF Instruction Register MCU control lines Program Counter (13 bits) Stack 13 bits x 8 levels RAM File Registers 368 X 8 bits 000-1FF Data Bus (8 bits) File Select Register Working (W) Register File Address Literal Op- code Instructions Address Arithmetic & Logic Unit Ports, Timers ADC, Serial I/O Status bits Timing control EEPROM 256 bytes Clock Reset Port A B C D E Program address Instruction Decode & CPU control Status (Flag) Register Shows the main parts of the chip in simplified form
  • 10.
    Bank 0 (000– 07F) Bank 1 (080 – 0FF) Bank 2 (100-180) Bank 3(180-1FF) Address Register Address Register Address Register Address Register 000h Indirect 080h Indirect 100h Indirect 180h Indirect 001h Timer0 081h Option 101h Timer0 181h Option 002h PC Low 082h PC Low 102h PC Low 182h PC Low 003h Status Reg 083h Status Reg 103h Status Reg 183h Status Reg 004h File Select 084h File Select 104h File Select 184h File Select 005h Port A data 085h PortA direction 105h - 185h - 006h Port B data 086h PortB direction 106h Port B data 186h PortB direction 007h Port C data 087h PortC direction 107h - 187h - 008h Port D data 088h PortD direction 108h - 188h - 009h Port E data 089h PortE direction 109h - 189h - 00Ah PC High 08Ah PC High 10Ah PC High 18Ah PC High 00Bh Interrupt Control 08Bh Interrupt Control 10Bh Interrupt Control 18Bh Interrupt Control 00Ch to 01Fh 20 Peripheral Control Registers 08Ch to 09Fh 20 Peripheral Control Registers 10Ch to 10Fh 4 Peripheral Control Registers 18Ch to 18Fh 4 Peripheral Control Registers 110h to 16Fh 96 General Purpose Registers 190h to 1EFh 96 General Purpose Registers 020h to 06Fh 80 General Purpose Registers 0A0h to 0EFh 80 General Purpose Registers 070h to 07Fh 16 Common Access GPRs 0F0h to 0FFh Accesses 70h – 7Fh 170h to 17Fh Accesses 70h – 7Fh 1F0h to 1FFh Accesses 70h – 7Fh PIC16F877 simplified file register map
  • 11.
    PIC microcontroller types MCUPins Data word (bits) Program memory (bytes) Typical Instruction Set Speed MIPS Comment 10FXXX = 6 8 <= 512 33 x 12 bits <= 2 Low pin count, small form factor, cheap No EEPROM, none low power, assembler program 12FXXX = 8 8 <= 2 KB 12 / 14 bits <= 5 Low pin count, small form factor, cheap EEPROM, 10-bit ADC, some low power, assembler 16FXXX <= 64 8 <= 14 KB 35 x 14 bits <= 5 Mid-range, UART, I2C, SPI many low power, C or assembler program 18FXXXX <= 100 8 <= 128 KB 75 x 16 bits <= 16 High range, CAN, USB J series 3V supply, C program 24FXXXX <= 100 16 <= 128 KB 76 x 24 bits = 16 Power range, 3V supply, no EEPROM, data RAM < 8 KB, C program
  • 12.
    I/O pin operation Thepin can be set for input or output data transfer CPU Data Bus Output Current Driver Output Data Latch Data Direction Latch Tri-state Output Enable Input Data Latch Write data bit Read data bit Write TRIS bit Analogue input multiplexer
  • 13.
    General Timer Operation Abinary counter is used as a timer when driven from the clock Binary Counter Pre-scaler (clock divide) Post-scaler (output divide) Timer Overflow/ Timeout (Interrupt) Flag Clock Source Select Instruction Clock External Pulse Capture register Compare register Capture signal Match flag
  • 14.
    ADC operation The ADCconverts an analog input into a binary code ANx Analogue to Digital Converter Vref+ Input volts 0-Vf Reference volts, Vf 8-bit or 16-bit integer result Setup ADC Read ADC
  • 15.
    Comparator operation Vc+ Vc- Compartor status bit Vc+> Vc- The comparator simply sets a bit if one input is higher than the other
  • 16.
    Parallel Slave Portoperation The PSP allows an external data bus to be connected to the MCU Parallel Slave Port Chip select Read Write EXTERNAL Data x 8 Interrupt INTERNAL Data x 8
  • 17.
    Interrupts sources inthe PIC 16F877 Interrupt Source Interrupt trigger event CCS C Interrupt label TIMERS Timer 0 Timer 0 register overflow INT_TIMER0 Timer 1 Timer 1 register overflow INT_TIMER1 CCP 1 Timer 1 capture or compare detected INT_CCP1 Timer 2 Timer 2 register overflow INT_TIMER2 CCP2 Timer 2 capture or compare detected INT_CCP2 PORTS RB0/INT pin Change on single pin RB0 INT_EXT Port B pins Change on any of four pins RB4 – RB7 INT_RB Parallel Slave Port Data received at PSP (write input active) INT_PSP Analog Converter A/D conversion completed INT_AD Analog Comparator Voltage compare true INT_COMP SERIAL UART Serial Port Received data available INT_RDA UART Serial Port Transmit data buffer empty INT_TBE SPI Serial Port Data transfer completed (read or write) INT_SSP I2C Serial Port Interface activity detected INT_SSP I2C Serial Port Bus collision detected INT_BUSCOL MEMORY EEPROM Non-volatile data memory write complete INT_EEPROM
  • 18.
    Program Execution Program Execution 1 Startcounter statement 2 Run Counter until overflow 5 Time-out Process (Interrupt Service Routine) 7 Continue 3 Timeout Interrupt 6 Return from Interrupt 4 Jump to ISR Timer Interrupt Process Time-out forces the program to be suspended and the ISR executed
  • 19.
    PIC MCU TX1 Transmit RX1Receive Ground HOST PC RX2 TX2 COM PORT Ground Line Driver Interface USART RS232 Signal Line drivers convert the signal to a bipolar, higher voltage +/- 12V
  • 20.
    Typical USART RS232signal Bit 2 Bit 3 Idle Start Bit Bit 0 Bit 1 Bit 4 Bit 5 Bit 6 Bit 7 Stop Bit Time 1 0 Bit period The data bits are timed from the falling edge of the start bit
  • 21.
    Master Serial Data Out,SDO Serial Data In, SDI Serial Clock, SCK Slave Select SS1 Outputs SS2 SS3 Slave 1 SDO SDI SCK !SS Slave 2 SDO SDI SCK !SS SPI Connections SPI uses hardware slave selection and separate clock
  • 22.
    SPI Signals 7 65 4 3 2 1 0 Data bits SDO/SDI SCK Clock Each data bit is transferred on the falling edge of the clock
  • 23.
    Master Slave1 Slave2 +5V SDA SCL etc I2CConnections Slave selection uses addresses issued by the Master
  • 24.
    7 6 54 3 2 1 0 SDA SCL Acknowledge Address / Data bits Start I2C Signals Data is strobed in using the master clock, and reception is acknowledged by the slave by taking the data line low
  • 25.
    A simple Cprogram /* OUTBYTE.C MPB 2-1-07 V1.0 */ #include "16F877A.h" // MCU select void main() // Main block { output_D(255); // Switch on outputs } This minimal program outputs a binary code to Port D
  • 26.
  • 27.
    Basic circuit Microchip PIC RESET *CLOCK POWER • 5V •GND *Optional – Certain microcontroller have built-in oscillator to replace external clock
  • 28.
  • 29.
  • 30.
    Microchip 16F877/877A *The pinhaving notation R can be used as digital I/O *The pin that are labeled as AN can be used as analog inputs
  • 31.
    Pin Direction How toset pin as input or output Microchip PIC Set_tris_c(0x0F) Set_tris_c(0b00001111) RC7 RC0 • Use ‘0x’ for hex format or ‘0b’ for binary format • Use ‘1’ for input or ‘0’ for output
  • 32.
    Input Command Microchip PIC data_s=input(PIN_C1) Data_p=portc 1 0V 5V 3 or0b00000011 • Output for data_s: 0 or 1 (bit) • Output for data_p: 0 or 1,2,…. , 255 (byte) How to read data?
  • 33.
    Output Command Microchip PIC 0V 5V • Singleoutput: output_high/low • Parallel output: output_x (* x is port name) How to write data? output_high(PIN_C4) output_high(PIN_C5) output_low(PIN_C6) output_low(PIN_C7) OR output_c(0b00110000) RC7 RC0
  • 34.
    IO Command Summary Pins SingleParallel MODE INPUT data = input(PIN_C0) 0 or 1 data = portc 0 or 1,2,…,255 OUTPUT output_high(PIN_C5) output_low(PIN_C6) output_c(value)
  • 35.
  • 36.
    Flowchart : Structures Statement SequenceSelection Loop Statement IF (Test) THEN ELSE DO WHILE LOOP DO WHILE YES NO YES NO
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
    Relay Relays allow onecircuit (control circuit) to switch a second circuit (main circuit) which can be completely separate from the first. For example a low voltage battery circuit can use a relay to switch a 230VAC mains circuit.
  • 57.
  • 58.
  • 59.