O Programming using MikroC

EXPERIMENT 6: I/O Programming using MikroC Objective: • Introduce students to MikroC compiler for PIC. • Understand I/O pin assignments and learn to p...
Author: Earl Henry
39 downloads 3 Views 414KB Size
EXPERIMENT 6: I/O Programming using MikroC Objective: • Introduce students to MikroC compiler for PIC. • Understand I/O pin assignments and learn to program the I/O port.

• •

To familiarize with Input Output Interfacing To utilize time delay in Input Output Interfacing

Pre-lab: Download MikroC PRO evaluation software and install in your personal computer/laptop. Familiarize yourself with the compiler before coming to the lab. Download link: http://www.mikroe.com/eng/product_downloads/download/ Introduction There are several C compilers on the market for the PIC18 series of microcontrollers. These compilers have many similar features, and they can all be used to develop C-based high-level programs for PIC18 microcontrollers. Some of the C compilers used most often in commercial, industrial, and educational PIC18 microcontroller applications are: • MikroC • PICC18 • C18 • CCS The popular and powerful MikroC, developed by MikroElektronika is easy to learn and comes with rich resources, such as a large number of library functions and an integrated development environment with a built-in simulator and an in-circuit debugger (e.g., mikroICD). A demo version of the compiler with a 2K program limit is available from MikroElektronika. PICC18, another popular C compiler, was developed by Hi-Tech Software and is available in two versions: standard and professional. A powerful simulator and an integrated development environment (Hi-Tide) are provided by the company. PICC18 is supported by the PROTEUS simulator (www.labcenter.co.uk) which can be used to simulate PIC microcontroller–based systems. A limited-period demo version of this compiler is available on the developer’s web site. C18 is a product of Microchip Inc. (web site: www.microchip.com). A limited-period demo version as well as a limited functionality version of C18 with no time limit, are available from the Microchip web site. C18 includes a simulator and supports hardware and software development tools such as in-circuit emulators (e.g., ICE2000) and in-circuit debuggers (e.g., ICD2). CCS has been developed by the Custom Computer Systems Inc. (web site: www.ccsinfo.com). The company offers a limited-period demo version of their compiler. CCS provides a large number of built-in functions and supports an in-circuit debugger (e.g., ICD-U40) which are very helpful in the development of PIC18 microcontroller–based systems. In this lab, we are mainly concentrating on the use of the MikroC compiler.

MikroC IDE

Figure 6.0 MikroC desktop MikroC allows users to quickly develop and deploy complex applications: • Write C source code using the highly advanced Code Editor • Use the included MikroC libraries to dramatically spped up the development: data acquisition, memory, displays, conversions, communications.. • Monitor user program structure, variables, and functions in the Code Explorer. Generate commented, human-readable assembly, and standard HEX compatible with all programmers. • Inspect program flow and debug executable logic with the integrated Debugger. Get detailed reports and graphs on code statistics, assembly listing, calling tree... • Provide plenty of examples to expand, develop, and use as building bricks in projects. Port Programming Depending on the type of microcontroller used, PIC microcontroller input-output ports are named as PORTA, PORTB, PORTC, and so on. Port pins can be in analog or digital mode. In analog mode, ports are input only and a built-in analog-to-digital converter and multiplexer circuits are used. In digital mode, a port pin can be configured as either input or output. The TRIS registers control the port directions, and there are TRIS registers for each port, named as TRISA, TRISB, TRISC, and so on. Clearing a TRIS register bit to 0 sets the corresponding port bit to output mode. Similarly, setting a TRIS register bit to 1 sets the corresponding port bit to input mode. Ports can be accessed as a single 8-bit register, or individual bits of a port can be accessed. In the following example, PORTB is configured as an output port and all its bits are set to a 1: TRISB = 0; PORTB = 0xFF;

// Set PORTB as output // Set PORTB bits to 1

Similarly, the following example shows how the 4 upper bits of PORTC can be set as input and the 4 lower bits of PORTC can be set as output: TRISC = 0xF0; Bits of an input-output port can be accessed by specifying the required bit number. In the following example, variable P2 is loaded with bit 2 of PORTB: P2 = PORTB.F2; All the bits of a port can be complemented by the statement: PORTB = ~PORTB; Building Application in MikroC MikroC organizes applications into projects, consisting of a single project file (extension .ppc) and one or more source files (extension .c). You can compile source files only if they are part of a project. A. New Project The easiest way to create a project is by means of New Project Wizard, drop down menu Project>New Project. Just fill the dialog with desired values (project name and description, location, device, clock, config word) and MikroC will create the appropriate project file. Also, an empty source file named after the project will be created by default.

B. Add/Remove Files from Project Project can contain any number of source files (extension .c). The list of relevant source files is stored in the project file (extension .ppc). To add source file to your project, select Project>Add to Project from the drop-down menu. To remove file(s) from your project, select Project>Remove from Project from drop-down menu. Note: For inclusion of header files, use the pre-processor directive

C. Compilation When the user have created the project and written the source code, you will want to compile it. Select Project>Build from the drop-down menu, or click Build icon, or simply hit CTRL+F9.

Progress bar will appear to inform you about the status of compiling. If there are errors, you will be notified in the Error Window. If no errors are encountered, MikroC will generate output files as follows: • .hex – Intel style hex records. Use this file to program PIC MCU. • .mcl – Binary distribution of application that can be included in other projects. • .lst – Overview of PIC memory allotment: instruction addresses, registers, routines, etc. • .asm – Human readable assembly with symbolic names, extracted from the List File. After compiling your program in MikroC, you can click View Assembly icon or select Project>View Assembly from the drop-down menu to review generated assembly code (.asm file) in a new tab window.

Procedure Part A: Running Light

1. Construct the following Circuit

Figure 6.1: Running Light Circuit

2. Complete the following source code so that the LED will light up one after another sequentially from LED L1 until LED L7 and the sequence repeats continuously.

!

"

#$ % & $ ' " ! &() +, ! ! + % . & $ '" ! &() * +, ! ! + % / & $ ' " $ &() 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 $ " 01 00000000000000000001 2 *

2

3. Write the source code in MikroC Compiler, build it and download the Hex file into the microcontroller and run the program. Print out the listing file and write down your observation.

4. Change the delay to 500ms, build, download and run the application. After that remove or commented out all delay instruction, build, download and run the Application. Write down your observation for both programs.

PART B: Controllable Running Light

Figure 6.2: Controllable Running Light Circuit

5. A push button in a Pull Up connection is already connected to PORTA Pin 4 (the push button is situated above the PIC on the board). This push button will be used for this application as an input to control the running light. 6. Complete and write the source code below in MikroC so that when the push button is released, all LED will be turn OFF else if it is pressed the running light application works as in PART A.

%55

& $ '"

&() !

" 6" / ! "

000001 #$ $ !# -$ 615/

% * +, ! % . * +, ! % / 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 $ " 01 00000000000000000001 2 ! 615/

-$ -4 " !$ 7 ! " !! & $ ' " ! &() ! + & $ '" ! &() ! + & $ ' " $ &()

0 0 0 0 0 0 112

&()

2 2

7. Build the source code, download and run the application. Write down your observation on the output based on the switch condition. Print out the source code.

Bit no

Transition from 1 to 0 (release of button)

8. Modify the code so that when a push button is pressed the interval between each LED to light up is 0.5 second. While when the push button is released, the Interval is 2 seconds. Build, download and run the application, get verification from the lab instructor. 9. (% 3 4

-

"

Components List: 1. Breadboard 2. 8 x LED 3. 8 x 330Ω resistor

1