Lab 4 microcontroller

10
CENTRE OF DIPLOMA STUDIES COMPUTER ADDED DESIGN LABORATORY LABORATORY INSTRUCTION SHEET Subject Code and Name DEK 3133 MICROCONTROLLER Experiment Code 04 Experiment Title Introduction to Keypad and LCD program Course Code DET/DEE/DEX

Transcript of Lab 4 microcontroller

Page 1: Lab 4 microcontroller

CENTRE OF DIPLOMA STUDIES

COMPUTER ADDED DESIGN LABORATORY

LABORATORY INSTRUCTION SHEET

Subject Code and NameDEK 3133 MICROCONTROLLER

Experiment Code 04

Experiment Title Introduction to Keypad and LCD program

Course Code DET/DEE/DEX

Page 2: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 1

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

SUBJECT INFORMATION

SUBJECT : DEK 3133 MICROCONTROLLER

TOPIC : Lab 4 – Introduction to Keypad and LCD program.

AIM To apply the knowledge and understanding on theory and applications of keypad.

1 OBJECTIVES

1.1 To understand the design of keypad.

1.2 To determine and analyze the function of keypad.

1.3 To understand the configuration of LCD.

2 EQUIPMENT

2.1 PIC Development Board – PICDEV

2.2 PIC16F877A

2.3 MPLAB IDE Program

2.4 Mikro C

2.5 Proteus

2.6 The PIC Development Board User manual

2.7 Power supply 9V

3 THEORY

3.1 KEYPAD SYSTEMKeypads are an excellent way of entering data into the microcontroller. The keys are usually numbered butthey could be labeled as function keys, for example in a remote control handset in a TV to adjust the sound orcolor, etc.

As well as remote controls, keypads find applications in burglar alarms, door entry system, calculators,microwave ovens, etc. Keypads are usually arranged in a matrix format to reduce the number of I/Oconnections. A 12 keypad is arranged in a 3 x 4 format requiring seven connections.

Page 3: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 2

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

This is arranged in three columns and four rows as shown in Table 1. There are seven connections tothe keypad – C1, C2, C3, R1, R2, R3 and R4.

Column1,C1 Column2,C2 Coulmn3,C3

Row1, R1 1 2 3

Row2, R2 4 5 6

Row3, R3 7 8 9

Row4, R4 * 0 #

Table 1

This connection to PIC is shown in Figure 1. The keypad works in the following way:

If, for example, key 6 is pressed then A3 will be joined to B5. For key 1, A1 would be joined to B4, etc. asshown in Figure 1.

RB0

RB1

RB2

RB3

RA1RA2RA3

1 2 3

4 5 6

7 8 9

* 0 #

RB4

RB5

RB6

RB7

Row1

Row2

Row3

Row4

Port A [1:3] = output Port B [0:3] = output and Port B [4:7] = input

Figure 1

The figure 1 above show the 4x3 keypad size which is 3 columns and 4 rows. To tell the PIC which button ispressed, the PIC need to set only one of the column to be high at one time. Then the PIC will scan which row is

Page 4: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 3

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

at current column send the HIGH value (1) to the PIC input at PORTB. For example, Let say the PIC need torecognize the buttons at column 1 which the available button are 1, 4, 7 and *. So the PIC will send thePORTA.F1 (COLUMN 1) to HIGH (1). Then the PIC will scan each row of PORTB begin from PORTB.F4(ROW 1). At this stage, if the scanned result is HIGH that means the pressed button is equal to 1. If the result isLOW (0), then the PIC will continue to scan for the next row(ROW2, ROW3, and ROW4). When all rows atPORTB are scanned, the PIC will send the HIGH (1) value to the next column (COLUMN 2 at PORTA.F2)and repeat to scan each row of port B. This process is repeated to all columns. When the last column(COLUMN3: PORTA.F3) is reached, PIC will scan again for all rows at column1, 2 and so on.

The value need to submit to the columns and read the rows value are actually can be vary. It depends on howthe keypad is designed.

3.2 LCD Display

Figure 2: 4 Bit Interface of 16x2 LCD

Figure 2 showing a Liquid Crystal Display (LCD) which is 16 x 2 characters. It has 4 bit interface data(D4,D5,D6,D7) which is parallel connected to the PIC port.

Page 5: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 4

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

3.2.1 Pin FunctionsSignal Function

RSSelects registers.0: Instruction register (for write) Busy flag:address counter (for read)1: Data register (for write and read)

R/W

Selects read or write.0: Write1: Read

E Starts data read/write.

D4 to D7

Four high order bidirectional tristate data buspins. Used for data transfer and receive betweenthe PIC and the LCD. DB7 can be usedas a busy flag.

VSS Ground

VDD Power

VEE Contact with variable resistor to control the LCD contrast

Prepared By:

Signature :Name: Mohamad Bin Md. SomDate: 3 August 2010

Approved by:

Signature:Name: Shamsul B. MohamadDate: 3 August 2010

Page 6: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 5

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

4 ATTENTION

4.1 Do not move any IC or device inside the board without any order from your instructor.

