Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Práctica de Informática Industrial. Programación de Microcontroladores en C. El objetivo de esta práctica es practicar los conocimientos adquiridos en las clases de teoría sobre programación en C. Las instrucciones necesarias para la instalación y configuración del compilador (ya realizadas) se encuentran en el apéndice 1 (en inglés). En este mismo apéndice también encontramos la información necesaria para el uso del entorno de depuración ICD de microchip. En el apéndice 2 (en inglés) encontramos la descripción y el esquema de la placa de evaluación del 16F877 empleada. Es fundamental disponer además de la documentación entregada sobre el compilador HT-PICC lite y sobre el 16F87X. Se pide: 1. Realizar un programa que encienda todos los LEDs cuando se actiba el pulsador. 2. Realizar un programa que encienda y apague el LED0 (sin emplear interrupciones) con una frecuencia de 10Hz (modificar el incluido en el apéndice1). 3. Igual pero empleando el Timer 1 para generar interrupciones. 4. Realizar un programa que lea con una periodicidad de 10Hz el valor de tensión generado por el potenciómetro y lo represente en los LEDs. Una vez realizado el apartado 5 debe grabarse el programa en el 16f877 de forma que se ejecute sin tener que conectar la placa de evaluación al ICD. Para ello deben buscarse las opciones adecuadas del compilador y del ICD. El desarrollo de la práctica y sus resultados debe resumirse en una memoria a entregar por cada grupo de prácticas dentro de los 15 días posteriores al desarrollo de la misma. Apendix1: MPLab and Hi-Tech C Tutorial The following tutorial covers PIC micros, MPLab and Hi-Tech C. • • • • • how to install MPLab how to install Hi-Tech C how to set up a project for Hi-Tech C under MPLab. Note: Other methods do not support libraries, additional C files, or incremental compiles. how to use the simulator how to use the MPLAB-ICD emulator Getting to know the MPLab and Hi-Tech C environment Installing MPLab First off, you will need the development program for PIC micros, MPLab. The latest version of MPLab at the time of writing is 5.70. If you go to the Microchip web site http://www.microchip.com/ you will find the latest version of this software. A screenshot of the Integrated Development Environment (IDE) is shown below. Installing a C compiler MPLab comes with it's own assembler. However, with the power of microprocessors these days, a C compiler is the preferred choice. Programming in C is not only easier and more powerful, code is also portable to other microprocessors such as Atmel, TI, or Zylog. In this tutorial will be using the officially supported Microchip C compiler, Hi-Tech C. This is available from http://www.htsoft.com/. The latest version at the time of writing is 7.86pl3. It is recommended that earlier versions are not used. In my two years of experience with Hi-Tech C, I have found it to be an excellent product. There has never been any case where the compiler has been at fault - I believe that the latest version is bug free. Telling MPLab where to find the Hi-Tech C compiler 1. After you have installed Hi-Tech C, you have to tell MPLab where to find the C compiler. 2. In MPLab, select menu ‘Project’ then ‘Install Language Tool’. You should see the dialogue box as shown below. 3. Change the language suite to ‘Hi-Tech’, and the tool name to ‘PIC-C compiler’, as below. 4. Browse to the Hi-Tech C directory ‘C:\HT-PIC\BIN\’ and find the executable PICC.EXE as shown in the screenshot below. 5. Change the tool name to ‘PIC-C linker’ and repeat the process, as shown in the screenshot below.. Note: Both Linker and Compiler must be specified. Both Linker and Compiler point to PICC.EXE. Trial Version: for the trial version of Hi-Tech C, point both the Linker and Compiler to PICL.EXE Creating your first project for Hi-Tech C This particular tutorial is for the 12X, 16X and 17X version of Hi-Tech C (piccdemo.exe or the retail version). It does not support the 18X version of Hi-Tech C (picc18demo.exe or the retail verison). There is a FAQ on http://www.htsoft.com/showing how to integrate the 18X compiler with MPLab. Note: It is recommended that this method is used. Its not important at the moment, as the tutorial will teach the correct method, but selecting 'PIC C Compiler under 'node properties' for 'project[.hex]' makes it impossible to have multiple files, object files or libraries. If you follow these steps exactly, you will get a working project. 1. From the MPLab IDE, select the menu option ‘Project’ then ‘New Project’. Go into Explorer, and create a directory for your project on the hard drive. Back in MPLab, browse to this directory. 2. In MPLab, type in the project file name, ‘project.pjt’ then click ‘Ok’. This brings up the edit project window as shown below. 3. Select development mode to be ‘MPLab-SIM, 16F877’ as below. Ignore the warning that 'No hex file has been built for this project'. 4. Select language tool suite to be ‘Hi-Tech’ as below. Ignore the warning that 'You will lose all command line options'. 5. Click on ‘project [.hex]’ then ‘node properties’ as above. 6. You will see a window. Change 'Language Tool' to PIC C Linker, then match the options to the screen shot below. 7. Note the 'Compile for MPLAB-ICD' option switch below. This reserves 256 program words at the top of memory and various ram bytes, to make the .hex file compatible with the MPLAB-ICD emulator. The 'Compile for MPLAB-ICD' option is not needed with the ICEPIC 2000 emulator. 8. Note the ‘-fakelocal’ switch in ‘additional command line options' below. This allows one to view local variables inside a function. 9. Click ‘OK’, twice, to the back into the MPLab IDE. Click ‘File’ then ‘New File’. Save it as ‘project.c’, in your own directory, as shown below. Now there should be two files in this directory project.pjt and project.c. 10. Go back into the "edit project" dialogue by selecting menu ‘Project’ then ‘Edit Project’. 11. Click on ‘add node’ then add ‘project.c’ as shown below: 12. Click on project[.c] then "node properties". Change the options to match the picture below. All other options are unchanged from the defaults. 13. Click ‘Ok’, twice, to go back into editing the project.c file. 14. Well, we are almost there. Type a short skeleton program to satisfy the compiler: #include <pic.h> main() { } 15. Select menu ‘Project’ and 'Make Project (F10)’ to compile everything. You should see the results displayed, as in the screenshot below. 16. If you have any errors at all, such as 'MPLab cannot find .hex file' or 'Build tool not installed properly', check that the language tools are installed properly. See here. 17. Later on, if you have more files, libraries or object files, you can add them to the project by clicking on 'add node'. These files will be compiled individually, then linked at the end. 18. If you want incremental compiles, add the line 'c:\ht-pic\include' under 'include path' in the 'edit project' dialogue. 19. Finished. The sample project created with these instructions is available for download. 20. If you didnt get a working project, follow through it again, copying the instructions exactly, paying particular care to the screenshots. If you still cant get it working email mailto:[email protected]?subject=Project setup issues. Using the simulator to test code MPLab has a built in microprocessor simulator. If you compile C or assembly code you can test its operation by simulating what it does. Select menu ‘options’ then ‘development mode’. Set the development mode to ‘MPLAB-SIM simulator’ See menu ‘Debug’ and ‘Run’ for running, stopping, single stepping, and reset commands. Set breakpoints by right clicking on a line of C, and selecting ‘breakpoint’. The stopwatch under ‘window’ and ‘stopwatch’ is useful for timing how long code takes to run – set breakpoints before and after then run it. Using an In Circuit Debugger (ICD) Normally, when you construct a device using a microprocessor, you plug the microprocessor into the socket on the PCB and it works. An ‘In Circuit Debugger’ (ICD) plugs into the socket instead of the microprocessor, under the control of a computer. The advantage of an emulator over a stand-alone microprocessor is that you can see what's going on inside the chip. You can single step through lines of code, set breakpoints, view variables, and time how long sections of code take to execute. Its operation is exactly the same as using the simulator. The difference between an emulator and a simulator is that a simulator is just a program running on the computer. An emulator plugs into the target circuit, and can interact with the real world such as memory chips, button presses, and variable voltages. Using the MPLAB-ICD emulator In this tutorial, we will be using the MPLAB-ICD emulator. Read through the documentation for the MPLAB-ICD emulator, to get a basic idea of how it works. This is available in the MPLab IDE, under ‘Help’ then ‘MPLAB-ICD help’ Next, set up the emulator. 1. Connect the emulator to COM1 of the computer with a serial cable. 2. The emulator probe is like any microprocessor -- its needs power, the reset pin pulled high, and a clock. 3. Plug the 28-pin or 40-pin emulator probe into the development board, making sure that it is plugged in the correct way around. 4. Give the development board power by connecting in the 9V plug pack. The red LED should be flashing on the emulator probe, indicating that is ready to the reset. 5. Start up MPLab. Load up the project, as before. Select the menu option ‘Options’ then ‘Development Mode’. Select the options to match the dialogue box, as shown below. 6. Click ‘Reset’ and if the emulator is connected properly to the computer, you should see the diaglogue box as shown below. Change the options so that it matches the control box below. 7. These settings are the default for almost every situation, with the exception of the oscillator. When using an external crystal with speeds of higher than 8Mhz use the settings as shown above. Crystal speeds of lower than 8MHz use ‘XT’ for the oscillator setting instead ‘HS’. When using a RC oscillator for the clock, made up of a resistor and a capacitor, use ‘RC’ for the oscillator setting instead of ‘HS’. 8. Next, to test if the emulator is working, type in the sample program shown below or download the project then unzip it into ‘c:\pic\’: //sample program for MPLAB-ICD development board //set oscillator to ’RC’ for 2Mhz, then check to see if all red LEDs flash @ 2Hz #include <pic.h> main() { unsigned int i; TRISC=0b00000000; o=output, 1=input while(1) { PORTC=0xFF; for(i=0;i<28000;i++) {}; gives ~500ms PORTC=0x00; for(i=0;i<28000;i++) {}; gives ~500ms } } //set ports of port C to output //loop forever //turn leds on, 0xFF = 0b11111111 //delay, its 9 clock ticks per i++, @2Mhz //turn leds off, 0x00 = 0b00000000 //delay, its 9 clock ticks per i++, @2Mhz 9. Compile the program by selecting menu option ‘Project’ then ‘Make Project’. Or you can press F10. 10. Download the program into the flash chip of the emulator by clicking ‘Program’ in the white MPLAB-ICD control box as shown above. 11. When it is finished, run the program by selecting menu option ‘Debug’ then ’Run’ then ‘Run’. Or you can press F9. 12. You should see the 8 red lights on the development board flashing at 1Hz. 13. Finish. We welcome any suggesions or comments! Send them to Shane Tolmie on mailto:[email protected]. This site is a completely separate site to www.microchip.com, and is maintained independently of Microchip Ltd., manufacturers of the PIC micro. All code on this site is free for non-commercial use. Commercial use normally free, however, it is prohibited without contacting DesignREM Ltd. for permission. All content on this site created by Shane Tolmie is copyrighted by Shane Tolmie 2000, 2001, 2002, 2003. Apendix2: MPLAB ICD Demo Board The MPLAB ICD Demo Board is a simple demonstration board that supports PIC16F87X microcontrollers. The board can be used stand-alone with a preprogrammed part or with the MPLAB ICD Module and MPLAB ICD Header. A sample program is provided to demonstrate the unique features of the devices. Although a 40-pin and a 28-pin socket are provided to accommodate the different packages, only one processor can be run at a time. Processor Sockets Available sockets are: • 40-pin socket for PIC16F871/874/877 • 28-pin socket for PIC16F870/872/873/876 Additional 20- and 14-pin male to male headers have been provided as standoffs. Simply insert the desired headers onto the MPLAB ICD Demo Board and then plug the MPLAB ICD Header into the stand-offs. Without the stand-offs, the MPLAB ICD Header would not plug into the sockets. Or, simply insert the device directly onto the MPLAB ICD Demo Board. Display Eight red LEDs are connected to PORTC of each processor type. The PORTC pins are set high to light the LEDs. DIP Switches Eight DIP Switches are provided in a package as SW3. When all switches are in the ON position, each of eight red LEDs is connected to a pin of PORTC. Power Supply The MPLAB ICD Demo Board uses a 9.0V power supply to create a 5.0V VDD voltage to power the MPLAB ICD Module. This 9V power supply is NOT provided with the MPLAB ICD system. If you own a PICSTART Plus Programmer, you can use its power supply to power the MPLAB ICD Demo Board. The part number is AC162039. The minimum power supply specifications for the MPLAB ICD Demo Board as shipped are: • DC power supply: 9VDC @ 0.3A • With barrel connector: ID = 2.5 mm, OD = 5.5 mm, Barrel length: 10.0 mm, Inside positive ICD Connection A modular cable connection next to the power supply can be used to connect the MPLAB ICD Demo Board directly to the MPLAB ICD Module. _ 2000 Microchip Technology Inc. DS51184D-page 69 RS-232 Serial Port A RS-232 level shifting circuit has been provided to support connection to a RS-232 host through the DB9 connector. The port is configured as DCE, and can be connected to a PC using a straight through cable, as opposed to a null modem cable. The circuitry must be populated by the user: • U4 – Analog Devices MAX233A or equivalent • R14-R19 – 330&, 1/8Wresistor • C7-C11 – 0.1 µF capacitors • J4 – DB9 female right angle connector Push-Button Switches Two push buttons provide the following functions: • MCLR to reset the processor • Active low switch connected to RB0 Oscillator Options You can use the on-board RC oscillator circuit or plug an oscillator in the 4-pin socket. Make sure to set the jumper (JP1) to the proper selection. • Socket provided for clock oscillator – use an oscillator from 32 kHz to 20 Mhz • RC circuit – the frequency generated by the R3 resistor and C4 capacitor ranges from about 3.5 Mhz to 6 Mhz, depending on the operating voltage and ambient temperature. Analog Input A 1Kohm potentiometer is connected through a series 470 ohm resistor (to protect the part should the pin be configured as an output) to RA0/AN0. The port can be adjusted from VDD to GND to provide an analog input to the PIC16F87X parts. .