designing embedded systems with pic microcontrollers

17
Designing Embedded Systems with PIC Microcontrollers Principles and applications Tim Wilmshurst $^NIMI ' AMSTERDAM • BOSTON • HEIDELBERG • LONDON • NEW YORK • OXFORD .jfcJJJLs. P ARJ S ' SAN DIEGO • SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO 2LSEVIER Newnes is an imprint of Elsevier

Upload: others

Post on 07-Jun-2022

20 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Designing Embedded Systems with PIC Microcontrollers

Designing Embedded Systems with PIC Microcontrollers

Principles and applications

Tim Wilmshurst

$ ^ N I M I ' AMSTERDAM • BOSTON • HEIDELBERG • LONDON • NEW YORK • OXFORD

.jfcJJJLs. PARJS ' SAN DIEGO • SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO

2LSEVIER Newnes is an imprint of Elsevier

Page 2: Designing Embedded Systems with PIC Microcontrollers

Contents

Introduction xxi Acknowledgements xxv

Section 1 Getting Started with Embedded Systems 1

1 Tiny Computers, hidden control 3

1.1 The main idea - embedded Systems in today's world 3 1.1.1 What is an embedded system? 3

1.2 Some example embedded Systems 4 1.2.1 The domestic refrigerator 4 1.2.2 A car door mechanism 5 1.2.3 The electronic'ping-pong' 6 1.2.4 The Derbot Autonomous Guided Vehicle 7

1.3 Some Computer essentials 8 1.3.1 Elements of a Computer 9 1.3.2 Instruction sets - CISC and RISC 9 1.3.3 Memory types 10 1.3.4 Organising memory 10

1.4 Microprocessors and microcontroUers 11 1.4.1 Microprocessors 11 1.4.2 Microcontrollers 12 1.4.3 Microcontroller families 13 1.4.4 Microcontroller packaging and appearance 14

1.5 Microchip and the PIC microcontroller 15 1.5.1 Background 15 1.5.2 PIC microcontroUers today 15

1.6 An introduction to PIC microcontroUers using the 12 Series 17 1.6.1 The 12F508 architecture 18

1.7 What others do - a Freescale microcontroller 20 Summary 22 References 22

Section 2 Minimum Systems and the PIC® 16F84A 23

2 Introducing the PIC® 16 Series and the 16F84A 25

2.1 The main idea - the PIC 16 Series family 25 2.1.1 A family overview 25

Page 3: Designing Embedded Systems with PIC Microcontrollers

vi Contents

2.1.2 The 16F84A 27 2.1.3 A caution on Upgrades 27

2.2 An architecture overview of the 16F84A 27 2.2.1 The Status register 29

2.3 A review of memory technologies 29 2.3.1 Static RAM (SRAM) 30 2.3.2 EPROM (Erasable Programmable Read-Only Memory) 31 2.3.3 EEPROM (Electrically Erasable Programmable Read-Only Memory) 31 2.3.4 Flash 31

2.4 The 16F84A memory 32 2.4.1 The 16F84A program memory 32 2.4.2 The 16F84A data and Special Function Register memory ('RAM') 33 2.4.3 The Configuration Word 35 2.4.4 EEPROM 35

2.5 Some issues of timing 37 2.5.1 Clock oscillator and Instruction cycle 37 2.5.2 Pipelining , 38

2.6 Power-up and Reset 38 2.7 What others do - the Atmel AT89C2051 40 2.8 Taking things further - the 16F84A on-chip reset circuit 41 Summary 44 References 44

3 Parallel ports, power supply and the clock oscillator 45

3.1 The main idea - parallel input/output 46 3.2 The technical challenge of parallel input/output 46

3.2.1 Building a parallel interface 46 3.2.2 Port electrical characteristics 49 3.2.3 Some special cases 49

3.3 Connecting to the parallel port 52 3.3.1 Switches 52 3.3.2 Light-emitting diodes 53

3.4 The PIC 16F84A parallel ports 55 3.4.1 The 16F84A Port B 55 3.4.2 The 16F84A Port A 55 3.4.3 Port output characteristics 56

3.5 The clock oscillator 59 3.5.1 Clock oscillator types 59 3.5.2 Practical oscillator considerations 60 3.5.3 The 16F84A clock oscillator 60

3.6 Power supply 61 3.6.1 The need for power, and its sources 61 3.6.2 16F84A operating conditions 62

