Advanced PIC Microcontroller Projects in C

Book description

This book is ideal for the engineer, technician, hobbyist and student who have knowledge of the basic principles of PIC microcontrollers and want to develop more advanced applications using the 18F series.

The architecture of the PIC 18FXXX series as well as typical oscillator, reset, memory, and input-output circuits is completely detailed. After giving an introduction to programming in C, the book describes the project development cycle in full, giving details of the process of editing, compilation, error handling, programming and the use of specific development tools. The bulk of the book gives full details of tried and tested hands-on projects, such as the 12C BUS, USB BUS, CAN BUS, SPI BUS and real-time operating systems.

  • A clear introduction to the PIC 18FXXX microcontroller's architecture
  • 20 projects, including developing wireless and sensor network applications, using I2C BUS, USB BUS, CAN BUS and the SPI BUS, which give the block and circuit diagram, program description in PDL, program listing and program description
  • Numerous examples of using developmental tools: simulators, in-circuit debuggers (especially ICD2) and emulators

Table of contents

  1. Copyright
  2. Preface
  3. Acknowledgments
  4. 1. Microcomputer Systems
    1. 1.1. Introduction
    2. 1.2. Microcontroller Systems
      1. 1.2.1. RAM
      2. 1.2.2. ROM
      3. 1.2.3. PROM
      4. 1.2.4. EPROM
      5. 1.2.5. EEPROM
      6. 1.2.6. Flash EEPROM
    3. 1.3. Microcontroller Features
      1. 1.3.1. Supply Voltage
      2. 1.3.2. The Clock
      3. 1.3.3. Timers
      4. 1.3.4. Watchdog
      5. 1.3.5. Reset Input
      6. 1.3.6. Interrupts
      7. 1.3.7. Brown-out Detector
      8. 1.3.8. Analog-to-Digital Converter
      9. 1.3.9. Serial Input-Output
      10. 1.3.10. EEPROM Data Memory
      11. 1.3.11. LCD Drivers
      12. 1.3.12. Analog Comparator
      13. 1.3.13. Real-time Clock
      14. 1.3.14. Sleep Mode
      15. 1.3.15. Power-on Reset
      16. 1.3.16. Low-Power Operation
      17. 1.3.17. Current Sink/Source Capability
      18. 1.3.18. USB Interface
      19. 1.3.19. Motor Control Interface
      20. 1.3.20. CAN Interface
      21. 1.3.21. Ethernet Interface
      22. 1.3.22. ZigBee Interface
    4. 1.4. Microcontroller Architectures
      1. 1.4.1. RISC and CISC
    5. 1.5. Number Systems
      1. 1.5.1. Decimal Number System
      2. 1.5.2. Binary Number System
      3. 1.5.3. Octal Number System
      4. 1.5.4. Hexadecimal Number System
    6. 1.6. Converting Binary Numbers into Decimal
      1. Example 1.1
      2. Solution 1.1
      3. Example 1.2
      4. Solution 1.2
    7. 1.7. Converting Decimal Numbers into Binary
      1. Example 1.3
      2. Solution 1.3
      3. Example 1.4
      4. Solution 1.4
      5. Example 1.5
      6. Solution 1.5
    8. 1.8. Converting Binary Numbers into Hexadecimal
      1. Example 1.6
      2. Solution 1.6
      3. Example 1.7
      4. Solution 1.7
      5. Example 1.8
      6. Solution 1.8
    9. 1.9. Converting Hexadecimal Numbers into Binary
      1. Example 1.9
      2. Solution 1.9
      3. Example 1.10
      4. Solution 1.10
    10. 1.10. Converting Hexadecimal Numbers into Decimal
      1. Example 1.11
      2. Solution 1.11
      3. Example 1.12
      4. Solution 1.12
    11. 1.11. Converting Decimal Numbers into Hexadecimal
      1. Example 1.13
      2. Solution 1.13
      3. Example 1.14
      4. Solution 1.14
    12. 1.12. Converting Octal Numbers into Decimal
      1. Example 1.15
      2. Solution 1.15
      3. Example 1.16
      4. Solution 1.16
    13. 1.13. Converting Decimal Numbers into Octal
      1. Example 1.17
      2. Solution 1.17
      3. Example 1.18
      4. Solution 1.18
    14. 1.14. Converting Octal Numbers into Binary
      1. Example 1.19
      2. Solution 1.19
      3. Example 1.20
      4. Solution 1.20
    15. 1.15. Converting Binary Numbers into Octal
      1. Example 1.21
      2. Solution 1.21
    16. 1.16. Negative Numbers
      1. Example 1.22
      2. Solution 1.22
      3. Example 1.23
      4. Solution 1.23
    17. 1.17. Adding Binary Numbers
      1. Example 1.24
      2. Solution 1.24
      3. Example 1.25
      4. Solution 1.25
    18. 1.18. Subtracting Binary Numbers
      1. Example 1.26
      2. Solution 1.26
    19. 1.19. Multiplication of Binary Numbers
      1. Example 1.27
      2. Solution 1.27
      3. Example 1.28
      4. Solution 1.28
    20. 1.20. Division of Binary Numbers
      1. Example 1.29
      2. Solution 1.29
    21. 1.21. Floating Point Numbers
    22. 1.22. Converting a Floating Point Number into Decimal
      1. Example 1.30
      2. Solution 1.30
      3. Example 1.31
      4. Solution 1.31
      5. 1.22.1. Normalizing Floating Point Numbers
        1. Example 1.32
        2. Solution 1.32
        3. Example 1.33
        4. Solution 1.33
      6. 1.22.2. Converting a Decimal Number into Floating Point
        1. Example 1.34
        2. Solution 1.34
        3. Example 1.35
        4. Solution 1.35
      7. 1.22.3. Multiplication and Division of Floating Point Numbers
        1. Example 1.36
        2. Solution 1.36
      8. 1.22.4. Addition and Subtraction of Floating Point Numbers
        1. Example 1.37
        2. Solution 1.37
    23. 1.23. BCD Numbers
      1. Example 1.38
      2. Solution 1.38
      3. Example 1.39
      4. Solution 1.39
    24. 1.24. Summary
    25. 1.25. Exercises
  5. 2. PIC18F Microcontroller Series
    1. 2.1. PIC18FXX2 Architecture
      1. 2.1.1. Program Memory Organization
      2. 2.1.2. Data Memory Organization
      3. 2.1.3. The Configuration Registers
        1. CONFIG1H
        2. CONFIG2L
        3. CONFIG2H
      4. 2.1.4. The Power Supply
      5. 2.1.5. The Reset
        1. Power-on Reset
      6. 2.1.6. The Clock Sources
        1. Crystal or Ceramic Resonator Operation
        2. External Clock Operation
        3. Resistor/Capacitor Operation
        4. Crystal or Resonator with PLL
        5. Internal Clock
        6. Clock Switching
      7. 2.1.7. Watchdog Timer
      8. 2.1.8. Parallel I/O Ports
        1. PORTA
        2. PORTB
        3. PORTC, PORTD, PORTE, and Beyond
      9. 2.1.9. Timers
        1. Timer 0
          1. 8-Bit Mode
          2. 16-Bit Mode
        2. Timer 1
        3. Timer 2
        4. Timer 3
      10. 2.1.10. Capture/Compare/PWM Modules (CCP)
        1. Capture Mode
        2. Compare Mode
        3. PWM Module
          1. Example 2.1
            1. Solution 2.1
      11. 2.1.11. Analog-to-Digital Converter (A/D) Module
        1. Analog Input Model and Acquisition Time
      12. 2.1.12. Interrupts
        1. RCON Register
        2. Enabling/Disabling Interrupts—No Priority Structure
        3. Enabling/Disabling Interrupts—Priority Structure
          1. Example 2.2
            1. Solution 2.2
          2. Example 2.3
            1. Solution 2.3
    2. 2.2. Summary
    3. 2.3. Exercises
  6. 3. C Programming Language
    1. 3.1. Structure of a mikroC Program
      1. 3.1.1. Comments
      2. 3.1.2. Beginning and Ending of a Program
      3. 3.1.3. Terminating Program Statements
      4. 3.1.4. White Spaces
      5. 3.1.5. Case Sensitivity
      6. 3.1.6. Variable Names
      7. 3.1.7. Variable Types
        1. (unsigned) char or unsigned short (int)
        2. signed char or (signed) short (int)
        3. (signed) int
        4. unsigned (int)
        5. (signed) long (int)
        6. unsigned long (int)
        7. float or double or long double
      8. 3.1.8. Constants
        1. Integer Constants
        2. Floating Point Constants
        3. Character Constants
        4. String Constants
        5. Enumerated Constants
      9. 3.1.9. Escape Sequences
      10. 3.1.10. Static Variables
      11. 3.1.11. External Variables
      12. 3.1.12. Volatile Variables
      13. 3.1.13. Enumerated Variables
      14. 3.1.14. Arrays
      15. 3.1.15. Pointers
        1. Array Pointers
      16. 3.1.16. Structures
      17. 3.1.17. Unions
      18. 3.1.18. Operators in C
        1. Arithmetic Operators
        2. Relational Operators
        3. Logical Operators
        4. Bitwise Operators
        5. Assignment Operators
        6. Conditional Operators
        7. Preprocessor Operators
          1. #define, #undef, #ifdef, #ifndef
          2. #include
          3. #if, #elif, #else, #endif
      19. 3.1.19. Modifying the Flow of Control
        1. Selection Statements
          1. if Statement
          2. switch Statement
          3. Example 3.1
            1. Solution 3.1
        2. Iteration Statements
          1. for Statement
          2. while Statement
          3. do Statement
        3. Unconditional Modifications of Flow
          1. goto Statement
          2. continue and break Statements
      20. 3.1.20. Mixing mikroC with Assembly Language Statements
    2. 3.2. PIC Microcontroller Input-Output Port Programming
    3. 3.3. Programming Examples
      1. Example 3.2
        1. Solution 3.2
      2. Example 3.3
        1. Solution 3.3
      3. Example 3.4
        1. Solution 3.4
      4. Example 3.5
        1. Solution 3.5
      5. Example 3.6
        1. Solution 3.6
      6. Example 3.7
        1. Solution 3.7
      7. Example 3.8
        1. Solution 3.8
      8. Example 3.9
        1. Solution 3.9
    4. 3.4. Summary
    5. 3.5. Exercises
  7. 4. Functions and Libraries in mikroC
    1. 4.1. mikroC Functions
      1. Example 4.1
        1. Solution 4.1
      2. Example 4.2
        1. Solution 4.2
      3. Example 4.3
        1. Solution 4.3
      4. Example 4.4
        1. Solution 4.4
      5. 4.1.1. Function Prototypes
        1. Example 4.5
          1. Solution 4.5
      6. 4.1.2. Passing Arrays to Functions
        1. Example 4.6
          1. Solution 4.6
        2. Example 4.7
          1. Solution 4.7
        3. Example 4.8
          1. Solution 4.8
      7. 4.1.3. Passing Variables by Reference to Functions
        1. Example 4.9
          1. Solution 4.9
      8. 4.1.4. Variable Number of Arguments
      9. 4.1.5. Function Reentrancy
      10. 4.1.6. Static Function Variables
    2. 4.2. mikroC Built-in Functions
      1. Example 4.10
        1. Solution 4.10
    3. 4.3. mikroC Library Functions
      1. 4.3.1. EEPROM Library
        1. Example 4.11
          1. Solution 4.11
      2. 4.3.2. LCD Library
        1. The HD44780 LCD Controller
          1. Lcd_Config
          2. Lcd_Init
          3. Lcd_Out
          4. Lcd_Out_Cp
          5. Lcd_Chr
          6. Lcd_Chr_Cp
          7. Lcd_Cmd
            1. Example 4.12
              1. Solution 4.12
      3. 4.3.3. Software UART Library
        1. Soft_Uart_Init
        2. Soft_Uart_Read
        3. Soft_Uart_Write
          1. Example 4.13
            1. Solution 4.13
      4. 4.3.4. Hardware USART Library
        1. Usart_Init
        2. Usart_Data_Ready
        3. Usart_Read
        4. Usart_Write
          1. Example 4.14
            1. Solution 4.14
      5. 4.3.5. Sound Library
        1. Sound_Init
        2. Sound_Play
          1. Example 4.15
            1. Solution 4.15
      6. 4.3.6. ANSI C Library
        1. Ctype Library
        2. Math Library
        3. Stdlib Library
          1. Example 4.16
            1. Solution 4.16
        4. String Library
          1. Example 4.17
            1. Solution 4.17
      7. 4.3.7. Miscellaneous Library
        1. Example 4.18
          1. Solution 4.18
        2. Example 4.19
          1. Solution 4.19
        3. Example 4.20
          1. Solution 4.20
    4. 4.4. Summary
    5. 4.5. Exercises
  8. 5. PIC18 Development Tools
    1. 5.1. Software Development Tools
      1. 5.1.1. Text Editors
      2. 5.1.2. Assemblers and Compilers
      3. 5.1.3. Simulators
      4. 5.1.4. High-Level Language Simulators
      5. 5.1.5. Integrated Development Environments (IDEs)
    2. 5.2. Hardware Development Tools
      1. 5.2.1. Development Boards
        1. LAB-XUSB Experimenter Board
        2. PICDEM 2 Plus
        3. PICDEM 4
        4. PICDEM HPC Explorer Board
        5. MK-1 Universal PIC Development Board
        6. SSE452 Development Board
        7. SSE8720 Development Board
        8. SSE8680 Development Board
        9. PIC18F4520 Development Kit
        10. BIGPIC4 Development Kit
        11. FUTURLEC PIC18F458 Training Board
      2. 5.2.2. Device Programmers
        1. Forest Electronics USB Programmer
        2. Mach X Programmer
        3. Melabs U2 Programmer
        4. EasyProg PIC Programmer
        5. PIC Prog Plus Programmer
      3. 5.2.3. In-Circuit Debuggers
        1. ICD2
        2. ICD-U40
        3. PICFlash 2
      4. 5.2.4. In-Circuit Emulators
        1. MPLAB ICE 4000
        2. RICE3000
        3. ICEPIC 3
        4. PICE-MC
      5. 5.2.5. Breadboards
    3. 5.3. mikroC Integrated Development Environment (IDE)
      1. 5.3.1. mikroC IDE Screen
        1. Top-Left Section
        2. Bottom-Left Section
        3. Middle Section
        4. Bottom Section
      2. 5.3.2. Creating and Compiling a New File
        1. Example 5.1
          1. Solution 5.1
      3. 5.3.3. Using the Simulator
        1. Example 5.2
          1. Solution 5.2
        2. Example 5.3
          1. Solution 5.3
      4. 5.3.4. Using the mikroICD In-Circuit Debugger
        1. The Circuit Diagram
        2. Debugging
      5. 5.3.5. Using a Development Board
        1. BIGPIC4 Development Board
        2. View the EEPROM Window
        3. View the RAM Window
        4. View the Code Window
        5. View the Statistics
    4. 5.4. Summary
    5. 5.5. Exercises
  9. 6. Simple PIC18 Projects
    1. 6.1. Program Description Language (PDL)
      1. 6.1.1. START-END
        1. Example:
      2. 6.1.2. Sequencing
        1. Example:
      3. 6.1.3. IF-THEN-ELSE-ENDIF
        1. Example:
      4. 6.1.4. DO-ENDDO
        1. Example:
        2. Example:
      5. 6.1.5. REPEAT-UNTIL
        1. Example:
    2. Project 6.1—Chasing LEDs
      1. Project Description
        1. Current Sinking Mode
        2. Current Sourcing Mode
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Further Development
    3. Project 6.2—LED Dice
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Using a Pseudorandom Number Generator
    4. Project 6.3—Two-Dice Project
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
    5. Project 6.4—Two-Dice Project Using Fewer I/O Pins
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Modifying the Program
    6. Project 6.5—7-Segment LED Counter
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Modified Program
    7. Project 6.6—Two-Digit Multiplexed 7-Segment LED
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
    8. Project 6.7—Two-Digit Multiplexed 7-Segment LED Counter with Timer Interrupt
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Modifying the Program
    9. Project 6.8—Voltmeter with LCD Display
      1. Project Description
      2. HD44780 LCD Module
      3. Connecting the LCD
      4. Project Hardware
      5. Project PDL
      6. Project Program
      7. A More Accurate Display
    10. Project 6.9—Calculator with Keypad and LCD
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Program Using a Built-in Keypad Function
    11. Project 6.10—Serial Communication–Based Calculator
      1. Project Description
      2. Project Hardware
      3. Project PDL
      4. Project Program
      5. Testing the Program
      6. Using Software-Based Serial Communication
  10. 7. Advanced PIC18 Projects—SD Card Projects
    1. 7.1. The SD Card
      1. 7.1.1. The SPI Bus
      2. 7.1.2. Operation of the SD Card in SPI Mode
        1. Reading Data
        2. Writing Data
          1. Card Size Parameters
    2. 7.2. mikroC Language SD Card Library Functions
    3. Project 7.1—Read CID Register and Display on a PC Screen
    4. Project 7.2—Read/Write to SD Card Sectors
    5. Project 7.3—Using the Card Filing System
    6. Project 7.4—Temperature Logger
  11. 8. Advanced PIC18 Projects—USB Bus Projects
    1. 8.1. Speed Identification on the Bus
    2. 8.2. USB States
    3. 8.3. USB Bus Communication
      1. 8.3.1. Packets
      2. 8.3.2. Data Flow Types
      3. 8.3.3. Enumeration
    4. 8.4. Descriptors
      1. 8.4.1. Device Descriptors
      2. 8.4.2. Configuration Descriptors
      3. 8.4.3. Interface Descriptors
      4. 8.4.4. HID Descriptors
      5. 8.4.5. Endpoint Descriptors
    5. 8.5. PIC18 Microcontroller USB Bus Interface
    6. 8.6. mikroC Language USB Bus Library Functions
    7. Project 8.1—USB-Based Microcontroller Output Port
      1. The PC Software
      2. The Microcontroller Software
        1. Generating the USB Descriptor File
        2. Microcontroller Clock
      3. Testing the Project
      4. Using a USB Protocol Analyzer
        1. UVCView
        2. USBTrace
      5. Using the HID Terminal of mikroC
    8. Project 8.2—USB-Based Microcontroller Input/Output
      1. Testing the Project
    9. Project 8.3—USB-Based Ambient Pressure Display on the PC
  12. 9. Advanced PIC18 Projects—CAN Bus Projects
    1. 9.1. Data Frame
      1. 9.1.1. Start of Frame (SOF)
      2. 9.1.2. Arbitration Field
      3. 9.1.3. Control Field
      4. 9.1.4. Data Field
      5. 9.1.5. CRC Field
      6. 9.1.6. ACK Field
    2. 9.2. Remote Frame
    3. 9.3. Error Frame
    4. 9.4. Overload Frame
    5. 9.5. Bit Stuffing
    6. 9.6. Types of Errors
    7. 9.7. Nominal Bit Timing
      1. Example 9.1
        1. Solution 9.1
    8. 9.8. PIC Microcontroller CAN Interface
    9. 9.9. PIC18F258 Microcontroller
      1. 9.9.1. Configuration Mode
      2. 9.9.2. Disable Mode
      3. 9.9.3. Normal Operation Mode
      4. 9.9.4. Listen-only Mode
      5. 9.9.5. Loop-Back Mode
      6. 9.9.6. Error Recognition Mode
      7. 9.9.7. CAN Message Transmission
      8. 9.9.8. CAN Message Reception
      9. 9.9.9. Calculating the Timing Parameters
        1. Example 9.2
          1. Solution 9.2
        2. Example 9.3
          1. Solution 9.3
    10. 9.10. mikroC CAN Functions
      1. 9.10.1. CANSetOperationMode
      2. 9.10.2. CANGetOperationMode
      3. 9.10.3. CANInitialize
      4. 9.10.4. CANSetBaudRate
      5. 9.10.5. CANSetMask
      6. 9.10.6. CANSetFilter
      7. 9.10.7. CANRead
      8. 9.10.8. CANWrite
    11. 9.11. CAN Bus Programming
    12. Project 9.1—Temperature Sensor CAN Bus Project
      1. The DISPLAY Processor
      2. The COLLECTOR Processor
      3. DISPLAY Program
      4. COLLECTOR Program
  13. 10. Multi-Tasking and Real-Time Operating Systems
    1. 10.1. State Machines
    2. 10.2. The Real-Time Operating System (RTOS)
      1. 10.2.1. The Scheduler
    3. 10.3. RTOS Services
    4. 10.4. Synchronization and Messaging Tools
    5. 10.5. CCS PIC C Compiler RTOS
      1. 10.5.1. Preparing for RTOS
      2. 10.5.2. Declaring a Task
    6. Project 10.1—LEDs
    7. Project 10.2—Random Number Generator
    8. Project 10.3—Voltmeter with RS232 Serial Output
      1. Using a Semaphore

Product information

  • Title: Advanced PIC Microcontroller Projects in C
  • Author(s): Dogan Ibrahim
  • Release date: August 2011
  • Publisher(s): Newnes
  • ISBN: 9780080558424