embedded systems circuits and programming

14
EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING Julio Sanchez Maria P. Canton ( r oC) CRC Press \V / Taylor &. Francis Group Boca Raton London New York CRC Press is an imprint of the Taylor Si Francis Group, an informa business

Upload: others

Post on 24-Oct-2021

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

EMBEDDED SYSTEMS CIRCUITS

and PROGRAMMING

Julio Sanchez Maria P. Canton

( roC) CRC Press \ V / Taylor &. Francis Group

Boca Raton London New York

CRC Press is an imprint of the Taylor Si Francis Group, an informa business

Page 2: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents

Preface xix

Chapter 1 - Real-Time Computing 1 1.0 Defining the Embedded System 1

1.1 Embedded Systems History 2

1.2 Hardware Complexity 2 1.2.1 Processor 3 1.2.2 Microcontrollers 3 1.2.3 Hardware and Software 3

1.3 Execution in Real-Time 4 1.3.1 Hard and Soft Real-Time Systems 5

Chapter 2 - Circuit Fundamentals 7 2.1 Electrical Circuit 7

2.2 Circuit Concepts and Components 8

2.3 Digital Electronics 9

2.4 Diode 10 2.4.1 Light-Emitting Diode (LED) 12

2.5 Transistors 13 2.5.1 Bipolar Transistor 13 2.5.2 MOS Transistor 15

Chapter 3 - Logic Gates and Circuit Components 17 17

18

19 19 20 21 23 23 24 25

26 26

3.1 3.2 3.3

3.4

Logic Gates Power Supplies Clocked Logic and Flip-Flops 3.3.1 RS Flip-Flop 3.3.2 Clocked Circuits 3.3.3 D Flip-Flop 3.3.4 Edge-Triggered D Flip-Flop 3.3.5 Preset and Clear Signals 3.3.6 D Flip-Flop Waveform Action 3.3.7 Flip-Flop Applications

Digital Clocks 3.4.1 Clock Waveforms

V

Page 3: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

VI Embedded Systems: Circuits

3.4.2 TTL Clock 3.4.3 555 Timer 3.4.4 Microcontroller Clocks

3.5 Counters and Frequency Dividers 3.5.1 Frequency Dividers 3.5.2 JK Flip-Flop Counter 3.5.3 Ripple Counters 3.5.4 Decoding Gates 3.5.5 Synchronous Counters 3.5.6 Counter ICs 3.5.7 Shift Registers

3.6 Multiplexers and Demultiplexers 3.6.1 Multiplexers 3.6.2. Demultiplexers 3.6.3 Multiplexer and Demultiplexer ICs

Chapter 4 - Input and Output Devices 4.1 Obtaining Input 4.2 Switches

4.2.1 Switch Contact Bounce 4.2.2 Keypads

4.3 Output Devices 4.3.1 Seven-Segment LED 4.3.2 Liquid Crystal Displays 4.3.3 LCD Technologies

Chapter 5 - From Circuit Schematics to PCB 5.1 Circuit Diagram

5.1.1 Symbols 5.1.2 Tools for Electronic Circuit Design

5.2 Circuit Board Design 5.2.1 Board Design Standards 5.2.2 Gerber File Format

5.3 Developing the Circuit Prototype 5.3.1 Breadboard

Limitations of Breadboards Breadboarding Tools and Techniques

5.3.2 Wire-Wrapping 5.3.3 Perfboards

5.4 Printed Circuit Boards 5.4.1 PCB Layers 5.4.2 PCB Connectors

5.5 Making Your Own PCB 5.5.1 Drawing the СРВ Circuit 5.5.2 Printing the PCB 5.5.3 Transferring the PCB Image 5.5.4 Etching the Board 5.5.5 Finishing the Board 5.5.6 Backside Image

Page 4: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents

5.6 Surface-Mount Components 5.6.1 SMT Adapters 5.6.2 Soldering SMT Components

5.7 Troubleshooting the Circuit Board 5.7.1 Circuit Testing Tools

Chapter 6 - Introducing the Microcontroller 6.1 A Computer on a Chip

