You are on page 1of 30

Programming

8-bit PIC
Microcontrollers
in C
Martin Bates
Elsevier 2008
This presentation contains illustrations from the book
‘Programming 8-bit PIC Microcontrollers in C’

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
Part 1

MICROCONTROLLER
SYSTEMS
Figure 1.1 Elements of a digital controller

Input Output
User input Peripherals CPU Peripherals User
output
Central
Processing
ROM Unit RAM
Program Read Only Read & Write
download Memory Memory

The microcontroller contains all these elements in one chip


Figure 1.2 16F877 pin-out

The microcontroller pins have multiple functions


Figure 1.3 PIC 16F877 MCU Block diagram

Flash Program Counter


ROM (13 bits)
Program
Memory Address
8192
x 14 bits
Stack RAM
0000 – 1FFF 13 bits File
x8 Registers
Instructions levels 368
X 8 bits

Instruction Register
000-1FF
File Address

Program address

Working (W) File Select


Register Register
Literal
Arithmetic & Logic
Unit
Status bits
Status (Flag)
Op- Register
Data Bus
code
(8 bits)

EEPROM
Instruction MCU 256 bytes
Decode & control
CPU control lines
Ports, Timers
ADC, Serial I/O

Timing control

Clock Reset
Port A B C D E

Shows the main parts of the chip in simplified form


Table 1.1 PIC16F877 simplified file register map
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  20 Peripheral 08Ch  20 Peripheral  10Ch  4 Peripheral  18Ch  4 Peripheral  
to Control to Control to Control to Control
01Fh Registers 09Fh Registers 10Fh Registers 18Fh Registers
   
 110h 96 General  190h  96 General
 020h  80 General  0A0h  80 General  
to Purpose to Purpose to Purpose to Purpose
06Fh Registers 0EFh Registers 16Fh Registers 1EFh Registers
 
 070h  16 Common  0F0h Accesses 170h  Accesses  1F0h  Accesses  
to Access GPRs to 70h – 7Fh to 70h – 7Fh to 70h – 7Fh
07Fh 0FFh 17Fh 1FFh
Table 1.2 PIC microcontroller types

 
  Data Program Typical
Speed  
MCU Pins word memory Instruction
MIPS Comment
  (bits) (bytes) Set
 

Low pin count, small form factor,


10FXXX 33 x 12
=6 8 <= 512 <= 2 cheap
  bits
No EEPROM, none low power,
assembler program
Low pin count, small form factor,
12FXXX
=8 8 <= 2 KB 12 / 14 bits <= 5 cheap
 
EEPROM, 10-bit ADC, some low
power, assembler

16FXXX 35 x 14 Mid-range, UART, I2C, SPI


<= 64 8 <= 14 KB <= 5
  bits many low power, C or assembler
program

18FXXXX <= <= 128 75 x 16


8 <= 16 High range, CAN, USB
  100 KB bits
J series 3V supply, C program

76 x 24
24FXXXX <= <= 128 Power range, 3V supply, no
16 bits = 16
  100 KB EEPROM,
 
data RAM < 8 KB, C program
Figure 1.4 I/O pin operation

Write TRIS bit Data


Direction Tri-state
Latch Output
Enable

Output Output
CPU Data Bus
Data Current
Latch Driver
Write data bit

Input
Data
Read data Latch
bit

Analogue input
multiplexer

The pin can be set for input or output data transfer


Figure 1.5 General Timer Operation

Capture
Capture
signal
register
Timer
Instruction Overflow/
Clock Pre- Post-scaler
Clock Timeout
Source scaler Binary Counter (output
(Interrupt)
Select (clock divide)
Flag
External Pulse divide)

Compare Match flag


register

A binary counter is used as a timer when driven from the clock


Figure 1.6 ADC operation

Input volts 0-Vf ANx Setup ADC

Analogue Read ADC


to Digital
Converter
8-bit or 16-bit
Reference volts, Vf Vref+ integer result

The ADC converts an analog input into a binary code


Figure 1.7 Comparator operation

Vc+

Compartor
status bit
Vc- Vc+ > Vc-

The comparator simply sets a bit if one input is higher than the other
Figure 1.8 Parallel Slave Port operation

Chip select Interrupt


Read Parallel
Write Slave
Port

EXTERNAL INTERNAL
Data x 8 Data x 8

The PSP allows an external data bus to be connected to the MCU


Table 1.3 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
Figure 1.9 Timer Interrupt Process
Program Execution

1 2
Start counter Run
statement Counter
until
overflow
Program Execution

3
Timeout
Interrupt
4
Jump to
ISR
7
Continue

5
Time-out
Process
(Interrupt
Service 6
Routine) Return
from
Interrupt

Time-out forces the program to be suspended and the ISR executed


Figure 1.10 USART RS232 Signal

HOST PC
PIC MCU

TX1 Transmit RX2


Line
Driver +/- 12V
RX1 Receive TX2
Interface
COM PORT
Ground Ground

Line drivers convert the signal to a bipolar, higher voltage


Figure 1.11 Typical USART RS232 signal

Bit period

0
Time
Idle Start Bit Bit Bit Bit Bit Bit Bit Bit Stop
Bit 0 1 2 3 4 5 6 7 Bit

The data bits are timed from the falling edge of the start bit
Figure 1.12 SPI Connections

Master

Serial Data Out, SDO


Serial Data In, SDI
Serial Clock, SCK

Slave 1 Slave 2

SDO SDO
SDI SDI
SCK SCK

!SS !SS

Slave Select SS1


Outputs SS2
SS3

SPI uses hardware slave selection and separate clock


Figure 1.13 SPI Signals

SDO/SDI 7 6 5 4 3 2 1 0 Data bits

SCK Clock

Each data bit is transferred on the falling edge of the clock


Figure 1.14 I2C Connections

+5V Master Slave1 Slave2 etc

SDA
SCL

Slave selection uses addresses issued by the Master


Figure 1.15 I2C Signals

Start Acknowledge
Address / Data bits

SDA 7 6 5 4 3 2 1 0

SCL

Data is strobed in using the master clock, and reception


is acknowledged by the slave by taking the data line low
Listing 1.1 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


Listing 1.2 Program hex file

:1000000000308A0004280000840183131F30830518
:1000100083161F149F141F159F1107309C00880121
:08002000FF3083128800630029
:02400E00733FFE
:00000001FF
;PIC16F877A

  The machine code is downloaded as a binary file to the chip


Figure 1.16 Screenshot of MPLAB Project

The C program is compiled and tested in simulation mode


Figure 1.17 PICkit2 demo system hardware

Basic hardware for downloading the program to a test board


Figure 1.18 ICSP target board connections

Application Board

Reset
MCU
10k
1 Vpp/!MCLR
ICSP Vdd
2
Interface Vss
3
4 PGD
5 PGC
Vdd Vss
Board +5V Supply

Connections to the target chip for programming


Figure 1.19 PICkit2 programmer dialogue

On-screen window for program downloading to target chip


Figure 1.20 Microchip ICD2 module

ICD2 provides in-circuit debugging


Figure 1.21 ICD2 program and debug system

Host PC
MPLAB PIC MCU
development ICD2 Target
system USB interface System
6-WAY
+ C Compiler connector

Block diagram of the ICD2 programming and in-circuit debugging system


Figure 1.22 ICD debugging windows

User interface for in-circuit programming & debugging

You might also like