Dự án PIC Microcontroller nâng cao với C: Từ USB đến RTOS sử dụng dòng PIC18F

Người đăng

Ẩn danh

Thể loại

Sách tham khảo

2008

561
0
0

Phí lưu trữ

135 Point

Tóm tắt

I. Tổng quan dự án vi điều khiển PIC nâng cao từ USB đến RTOS với PIC18F

Dự án vi điều khiển PIC nâng cao trong C từ USB đến RTOS với PIC18F đề cập đến việc xây dựng các ứng dụng phức tạp sử dụng dòng vi khiển PIC18F của Microchip. Các dự án này khai thác sức mạnh của ngôn ngữ lập trình C để phát triển firmware hiệu quả. Chúng bao gồm tích hợp giao tiếp USB tốc độ cao, quản lý thời gian thực với RTOS, và tận dụng các ngoại vi tích hợp. Dòng PIC18F cung cấp kiến trúc Harvard 8-bit mạnh mẽ, bộ nhớ flash lớn, và nhiều module phần cứng. Việc nắm vững nền tảng vi tính, hệ thống số, và kiến trúc PIC là bước đầu tiên. Tài liệu gốc trình bày chi tiết về các khái niệm này, từ hệ thập phân đến số dấu phẩy động. Các dự án này hướng đến ứng dụng thực tế trong điều khiển công nghiệp, thiết bị y tế và hệ thống nhúng tiêu dùng.

1.1. Kiến trúc và tính năng chính của dòng PIC18F

Dòng PIC18F sử dụng kiến trúc Harvard với bus chương trình và dữ liệu riêng biệt. Bộ nhớ chương trình dạng flash cho phép lập trình lại dễ dàng. PIC18FXX2 có các tính năng nổi bật như bộ phát hiện sụt áp (Brown-out Detector), bộ chuyển đổi ADC, giao tiếp nối tiếp (UART, SPI, I2C), và bộ nhớ EEPROM dữ liệu. Nó hỗ trợ ngắt đa cấp độ ưu tiên và chế độ hoạt động công suất thấp. Các thanh ghi đặc biệt điều khiển trực tiếp các hoạt động phần cứng. Hiểu rõ kiến trúc là cơ sở để viết mã C hiệu quả và khai thác tối đa tài nguyên vi điều khiển.

1.2. Nền tảng lập trình C và hệ thống số cho PIC18F

Ngôn ngữ C là lựa chọn hàng đầu cho lập trình PIC18F nhờ tính di động và hiệu quả. Nền tảng vững chắc về hệ thống số (thập phân, nhị phân, thập lục phân, bát phân) là bắt buộc. Việc chuyển đổi giữa các hệ số và hiểu phép toán nhị phân là then chốt. Kiến thức về số dấu phẩy động (floating point) theo chuẩn IEEE 754 cũng quan trọng cho các phép tính phức tạp. Trình biên dịch C cho PIC chuyển đổi mã nguồn sang ngôn ngữ máy PIC. Công cụ gỡ lỗi và mô phỏng giúp kiểm tra chương trình trước khi nạp vào phần cứng thực.

II. Phân tích thách thức khi phát triển dự án PIC18F nâng cao

Phát triển dự án PIC18F nâng cao đặt ra nhiều thách thức kỹ thuật. Quản lý bộ nhớ hiệu quả là mối quan tâm hàng đầu do tài nguyên hạn chế. Thiết kế phần cứng đáng yêu cầu kiến thức sâu về mạch điện và phần cứng ngoại vi. Xử lý thời gian thực chính xác, đặc biệt khi tích hợp RTOS, đòi hỏi kiến trúc phần mềm cẩn thận. Đảm bảo độ tin cậy và thời gian chạy liên tục là yếu tố sống còn trong ứng dụng công nghiệp. Tích hợp giao tiếp USB phức tạp hơn so với giao tiếp nối tiếp truyền thống. Việc gỡ lỗi các lỗi phần cứng-tích hợp phần mềm cũng rất nan giải. Đội ngũ phát triển cần có kiến thức liên ngành và kỹ năng phân tích hệ thống tốt.

