收藏 分享(赏)

8位单片机C语言编程:基于PIC16.pdf

上传人:刘岱文 文档编号:30034 上传时间:2018-06-29 格式:PDF 页数:278 大小:3.80MB
下载 相关 举报
8位单片机C语言编程:基于PIC16.pdf_第1页
第1页 / 共278页
8位单片机C语言编程:基于PIC16.pdf_第2页
第2页 / 共278页
8位单片机C语言编程:基于PIC16.pdf_第3页
第3页 / 共278页
8位单片机C语言编程:基于PIC16.pdf_第4页
第4页 / 共278页
8位单片机C语言编程:基于PIC16.pdf_第5页
第5页 / 共278页
点击查看更多>>
资源描述

1、Foreword Embedded microcontrollers are everywhere today. In the average household you will find them far beyond the obvious places like cell phones, calculators, and MP3 players. Hardly any new appliance arrives in the home without at least one controller and, most likely, there will be severalone m

2、icrocontroller for the user interface (buttons and display), another to control the motor, and perhaps even an overall system manager. This applies whether the appliance in question is a washing machine, garage door opener, curling iron, or toothbrush. If the product uses a rechargeable battery, mod

3、ern high density battery chemistries require intelligent chargers. A decade ago, there were significant barriers to learning how to use microcontrollers. The cheapest programmer was about a hundred dollars and application development required both erasable windowed partswhich cost about ten times th

4、e price of the one time programmable (OTP) versionand a UV Eraser to erase the windowed part. Debugging tools were the realm of professionals alone. Now most microcontrollers use Flash-based program memory that is electrically erasable. This means the device can be reprogrammed in the circuitno UV e

5、raser required and no special packages needed for development. The total cost to get started today is about twenty-five dollars which buys a PICkit 2 Starter Kit, providing programming and debugging for many Microchip Technology Inc. MCUs. Microchip Technology has always offered a free Integrated De

6、velopment Environment (IDE) including an assembler and a simulator. It has never been less expensive to get started with embedded microcontrollers than it is today. While MPLABincludes the assembler for free, assembly code is more cumbersome to write, in the first place, and also more difficult to m

7、aintain. Developing code using C frees the programmer from the details of multi-byte math and paging and generally improves code readability and maintainability. CCS and Hi-Tech both offer free “student” versions of the compiler to get started and even the full versions are relatively inexpensive on

8、ce the savings in development time has been taken into account. For-H8960.indd xiFor-H8960.indd xi 6/10/2008 10:36:25 AM6/10/2008 10:36:25 AMWhile the C language eliminates the need to learn the PIC16 assembly language and frees the user from managing all the details, it is still necessary to unders

9、tand the architecture. Clocking options, peripherals sets, and pin multiplexing issues still need to be solved. Martins book guides readers, step-by-step, on the journey from “this is a micro-controller” to “heres how to complete an application.” Exercises use the fully featured PIC16F877A, covering

10、 the architecture and device configuration. This is a good starting point because other PIC16s are similar in architecture but differ in terms of IO lines, memory, or peripheral sets. An application developed on the PIC16F877A can easily be transferred to a smaller and cheaper midrange PICmicro. The

11、 book also introduces the peripherals and shows how they can simplify the firmware by letting the hardware do the work. M P L A B, Microchips Integrated Development Environment, is also covered. MPLAB includes an editor and a simulator and interfaces with many compilers, including the CCS compiler u

12、sed in this book. Finally, the book includes the Proteussimulator which allows complete system simulation, saving time and money on prototype PCBs. Dan Butler Principal Applications Engineer Microchip Technology Inc. xii ForewordFor-H8960.indd xiiFor-H8960.indd xii 6/10/2008 10:36:25 AM6/10/2008 10:

13、36:25 AMPreface This book is the third in a series, including PIC Microcontrollers: An Introduction to Microelectronic Systems. Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation. Programming 8-bit PIC Microcontrollers in C: With Interactive Hardware Simulation. It completes

14、 a set that introduces embedded application design using the Microchip PICrange, from Microchip Technology Inc. of Arizona. This is the most popular microcontroller for education and training, which is also rapidly gaining ground in the industrial and commercial sectors. Interfacing PIC Microcontrol

15、lers and Programming PIC Microcontrollers present sample applications using the leading design and simulation software for microcontroller based circuits, Proteus VSM from Labcenter Electronics. Demo application files can be downloaded from the authors support Web site (see later for details) and ru

