Migrating from Assembly to C

Migrating from Assembly to C

Copyright: © 2017 |Pages: 22
DOI: 10.4018/978-1-68318-000-5.ch005
OnDemand PDF Download:
$30.00
List Price: $37.50

Chapter Preview

Top

Introduction

This chapter introduces the C18 compiler used to convert C language programs into PIC18 machine code. It is assumed that the reader has a basic background in C language or any other programming language. A number of elucidating examples are given in order to quick start you in this area. You may refer to the C18 Reference Guide in Appendix 5 for an elaborate overview of this programming language. This chapter highlights the following topics:

  • Conversion routines: Binary-to-BCD and vice versa.

  • Inline assembly language.

  • Delay functions.

  • I/O ports read/write access.

  • EEPROM read/write operations.

  • Built-in and user-defined macros.

  • 16-bit arithmetic.

Top

Integer And Floating-Point Types

The MPLAB C18 compiler supports the standard ANSI-defined integer types. It also supports a 24-bit integer type short long int in both a signed and unsigned variety. The ranges of integer data types are documented in Table 1.

Table 1.
Integer data types sizes and limits

Note that int data types require two bytes of storage in data memory on PIC18 processors. Consequently, this slows down processing speed. For instance, if two int variables are added together, then two 8-bit additions are required at the machine level. As a result, given the choice between int and unsigned char data types for a loop counter that does not exceed 255 iterations, the user should opt for the unsigned char type to speed up execution. Be aware that a plain char data type is signed by default.

Also note that short long (24-bit) and long data types (32-bit) require 3 and 4 bytes of storage respectively. Utilization of these data types must be avoided, unless there is no other alternative, due to their slow processing speed and large storage requirement.

As to 32-bit floating-point types, the user may employ either double or float data types. The ranges of the floating point types are documented in Table 2.

Table 2.
Floating-point data types sizes and limits
Top

C Language Operators

The standard C operators are essentially categorized as follows:

  • Arithmetic Operators: Addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).

  • Relational Operators: Greater than (>), greater than or equal to (>=), less than (<), less than or equal to (<=), equal to (==) and not equal to (!=).

  • Logical Operators: AND (&&), OR (||) and NOT operator (!).

  • Increment (++) and decrement (--) operators.

  • Bitwise Operators: AND (&), OR (|), XOR (^), shift left (<<) and shift right (>>).

  • Assignment operator (=).

Complete Chapter List

Search this Book:
Reset