You are on page 1of 5

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

Elements of a digital controller

Part 1

User input

Input Peripherals

CPU Central Processing Unit

Output Peripherals

User output

MICROCONTROLLER SYSTEMS

Program download

ROM Read Only Memory

RAM Read & Write Memory

The microcontroller contains all these elements in one chip

Figure 1.2

16F877 pin-out

Figure 1.3
Flash ROM Program Memory 8192 x 14 bits 0000 1FFF

PIC 16F877 MCU Block diagram


Program Counter (13 bits) Address Stack 13 bits x8 levels RAM File Registers 368 X 8 bits 000-1FF File Address Program address
Working (W) Register Arithmetic & Logic Unit File Select Register

Instructions

Instruction Register

Literal Status bits Opcode

Status (Flag) Register

Data Bus (8 bits) EEPROM 256 bytes Ports, Timers ADC, Serial I/O

Instruction Decode & CPU control

MCU control lines

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

03-Jun-11

Table 1.1
Bank 0 (000 07F) Address 000h 001h 002h 003h 004h 005h 006h 007h 008h 009h 00Ah 00Bh 00Ch to 01Fh 020h to 06Fh 070h to 07Fh Register Indirect Timer0 PC Low Status Reg File Select Port A data Port B data Port C data Port D data Port E data PC High Interrupt Control 20 Peripheral Control Registers 80 General Purpose Registers 16 Common Access GPRs

PIC16F877 simplified file register map


Bank 1 (080 0FF) Address 080h 081h 082h 083h 084h 085h 086h 087h 088h 089h 08Ah 08Bh 08Ch to 09Fh 0A0h to 0EFh 0F0h to 0FFh Register Indirect Option PC Low Status Reg File Select PortA direction PortB direction PortC direction PortD direction PortE direction PC High Interrupt Control 20 Peripheral Control Registers 80 General Purpose Registers Accesses 70h 7Fh Bank 2 (100-180) Address 100h 101h 102h 103h 104h 105h 106h 107h 108h 109h 10Ah 10Bh 10Ch to 10Fh 110h to 16Fh 170h to 17Fh Register Indirect Timer0 PC Low Status Reg File Select Port B data PC High Interrupt Control 4 Peripheral Control Registers 96 General Purpose Registers Accesses 70h 7Fh Bank 3(180-1FF) Address 180h 181h 182h 183h 184h 185h 186h 187h 188h 189h 18Ah 18Bh 18Ch to 18Fh 190h to 1EFh 1F0h to 1FFh Register Indirect Option PC Low Status Reg File Select PortB direction -

Table 1.2
Data word (bits)

PIC microcontroller types


Program memory (bytes) Typical Instruction Set

MCU

Pins

Speed MIPS

Comment

10FXXX

=6

<= 512

33 x 12 bits

<= 2

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

12FXXX

=8

<= 2 KB

12 / 14 bits

<= 5

16FXXX

<= 64

<= 14 KB

35 x 14 bits

<= 5

18FXXXX
PC High Interrupt Control 4 Peripheral Control Registers 96 General Purpose Registers Accesses 70h 7Fh

<= 100

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

Figure 1.4

I/O pin operation

Figure 1.5

General Timer Operation


Capture signal Capture register Timer Overflow/ Timeout (Interrupt) Flag

Write TRIS bit

Data Direction Latch

Tri-state Output Enable Output Current Driver

CPU Data Bus Write data bit

Output Data Latch

Instruction Clock External Pulse

Clock Source Select

Prescaler (clock divide)

Binary Counter

Post-scaler (output divide)

Read data bit

Input Data Latch

Compare register

Match flag

Analogue input multiplexer

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

The pin can be set for input or output data transfer

Figure 1.6

ADC operation

Figure 1.7

Comparator operation

Vc+ Input volts 0-Vf ANx Analogue to Digital Converter Reference volts, Vf Vref+ Setup ADC Read ADC 8-bit or 16-bit integer result VcCompartor status bit Vc+ > Vc-

The comparator simply sets a bit if one input is higher than the other The ADC converts an analog input into a binary code