2.1. Hạn chế tài nguyên và quản lý bộ nhớ trên PIC18F

PIC18F có bộ nhớ chương trình, dữ liệu và ngăn xếp hữu hạn. Quản lý bộ nhớ kém dẫn đến tràn bộ đệm, lỗi truy cập và chương trình hoạt động không ổn định. Mã C phải được tối ưu hóa để sử dụng ít tài nguyên nhất. Cần cân nhắc kỹ khi cấp phát bộ nhớ động và sử dụng các biến toàn cầu. Sử dụng hiệu quả EEPROM để lưu trữ dữ liệu không bay hơi. Việc lập kế hoạch bố trí bộ nhớ từ giai đoạn thiết kế là rất quan trọng. Công cụ phân tích bộ nhớ của trình biên dịch giúp đánh giá mức sử dụng tài nguyên.

2.2. Tích hợp và đồng bộ phần cứng phần mềm phức tạp

Các dự án nâng cao thường kết hợp nhiều module phần cứng: USB, ADC, timer, ngắt. Đồng bộ hóa hoạt động của chúng với mã C là một thách thức. Ví dụ, giao tiếp USB yêu cầu tuân thủ nghiêm ngặt các giao thức và thời gian. Xử lý ngắt phải nhanh và chính xác để không bỏ sót sự kiện. Sử dụng RTOS giúp quản lý đa nhiệm nhưng làm tăng độ phức tạp của phần mềm. Kiểm thử tích hợp toàn diện là cần thiết để đảm bảo mọi thành phần hoạt động hài hòa. Mô phỏng phần cứng và phần mềm có thể giảm thiểu rủi ro trước khi triển khai thực tế.

III. Giải pháp và phương pháp triển khai USB và RTOS trên PIC18F

Để triển khai thành công, cần áp dụng các phương pháp và giải pháp đã được chứng minh. Sử dụng các thư viện và ví dụ chính thức từ Microchip để triển khai USB. Lựa chọn RTOS phù hợp với PIC18F, như Salvo hoặc FreeRTOS (với port tương ứng). Thiết kế phần mềm theo mô-đun, tách biệt các lớp phần cứng và ứng dụng. Sử dụng ngắt một cách chiến lược để đáp ứng thời gian thực mà không gây quá tải CPU. Áp dụng kỹ thuật lập trình C nâng cao như macro, con trỏ hàm để tăng tính linh hoạt. Sử dụng công cụ phát triển tích hợp (IDE) như MPLAB X với trình biên dịch XC8. Kiểm thử từng mô-đun riêng biệt trước khi tích hợp toàn hệ thống.

3.1. Triển khai giao tiếp USB cho PIC18F bằng ngôn ngữ C

PIC18F series có nhiều model tích hợp module USB hỗ trợ tốc độ đầy đủ (Full-Speed). Triển khai USB đòi hỏi phải hiểu rõ các lớp giao thức: vật lý, liên kết dữ liệu, và thiết bị. Sử dụng bộ mô tả (descriptors) để khai báo thiết bị với máy chủ. Viết mã C xử lý các yêu cầu từ máy chủ, truyền nhận dữ liệu qua các endpoint. Microchip cung cấp các thư viện HID, MSD giúp giảm thời gian phát triển. Cần tuân thủ严格的规范 về điện áp và linh kiện phần cứng. Giao tiếp USB thường xuyên yêu cầu các hàm ngắt để xử lý sự kiện kịp thời.

3.2. Tích hợp hệ điều hành thời gian thực RTOS vào dự án PIC18F

