Module3_PIC_Programming.pdf
Document Details
Uploaded by Deleted User
Tags
Full Transcript
Embedded Systems Design PIC Programming Dr. Bilal Arain Based on slides from Dr. Bassel Soudan Programming Languages Program: A program is a sequence of instructions that bring data into the microcontroller, processes it and sends appropriate control signals back to t...
Embedded Systems Design PIC Programming Dr. Bilal Arain Based on slides from Dr. Bassel Soudan Programming Languages Program: A program is a sequence of instructions that bring data into the microcontroller, processes it and sends appropriate control signals back to the environment. There are many programming languages (C, C++, FORTRAN, JAVA, etc.) Machine language All of these programming languages can Assembly language be grouped into three main levels. High-level programming languages Machine Language Machine language is the lowest-level programming language. In this language, every instruction is described by binary patterns. For example, 0000 0111 0101 1001 in PIC machine language means ADD the contents of file register number 59H to the working register and store the result in the working register. This is the only form understood by the microcontroller (the machine) and in which the instructions are stored in memory. To keep things short, we convert binary to hexadecimal and write the instruction as (0xC04) instead. Machine language is designed to be used without the need for translation. Assembly Language Assembly Language It is important to know that a machine language and its associated assembly language are completely machine-dependent. In other words, they are not transferable from one microcontroller to a different one. For example, Motorolla has a microcontroller called the 6811 and the instruction set is different to the PIC16F877 microcontroller. Assembly Language How does assembly language get translated into machine language? Hand Assembly: The programmer translates each assembly language instruction into its equivalent machine language code. Then the machine code is entered into memory. Assembler: The other possibility is a program called an “assembler”, which does the translation automatically. The reverse operation is called disassembling. The contents of memory are examined, and the contents are translated to the appropriate assembly language equivalents. The Assembler High-level Programming Languages High-level Programming Languages One high level instruction translates into many assembly language (therefore, machine language) instructions. e.g. x = y + z may translate into: movwf 0x59 Move contents of location 0x59 to W addwf 0x60, w Add contents of locations of 0x60 to W movfw 0x61 Move W to location 0x61 High-level Programming Languages High-level programming languages are “portable” as well as more readable. The program can be “re-compiled” using a compiler for the different “machine” to produce the required “machine language”. These programs are translated into machine-specific assembly language using a compiler. Example There will be a different “compiler” for the different “machines”. If we write a program in C language, we will use a PIC C compiler to compile the program for the PIC microcontroller, and we will use a 6811 C compiler to compile the program for the 6811 microcontroller. Advantages: High-level Programming Languages Faster Development Cycle Algorithms are more easily written in a high-level programming language Easier to Develop Code Bank Switching and Paging handled by the compiler Easier to Debug Code High-level language implementation of control structures and decision branches are well- tested Existing function libraries Most compilers come with ready libraries to handle some of the simple repetitive tasks Disadvantages: High-level Programming Languages Inefficient memory utilization High-level programming language takes more Program Memory space than assembly language in most applications Program Execution Time More instructions per task = more time per task Execution time of high-level language programs needs to be verified Time-critical tasks are typically coded in the assembly Recap: Program Memory and Data Memory Program memory The program is stored in program memory. The microcontroller starts to execute the instructions from the program memory one at a time. Program memory stores the instructions that the microcontroller executes Instructions are stored in program memory (flashed Read-Only Memory), which is non- volatile (contents are retained when power is lost). The PIC16F877 has 8K words of program memory Data Memory: Memory is also used to hold the data The microprocessor reads the data from memory when it needs it and writes (stores) the results into memory when it is done. Data currently in use is kept in RAM data memory, also known as the file registers, which is volatile (contents are lost when power is lost). For example, declaring a variable. Programming the PIC in C C is NOT as efficient as assembly A good assembly programmer can usually do better than the compiler, no matter what the optimization level C WILL use more memory A cross-compiler is a compiler that runs on a processor (usually a PC) that is different from the target processor. Most embedded systems are now programmed using either C or C++ Several compilers can be used with the PIC: MikroC and CCS, MPLAB XC8 C/C++, … In this course, we will use the MikroC compiler. There might be small differences between compilers, especially when it comes to the number of bits used for each data type. Data Types Supported by MikroC The MikroC compiler supports three main groups of data types: Integer Types Floating Point Types Void Integer Data Types Supported by MikroC Integer data types supported by mikroC Variables and Data Storage Variables and Data Storage When a variable is declared, a certain amount of storage in the data memory is allocated (reserved) and assigned to that variable. Every time that variable is used in the program, the contents of this storage is used. Data types Integer types Floating point types Void Variable and Data Storage Declaring a Variable Variable name can include any of the alphabetical characters A-Z (a-z), the digits 0-9 and the underscore character '_’. The compiler is case-sensitive and differentiates between capital and small letters. Variable names must not start with a digit. Some of the names cannot be used as variable names as already being used by the compiler itself. Microcontroller Programming Floating point data types: The real numbers in the exponential form is called floating-point representation. C language offers three different types of floating point representation: float (32 bits), double (64 bits), and long double (80 or 128 bits). Some compilers such as MikroC consider all three to be identical (32 bits). Note: PIC16F877 is an 8-bit microcontroller without floating point unit. Don’t use floats unless you absolutely must. Void Data Types Supported by MikroC Void data types PIC16 Data Types Because PICs are 8-bit microcontrollers, the character data type is the most natural choice for many applications. The “unsigned char” is an 8-bit data type that takes a value in the range of 0 to 255 (00 to FFH). It matches with the natural size of memory locations and registers in the PIC. Signed vs. unsigned The default for the MicroC compiler is “unsigned” char. If you want a “signed char” you must explicitly include the “signed” qualifier. The char data type can, of course, be used to hold ASCII characters. Integers vs. Floats Compiler Directives These are not part of the program, but they give instructions to the compiler. The compiler processes these directives first before compiling the rest of the program. #define #include Symbolic Constants #define #include #include is a directive instructing the compiler program to include text from a specific file at that point. The text could include specific variable declarations or constant definitions. For example: #include will include the header file for the pic_lcd8 library. This is a library provided by the maker of the compiler that includes a set of functions for dealing with the LCD. After including such header files, you can use the functions and definitions in these files within your program Literals in C Literals in C PIC Microcontroller Programming Some Useful C Constructs Useful C Constructs Some Useful C Constructs C comparison operators Some Useful C Constructs Useful C Constructs Do not forget break at the end of each statement Some Useful C Constructs Useful C Constructs Some Useful C Constructs Some Useful C Constructs C Program Basics A special type of program loop is the endless loop. It is formed if the condition remains unchanged within the loop. while(1){... // Expressions enclosed within curly brackets will be... // endlessly executed (endless loop). } The result in brackets is always true (1=1), which means that the program remains in the same loop Functions The main idea is to divide a program into several parts using these functions in order to solve the actual problem easier. Every function must be properly declared so as to be properly interpreted during the process of compilation. Function name Function body List of parameters Declaration of parameters Type of function result C Program Basics Pointers Pointer Example Arrays Initializing Arrays Lookup Tables Common repetitive operations on known values can be easily replaced with lookup tables. Pre-calculate the results and store them in memory. Then when presented with values, simply read the result. Faster than re-calculating the same result every time. Example - Lookup table for 7 Segment Display Implementing the Lookup Table in C C Programming for Microcontrollers Microcontrollers contain a number of internal hardware elements that need to be configured and controlled. Timers, counters, ports, interrupt, serial port, ADC, etc. Typically, each of these elements has a “configuration” register to control its specific behavior. The bits of this register need to be set in an appropriate manner to determine how the element operates. For example, the PIC has a controllable interrupt mechanism. The interrupt mechanism is controlled through the bits of a register called “INTCON” The compiler will generate a microcontroller specific header file that includes the definition of these registers and include it in the project. In your code, you just need to use the proper name and assign the values in the proper order. No need to declare variables for these registers. That has already been done in the header file. Configuring Registers in C Mixing Assembly with C Manipulating C Objects in Assembler