E-Book, Englisch, 204 Seiten
Smith PIC Projects and Applications using C
1. Auflage 2012
ISBN: 978-0-08-099954-8
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
A Project-based Approach
E-Book, Englisch, 204 Seiten
ISBN: 978-0-08-099954-8
Verlag: Elsevier Science & Techn.
Format: EPUB
Kopierschutz: Adobe DRM (»Systemvoraussetzungen)
PIC Projects and Applications Using C details how to program the PIC microcontroller in the C language. The book takes a learn-by-doing approach, with applications covering topics such as inputs, outputs, keypads, alphanumeric displays, analogue-to-digital conversion, radio transmitters and receivers, data EEPROM, interrupts and timing. To aid debugging, the book provides a section detailing the use of the simulator and in-circuit debugger. With this book you will learn: - How to program the PIC microcontroller in C - Techniques for using the simulator and debuggers to find faults on your code - The ins and outs of interfacing circuits, such as radio modules and liquid crystal displays - How to use the PIC on-board functions, such as interrupts and timing modules, and make analogue measurements - Relevant parts of the language are introduced and explained when required for those new to the subject - Core principles are introduced gradually for self-paced learning - Explains how and why a software program works, and how to alter and expand the code
David Smith has had 30 years experience in the Electronics Industry. Before arriving at MMU he worked as an Electronics Design Engineer for ICL and Marconi. His teaching interests are focused on enabling Design and Technology students to implement microcontroller designs into their projects.
Autoren/Hrsg.
Weitere Infos & Material
Chapter 2 First C Program
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. MPLAB and MPLABX Installation
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.
Figure 2.1 The 18F1220 pin diagram. But before we program our device we need to understand a little of the C language. A Brief Introduction to C for the Microcontroller
Turning an output on/off
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; Delays
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. Loops
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 Delay100TCYx(78); // wait 1 s } 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. Entering Numbers in the Program
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. Writing the Code Using MPLAB IDE
Run MPLAB and the screen shown in Figure 2.2 will open.
Figure 2.2 MPLAB workspace. 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).
Figure 2.3 Creating the file flash.c. The screen shown in Figure 2.4 showing the file editor will open.
Figure 2.4 File editor screen. If the line numbers are not visible on the left hand side turn them on with Edit/Properties as shown in Figure 2.5.
Figure 2.5 Editor properties. Select File Type/Line Numbers. Click Apply then. Click OK as shown in Figure 2.6.
Figure 2.6...