RTOS cung cấp cơ chế lập lịch đa nhiệm và quản lý tài nguyên cho ứng dụng thời gian thực. Trên PIC18F, RTOS thường chạy trên một timer phần cứng làm nguồn nhịp xung. Mỗi tác vụ (task) được gán một mức ưu tiên và ngăn xếp riêng. RTOS quản lý việc chuyển ngữ cảnh (context switching) giữa các tác vụ. Cần phân bổ ngăn xếp đủ lớn cho mỗi tác vụ và ISR. Sử dụng semaphore, mutex để đồng bộ hóa truy cập tài nguyên chia sẻ. Việc tích hợp RTOS làm tăng độ phức tạp nhưng đảm bảo tính determinism và khả năng đáp ứng thời gian thực của hệ thống.

IV. Kết luận và ứng dụng thực tế của dự án PIC18F nâng cao

Dự án vi điều khiển PIC nâng cao trong C từ USB đến RTOS với PIC18F thể hiện sức mạnh của nền tảng này trong các ứng dụng nhúng phức tạp. Việc kết hợp lập trình C hiệu quả với kiến trúc PIC18F mạnh mẽ cho phép tạo ra các sản phẩm đáng tin cậy. Tích hợp USB cung cấp giao tiếp máy chủ tốc độ cao, trong khi RTOS đảm bảo khả năng đáp ứng thời gian thực. Các dự án thành công đòi hỏi kế hoạch kỹ lưỡng, kiến thức đa ngành và kỹ năng kỹ thuật vững vàng. Tài liệu gốc cung cấp nền tảng lý thuyết cần thiết về hệ thống số và kiến trúc vi tính. Với sự chuẩn bị đúng đắn, PIC18F là lựa chọn lý tưởng cho nhiều ứng dụng thương mại và công nghiệp.

4.1. Các ứng dụng điển hình của dự án PIC18F nâng cao

Các dự án PIC18F nâng cao được ứng dụng rộng rãi. Trong lĩnh vực công nghiệp, chúng dùng cho hệ thống giám sát và điều khiển quá trình (SCADA). Thiết bị y tế như máy đo huyết áp, máy đo đường huyết di động sử dụng PIC18F với giao tiếp USB. Các thiết bị tiêu dùng như bộ điều khiển trò chơi, thiết bị ngoại vi máy tính cũng khai thác PIC18F. Hệ thống an ninh, thiết bị đo lường, và tự động hóa gia đình là các lĩnh vực khác. Khả năng hoạt động thời gian thực và giao tiếp linh hoạt làm cho PIC18F phù hợp với nhiều ứng dụng đòi hỏi độ tin cậy cao.

4.2. Hướng phát triển và xu hướng công nghệ liên quan

Công nghệ vi điều khiển không ngừng phát triển. Dòng PIC18F đang được nâng cấp với các model mới có tốc độ cao hơn, tiêu thụ điện năng thấp hơn và tích hợp nhiều ngoại vi hơn. Xu hướng IoT đòi hỏi tích hợp kết nối không dây như Wi-Fi, Bluetooth alongside USB. RTOS trên vi điều khiển ngày càng phổ biến và được tối ưu hóa tốt hơn. Ngôn ngữ lập trình C vẫn là chủ đạo, nhưng C++ cũng đang được sử dụng cho các dự án lớn. Phát triển dựa trên mô hình (Model-Based Design) và kiểm thử tự động đang trở thành tiêu chuẩn. Việc học tập và áp dụng các dự án PIC18F nâng cao là bước đệm tốt để tiếp cận các công nghệ mới.

21/04/2026

Trích đoạn nội dung tài liệu