6.2 PICMicro Microcontroller 6.2.1 Programming the PIC

Development Boards 6.2.2 Prototyping a PIC Circuit

6.3 PIC Architecture 6.3.1 Baseline PIC Family

PIC10 Devices PIC12 Devices PIC14 Devices

6.3.2 Mid-Range PIC Family PIC16 Devices

6.3.3 High-Performance PIC Family PIC18 Devices

Chapter 7 - Architecture and Instruction Set 7.1 Mid-Range PIC Architecture

7.1.1 Harvard Architecture 7.1.2 CISC versus RISC 7.1.3 Single-Word Instructions 7.1.4 Instruction Format 7.1.5 Mid-Range Device Versions 7.1.6 Arithmetic-Logic Unit

7.2 Data Memory Organization 7.2.1 w Register 7.2.2 Data Registers

Memory Banks SFRs GPRs

7.2.3 Indirect Addressing

7.3 Mid-Range I/O and Peripherals 7.3.1 Ports 7.3.2 Timers 7.3.3 Capture and Compare Module 7.3.4 Master Synchronous Serial Port 7.3.5 USART Module 7.3.6 A/D Module

7.4 Mid-Range PIC Core Features 7.4.1 Oscillator 7.4.2 System Reset 7.4.3 Interrupts

7.5 Mid-Range Instruction Set

Page 5: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Vll l Embedded Systems: Circuits and Programming

102 7.5.1 STATUS and OPTION Registers 102

7.6 EEPROM Data Storage 104 7.6.1 EEPROM in Mid-Range PICs 105

Chapter 8 - Embedded Systems Programming 107 8.1 Assembly versus High-Level Languages 107

8.1.2 Embedded Systems 107

8.2 Integrated Development Environment 108 8.2.1 Installing MPLAB 109 8.2.2 MPLAB Project 111 8.2.3 Project Build Options 112 8.2.4 Building the Project 114 8.2.5 Quickbuild Option 114

8.3 Simulators and Debuggers 115 8.3.1 MPLAB SIM 115 8.3.2 MPLAB Hardware Debuggers 117 8.3.3 Improvised Debugger 119

8.4 Programmers 119

8.5 Engineering PIC Software 120 8.5.1 Using Program Comments 120

Program Header 121 Commented Banners 122 Commented Bitmaps 123

8.5.2 Defining Data Elements 123 cblock Directive 124

8.5.3 Banking Techniques 124 banksel Directive 125 Bank Selection Macros 125 Deprecated Banking Instructions 126

8.5.4 Processor and Configuration Controls 126 Configuration Bits 127

8.5.5 Naming Conventions 128 8.5.6 Errorlevel Directive 131

8.6 Pseudo Instructions 131

Chapter 9 - I/O Circuits and Programs 133 9.1 Simple Input and Output 133

9.1.1 16F84A Programming Template 133

9.2 Template Circuits 134 9.2.1 MCLR and Oscillator Template 135 9.2.2 Power Supplies 135

Voltage Regulator 136

9.3 Simple Circuits and Programs 136 9.3.1 Single LED Circuit 137

LED Flasher Program 139 9.3.2 LED/Pushbutton Circuit 141 9.3.3 Multiple LED Circuit 143

9.4 Seven-Segment LED 146

Page 6: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents

9.5 I/O Demo Board 9.5.1 TestDemol Program

9.6 Comparisons in PIC Programming 9.6.1 PIC Carry Flag

Chapter 10 - PIC Interrupt System 10.1 Interrupts

10.1.1 16F84 Interrupts 10.1.2 Interrupt Control Register 10.1.3 OPTION Register

10.2 Interrupt Sources 10.2.1 Port В External Interrupt 10.2.2 TimerO Interrupt 10.2.3 Port В Line Change Interrupt

Multiple External Interrupts 10.2.4 EEPROM Data Write Interrupt

10.3 Developing the Interrupt Handler 10.3.1 Context Saving Operations

Saving W and STATUS Registers 10.4 Interrupt Programming

10.4.1 Programming the External Interrupt RBO Interrupt Initialization RBO ISR