16、n on-screen so that the operation of each program can be studied in detail. The purpose of this book is to Introduce C programming specifically for microcontrollers in easy steps. Demonstrate the use of the Microchip MPLAB IDE for C projects. Provide a beginners guide to the CCS PCM C compiler for 1

17、6 series PICs. Explain how to use Proteus VSM to test C applications in simulated hardware. Describe applications for the Microchip PICDEM mechatronics board. Outline the principles of embedded system design and project development. Pre-H8960.indd xiiiPre-H8960.indd xiii 6/10/2008 10:38:12 AM6/10/20

18、08 10:38:12 AMC is becoming the language of choice for embedded systems, as memory capacity increases in microcontrollers. Microchip supplies the 18 and 24 series chips specifically designed for C programming. However, C can be used in the less complex 16 series PIC, as long as the applications are

19、relatively simple and therefore do not exceed the more limited memory capacity. The PIC 16F877A microcontroller is used as the reference device in this book, as it contains a full range of peripherals and a reasonable memory capacity. It was also used in the previous work on interfacing, so there is

20、 continuity if the book series is taken as a complete course in PIC application development. Microcontrollers are traditionally programmed in assembly language, each type having its own syntax, which translates directly into machine code. Some students, teachers, and hobbyists may wish to skip a det

21、ailed study of assembler coding and go straight to C, which is generally simpler and more powerful. It is therefore timely to produce a text that does not assume detailed knowledge of assembler and introduces C as gently as possible. Although several C programming books for microcontrollers are on t

22、he market, many are too advanced for the C beginner and distract the learner with undesirable detail in the early stages. This text introduces embedded programming techniques using the simplest possible programs, with on-screen, fully interactive circuit simulation to demonstrate a range of basic te

23、chniques, which can then be applied to your own projects. The emphasis is on simple working programs for each topic, with hardware block diagrams to clarify system operation, full circuit schematics, simulation screenshots, and source code listings, as well as working downloads of all examples. Stud

24、ents in college courses and design engineers can document their projects to a high standard using these techniques. Each part concludes with a complete set of self-assessment questions and assignments designed to complete the learning package. An additional feature of this book is the use of Proteus

25、 VSM (virtual system modeling). The schematic capture component, ISIS, allows a circuit diagram to be created using an extensive library of active components. The program is attached to the microcontroller, and the animated schematic allows the application to be comprehensively debugged before downl

26、oading to hardware. This not only saves time for the professional engineer but provides an excellent learning tool for the student or hobbyist. xiv PrefacePre-H8960.indd xivPre-H8960.indd xiv 6/10/2008 10:38:13 AM6/10/2008 10:38:13 AMPreface Links, Resources, and Acknowledgments Microchip Technology

27、 Inc. ( ) Microchip Technology Inc. is a manufacturer of PICmicrocontrollers and associated products. I gratefully acknowledge the support and assistance of Microchip Inc. in the development of this book and the use of the company trademarks and intellectual property. Special thanks are due to John

28、 Roberts of Microchip UK for his assistance and advice. The company Web site contains details of all Microchip hardware, software, and development systems. MPLAB IDE (integrated development system) must be downloaded and installed to develop new applications using the tools described in this book. T

29、he data sheet for the PIC 16F877A microcontroller should also be downloaded as a reference source. PIC, PICmicro, MPLAB, MPASM, PICkit, dsPIC, and PICDEM are trademarks of Microchip Technology Inc. Labcenter Electronics ( www.labcenter.co.uk ) Labcenter Electronics is the developer of Proteus VSM (v

30、irtual system modeling), the most advanced cosimulation system for embedded applications. I gratefully acknowledge the assistance of the Labcenter team, especially John Jameson, in the development of this series of books. A student/evaluation version of the simulation software may be downloaded from

31、 . A special offer for ISIS Lite, ProSPICE Lite, and the 16F877A simulator model can be found at . Proteus VSM, ISIS, and ARES are trademarks of Labcenter Electronics Ltd. Custom Computer Services Inc. ( ) Custom Computer Services Inc. specializes in compilers for PIC microcontrollers. The main r

32、ange comprises PCB compiler for 12-bit PICs, PCM for 16-bit, and PCH for the 18 series chips. The support provided by James Merriman at CCS Inc. is gratefully acknowledged. The manual for the CCS compiler should be downloaded from the company Web site (Version 4 was used for this book). A 30-day tri