3.7 The hardware design of the electronic ping-pong 63

Page 4: Designing Embedded Systems with PIC Microcontrollers

Contents vii

Summary ( 64 References 64

4 Starting to program - an introduction to Assembler 65

4.1 The main idea - what programs do and how we develop them 66 4.1.1 The problem of programming and the Assembler compromise 66 4.1.2 The process of writing in Assembler 67 4.1.3 The program development process 68

4.2 The PIC 16 Series instruction set, with a little more on the ALU 69 4.2.1 More on the PIC 16 Series ALU 69 4.2.2 The PIC 16 Series instruction set - an introduction 70

4.3 Assemblers and Assembler format 71 4.3.1 Introducing Assemblers and the Microchip MPASM™ Assembler 71 4.3.2 Assembler format 71 4.3.3 Assembler directives 72 4.3.4 Number representation 72

4.4 Creating simple programs 73 4.4.1 A simple data transfer program 73

4.5 Adopting a development environment 76 4.5.1 Introducing MPLAB 76 4.5.2 The elements of MPLAB 76 4.5.3 The MPLAB file structure 77

4.6 An introductory MPLAB tutorial 77 4.6.1 Creating a project 77 4.6.2 Entering source code 79 4.6.3 Assembling the project 80

4.7 An introduction to Simulation 81 4.7.1 Getting started 81 4.7.2 Generating port inputs 81 4.7.3 Viewing microcontroller features 82 4.7.4 Resetting and running the program 82

4.8 Downloading the program to a microcontroller 83 4.9 What others do - a brief comparison of CISC and RISC instruction sets 86 4.10 Taking things further - the 16 Series instruction set format 87 Summary 88 References 88

5 Building Assembler programs 89

5.1 The main idea - building structured programs 89 5.1.1 Flow diagrams 89 5.1.2 State diagrams 91

5.2 Flow control - branching and subroutines 92 5.2.1 Conditional branching and working with bits 92 5.2.2 Subroutines and the Stack 94

Page 5: Designing Embedded Systems with PIC Microcontrollers

viii Contents

5.3 Generating time delays and intervals 5.4 Dealing with data

5.4.1 Indirect addressmg and the File Select Register 5.4.2 Look-up tables 5.4.3 Example program with delays and look-up table

5.5 Introducing logical Instructions 5.6 Introducing arithmetic Instructions and the Carry flag

5.6.1 Using add Instructions 5.6.2 Using subtract Instructions 5.6.3 An arithmetic program example 5.6.4 Using indirect addressing to save the Fibonacci series

5.7 Taming Assembler complexity 5.7.1 Include Files 5.7.2 Macros 5.7.3 MPLAB special Instructions

5.8 More use of the MPLAB Simulator 5.8.1 Breakpoints 5.8.2 Stopwatch 5.8.3 Trace

5.9 The ping-pong program 5.9.1 A structure for the ping-pong program 5.9.2 Exploring the ping-pong program code

5.10 Simulating the ping-pong program - tutorial 5.10.1 Setting up input Stimulus 5.10.2 Setting up the Watch window 5.10.3 Single stepping 5.10.4 Animate 5.10.5 Run 5.10.6 Breakpoints 5.10.7 Stopwatch 5.10.8 Trace 5.10.9 Debugging the füll program

5.11 What others do - graphical Simulators Summary References

6 Working with time: interrupts, counters and timers 120

6.1 The main idea - interrupts 121 6.1.1 Interrupt structures 121 6.1.2 The 16F84Ainterrupt structure 122 6.1.3 The CPU response to an interrupt 124

6.2 Working with interrupts 125 6.2.1 Programming with a Single interrupt 125 6.2.2 Moving to multiple interrupts - identifying the source 126

Page 6: Designing Embedded Systems with PIC Microcontrollers

Contents ix

6.2.3 Stopping interrupts from wrecking your program 1 - context saving 127

6.2.4 Stopping interrupts from wrecking your program 2 - critical regions and masking 130

6.3 The main idea - counters and timers 131 6.3.1 The digital counter reviewed 131 6.3.2 The counter as timer 132 6.3.3 The 16F84A Timer Omodule 134

6.4 Applying the 16F84A Timer 0, with examples using the electronic ping-pong 136 6.4.1 Object or event counting 136 6.4.2 Hardware-generated delays 137

6.5 The Watchdog Timer 138 6.6 Sleep mode 139 6.7 What others do 140 6.8 Taking things further - interrupt latency 141 Summary 142

Section 3 Larger Systems and the PIC® 16F873A 143

7 Larger Systems and the PIC® 16F873A 145

7.1 The main idea - the PIC 16F87XA 146 7.2 The 16F873A block diagram and CPU 146

7.2.1 Overview of CPU and core 146 7.2.2 Overview of memory 147 7.2.3 Overview of peripherals 150

7.3 16F873A memory and memory maps 150 7.3.1 The 16F873A program memory 150 7.3.2 The 16F873A data memory and Special Function Registers 152 7.3.3 The Configuration Word 154

7.4 'Special' memory Operations 155 7.4.1 Accessing EEPROM and program memory 155 7.4.2 In-Circuit Serial Programming (ICSP™) 156

7.5 The 16F873A interrupts 158 7.5.1 The interrupt structure 158 7.5.2 The interrupt registers 159 7.5.3 Interrupt identification and context saving 161

7.6 The 16F873A oscillator, reset and power supply 161 7.6.1 The clock oscillator 161 7.6.2 Reset and power supply 161

7.7 The 16F873A parallel ports 161 7.7.1 The 16F873A Port A 163 7.7.2 The 16F873A Port B 164 7.7.3 The 16F873A Port C 164

Page 7: Designing Embedded Systems with PIC Microcontrollers

x Contents

7.8 Test, commission and diagnostic tools 165 7.8.1 The challenge of testing an embedded System 165 7.8.2 Oscilloscopes and logic analysers 167 7.8.3 In-circuit emulators 170 7.8.4 On-chip debuggers 170

7.9 The Microchip in-circuit debugger (ICD 2) 171 7.10 Applying the 16F873A: the Derbot AGV 172

7.10.1 Power supply, oscillator and reset 172 7.10.2 Use of the parallel ports 173 7.10.3 Assembling the hardware 174

7.11 Downloading, testing and running a simple program with ICD 2 176 7.11.1 A first Derbot program 176 7.11.2 Applying the ICD 2 178 7.11.3 Setting the configuration bits within the program 179

7.12 Taking things further - the 16F874A/16F877A Ports D and E 180

Summary 182 References 183

8 The human and physical interfaces 184

8.1 The main idea - the human interface 184 8.2 From Switches to keypads 187

8.2.1 The keypad 187 8.2.2 Design example: use of keypad in Derbot hand Controller 188

8.3 LED displays 193 8.3.1 LED arrays: seven-segment displays 193 8.3.2 Design example: the Derbot hand Controller

seven-segment display 194 8.4 Liquid crystal displays 199

8.4.1 The HD44780 LCD driver and its derivatives 199 8.4.2 Design example: use of LCD display in Derbot hand Controller 200

8.5 The main idea - interfacing to the physical world 203 8.6 Some simple sensors 203

8.6.1 The microswitch 204 8.6.2 Light-dependent resistors 204 8.6.3 Optical object sensing 205 8.6.4 The opto-sensor applied as a shaft encoder 205 8.6.5 Ultrasonic object sensor 207

8.7 More on digital input 207 8.7.1 16F873A input characteristics 207 8.7.2 Ensuring legal logic levels, and input protection 208 8.7.3 Switch debouncing 212

Page 8: Designing Embedded Systems with PIC Microcontrollers

Contents xi

8.8 Actuators: motors and servos 212 8.8.1 DC and stepper motors 212 8.8.2 Angular positioning: the'servo' 214

8.9 Interfacing to actuators 215 8.9.1 Simple DC switching 215 8.9.2 Simple switching on the Derbot 217 8.9.3 Reversible switching: the H-bridge 218 8.9.4 Motor switching on the Derbot 220

8.10 Building up the Derbot 220 8.11 Applying sensors and actuators - a 'blind' navigation Derbot program 222 Summary 223 References 223

Taking timing further 225

9.1 The main ideas - taking counting and timing further 225 9.2 The 16F87XA Timer 0 and Timer 1 226

9.2.1 Timer 0 226 9.2.2 Timer 1 226 9.2.3 Application of Timer 0 and Timer 1 as counters for Derbot odometry 228 9.2.4 Using Timer 0 and Timer 1 to generate repetitive interrupts 231

9.3 The 16F87XA Timer 2, comparator and PR2 register 232 9.3.1 Timer 2 " 232 9.3.2 The PR2 register, comparator and postscaler 234

9.4 The capture/compare/PWM (CCP) modules 235 9.4.1 A capture/compare/PWM overview 235 9.4.2 Capture mode 235 9.4.3 Compare mode 237

9.5 Pulse width modulation 237 9.5.1 The principle of PWM 237 9.5.2 Generating PWM signals in hardware - the 16F87XA PWM 239 9.5.3 PWM applied in the Derbot for motor control 241

9.6 Generating PWM in Software 244 9.6.1 An example of software-generated PWM 245 9.6.2 Further Assembler directives for memory dennition and branching 248

9.7 PWM used for digital-to-analog conversion 249 9.7.1 An example of PWM used for digital-to-analog conversion 249

9.8 Frequency measurement 252 9.8.1 The principle of frequency measurement 252 9.8.2 Frequency (speed) measurement in the Derbot 252

9.9 Speed control applied to the Derbot 255 9.10 Where there is no timer 258 9.11 Sleepmode 260 9.12 Where do we go from here? 261 9.13 Building up the Derbot 262

Page 9: Designing Embedded Systems with PIC Microcontrollers

xii Contents

Summary 262 References 262

10 Starting with serial 263

10.1 The main idea - introducing serial 263 10.2 Simple serial links - synchronous data communication 265

10.2.1 Synchronous basics 265 10.2.2 Implementing synchronous serial I/O in the microcontroller 266 10.2.3 Microwire and SPI (Serial Peripheral Interface) 266 10.2.4 Introducing multiple nodes 267

10.3 The 16F87XA Master Synchronous Serial Port (MSSP) module in SPI mode 267 10.3.1 Portoverview 268 10.3.2 Port configuration 268 10.3.3 Setting the clock 270 10.3.4 Managing data transfer 271

10.4 A simple SPI example 273 10.5 The limitations of Microwire and SPI, and of simple

synchronous serial transfer 275 10.6 Enhancing synchronous serial, and the Inter-Integrated Circuit bus 275

10.6.1 Main I2C features and physical interconnection 275 10.6.2 The pull-up resistor 275 10.6.3 I2C signal characteristics 276

10.7 The MSSP configured for I2C 277 10.7.1 The MSSP I2C registers and their preliminary use 277 10.7.2 The MSSP in I2C Slave mode 281 10.7.3 The MSSP in I2C Master mode 283

10.8 I2C applied in the Derbot AGV 286 10.8.1 The Derbot hand Controller as a serial node 286 10.8.2 The AGV as an I2C master 286 10.8.3 The hand Controller as an I2C slave 290 10.8.4 Evaluation of the Derbot I2C programs 292

10.9 Evaluation of synchronous serial data communication and an introduction to asynchronous 293 10.9.1 Asynchronous principles 293 10.9.2 Synchronising serial data - without an incoming clock 293

10.10 The 16F87XA Addressable Universal Synchronous Asynchronous Receiver Transmitter (USART) 295 10.10.1 Portoverview 295 10.10.2 The USART asynchronous transmitter 295 10.10.3 The USART baud rate generator 298 10.10.4 The USART asynchronous receiver 299 10.10.5 An asynchronous example 300 10.10.6 Using address detection with the USART receive mode 302 10.10.7 The USART in synchronous mode 302

Page 10: Designing Embedded Systems with PIC Microcontrollers

Contents xiii

10.11 Implementing serial without a serial port - 'bit banging' 303 10.12 Building up the Derbot 303 Summary 303 References 303

Data acquisition and manipulation 304

11.1 The main idea - analog and digital quantities, their acquisition and use 304

11.2 The data acquisition System 305 11.2.1 The analog-to-digital Converter 306 11.2.2 Signal conditioning - amplification and filtering 308 11.2.3 The analog multiplexer 308 11.2.4 Sample and hold, and acquisition time 309 11.2.5 Timing and microprocessor control 310 11.2.6 Data acquisition in the microcontroller environment 311

11.3 ThePIC® 16F87XAADCmodule 312 11.3.1 Overview and block diagram 312 11.3.2 Controlling the ADC 313 11.3.3 The analog input model 317 11.3.4 Calculating acquisition time 318 11.3.5 Repeated conversions 319 11.3.6 Trading off conversion speed and resolution 319

11.4 Applying the ADC in the Derbot light meter program 319 11.4.1 Configuration of the ADC 319 11.4.2 Acquisition time 320 11.4.3 Data conversion 321

11.5 Some simple data manipulation techniques 321 11.5.1 Fixed-and floating-point arithmetic 322 11.5.2 Binary to Binary Coded Decimal conversion 323 11.5.3 Multiplication 324 11.5.4 Scaling and the Derbot light meter example 324 11.5.5 Using the voltage reference for scaling 326

11.6 The Derbot light-seeking program 326 11.7 The comparator module 327

11.7.1 Review of comparator action 327 11.7.2 The 16F87XAcomparators and voltage reference 329

11.8 Applying the Derbot circuit for measurement purposes 329 11.8.1 The electronic tape measure 329 11.8.2 The light meter 331 11.8.3 The Voltmeter 331 11.8.4 Other measurement Systems 331

11.9 Configuring the Derbot AGV as a light-seeking robot 332 Summary 332 References 332

Page 11: Designing Embedded Systems with PIC Microcontrollers

xiv Contents

© Section 4 Smarter Systems and the PIC® 18FXX2

12 Smarter Systems and the PIC® 18FXX2

12.1 The main idea - the PIC 18 Series and the 18FXX2 12.2 The 18F2X2 block diagram and Status register 12.3 The 18 Series instruction set

12.3.1 Instructions which are unchanged 12.3.2 Instructions which have been upgraded 12.3.3 New, variant, Instructions 12.3.4 New instructions

12.4 Data memory and Special Function Registers 12.4.1 The data memory map 12.4.2 Access RAM 12.4.3 Indirect addressing and accessing tables in data memory

12.5 Program memory 12.5.1 The program memory map 12.5.2 The Program Counter 12.5.3 Upgrading from the 16 Series and computed goto instructions 12.5.4 The Configuration registers

12.6 The Stacks 12.6.1 Automatic Stack Operations 12.6.2 Programmer access to the Stack ' 12.6.3 The Fast Register Stack

12.7 The interrupts 12.7.1 An interrupt structure overview 12.7.2 The interrupt sources, their enabling and prioritisation 12.7.3 Overall interrupt prioritisation enabling 12.7.4 Global enabling 12.7.5 Other aspects of the interrupt logic 12.7.6 The Interrupt registers 12.7.7 Context saving with interrupts

12.8 Power supply and reset 12.8.1 Power supply 12.8.2 Power-up and Reset

12.9 The oscillator sources 12.9.1 LP, XT, HS and RC oscillator modes 12.9.2 EC, ECIO and RCIO oscillator modes 12.9.3 HS + PLL oscillator mode 12.9.4 Clock source switching

12.10 Introductory programming with the 18F242 12.10.1 Using the MPLAB IDE for the 18 Series 12.10.2 The Fibonacci program

Summary References 367

Page 12: Designing Embedded Systems with PIC Microcontrollers

Contents xv

13 The PIC® 18FXX2 peripherals 368

13.1 The main idea - the 18FXX2 peripherals 368 13.2 The parallel ports 369

13.2.1 The 18FXX2 Port A 369 13.2.2 The 18FXX2 Port B 369 13.2.3 The 18FXX2 Port C 371 13.2.4 The parallel slaveport 371

13.3 Thetimers 371 13.3.1 Timer 0 371 13.3.2 Timer 1 373 13.3.3 Timer 2 373 13.3.4 Timer 3 373 13.3.5 The Watchdog Timer 376

13.4 The capture/compare/PWM (CCP) modules 376 13.4.1 The control registers 376 13.4.2 Capturemode 376 13.4.3 Compare mode 377 13.4.4 Pulse width modulation 378

13.5 The serial ports 378 13.5.1 The MSSP in SPI mode 379 13.5.2 The MSSP in I2C mode 379 13.5.3 TheUSART 380

13.6 The analog-to-digital Converter (ADC) 380 13.7 Low-voltage detect 380 13.8 Applying the 18 Series in the Derbot-18 382 13.9 The 18F2420 and the extended Instruction set 383

13.9.1 Nanowatt technology 383 13.9.2 The extended instruction set 384 13.9.3 Enhanced peripherals 384

Summary 385 Reference 385

14 Introducing C 386

14.1 The main idea - why C? 387 14.2 An introduction to C 387

14.2.1 A little history 387 14.2.2 A first program 388 14.2.3 Laying out the program - declarations, Statements, comments and space 388 14.2.4 Ckeywords 390 14.2.5 TheCfunction 391 14.2.6 Data type and storage 392 14.2.7 C Operators 392 14.2.8 Control of program flow, and the while keyword 393

Page 13: Designing Embedded Systems with PIC Microcontrollers

xvi Contents

14.2.9 The C preprocessor and its directives 14.2.10 Use of libraries, and the Standard Library

14.3 Compiling the C program 14.4 The MPLAB C18 Compiler

14.4.1 Specification of radix 14.4.2 Arithmetic Operations

14.5 AC18tutorial 14.5.1 The Linker and Linker Scripts 14.5.2 Linking header and library flies 14.5.3 Building the project 14.5.4 Project files

14.6 Simulating a C program 14.7 A second C example - the Fibonacci program

14.7.1 Program preliminaries - more on declanng variables 14.7.2 The do-while construct 14.7.3 Labels and the goto keyword 14.7.4 Simulating the Fibonacci program

14.8 The MPLAB C18 libraries 14.8.1 Hardware peripheral functions 14.8.2 The Software peripheral library 14.8.3 The general Software library 14.8.4 The maths library

14.9 Further reading Summary References

15 C and the embedded environment

15.1 The main idea - adapting C to the embedded environment 15.2 Controlling and branching on bit values

15.2.1 Controlling individual bits 15.2.2 The if and if-else conditional branch structures 15.2.3 Setting the configuration bits 15.2.4 Simulating and running the example program

15.3 More on functions 15.3.1 The function prototype 15.3.2 The function definition 15.3.3 Function calls and data passing 15.3.4 Library delay functions, and DelaylOKTCYx()

15.4 More branching and looping 15.4.1 Using the break keyword 15.4.2 Using the for keyword

15.5 Using the timer and PWM peripherals 15.5.1 Using the timer peripherals

Page 14: Designing Embedded Systems with PIC Microcontrollers

Contents xvii

15.5.2 Using PWM 421 15.5.3 The main program loop 421

Summary 422

16 Acquiring and using data with C 423

16.1 The main idea - using C for data manipulation 423 16.2 Using the 18FXX2ADC 423

16.2.1 The light-seeking program structure 427 16.2.2 UseoftheADC 428 16.2.3 Further use of if-else 429 16.2.4 Simulating the light-seeking program 429

16.3 Pointers, arrays and strings 431 16.3.1 Pointers 431 16.3.2 Arrays 432 16.3.3 Using pointers with arrays 432 16.3.4 Strings 433 16.3.5 An example program: using pointers, arrays and strings 433 16.3.6 A word on evaluating the while condition 434 16.3.7 Simulating the program example 435

16.4 Using the I2C peripheral 437 16.4.1 An example I2C program 437 16.4.2 Use of + + and — Operators 439

16.5 Formatting data for display 440 16.5.1 Overview of example program 440 16.5.2 Using library functions for data formatting 442 16.5.3 Program evaluation 442

Summary 443

17 More C and the wider C environment 444

17.1 The main idea - more C and the wider C environment 444 17.2 Assembler inserts 445 17.3 Controlling memory allocation 446

17.3.1 Memory allocation pragmas 447 17.3.2 Setting the Configuration Words 447

17.4 Interrupts 448 17.4.1 The Interrupt Service Routine 448 17.4.2 Locating and identifying the ISR 449

17.5 Example with interrupt on overflow - flashing LEDs on the Derbot 449 17.5.1 Using Timer 0 450 17.5.2 Using interrupts, and the ISR action 451 17.5.3 Simulating the flashing LEDs program 452

17.6 Storage classes and their application 453 17.6.1 Storage classes 453

Page 15: Designing Embedded Systems with PIC Microcontrollers

xviii Contents

17.6.2 Scope 454 17.6.3 Duration 454 17.6.4 Linkage 455 17.6.5 Working with 18 Series memory 455 17.6.6 Storage class examples 455

17.7 Start-up code: c018i.c 456 17.7.1 The C18 start-up files 456 17.7.2 The c018i.c structure 457 17.7.3 Simulating c018i.c 457

17.8 Structures, unions and bit-nelds 459 17.9 Processor-specific header files 460

17.9.1 SFR definitions 460 17.9.2 Assembler Utilities in the header file 461

17.10 Taking things further - the MPLAB Linker and the .map file 462 17.10.1 What the Linker does 462 17.10.2The Linker Script 462 17.10.3 The .map file 464

Summary 465 References 465

18 Multi-tasking and the Real Time Operating System 466

18.1 The main ideas - the challenge of multi-tasking and real time 466 18.1.1 Multi-tasking - tasks, priorities and deadlines 467 18.1.2 So what is'real time'? 468

18.2 Achieving multi-tasking with sequential programming 469 18.2.1 Evaluating the super loop 469 18.2.2 Time-triggered and event-triggered tasks 469 18.2.3 Using interrupts for pnoritisation - the foreground/background structure 469 18.2.4 Introducing a 'clock tick' to synchronise program activity 470 18.2.5 A general-purpose'operating System' 471 18.2.6 The limits of sequential programming when multi-tasking 471

18.3 The Real Time Operating System (RTOS) 472 18.4 Scheduling and the scheduler 473

18.4.1 Cyclic scheduling 473 18.4.2 Round robin scheduling and context switching 473 18.4.3 Task states 474 18.4.4 Prioritised pre-emptive scheduling 475 18.4.5 Cooperative scheduling 476 18.4.6 The role of interrupts in scheduling 477

18.5 Developing tasks 477 18.5.1 Defining tasks 477 18.5.2 Writing tasks and setting priority 478

Page 16: Designing Embedded Systems with PIC Microcontrollers

Contents xix

18.6 Data and resource protection - the semaphore 478 18.7 Where do we go from here? 479 Summary 479 References 479

19 The Salvo™ Real Time Operating System 480

19.1 The main idea - Salvo, an example RTOS 480 19.1.1 Basic Salvo features 480 19.1.2 Salvo versions and references 481

19.2 Configuring the Salvo application 482 19.2.1 Building Salvo applications - the library build 482 19.2.2 Salvo libraries 482 19.2.3 Using Salvo with Cl8 483

19.3 Writing Salvo programs 483 19.3.1 Initialisation and scheduling 484 19.3.2 Writing Salvo tasks 485

19.4 A first Salvo example 485 19.4.1 Program overview and the main function 487 19.4.2 Tasks and scheduling 488 19.4.3 Creating a Salvo/C18 project 488 19.4.4 Setting the configuration file 489 19.4.5 Building the Salvo example 489 19.4.6 Simulating the Salvo program 490

19.5 Using interrupts, delays and semaphores with Salvo 491 19.5.1 An example program using an interrupt-based clock tick 492 19.5.2 Selecting the library and configuration 494 19.5.3 Using interrupts and establishing the clock tick 494 19.5.4 Using delays 496 19.5.5 Using a binary semaphore 496 19.5.6 Simulating the program 497 19.5.7 Running the program 499

19.6 Using Salvo messages and increasing RTOS complexity 499 19.7 A program example with messages 500

19.7.1 Selecting the library and configuration 505 19.7.2 The task: USnd_ Task 505 19.7.3 The task: Motor_ Task 505 19.7.4 The use of messages 506 19.7.5 The use of interrupts, and the ISRs 507 19.7.6 Simulating or running the program 509

19.8 The RTOS overhead " 509 Summary 510 References 510

Page 17: Designing Embedded Systems with PIC Microcontrollers

xx Contents

Section 5 Techniques of Connectivity and Networking

20 Connectivity and networks

20.1 The main idea - networking and Connectivity 20.1.1 A word on protocols

20.2 Infrared Connectivity 20.2.1 The IrDA and the PIC microcontroller

20.3 Radio Connectivity 20.3.1 Bluetooth 20.3.2 Zigbee 20.3.3 Zigbee and the PIC microcontroller

20.4 Controller Area Network (CAN) and Local Interconnect Network (LIN) 20.4.1 Controller Area Network (CAN) 20.4.2 CAN and the PIC microcontroller 20.4.3 Local Interconnect Network (LIN) 20.4.4 LIN and the PIC microcontroller

20.5 Embedded Systems and the Internet 20.5.1 Connecting to the Internet with the PIC microcontroller

20.6 Conclusion Summary References

Appendix 1 The PIC® 16 Series instruction set Appendix 2 The electronic ping-pong Appendix 3 The Derbot AGV - hardware design details Appendix 4 Some basics of Autonomous Guided Vehicles Appendix 5 PIC® 18 Series instruction set (non-extended) Appendix 6 Essentials of C

Index