com Advanced PIC Microcontroller Projects in C www.com This page intentionally left blank Advanced PIC Microcontroller Projects in C From USB to RTOS with the PIC18F Series www.com Dogan Ibrahim Newnes is an imprint of Elsevier 30 Corporate Drive, Suite 400, Burlington, MA 01803, USA Linacre House, Jordan Hill, Oxford OX2 8DP, UK Copyright # 2008, Elsevier Ltd. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. Permissions may be sought directly from Elsevier s Science & Technology Rights Department in Oxford, UK: phone: (þ44) 1865 843830, fax: (þ44) 1865 853333, E-mail: permissions@elsevier. You may also complete your request online via the Elsevier homepage (http://elsevier.com), by selecting “Support & Contact” then “Copyright and Permission” and then “Obtaining Permissions.com Recognizing the importance of preserving what has been written, Elsevier prints its books on acid-free paper whenever possible. Library of Congress Cataloging-in-Publication Data Ibrahim, Dogan. Advanced PIC microcontroller projects in C: from USB to RTOS with the PIC18F series/Dogan Ibrahim p. Includes bibliographical references and index.80 95––dc22 2007050550 British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library. ISBN: 978-0-7506-8611-2 For information on all Newnes publications visit our Web site at www.com Printed in the United States of America 08 09 10 11 12 13 9 8 7 6 5 4 3 2 1 Contents www. xv Chapter 1: Microcomputer Systems.7 Brown-out Detector .8 Analog-to-Digital Converter .9 Serial Input-Output .10 EEPROM Data Memory .13 Real-time Clock.15 Power-on Reset.com vi Contents 1.16 Low-Power Operation .17 Current Sink/Source Capability .19 Motor Control Interface .1 RISC and CISC .1 Decimal Number System .2 Binary Number System .3 Octal Number System .4 Hexadecimal Number System .6 Converting Binary Numbers into Decimal.7 Converting Decimal Numbers into Binary.8 Converting Binary Numbers into Hexadecimal.9 Converting Hexadecimal Numbers into Binary.10 Converting Hexadecimal Numbers into Decimal .11 Converting Decimal Numbers into Hexadecimal .12 Converting Octal Numbers into Decimal.13 Converting Decimal Numbers into Octal.14 Converting Octal Numbers into Binary .15 Converting Binary Numbers into Octal .17 Adding Binary Numbers .18 Subtracting Binary Numbers .19 Multiplication of Binary Numbers.20 Division of Binary Numbers .21 Floating Point Numbers .22 Converting a Floating Point Number into Decimal .1 Normalizing Floating Point Numbers .2 Converting a Decimal Number into Floating Point .3 Multiplication and Division of Floating Point Numbers .4 Addition and Subtraction of Floating Point Numbers . 40 Chapter 2: PIC18F Microcontroller Series .1 PIC18FXX2 Architecture.1 Program Memory Organization .com Contents vii 2.2 Data Memory Organization .3 The Configuration Registers.4 The Power Supply .6 The Clock Sources.10 Capture/Compare/PWM Modules (CCP) .11 Analog-to-Digital Converter (A/D) Module .com Chapter 3: C Programming Language.1 Structure of a mikroC Program.2 Beginning and Ending of a Program .3 Terminating Program Statements.19 Modifying the Flow of Control .20 Mixing mikroC with Assembly Language Statements .2 PIC Microcontroller Input-Output Port Programming .com viii Contents Chapter 4: Functions and Libraries in mikroC.2 Passing Arrays to Functions.3 Passing Variables by Reference to Functions.4 Variable Number of Arguments .6 Static Function Variables .2 mikroC Built-in Functions .3 mikroC Library Functions.3 Software UART Library .4 Hardware USART Library . 219 Chapter 5: PIC18 Development Tools .1 Software Development Tools .2 Assemblers and Compilers.4 High-Level Language Simulators .5 Integrated Development Environments (IDEs).2 Hardware Development Tools.3 In-Circuit Debuggers .4 In-Circuit Emulators .3 mikroC Integrated Development Environment (IDE) .1 mikroC IDE Screen .2 Creating and Compiling a New File.3 Using the Simulator .4 Using the mikroICD In-Circuit Debugger.5 Using a Development Board .com Contents ix Chapter 6: Simple PIC18 Projects .1 Program Description Language (PDL) .3 IF-THEN-ELSE-ENDIF .3—Two-Dice Project.4—Two-Dice Project Using Fewer I/O Pins .5—7-Segment LED Counter.6—Two-Digit Multiplexed 7-Segment LED.7—Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt.8—Voltmeter with LCD Display .9—Calculator with Keypad and LCD .10—Serial Communication–Based Calculator . 352 Chapter 7: Advanced PIC18 Projects—SD Card Projects .1 The SD Card .1 The SPI Bus.2 Operation of the SD Card in SPI Mode .2 mikroC Language SD Card Library Functions .1—Read CID Register and Display on a PC Screen .2—Read/Write to SD Card Sectors.3—Using the Card Filing System . 397 Chapter 8: Advanced PIC18 Projects—USB Bus Projects .1 Speed Identification on the Bus .3 USB Bus Communication.2 Data Flow Types.5 PIC18 Microcontroller USB Bus Interface .6 mikroC Language USB Bus Library Functions .1—USB-Based Microcontroller Output Port .2—USB-Based Microcontroller Input/Output .3—USB-Based Ambient Pressure Display on the PC . 464 Chapter 9: Advanced PIC18 Projects—CAN Bus Projects .1 Start of Frame (SOF) .6 Types of Errors .7 Nominal Bit Timing .8 PIC Microcontroller CAN Interface .3 Normal Operation Mode.4 Listen-only Mode .5 Loop-Back Mode .6 Error Recognition Mode.7 CAN Message Transmission.8 CAN Message Reception.9 Calculating the Timing Parameters .10 mikroC CAN Functions .11 CAN Bus Programming .1—Temperature Sensor CAN Bus Project .com Contents xi Chapter 10: Multi-Tasking and Real-Time Operating Systems.2 The Real-Time Operating System (RTOS) .4 Synchronization and Messaging Tools .5 CCS PIC C Compiler RTOS.1 Preparing for RTOS .2—Random Number Generator.3—Voltmeter with RS232 Serial Output .com This page intentionally left blank Preface www.com A microcontroller is a microprocessor system which contains data and program memory, serial and parallel I/O, timers, and external and internal interrupts—all integrated into a single chip that can be purchased for as little as two dollars. About 40 percent of all microcontroller applications are found in office equipment, such as PCs, laser printers, fax machines, and intelligent telephones. About one third of all microcontrollers are found in consumer electronic goods. Products like CD players, hi-fi equipment, video games, washing machines, and cookers fall into this category. The communications market, the automotive market, and the military share the rest of the applications. This book is written for advanced students, for practicing engineers, and for hobbyists who want to learn more about the programming and applications of PIC18F-series microcontrollers. The book assumes the reader has taken a course on digital logic design and been exposed to writing programs using at least one high-level programming language. Knowledge of the C programming language will be useful, and familiarity with at least one member of the PIC16F series of microcontrollers will be an advantage. Knowledge of assembly language programming is not required since all the projects in the book are based on the C language. Chapter 1 presents the basic features of microcontrollers, discusses the important topic of numbering systems, and describes how to convert between number bases. Chapter 2 reviews the PIC18F series of microcontrollers and describes various features of these microcontrollers in detail. Chapter 3 provides a short tutorial on the C language and then examines the features of the mikroC compiler.com xiv Preface Chapter 4 covers advanced features of the mikroC language. Topics such as built-in functions and libraries are discussed in this chapter with examples. Chapter 5 explores the various software and hardware development tools for the PIC18F series of microcontrollers. Various commercially available development kits as well as development tools such as simulators, emulators, and in-circuit debuggers are described with examples. Chapter 6 provides some simple projects using the PIC18F series of microcontrollers and the mikroC compiler. All the projects are based on the PIC18F452 micro- controller, and all of them have been tested. This chapter should be useful for those who are new to PIC microcontrollers as well as for those who want to extend their www.com knowledge of programming PIC18F microcontrollers using the mikroC language. Chapter 7 covers the use of SD memory cards in PIC18F microcontroller projects. The theory of these cards is given with real working examples. Chapter 8 reviews the popular USB bus, discussing the basic theory of this bus system with real working projects that illustrate how to design PIC18F-based projects communicating with a PC over the USB bus. The CAN bus is currently used in many automotive applications. Chapter 9 presents a brief theory of this bus and also discusses the design of PIC18F microcontroller- based projects with CAN bus interface. Chapter 10 is about real-time operating systems (RTOS) and multi-tasking. The basic theory of RTOS systems is described and simple multi-tasking applications are given. The CD-ROM that accompanies this book contains all the program source files and HEX files for the projects described in the book. In addition, a 2K size limited version of the mikroC compiler is included on the CD-ROM. Dogan Ibrahim London, 2007 www.com Acknowledgments www.com The following material is reproduced in this book with the kind permission of the respective copyright holders and may not be reprinted, or reproduced in any other way, without their prior consent.2 are taken from Microchip Technology Inc. data sheets PIC18FXX2 (DS39564C) and PIC18F2455/2550/4455/4550 (DS39632D).5 is taken from the web site of BAJI Labs.8 are taken from the web site of Shuan Shizu Ent.18 are taken from the web site of Custom Computer Services Inc.43 are taken from the web site of mikroElektronika Ltd.11 is taken from the web site of Futurlec.21 is taken from the web site of Smart Communications Ltd.22 is taken from the web site of RF Solutions.23 is taken from the web site of Phyton.14 are taken from the web site of microEngineering Labs Inc.16 is taken from the web site of Kanda Systems. Thanks is due to mikroElektronika Ltd. for their technical support and for permission to include a limited size mikroC compiler on the CD-ROM that accompanies this book. PICW, PICSTARTW, and MPLABW are all registered trademarks of Microchip Technology Inc.com CHAPTER 1 Microcomputer Systems www.1 Introduction The term microcomputer is used to describe a system that includes at minimum a microprocessor, program memory, data memory, and an input-output (I/O) device. Some microcomputer systems include additional components such as timers, counters, and analog-to-digital converters. Thus, a microcomputer system can be anything from a large computer having hard disks, floppy disks, and printers to a single-chip embedded controller. In this book we are going to consider only the type of microcomputers that consist of a single silicon chip. Such microcomputer systems are also called microcontrollers, and they are used in many household goods such as microwave ovens, TV remote control units, cookers, hi-fi equipment, CD players, personal computers, and refrigerators. Many different microcontrollers are available on the market. In this book we shall be looking at programming and system design for the PIC (programmable interface controller) series of microcontrollers manufactured by Microchip Technology Inc.2 Microcontroller Systems A microcontroller is a single-chip computer. Micro suggests that the device is small, and controller suggests that it is used in control applications. Another term for microcontroller is embedded controller, since most of the microcontrollers are built into (or embedded in) the devices they control. A microprocessor differs from a microcontroller in a number of ways. The main distinction is that a microprocessor requires several other components for its operation, www.com 2 Chapter 1 such as program memory and data memory, input-output devices, and an external clock circuit. A microcontroller, on the other hand, has all the support chips incorporated inside its single chip. All microcontrollers operate on a set of instructions (or the user program) stored in their memory. A microcontroller fetches the instructions from its program memory one by one, decodes these instructions, and then carries out the required operations. Microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast, it has several disadvantages. An assembly program consists of mnemonics, which makes learning and maintaining a program written using the assembly language difficult.com microcontrollers manufactured by different firms have different assembly languages, so the user must learn a new language with every new microcontroller he or she uses. Microcontrollers can also be programmed using a high-level language, such as BASIC, PASCAL, or C. High-level languages are much easier to learn than assembly languages. They also facilitate the development of large and complex programs. In this book we shall be learning the programming of PIC microcontrollers using the popular C language known as mikroC, developed by mikroElektronika.

Nội dung được bảo vệ bản quyền — Tải xuống đầy đủ