10.4.2 Wake-Up from SLEEP Using the RBO Interrupt SleepDemo Program

10.4.3 Port В Bits 4-7 Status Change Interrupt RB4-7 Interrupt Initialization RB4-7 Change ISR

10.5 Sample Programs 10.6 Demonstration Programs

10.6.1 RBOInt Program 10.6.2 SleepDemo Program 10.6.3 RB4to7lnt Program

Chapter 11 - Timers and Counters 11.1 Controlling the Time Lapse

11.1.1 16F84 TimerO Module 11.1.2 TimerO Operation

TimerO Interrupt TimerO Prescaler

11.2 Delays Using TimerO 11.2.1 Long Delay Loops

How Accurate Is the Delay? 11.3 TimerO as a Counter 11.4 TimerO Programming

11.4.1 Programming a Counter Timer/Counter Test Circuit TimerCounter Program Code Details

Page 7: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

X Embedded Systems: Circuits and Programming

11.4.2 TimerO as a Delay Timer 192 Delay Timer Circuit 192

11.4.3 DelayTimer Program 193 Code Details 193

11.4.4 Variable Time Lapse 194 11.4.5 Variable Lapse Timer Program 195

Code Details 195 11.4.6 Interrupt-Driven Timer 198 11.4.7 Timerlnt Program 198

Code Details 198

11.5 Watchdog Timer 201 11.5.1 Watchdog Timer Programming 202

11.6 Demonstration Programs 202 11.6.1 TmrOCounter program 202 11.6.2 TimerO Program 205 11.6.3 LapseTimer Program 207 11.6.4 LapseTmrlnt Program 211

Chapter 12 - LCD Hardware and Programming 217 12.1 Liquid Crystal Display 217

12.1.1 LCD Features and Architecture 217 12.1.2 LCD Functions and Components 218

Internal Registers 218 Busy Flag 218 Address Counter 218 Display Data RAM (DDRAM) 218 Character Generator ROM (CGROM) 218 Character Generator RAM (CGRAM) 219 Timing Generation Circuit 219 Liquid Crystal Display Driver Circuit 220 Cursor/Blink Control Circuit 220

12.1.3 Connectivity and Pin-Out 220

12.2 Interfacing with the HD44780 221 12.2.1 Busy Flag or Timed Delay Options 222 12.2.2 Contrast Control 223 12.2.3 Display Backlight 223 12.2.4 Display Memory Mapping 223

12.3 HD44780 Instruction Set 225 12.3.1 Instruction Set Overview 225

Clearing the Display 225 Return Home 226 Entry Mode Set 226 Display and Cursor ON/OFF 226 Cursor/Display Shift 226 Function Set 227 Set CGRAM Address 227 Set DDRAM Address 227 Read Busy Flag and Address Register 227 Write Data 227 Read Data 228

12.3.2 A 16F84 8-Bit Data Mode Circuit 228

Page 8: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents xi

12.4 LCD Programming 229 12.4.1 Defining Constants and Variables 229

Using MPLAB Data Directives 231 12.4.2 LCD Initialization 232

Function Set Command 232 Display Off 233 Display and Cursor On 233 Set Entry Mode 234 Cursor and Display Shift 234 Clear Display 235

12.4.3 Auxiliary Operations 235 Time Delay Routine 235 Pulsing the E Line 237 Reading the Busy Flag 237 Bit Merging Operations 238

12.4.4 Text Data Storage and Display 240 Generating and Storing a Text String 241 Displaying the Text String 243

12.4.5 Data Compression Techniques 244 4-Bit Data Transfer Mode 244 Master/Slave Systems 246

12.5 Sample Programs 248

Chapter 13 - Analog-to-Digital and Real-Time Clocks 251 13.1 Clocks and the Digital Revolution 251

13.2 A/D Converters 252 13.2.1 Converter Resolution 252 13.2.2 ADC Implementation 253

13.3 A/D Integrated Circuits 254 13.3.1 ADC0331 Sample Circuit and Program 255

13.4 PIC Onboard A/D Hardware 257 13.4.1 A/D Module on the 16F87x 257

ADCON0 Register 258 ADCON1 Register 261 SLEEP Mode Operation 262