4.2 Make sure all jumpers on the development board are placed in correct positions.

5 EXPERIMENT PROCEDURE

5.1 KEYPAD (See Circuit 1)5.1.1 The code below is shown how to scan the keypad buttons at column 1 (button 1, 4, 7 and*). Pressed button will switch ON the LED base on the binary numbers of the buttonlocation. Ie. Button no. 1 = 0001, button no. 4 = 0100, button no. 7 = 0111, and button * =1111. Type the code below and test on the development board. Write your observation.//####################################START###########################define COLUMNS PORTA#define ROW1 PORTB.F4#define ROW2 PORTB.F5#define ROW3 PORTB.F6#define ROW4 PORTB.F7

void main(void){

TRISA = 0b00000000; //portA as outputTRISB = 0b11110000; //portB at High nibble: input, Low nibble: outputPORTB = 0b00000000; //Clear PORTBPORTA = 0b00000000; //Clear PORTA

while(1) //endless loop{

//==============Column1==============================COLUMNS = 0b00000010; //Set only column1 at PORTA.F1 to high = 1 (For button 1 , 4 , 7, and *)//Scan Row 1 at column1if (ROW1==1){ //Button no 1 is press?

while(ROW1==1)continue; //wait here until button is released.PORTB = 0b00000001; //Display LED1,2,3,4 with binary of no. 1

}//Scan Row 2 at column1else if (ROW2==1){ //Button no 4 is press?

while(ROW2==1)continue; //wait here until button is released.PORTB = 0b00000100; //Display LED1,2,3,4 with binary of no. 4

}//Scan Row 3 at column1

Page 7: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 6

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

else if (ROW3==1){ //Button no 7 is press?while(ROW3==1)continue; //wait here until button is released.PORTB = 0b00000111; //Display LED1,2,3,4 with binary of no. 7

}//Scan Row 4 at column1else if (ROW4==1){ //Button no. * is press?

while(ROW4==1)continue;PORTB = 0b00001111; //Display LED1,2,3,4 with binary of no. *

}

//=====??????????====Column2 =======????????==========

//=====??????????====Column3 =======????????==========

}//end of while loop}//end of main function//####################################END##########################

5.2 Exercise 1 : Keypad

5.2.1 Base on the code above, complete the source code, so that the PIC can scan all the pressedbuttons and show it on LED as a binary value.5.2.2 Draw a flow chart for the complete program.5.3 LCD Display (See Circuit 2)5.3.1 The code below is a program to show texts in LCD 16x2. Type the code and test it on thedevelopment board or Proteus simulation. Write your observation on the report.//####################################START##########################char *text1 = "Hello";char *text2 = "PIC16F877A";

void main(void){

TRISB = 0; //Port B as outputLcd_Init(&PORTB); //Initialize LCD to use PORTB//Configure LCD at PORTB , RS, EN, WR, D7, D6, D5, D4 with pin settingsLcd_Config(&PORTB,3,1,2,7,6,5,4);

while(1){

Lcd_Cmd(Lcd_CLEAR); //Clear LCD displayLcd_Out(1, 1, "DEK3133"); //Print text on LCD at line 1, char 1Lcd_Out(2, 3, "Microcontroller"); //Print text on LCD at line 2, char 3

Page 8: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 7

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

Delay_ms(2000); //Delay for 2 secondLcd_Cmd(Lcd_CLEAR);Lcd_Out(1, 1, text1);Lcd_Out(2, 3, text2);

Delay_ms(2000);}

}//####################################END##########################

5.4 Exercise 2: LCD and Button(PORTA.F4)5.4.1 Base on the circuit 2(see attachment). Write a program that will show a text “BUTTON ISPRESSED” on the LCD when the button at PORTA.F4 is pressed and show a text “BUTTONIS NOT PRESSED” if the button at PORTA.F4 is not pressed.5.4.2 Draw a flow chart for a complete program.6 REPORT PREPARATION AND SCHEMA.

(1) 2 persons for 1 report.

(2) Due date to send report is 1 weeks after lab date.

(3) Report schema following below requirements:

Lab report cover sheet for 1st page.

Objective, theory, equipments for the 2nd page. (5) ( 5 M )

Observations. (20)

1.Keypad observations from 5.1.1 (10 M)

2.LCD observations from 5.3.1 (10 M )

Result. (40)

1. Keypad: Exercise 1 source code & Flow Chart ( 20 M )

2. LCD: Exercise 2 source code & Flow Chart (20 M)

Discussion. (20)

1. By using all the PORT A and PORT B, what is the maximum buttons for PIC16F877A can support?Draw a diagram to support your answer. (15 M)

2. Give the example of applications that using a keypad with PIC. (5 M)

Page 9: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 8

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

Conclusions. (15)

7 CIRCUIT ATTACMENT

Circuit 1: KEYPAD and LED

Page 10: Lab 4 microcontroller

Document ReferenceNo. RPP-05 Page. Number P a g e | 9

Document TitleLABORATORYPRACTICUM

Edition 1

Revision No. 4

Effective Date 03/8/2010

Amendment Date 03/8/2010

Circuit 2: LCD with Button