Martin Bates Elsevier 2008 Programming 8-bit PIC Microcontrollers in C This presentation contains illustrations from the book ‘Programming 8-bit PIC Microcontrollers in C’ Part 1 Micr
Trang 1Martin Bates Elsevier 2008
Programming
8-bit PIC
Microcontrollers
in C
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
MICROCONTROLLER
SYSTEMS
Part 1
Figure 1.1 Elements of a digital controller
CPU Central Processing Unit
Input Peripherals
Output Peripherals
ROM Read Only Memory
RAM Read & Write Memory
Program download
User input
User output
The microcontroller contains all these elements in one chip
Flash 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
Program address
Instruction Decode &
CPU control Status (Flag) Register
Trang 2Table 1.1 PIC16F877 simplified file register map
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
to 17Fh
to 1FFh Accesses
Data word Program memory (bytes) Typical Instruction Set Speed
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
Figure 1.4 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
Input
Data
Latch
Write data bit
Read data
bit
Write TRIS bit
Analogue input
multiplexer
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
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
Setup ADC Read ADC
Vc+
Vc-Compartor status bit Vc+ >
Vc-The comparator simply sets a bit if one input is higher than the other
Trang 3Figure 1.8 Parallel Slave Port operation
The PSP allows an external data bus to be connected to the MCU
Parallel Slave Port
Chip select
Read
EXTERNAL
Data x 8
Interrupt
INTERNAL Data x 8
Table 1.3 Interrupts sources in the PIC 16F877 Interrupt Source Interrupt trigger event CCS C Interrupt label
TIMERS
PORTS
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
SERIAL
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
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
6 Return from Interrupt
4
Jump to
ISR
Figure 1.9 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
Line drivers convert the signal to a bipolar, higher voltage
+/- 12V
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
Trang 4Figure 1.13 SPI Signals
7 6 5 4 3 2 1 0 Data bits
SDO/SDI
Each data bit is transferred on the falling edge of the clock
+5V
SDA etc
Figure 1.14 I 2 C Connections
Slave selection uses addresses issued by the Master
7 6 5 4 3 2 1 0
SDA
SCL
Acknowledge Address / Data bits
Start
Figure 1.15 I 2 C Signals
Data is strobed in using the master clock, and reception
is acknowledged by the slave by taking the data line low
/*
OUTBYTE.C MPB 2-1-07 V1.0
*/
#include "16F877A.h" // MCU select
{
}
This minimal program outputs a binary code to Port D
Listing 1.2 Program hex file
:1000000000308A0004280000840183131F30830518
: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
Trang 5Figure 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
MCU
Vpp/!MCLR Vdd Vss PGD
1 3 5
ICSP Interface
Reset 10k
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
ICD2 provides in-circuit debugging
Host PC
MPLAB
development
system
+ C Compiler
ICD2
interface
PIC MCU
Target System
connector
Block diagram of the ICD2 programming and in-circuit debugging system