experiment 4: intermediate i/o programming …metalab.uniten.edu.my/~jamaludin/eeeb371/eeeb371...

13
EEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction to 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 LCD Display and Keypad 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.

Upload: vuonghuong

Post on 03-Apr-2018

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-1

EXPERIMENT 4: Intermediate I/O Programming (Introduction to 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 LCD Display and Keypad 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.

Page 2: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-2

MikroC IDE

Figure 4.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. 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. Note: Make sure you click Edit Project under Project tab to set the configurations as you as have set in MPLAB under configuration settings.

Page 3: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-3

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 #include.

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.

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; // Set PORTB as output PORTB = 0xFF; // Set PORTB bits to 1

Page 4: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-4

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;

LCD Display The 2x16 character LCD offers character display for embedded system. It can be used to display numerical information, text message and also special symbol. We can control a LCD using either 8 pins (8-bit interface: RD0-RD7) or 4 pins (4-bit interface: RD4-RD7), depending on the I/O pins that we have. For learning purposes, 4-bit interfacing using MikroC library is recommended as it will be relatively easy. Figure 4.1 shows the schematic for LCD connection in PTK40A Training Kit.

Figure 4.1 Connection of a 2x16 character LCD

Page 5: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-5

Controlling LCD Before using the LCD for display purpose, the LCD has to be initialized by sending the commands to initialize the LCD. After initializing the LCD, we can easily display any character we like on the LCD by using mikroC LCD library. The mikroC PRO for PIC provides library routines for communication with LCDs through the 4-bit interface. Library Routines:

• Lcd_Init • Lcd_Out • Lcd_Cmd

Page 6: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-6

Page 7: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-7

4x4 Keypad Keypad is an array of switch. There will be 2 terminal pins connected each time a button is pressed. For example; when button ‘1’ is pressed, pin COL1 and ROW1 is connected. Initially, there is no connection between rows and columns. The button connects it. The keypad’s pins need to be pulled up or pulled down to avoid floating case. Pull up normally connect to 5V and pull down is connect to ground. 4x4 Keypad pin can directly connect to microcontroller or keypad decoder IC. In this section, we are trying to scan the button (character) being pressed on keypad. The 4x4 keypad is a 16-way XY-Matrix hexadecimal keypad. It has eight connections where four pins for row (ROW1-ROW4) and four pins for column (COL1-COL4). The internal connection for the keypad is shown in Figure 4.2.

Page 8: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-8

Figure 4.2 Internal Connection of a 4x4 Keypad

From the schematic in Figure 4.3, the row pins are connected directly to PIC, and being configured as writeable pins (Output from PIC). Column pins are being pulled high to 5V, and being configured as readable pins (Input to PIC). To check which button is pressed, users need to scan it column by column and row by row. Make rows as output and columns as input as explained earlier. For example, set (5V, high logic) all rows by default. 1st scan, clear (logic low) row 1 and scan column 1 to column 4 (‘1’, ‘2’, ‘3’ and ‘A’) for low logic, this will determine which button is pressed in row 1. If one of those buttons is pressed, record it and jump out from the scanning loop and continue with the action required. If none button is pressed in row 1, set it back to default (logic high) and clear row 2. Scan column 1 to column 4 again and this will scan button press of ‘4’, ‘5’, ‘6’ and ‘B’. This process should be repeated until all four rows are being completed scanned.

Page 9: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-9

Figure 4.3 Connection 4x4 keypad to microcontroller

Page 10: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-10

Procedure Part A: LCD Display

1. Write the source code in MikroC Compiler, build it and download the Hex file into the

microcontroller and run the program. Print out the source file and write down your observation.

2. From Help menu in mikroC, click on the search tab and type lcd. From the example in the help file, change the program so that the text will be displayed at the second row, third column and the text display should shift left continuously. Adjust the time delay accordingly so that the text can be visible while shifting left. Write down your observation and explain on the changes which have been made. Print out the source file.

//Put students’ ID No and Names here! // LCD module connections sbit LCD_RS at RA2_bit; sbit LCD_EN at RA3_bit; sbit LCD_D4 at RD4_bit; sbit LCD_D5 at RD5_bit; sbit LCD_D6 at RD6_bit; sbit LCD_D7 at RD7_bit; sbit LCD_RS_Direction at TRISA2_bit; sbit LCD_EN_Direction at TRISA3_bit; sbit LCD_D4_Direction at TRISD4_bit; sbit LCD_D5_Direction at TRISD5_bit; sbit LCD_D6_Direction at TRISD6_bit; sbit LCD_D7_Direction at TRISD7_bit; // End LCD module connections char *text = "HELLO WORLD"; void main() { ADCON1 = 0x0F; // Configure all ports with analog function as digital CMCON = 7; // Disable comparators Lcd_Init(); //Initialize LCD based on LCD module connections Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off Lcd_Out(1,3,text); //Display text at first row, third column }

Page 11: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-11

PART B: Keypad 3. Complete and write the source code below in MikroC so that each keypad characters will

be displayed at the LCD display when the respective keypads are pressed.

//Put students’ ID No and Names here! // LCD module connections sbit LCD_RS at RA2_bit; sbit LCD_EN at RA3_bit; sbit LCD_D4 at RD4_bit; sbit LCD_D5 at RD5_bit; sbit LCD_D6 at RD6_bit; sbit LCD_D7 at RD7_bit; sbit LCD_RS_Direction at TRISA2_bit; sbit LCD_EN_Direction at TRISA3_bit; sbit LCD_D4_Direction at TRISD4_bit; sbit LCD_D5_Direction at TRISD5_bit; sbit LCD_D6_Direction at TRISD6_bit; sbit LCD_D7_Direction at TRISD7_bit; // End LCD module connections int column; void getkey() //a function to check which column is active { column=0; if(Button(&PORTB, 0, 1, 0)) //if RB0 is low, column 1 is active { column=1; } else if(Button(&PORTB, 1, 1, 0)) //if RB1 is low, column 2 is active { column=2; } else if(Button(&PORTB, 2, 1, 0)) //if RB2 is low, column 3 is active { column=3; } else if(Button(&PORTB, 3, 1, 0)) //if RB3 is low, column 4 is active { column=4; } }//End of getkey()

Page 12: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-12

void main() { int stat; //Assign status (stat) to integer char *key; //Assign key (to hold keypad values) to char TRISB=0xFF; //Configure PortB as input TRISD=0x00; //Configure PortD as output ADCON1 = 0x0F; // Configure all ports with analog function as digital CMCON = 7; // Disable comparators Lcd_Init(); //Initialize LCD based on LCD module connections Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off Lcd_Out(1,1,"Keypad Pressed:"); //Display ‘Keypad pressed:’ at row1,column1 do //continuous do..while loop { stat=0; //Initialize status (stat) to 0 //enable 1st row (logic low) - other rows remain logic high PORTD=0xEF; //scanning keypad for first row getkey(); //and get the input of the keypad if(column==1) //if column 1 is low, it means that { key="1"; // key ‘1’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} //as an indicator that a key has been pressed else if(column==2) //if column 2 is low, it means that { key="2"; // key ‘2’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} else if(column==3) //if column 3 is low, it means that { key="3"; // key ‘3’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} else if(column==4) //if column 4 is low, it means that { key="A"; // key ‘A’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} if(stat==1) //reset back stat value {delay_ms(100); //a short delay before checking for the next input stat=0;}

Page 13: EXPERIMENT 4: Intermediate I/O Programming …metalab.uniten.edu.my/~jamaludin/EEEB371/EEEB371 1718S1 PIC_exp4.pdfEEEB371 E4-1 EXPERIMENT 4: Intermediate I/O Programming (Introduction

EEEB371 E4-13

4. Build the source code, download and run the application. Write down your observation. Print out the source code.

5. Turn off the PTK40A power supply, rearrange the USB cable back into the Training Kit. Exit from MPLAB and PICKit 2 programmer. Shutdown your PC and rearrange your workstation before you leave.

//enable 2nd row (logic low) - other rows remain logic high PORTD=0xDF; //scanning keypad for second row getkey(); //and get the input of the keypad if(column==1) //if column 1 is low, it means that { key="4"; // key ‘4’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} else if(column==2) //if column 2 is low, it means that { key="5"; // key ‘5’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} else if(column==3) //if column 3 is low, it means that { key="6"; // key ‘6’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} else if(column==4) //if column 4 is low, it means that { key="B"; // key ‘B’ is pressed Lcd_Out(1,16,key); //display at row1,column16 at LCD display stat=1;} if(stat==1) //reset back stat value {delay_ms(100); //a short delay before checking for the next input stat=0;} //enable 3rd row (logic low) - other rows remain logic high …..complete the program //enable 4th row (logic low) - other rows remain logic high …..complete the program }while(1); //End of Do…While loop }//End of void main()