03-Jun-11
This presentation contains illustrations from the book
‘Programming 8-bit PIC Microcontrollers in C’
Programming
8-bit PIC
Microcontrollers
in C
Martin Bates
Elsevier 2008
Part 1 Microcontroller Systems
describes in detail the internal architecture and interfaces
available in the PIC 16F887A, a typical PIC chip, as well as
outlining the main features of the development system
Part 2 C Programming Essentials
provides simple example programs for the microcontroller
which show the basic principles of C programming,
and interfacing to basic I/O devices
Part 3 C Peripheral Interfaces
provides example programs for operating PIC chips with a
full range of peripherals, using timers and interrupts
Figure 1.1
Part 1
MICROCONTROLLER
SYSTEMS
The microcontroller contains all these elements in one chip
Figure 1.2
16F877 pin-out
Figure 1.3
PIC 16F877 MCU Block diagram
Flash
ROM
Program
Memory
8192
x 14 bits
Program Counter
(13 bits)
Address
Stack
13 bits
x8
levels
0000 – 1FFF
Instructions
RAM
File
Registers
(8 bits)
EEPROM
256 bytes
MCU
control
lines
Ports, Timers
ADC, Serial I/O
Timing control
Clock
Reset
Port
A
B
C
D
E
The microcontroller pins have multiple functions
Shows the main parts of the chip in simplified form
000h
Indirect
080h
Indirect
100h
Indirect
180h
Indirect
001h
Timer0
081h
Option
101h
Timer0
181h
183h
Status Reg
004h
File Select
084h
File Select
104h
File Select
184h
File Select
005h
Port A data
085h
PortA direction
105h
107h
-
187h
-
008h
Port D data
088h
PortD direction
108h
-
188h
-
009h
Port E data
089h
Interrupt Control
10Bh
Interrupt Control
18Bh
Interrupt Control
00Ch
to
01Fh
20 Peripheral
Control
Registers
08Ch
to
09Fh
20 Peripheral
Control
Registers
020h
to
06Fh
4 Peripheral
Control
Registers
96 General
Purpose
Registers
18Ch
to
18Fh
190h
to
1EFh
4 Peripheral
Control
Registers
96 General
Purpose
Registers
170h
to
17Fh
Accesses
70h – 7Fh
1F0h
to
=6
8
Latch
Pins
10FXXX
12FXXX
16FXXX
Comment
Low pin count, small form factor,
cheap
No EEPROM, none low power,
assembler program
Low pin count, small form factor,
cheap
EEPROM, 10-bit ADC, some low
power, assembler
Mid-range, UART, I2C, SPI
many low power, C or assembler
program
-
I/O pin operation
Data
Direction
Binary Counter
Compare
register
Input
Data
Latch
Power range, 3V supply, no
EEPROM,
data RAM < 8 KB, C program
General Timer Operation
Capture
signal
Instruction
Clock
76 x 24
bits
High range, CAN, USB
J series 3V supply, C program
Post-scaler
(output
divide)
Reference volts, Vf
Vref+
Setup ADC
Read ADC
Vc-
Compartor
status bit
Vc+ > Vc-
8-bit or 16-bit
integer result
The comparator simply sets a bit if one input is higher than the other
The ADC converts an analog input into a binary code
2
03-Jun-11
Table 1.3
Figure 1.8
Parallel Slave Port operation
CCP2
Timer 2 capture or compare detected
INT_CCP2
TIMERS
Chip select
Read
Write
Interrupt
Parallel
Slave
Port
PORTS
EXTERNAL
Data x 8
INTERNAL
Data x 8
RB0/INT pin
Change on single pin RB0
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
Program Execution
3
Timeout
Interrupt
TX1 Transmit
4
Jump to
ISR
RX2
Line
Driver
Interface
RX1 Receive
7
Continue
+/- 12V
TX2
COM PORT
Ground
Ground
5
Time-out
1
Slave 1
0
Time
Idle
Start
Bit
Bit
0
Bit
1
Bit
2
Bit
3
Bit
4
Bit
5
SS3
SPI uses hardware slave selection and separate clock
3
03-Jun-11
Figure 1.13
+5V
7
SDO/SDI
6
5
4
3
2
I2C Connections
Figure 1.14
Figure 1.15
/*
OUTBYTE.C
Start
SDA
Acknowledge
Address / Data bits
7
6
5
4
3
2
1
0
MPB
#include "16F877A.h"
:1000100083161F149F141F159F1107309C00880121
:08002000FF3083128800630029
:02400E00733FFE
:00000001FF
;PIC16F877A
The machine code is downloaded as a binary file to the chip
The C program is compiled and tested in simulation mode
4
03-Jun-11
Figure 1.17
Figure 1.18
PICkit2 demo system hardware
ICSP target board connections
Application Board
Reset
10k
ICSP
Interface
MCU
+ C Compiler
ICD2 program and debug system
USB
ICD2
interface
6-WAY
connector
Figure 1.20
Microchip ICD2 module
ICD2 provides in-circuit debugging
Figure 1.22
ICD debugging windows
PIC MCU
Target
System
Block diagram of the ICD2 programming and in-circuit debugging system
User interface for in-circuit programming & debugging