Tài liệu Designing Embedded Systems with PIC Microcontrollers Principles and applications

Thảo luận trong 'Điện - Điện Tử' bắt đầu bởi Thúy Viết Bài, 5/12/13.

  1. Thúy Viết Bài

    Thành viên vàng

    Bài viết:
    198,891
    Được thích:
    167
    Điểm thành tích:
    0
    Xu:
    0Xu
    Designing Embedded Systems with PIC Microcontrollers Principles and applications

    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 microcontrollers 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 microcontrollers today 15
    1.6 An introduction to PIC microcontrollers 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
    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
    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 MPASMTM 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
    viii Contents
    5.3 Generating time delays and intervals 95
    5.4 Dealing with data 97
    5.4.1 Indirect addressing and the File Select Register 97
    5.4.2 Look-up tables 98
    5.4.3 Example program with delays and look-up table 99
    5.5 Introducing logical instructions 101
    5.6 Introducing arithmetic instructions and the Carry flag 102
    5.6.1 Using add instructions 102
    5.6.2 Using subtract instructions 102
    5.6.3 An arithmetic program example 102
    5.6.4 Using indirect addressing to save the Fibonacci series 104
    5.7 Taming Assembler complexity 106
    5.7.1 Include Files 106
    5.7.2 Macros 107
    5.7.3 MPLAB special instructions 108
    5.8 More use of the MPLAB simulator 109
    5.8.1 Breakpoints 109
    5.8.2 Stopwatch 110
    5.8.3 Trace 110
    5.9 The ping-pong program 112
    5.9.1 A structure for the ping-pong program 112
    5.9.2 Exploring the ping-pong program code 115
    5.10 Simulating the ping-pong program – tutorial 116
    5.10.1 Setting up input stimulus 116
    5.10.2 Setting up the Watch window 116
    5.10.3 Single stepping 116
    5.10.4 Animate 117
    5.10.5 Run 117
    5.10.6 Breakpoints 117
    5.10.7 Stopwatch 117
    5.10.8 Trace 117
    5.10.9 Debugging the full program 118
    5.11 What others do – graphical simulators 118
    Summary 119
    References 119
    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 16F84A interrupt 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
    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 0 module 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 (ICSPTM) 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
    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
    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
    9 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 definition 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 Sleep mode 260
    9.12 Where do we go from here? 261
    9.13 Building up the Derbot 262
    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 Port overview 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 Port overview 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
    Contents xiii
    10.11 Implementing serial without a serial port – ‘bit banging’ 303
    10.12 Building up the Derbot 303
    Summary 303
    References 303
    11 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 The PIC® 16F87XA ADC module 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 16F87XA comparators 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
    xiv Contents
    Section 4 Smarter Systems and the PIC® 18FXX2 333
    12 Smarter systems and the PIC® 18FXX2 335
    12.1 The main idea – the PIC 18 Series and the 18FXX2 336
    12.2 The 18F2X2 block diagram and Status register 337
    12.3 The 18 Series instruction set 340
    12.3.1 Instructions which are unchanged 344
    12.3.2 Instructions which have been upgraded 344
    12.3.3 New, variant, instructions 345
    12.3.4 New instructions 345
    12.4 Data memory and Special Function Registers 345
    12.4.1 The data memory map 345
    12.4.2 Access RAM 347
    12.4.3 Indirect addressing and accessing tables in data memory 347
    12.5 Program memory 347
    12.5.1 The program memory map 349
    12.5.2 The Program Counter 349
    12.5.3 Upgrading from the 16 Series and computed goto instructions 349
    12.5.4 The Configuration registers 350
    12.6 The Stacks 352
    12.6.1 Automatic Stack operations 352
    12.6.2 Programmer access to the Stack 352
    12.6.3 The Fast Register Stack 352
    12.7 The interrupts 353
    12.7.1 An interrupt structure overview 353
    12.7.2 The interrupt sources, their enabling and prioritisation 353
    12.7.3 Overall interrupt prioritisation enabling 354
    12.7.4 Global enabling 354
    12.7.5 Other aspects of the interrupt logic 355
    12.7.6 The Interrupt registers 355
    12.7.7 Context saving with interrupts 356
    12.8 Power supply and reset 358
    12.8.1 Power supply 358
    12.8.2 Power-up and Reset 358
    12.9 The oscillator sources 360
    12.9.1 LP, XT, HS and RC oscillator modes 362
    12.9.2 EC, ECIO and RCIO oscillator modes 363
    12.9.3 HS + PLL oscillator mode 363
    12.9.4 Clock source switching 363
    12.10 Introductory programming with the 18F242 364
    12.10.1 Using the MPLAB IDE for the 18 Series 364
    12.10.2 The Fibonacci program 365
    Summary 367
    References 367
    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 slave port 371
    13.3 The timers 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 Capture mode 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 The USART 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 C keywords 390
    14.2.5 The C function 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
    xvi Contents
    14.2.9 The C preprocessor and its directives 394
    14.2.10 Use of libraries, and the Standard Library 394
    14.3 Compiling the C program 394
    14.4 The MPLAB C18 compiler 395
    14.4.1 Specification of radix 396
    14.4.2 Arithmetic operations 396
    14.5 A C18 tutorial 396
    14.5.1 The Linker and Linker Scripts 396
    14.5.2 Linking header and library files 397
    14.5.3 Building the project 397
    14.5.4 Project files 398
    14.6 Simulating a C program 400
    14.7 A second C example – the Fibonacci program 401
    14.7.1 Program preliminaries – more on declaring variables 402
    14.7.2 The do–while construct 403
    14.7.3 Labels and the goto keyword 403
    14.7.4 Simulating the Fibonacci program 403
    14.8 The MPLAB C18 libraries 403
    14.8.1 Hardware peripheral functions 404
    14.8.2 The software peripheral library 404
    14.8.3 The general software library 405
    14.8.4 The maths library 406
    14.9 Further reading 406
    Summary 407
    References 407
    15 C and the embedded environment 409
    15.1 The main idea – adapting C to the embedded environment 409
    15.2 Controlling and branching on bit values 409
    15.2.1 Controlling individual bits 411
    15.2.2 The if and if–else conditional branch structures 411
    15.2.3 Setting the configuration bits 412
    15.2.4 Simulating and running the example program 412
    15.3 More on functions 413
    15.3.1 The function prototype 413
    15.3.2 The function definition 414
    15.3.3 Function calls and data passing 414
    15.3.4 Library delay functions, and Delay10KTCYx( ) 415
    15.4 More branching and looping 415
    15.4.1 Using the break keyword 415
    15.4.2 Using the for keyword 416
    15.5 Using the timer and PWM peripherals 417
    15.5.1 Using the timer peripherals 420
    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 18FXX2 ADC 423
    16.2.1 The light-seeking program structure 427
    16.2.2 Use of the ADC 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
    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-fields 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.2 The 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 prioritisation – 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
    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 SalvoTM 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 C18 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
    xx Contents
    Section 5 Techniques of Connectivity and Networking 511
    20 Connectivity and networks 513
    20.1 The main idea – networking and connectivity 513
    20.1.1 A word on protocols 514
    20.2 Infrared connectivity 515
    20.2.1 The IrDA and the PIC microcontroller 515
    20.3 Radio connectivity 516
    20.3.1 Bluetooth 516
    20.3.2 Zigbee 517
    20.3.3 Zigbee and the PIC microcontroller 517
    20.4 Controller Area Network (CAN) and Local Interconnect Network (LIN) 518
    20.4.1 Controller Area Network (CAN) 518
    20.4.2 CAN and the PIC microcontroller 520
    20.4.3 Local Interconnect Network (LIN) 520
    20.4.4 LIN and the PIC microcontroller 521
    20.5 Embedded systems and the Internet 522
    20.5.1 Connecting to the Internet with the PIC microcontroller 523
    20.6 Conclusion 523
    Summary 524
    References 524
    Appendix 1 The PIC® 16 Series instruction set 527
    Appendix 2 The electronic ping-pong 528
    Appendix 3 The Derbot AGV – hardware design details 533
    Appendix 4 Some basics of Autonomous Guided Vehicles 537
    Appendix 5 PIC® 18 Series instruction set (non-extended) 541
    Appendix 6 Essentials of C 544
    Index 549


    Introduction
    This is a book about embedded systems, introduced primarily through the application of three PIC® microcontrollers.
    Starting from an introductory level, the book aims to make the reader into a competent and
    independent practitioner in the field of embedded systems, to a level whereby he or she has the skills
    necessary to gain entry to professional practice in the embedded world.
    The book achieves its aims by developing the underlying knowledge and skills appropriate to today’s
    embedded systems, in both hardware and software development. On the hardware side, it includes in-depth
    study both of microcontroller design, and of the circuits and transducers to which the microcontroller must
    interface. On the software side, programming in both Assembler and C is covered. This culminates in the
    study and application of a Real Time Operating System, representing the most elegant way that an embedded
    system can be programmed.
    The book is divided into introductory and concluding sections and three main parts, and develops its
    themes primarily around three example PIC microcontrollers, which form the basis of each part. These
    are the 16F84A, the 16F873A and the 18F242. It works through these in turn, using each to develop the
    sophistication of the ideas introduced. Nevertheless, the book should not be viewed just as a manual on PIC
    microcontrollers. Using these as the medium of study, the main issues of embedded design are explored. The
    skills and knowledge acquired through the study of this set of microcontrollers can readily be transferred
    to others.
    Adistinctive feature of the book is its combination of practical and theoretical. The vast majority of topics are
    directly illustrated by practical application, in hardware or in program simulation. Thus, at no point is there
    abstract theory presented without application. The main project in the book is the DerbotAGV (Autonomous
    Guided Vehicle). This is a customisable design, which can be used as a self-contained development platform.
    As an AGV it can be developed into many different forms. It can also be adapted into plenty of other things
    as well, for example a waveform generator, an electronic tape measure or a light meter. Before the Derbot
    is introduced, use is made of a very simple project, the electronic ping-pong game. The example projects
    can be built by the reader, with design information being given on the book CD. Alternatively, projects can
    simply be used as theoretical case studies.
    This book is aimed primarily at second- or third-year undergraduate engineering or technology students.
    It will also be of interest to the informed hobbyist, and parts to the practising professional. Readers are
    expected to have a reasonable knowledge of electronics, equivalent to, say, a first-year undergraduate course.
    This will include an understanding of the operation of transistors and diodes, and simple analog and digital
    electronic subsystems. It is also beneficial to have some knowledge of computer architecture, for example
    gained by an introductory course on microprocessors.
    xxii Introduction
    Because the book moves in three distinct stages from the introductory to the advanced, it will in general
    provide material for more than one course or module. The first six chapters can be used for a short and
    self-contained one-semester course, covering an introduction to microcontrollers and their programming
    in Assembler. The 16F84A is chosen as the example for these chapters. It is an excellent introductory
    microcontroller, due to its simplicity. Chapters 7–11 can form an intermediate course, using Assembler
    to program more complex systems. This leads to a detailed knowledge of microcontroller peripherals and
    their use, as exemplified by the 16F873A. Chapters 12–20 can then be used to form an advanced course,
    working with C and the 18F242, and leading up to use of the RTOS. Alternatively, lecturers may wish to
    ‘pick and choose’ in Chapters 7–20, depending on their preference for C or Assembler, and their preference
    for the microcontroller used. Having worked through Chapters 1–6, it is just possible to go directly to
    Chapter 12, thereby apparently skipping Chapters 7–11. The detail of the middle chapters is missed, but
    this approach can also work. Using C demands less detailed knowledge of the peripherals than is required
    if using Assembler, and cross-reference is made to the middle chapters where it is needed.
    Whatever sequence of reading is chosen, the reader is expected as a minimum to have ready access to the
    Microchip MPLAB® Integrated Development Environment, which is available on the book CD. This allows
    the example programs in the book to be simulated and then modified and developed. Almost inevitably the
    book starts with some study of hardware, so that the reader has a basic knowledge of the system that the
    software will run on. To some extent the first few chapters, on PIC microcontroller architecture, represent a
    steep learning curve for the beginner. The fun then starts in Chapter 4, when programming and simulation
    can begin. From here, with the foundations laid, hardware and software run more or less in parallel, each
    gaining in sophistication and complementing the other. For the final third of the book, the Microchip C18
    C compiler should be used. The student version of this is also available on the book CD. For Chapter 19,
    the ‘Lite’ version of the SalvoTM RTOS can be installed, again from the book CD.
    Beyond program simulation, it is hoped that the reader has access to electronic build and test facilities,
    whether at home, college, university or workplace. With these, it is possible to build up some of the example
    project material or work on equivalent systems. By so doing, the satisfaction of actually implementing real
    embedded systems will be achieved. When working through the middle or later chapters, the best thing a
    lecturer or instructor can do is to get a Derbot printed circuit board into the hands of every student on the
    course, along with a basic set of components. Guide them through initial development and then give them
    suggestions for further customisation. It is wonderful what ideas they then come up with. Design details
    are on the book CD.
    An essential skill of any professional designer in this field is the ability to work with the manufacturer’s data
    sheets. These are the main source of information when designing with microcontrollers and the ultimate
    point of reference in the professional world. It is in general not desirable to work from intermediate drawings
    by a third party, even if these are meant to simplify the information. Therefore, this book unashamedly uses
    (with permission) a large number of diagrams straight from the Microchip data sheets. Many are made
    more accessible by the inclusion of supplementary labelling. The reader is encouraged to download the full
    version of the data sheet in use and to refer directly to it.
    Acomplete knowledge of the field of embedded systems requires both breadth and depth. This is particularly
    true of embedded systems, which combine elements of hardware and software, semiconductor technology,
    analog and digital electronics, computer architecture, sensors and actuators, and more. With its focus on
    Introduction xxiii
    the PIC microcontroller, this book cannot cover all these areas. For the wider contextual background, the
    author’s earlier book, An Introduction to the Design of Small-Scale Embedded Systems, is recommended.
    With whole chapters on memory technology, power supply, numerical algorithms, interfacing to transducers
    and the design process, it provides a ready complement to this book.
    I hope that you enjoy working through this book. In particular I hope you go on to enjoy the challenge and
    pleasure of designing and building embedded systems.
    Tim Wilmshurst
    University of Derby, UK
     
Đang tải...
Chủ đề tương tự
  1. Thúy Viết Bài
    Trả lời:
    0
    Xem:
    446