03-Jun-11

Table 1.3 Figure 1.8 Parallel Slave Port operation


Interrupt Source Timer 0 Timer 1 CCP 1 Timer 2 CCP2 RB0/INT pin

Interrupts sources in the PIC 16F877


Interrupt trigger event TIMERS Timer 0 register overflow Timer 1 register overflow Timer 1 capture or compare detected Timer 2 register overflow Timer 2 capture or compare detected PORTS Change on single pin RB0 Change on any of four pins RB4 RB7 Data received at PSP (write input active) A/D conversion completed Voltage compare true SERIAL Received data available Transmit data buffer empty Data transfer completed (read or write) Interface activity detected Bus collision detected MEMORY Non-volatile data memory write complete INT_EXT INT_RB INT_PSP INT_AD INT_COMP INT_RDA INT_TBE INT_SSP INT_SSP INT_BUSCOL INT_EEPROM INT_TIMER0 INT_TIMER1 INT_CCP1 INT_TIMER2 INT_CCP2 CCS C Interrupt label

Chip select Read Write

Interrupt Parallel Slave Port

EXTERNAL Data x 8

INTERNAL Data x 8

Port B pins Parallel Slave Port Analog Converter Analog Comparator UART Serial Port UART Serial Port SPI Serial Port I2C Serial Port I2C Serial Port EEPROM

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

Figure 1.9

Timer Interrupt Process


Program Execution

Figure 1.10

USART RS232 Signal

1 Start counter statement

2 Run Counter until overflow

HOST PC PIC MCU

Program Execution 3 Timeout Interrupt 4 Jump to ISR 7 Continue 5 Time-out Process (Interrupt Service Routine)

TX1 Transmit RX1 Receive Ground

Line Driver Interface

RX2 +/- 12V TX2 COM PORT Ground

6 Return from Interrupt

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

Line drivers convert the signal to a bipolar, higher voltage

Figure 1.11

Typical USART RS232 signal

Figure 1.12
Master Serial Data Out, SDO Serial Data In, SDI Serial Clock, SCK

SPI Connections

Bit period 1

Slave 1

Slave 2 SDO SDI SCK !SS

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

Time

SDO SDI SCK !SS Slave Select Outputs SS1 SS2 SS3

The data bits are timed from the falling edge of the start bit

SPI uses hardware slave selection and separate clock

03-Jun-11

Figure 1.13

SPI Signals
+5V

Figure 1.14

I2C Connections

Master

Slave1

Slave2

etc

SDO/SDI

Data bits
SDA SCL

SCK

Clock

Slave selection uses addresses issued by the Master Each data bit is transferred on the falling edge of the clock

Listing 1.1 Figure 1.15


Start SDA
7 6

A simple C program

I2C Signals
/* OUTBYTE.C MPB 2-1-07 V1.0 Acknowledge
2 1 0

Address / Data bits


5 4 3

*/ #include "16F877A.h" void main() { output_D(255); } // MCU select // Main block // Switch on outputs

SCL

Data is strobed in using the master clock, and reception is acknowledged by the slave by taking the data line low

This minimal program outputs a binary code to Port D

Listing 1.2

Program hex file

Figure 1.16

Screenshot of MPLAB Project

:1000000000308A0004280000840183131F30830518 :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

03-Jun-11

Figure 1.17

PICkit2 demo system hardware

Figure 1.18

ICSP target board connections


Application Board Reset 10k MCU Vpp/!MCLR Vdd Vss PGD PGC Vdd Vss Board +5V Supply

ICSP Interface

1 2 3 4 5

Connections to the target chip for programming Basic hardware for downloading the program to a test board

Figure 1.19

PICkit2 programmer dialogue

Figure 1.20

Microchip ICD2 module

On-screen window for program downloading to target chip

ICD2 provides in-circuit debugging

Figure 1.21

ICD2 program and debug system

Figure 1.22

ICD debugging windows

Host PC MPLAB development system + C Compiler

USB

ICD2 interface

6-WAY connector

PIC MCU Target System

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

User interface for in-circuit programming & debugging