13.4.2 A/D Module Sample Circuit and Program 262

13.5 Real-Time Clocks 266 13.5.1 NJU6355 Real-Time Clock 266 13.5.2 RTC Demonstration Circuit and Program 268

BCD Conversion Procedures 273

13.6 Demonstration Programs 276 13.6.1 ADF84 Program 276 13.6.2 A2DinLCD Program 288 13.6.3 RTC2LCD Program 303

Chapter 14 - Data EEPROM 321 14.1 EEPROM Programming 321

14.1.1 Data EEPROM 321

14.2 EEPROM Programming 322

Page 9: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

xii Embedded Systems: Circuits and Programming

14.2.1 Reading EEPROM Data 323 14.2.2 EEPROM Data Memory Write 323

14.3 EEPROM Programming Application 324 14.3.1 EECounter Program 324

Code Details 324

14.4 Demonstration Programs 329 14.4.1 EECounter Program 329 14.4.2 Ser2EEP Program 341 14.4.3 I2CEEP Program 358

Chapter 15-Stepper Motors 379 15.1 Description and Operation 379

15.1.1 Stepper Motor Types 381 Variable Reluctance 381 Permanent Magnet 381 Hybrid 381

15.1.2 Unipolar Stepper Motors 382 15.1.3 Determining Unipolar and Bipolar Wiring 383

Four-Wire Motor 383 Six-Wire Unipolar Motor 383 Five-Wire Unipolar 383

15.1.4 Bipolar Stepper Motors 384

15.2 Stepper Motor Controls 384 15.2.1 Stepping Modes 385

Wave Drive Mode 385 Full Step Mode 386 Half Step Mode 387 Microstepping 387

Chapter 16 - Stepper Motor Circuit Components 389 16.1 Circuit Elements 389

16.1.1 Input, Output, and Feedback 390

16.2 Translator 390 16.2.1 PIC Microcontroller as a Translator 390

16.3 Translator/Drivers 391 16.3.1 UCN5804 391 16.3.2 L297 392 16.3.3 EDE1204 394 16.3.4 SLA7060 and SLA7024 394

16.4 Power Driver 395 16.4.1 Unipolar Drivers 395

PIC Microcontroller as a Driver 395 ULN2803A 395 TIP 120 396

16.4.2 Bipolar Drivers 397 16.4.3 Transistorized H Bridge 397

Snubber Diodes 398 16.4.4 H Bridge ICs 399

L293D 399 L298 400

Page 10: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents

16.5 Modules in Circuit Schematics 16.5.1 Example 16F84 Translator Modules

Chapter 17 - Unipolar Motor Circuits and Programs 17.1 Stepper Motor Control Circuits

17.1.1 Stepper Motor Circuit Schematic Conventions

17.2 Motor Speed Control 17.2.1 Speed Control from Digital Input 17.2.2 Analog Input Speed Control

17.3 Unipolar Motor Control Circuits 17.3.1 Matching Circuit to Motor Power 17.3.2 16F84 Unipolar Circuit

Sample Program SMU_PIC16F84.asm 17.3.3 5804 Unipolar Circuit

Sample Program SMU_5804.asm Generating the Motor Pulses Interrupt-Driven Motor Pulsing

17.3.4 16F686 PIC Circuit 17.3.5 16F686 Programming

Sample Program SMU„PIC16F684.asm Sample Program SMU_PIC16F684_INT.asm

17.3.6 Stepper Motor Position Control Sample Program SMU_POSITION.asm

17.4 Demonstration Programs 17.4.1 SMB_297_293D.asm 17.4.2 SMU_PIC16F84.asm Program 17.4.3 SMU_5804.asm 17.4.4 SMU_5804JNT.asm 17.4.5 SMU_PIC16F684.asm 17.4.6 SMU_PIC16F684_INT.asm 17.4.7 SMU_POSITION.asm

Chapter 18 - Constant-Voltage Bipolar Motor Controls 18.1 Unipolar versus Bipolar

18.1.1 Bipolar Drive Circuits

