Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
This chapter explains how to install and use the two current Microchip Integrated Development Environments, MPLAB and MPLABX. The explanation is centred around a basic program, flashing a light emitting diode on and off. The chapter explains how to install the IDE, how the program is written, compiled and then programmed into the microcontroller using the development tools and C8 compiler. The discussion of the integrated development environment shows how to write and develop the C source code file and how to attach it to a project and fix errors. An explanation of the operation of the code is given.
Keywords
C program
MPLAB
MPLABX
delays
loops
C18
outputs
errors
programming
PICkit3
ICD3
debugger
project
Project Wizard
include statement
pragma statement
OSSCON
WDT
PWRT
LVP
MCLRE
Main function
ADCON1
IDE
In order to program the microcontroller we are going to:
Write the code in C.
Convert the code to a hex file using a compiler.
Program the hex file into the microcontroller.
The code which we are going to write in the C language can be written on any text editor such as WORD. Any suitable C compiler can be used to convert the code to a hex file and there are numerous programmers on the market that will blow your hex file into the microcontroller.
Throughout this book I am going to use a dedicated piece of software called MPLAB integrated development environment (IDE) written by the PIC microcontroller manufacturer, Microchip. This acts as a text editor, compiler, and driver for the Microchip programmer. MPLAB IDE is free and can be downloaded from the Microchip Web site at Microchip.com
At the time of writing Microchip have upgraded MPLAB v8 and have called it MPLABX. I have discussed both IDEs here and left it up to the reader to decide which one they prefer to use.
MPLAB and MPLABX also include a simulator that help to debug your code. I use Microchips own programmer/in-circuit debugger (ICD) called Microchip MPLAB ICD3 and PICkit3. The ICDs allow you to connect your circuit to the computer so that you can view the registers inside the micro when the program is running. But we can see more of the simulator and debugger later.
Install the latest version of MPLAB, as of 7/1/2011 that is MPLAB v8.73a., and the C compiler is MPLAB C v3.40 LITE. NB. MPLAB and the LITE version of the C compiler are free from Microchip.com
Or install MPLABX IDE and the C compiler XC8.
Make a new folder to keep your programs in, say PicProgs on your desktop.
For our first program we are going to flash an LED on and off at 1 s intervals on the output pin, PORTB,4.
The pin connection for the 18F1220 is shown in Figure 2.1.
But before we program our device we need to understand a little of the C language.
If we wish to turn an LED on PORTB bit4 on, the C code is:
PORTBbits.RB4 = 1;
This is called a statement. NB all C statements end in ;
If we wish to turn the LED off the code is:
PORTBbits.RB4 = 0;
In the C language suite we have installed a file called Delay.h. As its name suggests there are a number of routines in this file which can create a delay in our program. The address for this file if you want to read it is "C:\Program Files\Microchip\mplabc18\v3.40\h" after installing MPLAB from Microchip.com
The subroutines are:
Delay1TCY()
Delay10TCYx()
Delay100TCYx()
Delay1KTCYx()
Delay10KTCYx()
If you wish to call a subroutine in C you just state its name, i.e.,
Delay1KTCYx();
These delays are multiples of timing cycles, i.e., 1, 10, 100, 1000, and 10,000.
A timing cycle is the time taken to execute an instruction and it is the basis of the timing in the microcontroller system. The timing comes from the oscillator which can be an external clock source, an external crystal, or an internal oscillator. For now we are going to use the internal oscillator set at 31.25 kHz.
The timing cycle runs at one-fourth of this frequency, i.e., at 7.8125 kHz. This means the period of the timing cycle is 0.128 ms.
So the Delay100TCYx() subroutine will have a time of 100×0.128 ms = 12.8 ms.
In order to achieve a delay of 1 s we would need 78 of these 12.8 ms.
78 × 12.8 ms = 0.9984 s, not quite 1 s but near enough for this application.
To do this the C code is:
Delay100TCYx(78);
Note the number of times the subroutine is executed is written in the brackets (78) in this case. NB. 255 is the maximum value that can be entered.
In order to make the program execute some code a number of times or indefinitely, we use a loop.
The WHILE LOOP as it is called looks like this:
while ( )
{
}
The code to be executed is written between the brackets { } while the condition for executing the code is written between the brackets ( )
Suppose we wish to turn an alarm on if the temperature goes above 60°C, the code would look like:
while (Temperature>60)
PORTBbits.RB0 = 1; // turn on PORTB bit0
If we wish to execute a piece of code indefinitely such as flashing our LED on PORTB bit 4 on and off continuously, the loop is:
while (1)
PORTBbits.RB4 = 1; // turn on PORTB bit4
Delay100TCYx(78); // wait 1 s
PORTBbits.RB4 = 0; // turn off PORTB bit4
The function while (1) means while 1 is true! But 1 is always 1, so the loop is always executing. Note the function while (1) does not end with a ;
The // code means ignore what follows on the line; it is for our reference not for the compiler.
A useful while loop involves just 1 line of code, i.e.,
while (PORTBbits.RB4==1);
This means continue the loop (just 1 line) until PORTBbits.RB4==1 is no longer true, i.e., wait until PORTBbits.RB4==0 before moving on in the program.
Note: == means is equal to,=means equals. One is a question and the other a statement.
Numbers can be entered in the program as hexadecimal, binary or decimal.
A hexadecimal number 7 F is written as 0 × 7 F.
A binary number 1111 0101 is written as 0b11110101.
A decimal number 45 is just written as 45.
We are now ready to write our C program.
NOTE
In the remainder of this book we will be writing C files and putting them into projects. You can choose to do this with MPLAB or MPLABX.
Run MPLAB and the screen shown in Figure 2.2 will open.
For our first program we are going to flash an LED on and off at 1 s intervals on the output pin, PORTB,4. We will call this, flash.c
Under the file menu select New (or if you have the code Open flash.c) (Figure 2.3).
The screen shown in Figure 2.4 showing the file editor will open.
If the line numbers are not visible on the left hand side turn them on with Edit/Properties as shown in Figure 2.5.
Select File Type/Line Numbers.
Click Apply then.
Click OK as shown in Figure 2.6.
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.