You are on page 1of 638
Embedded Systems Building Blocks, Second Edition Complete and Ready-to-Use Modules in C Jean J. Labrosse R&D Books Lawrence, KS 66046 R&D Books 1601 West 23rd Street, Suite 200 Lawrence, Kansas 66046 USA Designations used by companies to distinguish their products are often claimed as trademarks. In all instances where R&D Books is aware of a trademark claim, the product name appears in initial capital letters, in all capital leters, or in accordsce with the vendor's capitalization preference, Readers should contact the appropriate companies for more complete information on trademarks and trademark regis- trations. All trademarks and rogistered trademarks in this book are the property of their respective hold- Copyright © 2000 by Miller Freeman, Inc., except where noted otherwise. Published by R&D Books, ‘an imprint of Miller Freeman, Inc. Al rights seserved. Printed in the United States of America. No part of this publication may be reproduced or distributed in any form or by any means, or stored in a data ‘base or retrieval system, without the prior writen permission of the publisher; with the exception that the program listings may be entered, stored, ard executed in a computer system, but they may not be reproduced for publication, ‘The programs in this book are presented for insuctional value, The programs have been carefully tested, but they are not guaranteed for any particuler purpose. The publisher does not offer any warran- ties and does not guarantee the accuracy, adequacy, or completeness of any information herein and is not responsible for any errors or omissions. The publisher assumes no liablilty for damages resulting, from the use of the information in this book or for any infringement of the intellectual property rights of third parties which would result from the use of this information, Cover art created by: Robert Ward. Distributed in the U.S. and Canada by: Publishers Group West 1700 Fourth Street Berkeley, CA 94710 1-800-788-3123 ISBN 0-87930-604-1 Uf Miller Freeman A United News & Media publication To my loving and caring wife and best friend, Manon, and to our two lovely children, James and Sabrina. Table of Contents Preface .........2.655- seteeetetsveeeeeee What's new in the Second Edition? .... Goals Intended Audience . Portability... : ‘What Will You Need to Use this Book?... Acknowledgments... introduction. . Figure, Listing, and Table Conventions. Source Code Conventions Chapter Contents.. Web Site.. Bibliography Chapter 1 Sample Code .. 1.00 Installing Embedded Systems Building Blocks. 1.01 How Each Chapter Is Organized... 1.02 INCLUDES. H, 1.03 Campiter independent Data Types 1.04 CrG.C and CFG.H. 1.05 Global Variables 1.06 0S_ENTER CRITICAL () and OS_EKIT_CRITICAL () . 1.07 ESBB Sample Code... 1.08 Bibtiograpiy .. vi — Embedded Systems Building Blocks, Second Edition Chapter 2 Real-Time Systems Concepts. 2.00 Foreground/Background Systems. 2.01 Critical Section of Code.. 2.02 ReSOUFCE sernnnnn 2.08 Shared Resource. 2.04 Multitasking 63 2.05 Task, 63 2.06 Context Switch (or Task Switch) . 2.07 Kernel 2.08 Scheduler.. 2.09 Non-Preemptive Kernel 2.10 Preemptive Kernel 2.11 Reentraney 2.12 Round-Robin Scheduling. 2.13 Task Priority... 2.14 Static Priorities. 2.15 Dynamic Priorities 2.16 Priority Inversio 2.17 Assigning Task Priorities... 2.18 Mutual Exclusion... 2.19 Deadlock (or Deadly Embrace) .. 2.20 Synchronization 2.21 Bvent Flags. 2.22 Intertask Communication. 2.23 Message Mailboxes. 2.24 Message Queues. 2.25 Interrupts 2.26 Interrupt Latency... 2.27 Intercupt Response 2.28 Interrupt Recovery ... . 2.29 Interrupt Latency, Response, and Recovery 2.30 ISR Processing Time... 2.31 Nonmaskable Interrupts (NMIs) 232. Clock Tick... 2.33 Memory Requirements. 2.34 Advantages and Disadvantages of Real-Time Kernels 2.35 Real-Time Systems Summary... 2.36 Bibliography ... Chapter 3 Chapter 4 Chapter 5 Table of Contents — vit 101 101 Keyboards .. 3.00 Keyboard Basics 3.01 Matrix Keyboard Scanning Algorithm 3.02 Matrix Keyboard Module 3.03 Internals... 3.04 Interface Functions, ReyFlush () . KeyGetkey() KeyGetKeyDownTime ().. KeyHit().. KeyInit {). 3.05 Configuration ... 3.06 How to Use the Matrix Keyboard Module. 3.07 Bibliography . Multiplexed LED Displays. 4,00 LED Displays. 4.01 Multiplexed LED Display Module 4,02 Internals.. 4,03 Interface Function: DispClrSer () DispInit {} DispStatClr() DispStatSet () Dispstr () 4.04 Configuration 4.05 How to Use the Multiplexed LED Display Module: 4.06 Bibliography .. Character LCD Modules 5.00 Liquid Crystal Displays 5.01 Character LCD Modules. 5.02 Character LCD Module, Internals, 5.03 Interface Functions. DispChar () DispClrLine() DispClrScr ().. DispDefChar () DispHorBar ().. DispHorBarInit () Chapter 6 Chapter 7 Embedded Systems Building Blocks, Second Edition DispInit () 176 Dispstr ( 5.04 LCD Module Display, Configuration 178 5.05 LCD Module Manufacturers........ 178 Time-of-Day Clock 191 6.00 Clocks/Calendars .. 191 6.01 Clock/Calendar Module 192 6.02 Internals. 192 6.03 Interface Functions. 195, - 196 198 ClkFormatDate () ClkFormatTime () ClkFormats () 199 Clket Ts () .. 200 clkinit (). 201 ClkMakeTS () 202 ClksetDate ( 203 ClksetDateTime() . ClkSetTime( 205 6.04 Clock/Calendar Module, Configuration 6.05 Bibliography .. . 229 Timer Manager. 7.00 Timer Manager Module 7.01 Timer Manager Moduler, Internals. 7.02 Timer Manager Module, Interface Function: TmeCégPnct (} 24 ‘TrChk () : 236 ‘TmrFormat () ... 237 ‘TorInit (). ‘TmrReset () .. ‘TmrSetMST () ‘TmrsetT( ‘Tmrstart() 230 233 242 ‘tmrStop |)... 243 7.08 Timer Manager Module, Configuration 244 1.04 Bibliography onsen on 244 Chapter 8 Chapter 9 Chapter 10 Table of Contents —ix Discrete I/Os..... 8.00 Discrete Inputs 8.01 Discrete Outputs... 8.02 Discrete /O Module 8.03 Discrete YO Module, Internals 8.04 Discrete /O Module, Interface Functions .. DICEgEdgeDetectFnct () .. DICEgMode() DICLr() Diet (). DIOInit (). DISetBypass () DISetBypassEn () . DOCEgBLink() DOCEgMode () DoGet () .. poset (} DOSet Bypass () DOSetBypassEn() DOSetSyncCtrMax() 8.05 Configuration.. 8.06 How to Use the Discrete /O Module... Fixed-Point Math... 9.06 Fixed-Point Numbers. 9.01 Fixed-Point Addition and Subtraction 9.02 Fixed-Point Mult 9.04 Fixed-Point Comparison 9.05 Using Fixed-Point Arithmetic, Example #1 9.06 Using Fixed-Point Arithmetic, Example #2. 9.07 Using Fixed-Point Arithmetic, Example #3. 9.08 Conclusion... — 9.09 Bibliography Analog I/Os... 10.00 Analog Inputs.. 10.01 Reading an ADC. 10.02 Temperature Measurement Example 10.03 Analog Outputs. 10.04 Temperature Display Example X— Embedded Systems Building Blocks, Second Edition 10.05 Analog /O Module.. 10.06 Internals. 10.07 Interface Functions... AICEgCal () AICEgConv() AICfgScal ing ().. UGE () eee AtoInit() ATSetBypass) AISetBypassEn () AOC£gCal () ... AOCEgConv() aoctgscaling().. noset () AOSetBypass () AOSetBypassEn () 10.08 Analog /O Module, Configuration. 10.09 How to Use the Analog I/O Module. 10.10 Bibliography ... Chapter 11 Asynchronous Serial Communications. 11.00 Asynchronous Communications 11.01 RS-232C. 11.02 RS-485 11.03 Sending and Receiving Duta. 11.04 Serial Ports on a PC 11.05 Low-Level PC Serial 1/0 Module (Cot, 20) ConmcfgPort (). : CommexF Lush () CommRxintDis ( CommxInt En () CommPxIntDis () ConmiPxIntEn () Conmsetintvect () .. CommReLintVect () 11.06 Buffered Serial /O Module (COMMBGND).. ConeietChar () Conminit |) .. CommisEmoty () CommisFull () CommbutChar(). Table of Contents — ai 11.07 Buffered Serial VO Module (COMMRTOS) ....... 442 . CommgetChar () 445 Comminit () .. 447 CommisEmpty () CommIsFul () . CommPutChar () 448, 449. 450 11.08 Configuration 452 11.09 How to.use the COMM_PC and the COMMBGND Module... 452 11.10 How to use the COMM,_PC and the COMMRTOS Module 1 453 11.11 Bibliography 455 Chapter 12 PC Services. emer 495, 12.00 Character Based Displas 12.01 Saving and Restoring DOS's Context. 12.02 Elapsed Time Measurement. 12.03 Miscellaneous 12.04 Interface Functions. PC_Dispchar () PC_DispClrcol ( PC_DispClrRow( PC_DispClrSer() PC_DispStr(). PC_DOSReturn ().. PC_DOSSaveReturn() PC_Elapsedinit () PC_Elapsedstart () PC_Elapseastop() PC_GetDateTime() PC_Getkey () PC_SetTickRat FC_VectGet (}.. PC_VectSet ( 12.05 Bibliography Appendix A. uC/OS-II, The Real-Time Kernel 495 498, 500 500 - 501 - 502 503 504 50S 506 - 508 509 510 Su 513 514. 515 516 517 518 519 535 ostnit ()... 537 ossemCreate (). 538 OsSemPend () 539) oOsSemPost () 541

You might also like