18.2 Simple, L293 Bipolar Circuit 18.2.1 L297- and L293-Based Circuit 18.2.2 Minimal L297- and L298-based Circuit

18.3 Demonstration Programs 18.3.1 SMB_L293D.asm 18.3.2 SMB_297_293D.asm 18.3.3 SMB_297_298.asm

Chapter 19 - Advanced Motor Controls 19.1 Choppers and Microstepping

19.2 Chopper Circuit Fundamentals

19.3 L297/298 Chopper Circuit 19.3.1 Setting the Reference Voltage

Page 11: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

xiv Embedded Systems: Circuits and Programming

19.4 A Chopper-Based Demo Board 492 19.4.1 Motor Circuit Power Requirements 492 19.4.2 Chopper Demo Program 494

19.5 Microstepping 494 19.5.1 Microstepping Fundamentals 497

Microstepping Theory 497 Pulse Width Modulation (PWM) 499

19.6 Programming PWM 500 19.6.1 CCP Module 500 19.6.2 PWM Circuit and Software 502 19.6.3 Microstepping by PWM 505 19.6.4 Microstepping Sample Program 506

19.7 Microstepping ICs 508 19.7.1 Allegro 3955 1С 508 19.7.2 3955-Based Circuit 510 19.7.3 3955 Motor Driver Program 512

19.8 Demonstration Programs 514 19.8.1 PWM_DEMO_873.asm 514 19.8.2 PIC_Chopper.asm 518 19.8.3 PWM_Micstep.asm 522 19.8.4 PIC873_3955.asm 530

Chapter 20 - Communications 543 20.1 PIC Communications Overview 20.2 Serial Data Transmission

20.2.1 Asynchronous Serial Transmission 20.2.2 Synchronous Serial Transmission 20.2.3 PIC Serial Communications 20.2.4 RS-232-C Standard

Essential Concepts Serial Bit Stream Parity Testing Connectors and Wiring Null Modem Null Modem Cable

20.2.5 EIA-485 Standard EIA-485 in PIC-based Systems

20.3 Parallel Data Transmission 20.3.1 PIC Parallel Slave Port (PSP)

20.4 PIC "Free-Style" Serial Programming 20.4.1 PIC-to-PIC Serial Communications

PIC-to-PIC Serial Communications Circuits PIC-to-PIC Serial Communications Programs

20.4.2 Program Using Shift Register ICs 74HC165 Parallel-to-Serial Shift Register 74HC164 Serial-to-Parallel Shift Register

20.5 PIC Protocol-Based Serial Programming 20.5.1 RS-232-C Communications on the 16F84

RS-232-C Transceiver 1С PIC-to-PC Communications

543 544 544 546 546 547 548 549 549 550 550 551 553 554 554 555 555 556 556 558 564 565 568 570 570 571 572

Page 12: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents

RS-232-C TTY Board 16F84A UART Emulation LCD Scrolling Routine

20.5.2 RS-232-C Communications on the 16F87x 16F87x USART Module USART Baud Rate Generator 16F87x USART Asynchronous Transmitter 16F87x USART Asynchronous Receiver PIC-to-PC RS-232-C Communications Circuit 16F877 PIC Initialization Code USART Receive and Transmit Routines USART Receive Interrupt

20.6 Demonstration Programs 20.6.1 SerialSnd Program 20.6.2 SeriaIRcv Program 20.6.3 Serial6465 Program 20.6.4 TTYUsart Program 20.6.5 SerComLCD Program 20.6.6 SerlntLCD Program

Appendix A - Resistor Color Codes

Appendix В - Essential Electronics B.1 Atom B.2 Isotopes and Ions B.3 Static Electricity В.4 Electrical Charge

B.4.1 Voltage B.4.2 Current B.4.3 Power B.4.4 Ohm's Law

B.5 Electrical Circuits B.5.1 Types of Circuits

B.6 Circuit Elements B.6.1 Resistors B.6.2 Revisiting Ohm's Law B.6.3 Resistors in Series and Parallel B.6.4 Capacitors B.6.5 Capacitors in Series and in Parallel B.6.6 Inductors B.6.7 Transformers