33、al version, which will compile code for the 16F877A, is available at the time of writing. Pre-H8960.indd xvPre-H8960.indd xv 6/10/2008 10:38:13 AM6/10/2008 10:38:13 AMxvi PThe Authors Web Site ( www.picmicros.org.uk ) This book is supported by a dedicated Web site, www.picmicros.org.uk. All the appl

34、ication examples in the book may be downloaded free of charge and tested using an evaluation version of Proteus VSM. The design files are locked so that the hardware configuration cannot be changed without purchasing a suitable VSM license. Similarly, the attached program cannot be modified and reco

35、mpiled without a suitable compiler license, available from the CCS Web site. Special manufacturers offers are available via links at my site. This site is hosted by and special thanks are due to Gabe Hudson of LarrytechInternet Services for friendly maintenance and support. I can be contacted at th

36、e e-mail address martinpicmicros.org.uk with any queries or comments related to the PIC book series. Finally, thanks to Julia for doing the boring domestic stuff so I can do the interesting technical stuff. About the AuthorMartin P. Bates is the author of PIC Microcontrollers, Second Edition. He is

37、currently lecturing on electronics and electrical engineering at Hastings College, UK. His interests include microcontroller applications and embedded system design.Pre-H8960.indd xviPre-H8960.indd xvi 6/10/2008 10:38:13 AM6/10/2008 10:38:13 AMIntroduction The book is organized in five parts. Part 1

38、 includes an overview of the PIC microcontroller internal architecture, describing the features of the 16F877A specifically. This chip is often used as representative of the 16 series MCUs because it has a full range of peripheral interfaces. All 16 series chips have a common program execution core,

39、 with variation mainly in the size of program and data memory. During programming, certain operational features are configurable: type of clock circuit, watchdog timer enable, reset mechanisms, and so on. Internal features include the file register system, which contains the control registers and RA

40、M block, and a nonvolatile EEPROM block. The parallel ports provide the default I/O for the MCU, but most pins have more than one function. Eight analog inputs and serial interfaces (UART, SPI, and I 2C) are brought out to specific pins. The hardware features of all these are outlined, so that I/O p

41、rogramming can be more readily understood later on. The application development process is described, using only MPLAB IDE in this initial phase. A sample C program is edited, compiled, downloaded, and tested to demonstrate the basic process and the generated file set analyzed. The debugging feature

42、s of MPLAB are also outlined: run, single step, breakpoints, watch windows, and so on. Disassembly of the object code allows the intermediate assembly language version of the C source program to be analyzed. Part 2 introduces C programming, using the simplest possible programs. Input and output are

43、dealt with immediately, since this is the key feature of embedded programs. Variables, conditional blocks ( IF ), looping ( WHILE,FOR ) are quickly introduced, with a complete example program. Variables and sequence control are considered in a little more detail and functions introduced. This leads

44、on to library functions for operating timers and ports. The keypad and alphanumeric LCD are used in a simple calculator program. More data types (long integers, floating point numbers, arrays, etc.) follow as well as assembler directives and the purpose of the header file. Finally, insertion of asse

45、mbler into C programs is outlined. Itr-H8960.indd xviiItr-H8960.indd xvii 6/10/2008 10:39:06 AM6/10/2008 10:39:06 AMxviii IPart 3 focuses on programming input and output operations using the CCS C library functions. These simplify the programming process, with a small set of functions usually provid

46、ing all the initialization and operating sequences required. Example programs for analog input and the use of interrupts and timers are developed and the serial port functions demonstrated in sample applications. The advantages of each type of serial bus are compared, and examples showing the connec

47、tion of external serial EEPROM for data storage and a digital to analog converter output are provided. These applications can be tested in VSM, but this is not essential; use of VSM is optional throughout the book. Part 4 focuses specifically on the PICDEM mechatronics board from Microchip. This has

48、 been selected as the main demonstration application, as it is relatively inexpensive and contains a range of features that allow the features of a typical mechatronics system to be examined: input sensors (temperature, light, and position) and output actuators (DC and stepper motor). These are tested individually then the requirements of a temperature controller outlined. Operation of the 3.5-digit seven-segment LCD is explained in detail, as this is not covered elsewhere. A simulation vers

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 技术资料 > 技术总结

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报