B.7 Semiconductors B.7.1 Integrated Circuits B.7.2 Semiconductor Electronics B.7.3 P-Type and N-Type Silicon B.7.4 Diode

Page 13: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

xvi Embedded Systems: Circuits and Programming

Appendix С - Numeric Data 683 C.1 Numbers in Computing 683

C.1.1 Counting 683 01 .2 Tally System 683 C.1.3 Roman Numerals 684

C.2 Origins of the Decimal System 684 C.2.1 Number Systems for Digital-Electronics 686 C.2.2 Positional Characteristics 686 C.2.3 Radix or Base of a Number System 687

C.3 Types of Numbers 687 C.3.1 Whole Numbers 688 C.3.2 Signed Numbers 688 C.3.3 Rational, Irrational, and Imaginary Numbers 688

C.4 Radix Representations 689 C.4.1 Decimal versus Binary Numbers 689 C.4.2 Hexadecimal and Octal 690

C.5 Number System Conversions 691 C.5.1 Binary-to-ASCN-Decimal 692 C.5.2 Binary-to-Hexadecimal Conversion 693 C.5.3 Decimal-to-Binary Conversion 693

Appendix D - Character Data 697 D.1 Character Representations 697

D.1.1 Electronic-Digital Machines 697 D.2 Character Representations 697

D.2.1 ASCII 698 D.2.2 EBCDIC and IBM 700 D.2.3 Unicode 700

D.3 Storage and Encoding of Integers 701 D.3.1 Signed and Unsigned Representations 701 D.3.2 Word Size 702 D.3.3 Byte Ordering 703 D.4.4 Sign-Magnitude Representation 704 D.3.5 Radix Complement Representation 705

D.4 Encoding of Fractional Numbers 708 D.4.1 Fixed-Point Representations 709 D.4.2 Floating-Point Representations 710 D.4.3 Standardized Floating-Point Representations 711 D.4.4 IEEE 754 Single Format 712 D.4.5 Encoding and Decoding Floating-Point Numbers 714

D.5 Binary-Coded Decimals (BCD) 715 D.5.1 Floating-Point BCD 716

Appendix E - Digital Arithmetic and Conversions 719 E.1 Microcontroller Arithmetic 719 E.2 Unsigned and Two's Complement Arithmetic 719

E.2.1 Operations on Decimal Numbers 721 E.3 Bit Manipulations and Auxiliary Operations 723

Page 14: EMBEDDED SYSTEMS CIRCUITS and PROGRAMMING

Table of Contents xv i i

E.3.1 Bit Shift and Rotate 723 E.3.2 Comparison Operations 724 E.3.3 Other Support Operations 724

E.4 Unsigned Binary Arithmetic 725 E.4.1 Multi-Byte Unsigned Addition 725 E.4.2 Unsigned Multiplication 726 E.4.3 Unsigned Division 728

E.5 Signed Binary Arithmetic 729 E.5.1 Overflow Detection in Signed Arithmetic 730 E.5.2 Sign Extension Operations 732 E.5.3 Multi-Byte Signed Operations 732

E.6 Data Format Conversions 733 E.6.1 BCD Digits to ASCII Decimal 733 E.6.2 Unsigned Binary to ASCII Decimal Digits 734 E.6.3 ASCII Decimal String to Unsigned Binary 734 E.6.4 Unsigned Binary to ASCII Hexadecimal Digits 736 E.6.5 Signed Numerical Conversions 736

Appendix F - Mid-Range Instruction Set 739

Appendix G - Printed Circuit Boards 777 G.1 Introduction 777

G.2 Printed Circuit Boards (PCBs) 777

G.3 Parts Lists 778

G.4 Building Your Own Circuit Boards 779 G.4.1 Tools and Materials 779 G.4.2 Single-Sided Demo Board 780 G.4.3 PCB Images for Demo Board 780

Drawing the Circuit Diagram 782 Printing the PCB Diagram 783 Selecting the Paper 783 Transferring the PCB Image 784 Etching the Board 784 Finishing the Board 784 Component-Side Image 784

G.5 Caveats 785

Appendix H - Additional Code 787

Index 863