introduction - Faculty Home - Universiti Teknologi Malaysia

Transcription

introduction - Faculty Home - Universiti Teknologi Malaysia
MODULE FOR APPLICATIONS OF 4X4 KEYPAD
AND LCD PANEL USING ATMEGA32
MOHAMAD FAKHRUL HAYAT BIN IDRIS
UNIVERSITI TEKNOLOGI MALAYSIA
i
i
MODULE FOR APPLICATIONS OF 4X4 KEYPAD
AND LCD PANEL USING ATMEGA32
MOHAMAD FAKHRUL HAYAT BIN IDRIS
A report submitted in fulfillment of the
requirements for the award of the degree of
Bachelor of Electrical & Electronic Engineering
Faculty of Electrical Engineering
Universiti Teknologi Malaysia
JUNE 2015
ii
iii
ACKNOWLEDGEMENT
I would to convey my deepest thanks and gratitude to my supervisor En. Zuraimi Yahya
for every support and knowledge lent throughout the project progress. I also would give my
appreciation to lab assistant En. Jeffri for the aid given.
Furthermore, I owe my gratitude toward En. Nasir and En. Arif for support provide while
accomplishing this task.
iv
ABSTRACT
A module to understand the operation of a device which consist of AtMega32, a keypad
and a LCD screen which capable of receiving inputs from the keypad and display them on the
LCD screen. The keypad operates by giving inputs from its row and column and the program
reads the input to decide which button is pressed. The input is then displayed in the LCD screen.
The flow progress of the program will provide understanding of the process involved as well as
some initial procedures involved.
v
ABSTRAK
Satu modul untuk memahami operasi sebuah alat yang terdiri daripada AtMega32, satu
papan kekunci dan satu skrin LCD yang berupaya untuk menerima input daripada papan kekunci
tersebut dan menayangkannya pada skrin LCD tersebut. Papan kekunci tersebut beroperasi
dengan member input daripada baris dan lajurnya dan seterusnya satu program akan membaca
input tersebut untuk menentukan butang mana ditekan. Input tersebut kemudian dipaparkan pada
skrin LCD. Progres aliran program tersebut akan memberi kefahaman tentang prosess terlibat
dan sedikit sebanyak tentang beberapa prosedur awal yang terlibat.
vi
TABLE OF CONTENT
CHAPTER
1
2
TITLE
PAGE
TITLE PAGE
ii
DECLARATION PAGE
iii
ACKNOWLEDGEMENT
iv
ABSTRACT
v
ABSTRAK
vi
TABLE OF CONTENTS
vii
LIST OF TABLES
xi
LIST OF FIGURES
x
LIST OF APPENDICES
xi
INRODUCTION
1.1 Background of Research
1.2 Statement of Problem
1.3 Objectives of the Research
1.4 Aim of Research
1.5 Scope of the Study
1.6 Significance of the Research
LITERATURE REVIEW
2.0 General
2.1 AtMega32
2.1.1 Overview
2.1.2 Specification
2.1.3 Device Architecture
2.2 JTAGICE mkII
2.2.1 Overview
2.2.2 Connection through JTAG interface
2.3 LCD Screen
2.3.1 Overview
2.3.2 Applications
1
2
3
3
3
4
5
6
6
7
9
9
10
10
vii
2.3.3
2.3.4
3
4
5
Methods of Operation
LCD_Write_command
METHODOLOGY
3.0 Introduction
3.1 Software Implementation
3.1.1 Initialization
3.1.2 LCD Functions
3.1.3 Keypad Functions
3.2 Hardware Configuration
3.2.1 The connections of 4x4 keypad to the Atmega32A chip on
AtMega32 board
3.2.2 The connections of 20x2 LCD to the Atmega32A chip on
AtMega32 board
3.2.3 The connections of LCD and keypad to AtMega32
microcontroller and board
3.2.4 Safety precautions and known issues.
11
11
13
15
16
19
23
24
25
26
RESULTS AND DICUSSION
4.0 Introduction
4.1 Results of Program Execution
4.2 Discussion
27
28
30
CONCLUSION AND RECOMMENDATION
5.0 Conclusion
5.1 Limitation
5.2 Future Recommendations
31
32
32
REFERENCES
34
APPENDIX A
35
APPENDIX B
43
APPENDIX C
52
iix
LIST OF TABLES
TABLE NO.
TITLE
2.0 Comparisons between AtMega series
2.1 AtMega32 Target Board parts function
2.2 LCD_Write_Command bit functions
PAGE
6
8
12
ix
LIST OF FIGURES
FIGURE NO.
TITLE
PAGE
2.0
AtMega32 Target Board
7
2.1
JTAGICE mkII programmer
9
2.2
JTAG 10 pin connector
10
2.3
20x2 16 pins LCD
10
2.4
20x2 LCD address
11
3.0
Flow Chart of Research Activities
14
3.1
Hex addresses for individual Hex key
20
3.2
Debouncing waits the “bounces” finish first
before takes the reading
20
3.3
Flowchart for the program operation steps
22
3.4
Rows and columns of the 4x4 keypad
23
3.5
Pins function for the 16 pins LCD
24
3.6
Circuit connection of the devices
25
4.0
Introductory display
28
4.1
Awaiting user input
28
4.2
Some of displayed results from pressing the buttons
29
4.3
Relationships between address and rows and columns
30
x
LIST OF APPENDICES
APPENDIX
TITLE
PAGE
A
Tutorial Module
35
B
LCD and 4x4 matrix keypad interfacing program
43
C
Cost and Budget table
52
xi
CHAPTER 1
INTRODUCTION
1.1 Background of Research
AtMega32 is single-chip microcontroller produce by the Atmel cooperation. The
microcontroller belongs in the megaAVR series along with tinyAVR and XMEGA. In overall,
the AtMega series is the more powerful version of tinyAVR by providing more memories, pin
and functions.
The device can be used for many applications such as a controller. The process of turning
the chip into a working controller is an interesting challenge as there are my procedures involved
as well as some knowledge in electronics.
1
1.2 Statement of Problem
Even thought the design of AtMega making it easy to use, there are many elements that
should be taken into consideration. AtMega is programmable using Assembly or C Language via
several compiler software such as AVR Studio and Arduino. Programming in such language
requires deep understanding of basic programming methods and hardware knowledge. Most
problems comes from that certain hardware requires specific initialization codes in order to make
it functions and these codes usually written during the process of debugging and testing the
hardware.
Like most of other devices, a keypad and LCD needs proper steps in using them with
AtMega32. Therefore,
A keypad functions like many switches combined together. Unlike switches, a keypad
came select which bit for the operation making them more convenient than switches. As for an
output port, a LCD screen is great as it can be used for many functions.
Why keypad and LCD.
Therefore, based on the following above, these questions arise in order to inspire the
research problem:
a) What are the initial steps in using AtMega boards and its hardware?
b) How does the program control the Hex Keypad and LCD panel?
c) What information is obtained from the project?
2
1.2 Objectives of the Research
The objectives for this research are stated as follows:
i.
To write programs that operates a keypad and LCD connected to AtMega32.
ii.
To build a tutorial module on how to program a keypad and LCD that is connected to
an ATmega32.
1.3 Aim of Research
This research aims to documents the procedure of various functions of AtMega32 in
terms of software and hardware approach.
1.4 Scope of the Study
The program is written using C language on AVR Studio platform. The study covers the
fundamental in using AtMega32 board using AVR Studio as the programming platform. Further
section discusses the procedure on employing the keypad and LCD using AtMega32 through the
platform.
C language will be used to write program for the ATmega32. The tutorial will be base of
reading information from keypad, displaying information to LCD
3
1.5 Significance of the Research
This project aims to provide a helpful guide in understanding AtMega32 and its
related hardware. The methods of using AtMega32 target board can provide basis of
other studies that uses similar approach.
4
CHAPTER 2
LITERATURE REVIEW
2.0 General
Literature review is an account of what has been published on a research area. A
literature review was carried out to establish some knowledge of the research topic.
Literature review materials consist of journal, articles, reference books, publisher,
standard guideline and handbooks from all kinds of media including writing and web. It
can also guide researcher through the kind of work that others author’s studies case have
done.
5
2.1
Atmega32
2.1.1 Overview
AtMega32 is a microprocessor from AtMega family produced by Atmel Corporation.
AtMega32 is the bigger and more powerful version of AtTinyAvr with more memories, pin
numbers and functions.
2.1.2 Specification
The specifications of Atmega32 are generally higher than other microcontroller in the
same series even though most of the differences are in memory size, boot loader support and
interrupt size [1].
Device
Flash
EEPROM
RAM
Interrupt Vector Size
Memory
AtMega328P
32K Bytes
1K Bytes
2K Bytes
2 instructions words/vector
AtMega168PA
16K Bytes
512 Bytes
1K Bytes
2 instructions words/vector
AtMega88PA
8K Bytes
512 Bytes
1K Bytes
1 instructions words/vector
AtMega48PA
4K Bytes
256 Bytes
512 Bytes
1 instructions words/vector
Table 2.0: Comparisons between AtMega series.
6
2.1.3 Device Architecture
AtMega32 microcontroller consists of 28 pins with 4 ports for input or output. Each port
has 4 pins for 4 bits I/O which determined by the program initialization.
The AtMega32 board consists of many devices placed together on a board. The 4x4
keypad used for this project is located on this board and it is already hardwire to the board and
no further connections are required. The other parts used are the LCD1602 port which controls
the energy aspect of LCD.
4
1
3
5
2
Figure 2.0: AtMega32 Target Board.
7
No
Device
Description
1
4x4 keypad
16 buttons used to give inputs to the chip. It is connected to PORTB of
the micro controller
2
1602 LCD
port
The port gives VCC and GND to LCD as well as pin that control the
contrast of LCD. The contrast can adjusted from the variable resistor
next to it.
3
AtMega32
chip
The 40 pins microcontroller which is the primary components in the
board. The VCC and GND are pre-connected as well as to other device
on the board.
4
8bit LEDs
The LEDs which gives output for the keypad. The output given is
according to keypad coordinates in for of hexadecimal which
correspond to the row and column.
5
JTAG port
The port which the JTAGICE mkII programmer is connected. When the
programmer is connected, the pre-programmed board will go into
standby steps and only runs when control by AVR software.
Table 2.1: AtMega32 Target Board parts function.
The reset button located on the board is used to rest back the program into beginning and
reruns it again.
8
2.1 JTAGICE mk II
2.2.1
Overview
JTAGICE mkII is a debugging platform necessary debugging tool for programming ATMega32
board. The device is connected to AtMega32 with a 10pin cable and connected to the computer by a USB
cable or RS-232.
Figure 2.1: JTAGICE mkII programmer.
2.2.2
Connection through JTAG interface
JTAGICE mkII is connected through the target by a least 6 wires consisting the signals TCK, TDO,
TDI, TMS, VTref and GND. Two unused line nSRST and nTRST is reserved for other equipment [1].
To connect to the AVR Studio, JTAGICE mkII must be connected between the front end PC and the
target board. All of this connection needs to be made before starting AVR studio to ensure auto detection
from AVR Studio.
9
Figure 2.2: JTAG 10 pin connector
2.2 LCD Screen
2.3.1
Overview
The LCD screen used is a 16-pin with a dimension of 20x2. This LCD is capable of displaying
many types of characters as well as custom characters. The LCD uses outputs from two ports
where one for 8 bit data port and another port for register/enable input together with pins to
control the display such VCC, GND and brightness [5].
2.3.2 Applications
The LCD is used as an output device that will receive output from a port and process the
output into a display. The LCD also preprogrammed with other LCD modules.
Figure 2.3: 20x2 16 pins LCD.
10
2.3.3 Methods of Operation
The LCD is controlled by the HD44780 controller which contains 80byte of Display Data
Random Access Memory (DDRAM) [2]. Each space in the display is stored in a specific address
which unique to each space.
Figure 2.4: 20x2 LCD address
These DDRAM memory address correspond to a character position on a display and as
each character is written, the memory address is incremented to the next address. However, as
the address reaches the line limit (13), the controller automatically adds the address into second
line (40).
The memory address works by storing an ASCII code which makes up letters and
numbers and the initializations clears this stored code.
2.3.4 LCD_Write_command
LCd_Write_Command is a function that capable of controlling the LCD by simple data
inputs. The commands are pre-programmed in LCD module and only require the calling function
to use them. The commands usually involves with controlling the screen refreshes, cursor
location and LCD memory. Most of these commands are necessary to be called before starting
the LCD so the LCD will work properly. The command is called by feeding a hexadecimal value
which converted in binary where the HIGH will correspond to each bit. Each HIGH in bit will
perform specific function as below.
11
RS
0
Command
Function
0x38
Enables the uses of data lines in LCD
0x08
Display OFF, Cursor OFF
0x01
Clear Screen
0x06
Increment cursor
0x0F
LCD ON, Cursor ON, Cursor blinking ON
R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
0
0
0
0
0
1
D
C
B
D: The display is ON when D = 1 and OFF when D = 0.
C: The cursor is displayed when C = 1 and is not displayed when C = 0.
B: The character at the cursor position blinks when B = 1.
Table 2.2: LCD_Write_Command bit functions.
12
CHAPTER 3
METHODOLOGY
3.0 Introduction
This chapter emphasis the methodology used in order to achieve the proposed objectives.
The project is conducted via two approach which is software and hardware approach. The
software part includes the initialization of all ports, variables and functions in the program.
While the hardware components describe all of electronics devices involved in the project and
their connections as well as other important things.
13
Software
Implementations
 Port/Main
initializations
 Main program
 LCD program
 Keypad program
Hardware
Implementations
 JTAGICE mkII
connections
 Keypad and LCD
connections
Testing and
Debugging
NO
Testing
worked?
YES
Document the results
and observations.
Figure 3.0: Flow Chart of Research Activities
14
3.1
Software Implementation
3.1.1 Initialization
At the initialization part, the external header file and port is included and defined. Io.h is
the basic input/output enabler and delay.h provides the function for timedelay.
Various functions for LCD is declared here where ENABLE_LCD and DISABLE_LCD
are the variables responsible for refreshing the LCD. Where SET_LCD_DATA and
SET_LCD_CMD are for writing data into LCD and all of these are utilized at PORT D.
void LCD_init(void);
void LCD_WriteCommand (unsigned char Command);
void LCD_WriteData (unsigned char Data);
void LCD_DisplayString (char row, char column, char *string);
void LCD_Cursor(char row, char column);
void LCD_CursorHome(void);
int ReadKeyPad(void);
#define ENABLE_LCD
PORTD |= 0x80
#define DISABLE_LCD
PORTD &= ~0x80
#define SET_LCD_DATA
PORTD |= 0x20
#define SET_LCD_CMD
PORTD &= ~0x20
#define KB_PORT_OUT
PORTB
#define KB_PORT_IN
PINB
15
3.1.2
LCD Functions
Before the LCD is usable, several key functions need to be declared first. These functions will
decide how the LCD will behave when receiving inputs from the microcontroller.
Void LCD_init(void) function is to set the LCD when is turned on. It proceeds to refresh the LCD
and place the cursor in a position so that the next input will be written correctly. This function is called
whenever a new input is needed to written of previous input as it erased the previous text and reposition
the cursor.
void LCD_init(void)
{
delay_ms(100); // wait for 100ms
LCD_WriteCommand (0x38); // 8 data lines
LCD_WriteCommand (0x06); // cursor setting
LCD_WriteCommand (0x0f); // display ON
LCD_WriteCommand (0x01); // clear LCD memory
delay_ms (10); // 10ms delay after clearing LCD
}
16
Displaying the output on CD is done with the LCD_WriteData function. The function goes in
enable mode by SET_LCD_DATA and then import data from PORTD. The next process involves
sending the LCD into delay mode which is to create a delay for the LCD to work physically. This is done
by the asm(“nop”) command although is process is optional.
void LCD_WriteData (unsigned char Data)
{
SET_LCD_DATA; // Set LCD in data mode
PORTD = Data; // Load data to port
ENABLE_LCD; // Write data to LCD
asm("nop");
asm("nop");
DISABLE_LCD; // Disable LCD
delay_ms(1); // wait for 1ms
}
17
The following functions are used for placing cursors write a string at the cursor location.
Primarily used to point and write a string at specific a location.
void LCD_DisplayString (char row, char column, char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_WriteData(*string++);
}
void LCD_WriteString (char *string)
{
while (*string)
LCD_WriteData(*string++);
}
void LCD_Cursor (char row, char column)
{
switch (row)
{
case 1: LCD_WriteCommand (0x80 + column - 1); break;
case 2: LCD_WriteCommand (0xc0 + column - 1); break;
default: break;
}
}
18
3.1.3
Keypad Functions
The keypad function uses if, while and case statement to check which button is pressed. It will
repetitively read the inputs from PORTB and checks whether the readings match with the assigned
reading for each button.
The case function here checks which key is pressed and its corresponding hex value is returned
by the functions which later read by main function for next output. Each hexadecimal is based on the
location of the button via the row and column coordination.
switch (keyCode)
{
case (0xee): keyPressed = 0;
break;
case (0xde): keyPressed = 1;
break;
…
case (0x77): keyPressed = 0xF;
break;
default
: keyPressed = 255;
}
OUT:;
return (keyPressed);
19
1
2
3
4
0xEE
0xDE
0xBE
0x7E
5
6
7
8
0xED
0xDD
0xBD
0x7D
9
10
11
12
0xEB
0xDB
0xBB
0x7B
13
14
15
16
0xE7
0xD7
0xB7
0x77
Figure 3.1: Hex addresses for individual Hex key
Since the function involves the usage of buttons, a debouncing function is include before
this. The debouncing function eliminates the oscillating reading obtained from pressing buttons.
This reading is caused by the spring inside the button that bounces several times yielding
multiple inputs. The debouncing function overcomes this by introducing a delay after a key is
pressed before reads it [6].
Figure 3.2: Debouncing waits the “bounces” finish first before takes the reading
20
After the ReadKeyPad function is executed, it will return a value to the while loop in
Main. This value is converted into string value and used to display the output as the button
number. Since the keypad actually begins with 0, the value is added by 1 so it will correspond
with the numbering correctly.
while(1)
{
LCD_DisplayString (1,1,"Press any button....");
KeyPadData=ReadKeyPad();
if (KeyPadData<16)
{
LCD_WriteCommand (0x01);
LCD_Cursor(2,1);
LCD_WriteString("Key ");
itoa (KeyPadData+1,buffer,10);
LCD_WriteString(&buffer);
LCD_WriteString(" was pressed");
_delay_ms(1000);
}
else
{
LCD_Cursor(2,1);
LCD_WriteString("Waiting...........");
}
}
21
The whole execution process from the pressing of buttons to the display of LCD can be
simplified into the following flowchart.
During the initialization, the external header files, port and function declarations are
executed. Later, the display will show introductions text just for decorative purpose. Both of this
only happens once during the initialization.
Figure 3.3: Flowchart for the program operation steps.
22
3.3
Hardware Configuration
3.3.1 The connections of 4x4 keypad to the Atmega32A chip on AtMega32 board
The figure shows the connections of a Hexadecimal keypad to the PORTB of the chip.
The column is represented by C1 to C4 and the row is represented by R1 to R2. The rows and
columns is connected to the 8bit pin of PORTB and since the keypad used is embedded oon the
AtMega32 board, no necessary connections is needed.
Initially, the ports at all rows and columns are set at high. When a button is pressed, the
closed position of the switch will send low signal to the corresponding row and column. Each
button sends specific low to specific row and column thus making each button pressed
distinguishable by its lows only. For example, when the button 5 is pressed, it will send 1011
1110 which also equals to 0xbe in hexadecimal. By reading the PORTB of 0xbe, which button is
pressed can be determined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Figure 3.4: Rows and columns of the 4x4 keypad.
23
3.3.2
The connections of 20x2 LCD to the Atmega32A chip on AtMega32 board
The LCD which consists of 16 pins is connected to two different parts of the board across
two ports. The data pins, pin 7 to 14 are connected to PORTA of the chip and R/S, R/W and
Enable is connected to PORTD. The rest are connected to LCD1602 pins on the board that
provides VCC, GND and contrast control. The contrast is controllable by a variable resistor
located next to LCD1602 port.
Figure 3.5: Pins function for the 16 pins LCD
24
3.3.3
The connections of LCD and keypad to AtMega32 microcontroller and board.
The overall circuit connections of the devices to the board are as follows. The
connections are made using jumper cables. The keypad is internally connected to the board
therefore no connections are made. However, the keypad sends inputs to PORTA thus reserving
PORTA only for inputs.
Figure 3.6: Circuit connection of the devices
25
3.3.4
Safety precautions and known issues.
Dealing with electronics will require specific care for safety to avoid damages to the
equipments as well as unnecessary injuries. Electronics equipments are sensitive to static
currents found in the environments and body. This needs appropriate discharging procedures
such as touching a metal object and wearing static strap before handling electronics [8]. When
using solder, make sure the soldering cork is damped and the solder bit is set and heated at
correct temperature before using them. The soldering lead applied needs to be at correct amount
to prevent short circuit or loose connection which leads to open circuit.
While disconnecting JTAGEICE mkII from the AtMega32 target board, make sure the
target board is turn off first before the JTAGICE because the charge buildup is the programmer
can lead to damage to programmer of the target board. On the other note, the jumper cables can
be easily damaged by rough handlings. It is wise to check the connectivity of the cables using a
voltmeter first before applying them to the connection.
During the course of the project, several issues found are related to the keypad of the
target board. Since the board had been used many times before, the keypad need to be pressed
thoroughly before it respond. However, this should not be a problem since it still function
correctly and this should be a reminder for us that we should use all what we had and appreciate
all the little things we had.
26
CHAPTER 4
RESULTS AND DISCUSSION
4.1 Introduction
This chapter presents the results of the project and demonstrates the achievements from
the program developed. The discussion of the result is shown and discussed in clarity and further
explanations are expressed by the discussion.
27
4.2 Results of Program Execution
The figure below show the results of LCD display by running the program. Figure 1 show
the initial introduction display which consists of three steps proceeding through the display as
figure 4.0. During the display in figure 15, the program perform continuous while loop waiting
for user input.
Figure 4.0: Introductory display
Figure 4.1: Awaiting user input
28
Figure 4.2: Some of displayed results from pressing the buttons
The displayed results are expected from the written program as no errors or bug
encountered. The outputs from pressing buttons are all correct ranging from the 1st button
(0xEE) to the 16th button (0x77). The RESET button on the board will restart the whole program
thus showing the introduction again.
29
4.3 Discussion
This project is to demonstrate the principal of a Hexadecimal keypad and its input to the
LCD. From the program written and its execution, the hexadecimal keypad operates by assigning
a 4bit binary address to the row and column. These address then converted into hexadecimal
where the first digit represent the row and the second digit represent the column. These values
can be read as input by the program and by these values, which button pressed can be determined.
Figure 4.3: Relationships between address and rows and columns
30
CHAPTER 5
CONCLUSION AND RECOMMENDATION
5.0
Conclusion
In conclusion, a working program and device has been developed. The project has
successfully achieved the objectives to demonstrate the principal of a hexadecimal keypad and
displaying outputs to a LCD.
During the process of writing a program for a device, it is important to know the
mechanism of how the device works as much as possible. This not only allow for no failure in
using the device but also allow the user to see every aspect in different angles as well as making
new improvements over existing model.
The keypad constructed can provide a basic foundation for a controller device. With
sixteen inputs, a controller such as cursor control, mode selector or a timer may be implemented
31
with this device. In other way, the functionality of the LCD panel is also important since the
LCD require several startup procedures that are independent of other device.
Nevertheless, the objective of this project is to provide understanding of AtMega32
microcontroller and its target board, 4x4 keypad and the LCD panel. Hence, it is possible to
write a program detect the keypad button pressed and translate the data from the keypad into a
display in the LCD using C language. The outcome of this project is achieved with satisfying
results where the devices works properly as according to the plan.
5.1
Limitation
The limitation of this project is that it only use the keypad only to display which key is
pressed where many other wonderful feature can be implemented. On the other hand, limit of
four ports of AtMega32 is overwhelmed by the LCD. The LCD uses two output ports which is
PORTA and PORTD thus preventing other output device is used. The keypad also limits of one
button pressed at a time and slow response time making complex control impsossible.
5.2
Future Recommendation
For the future research, there are many ways of the device can be improved. By
implementing an Interrupt can vastly improve the program as Interrupt enable a loop to be
broken and creating a device capable of switching functions for example a timer that can
switches into a clock back and forth.
32
This program is also implementable in Arduino target board since it also use AtMega32
chip and same language. Since Arduino is simpler and easier to use, it will make compact
version of this device all together.
A device such a calculator may be possible to be made with this device since the keypad
has a suitable number of buttons and a display is available. A complex programming that
requires integers and float values stored on a variable and then a mathematical operation is
performed is quite challenging to achieve.
33
REFERENCES
1
“AVR ATmega32 Manual”, Atmel Corporation 2011.
2
Donald Weiman “LCD Addressing”, Retrieved 29 September 2012.
http://web.alfredstate.edu/weimandn/lcd/lcd_addressing/lcd_addressing_index.html
3
“AVR245: Code Lock with 4x4 Keypad and I2C™ LCD “, Atmel Corporation 2005.
4
CC Dharmani “LCD interfacing with ATmega32”, Retrieved 13July 2008,
http://www.dharmanitech.com
5
"Hitachi HD44780 LCD controller Datasheet" ,
https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
6
“How to Use Interrupts with the Switches”, CS 224 Computer Systems.
https://students.cs.byu.edu/~cs224ta/references/HowTos/HowTo_Interrupts.php
7
“Introduction to I/O Registers”, Protostack.com, Retrieved 30 June 2010,
http://www.protostack.com/blog/2010/06/introduction-to-io-registers/
8
“Handling Electronic Assemblies”, Circuit Technology Center, Inc.
http://www.circuitrework.com/guides/2-1.shtml
34
APPENDIX A
TITLE:
Tutorial Module On How To Program A Keypad And LCD That Is Connected To
An Atmega32
OBJECTIVE:
1. To demonstrate how to connect a LCD to the ATmega32 chip on a the ATmega32 Target
board.
2. To show the built-in connection of the Hex keypad to the ATmega32 chip on a the
ATmega32 Target board.
3. To run a sample program to test the Hex keypad and the LCD Panel.
THEORY:
The AVR Debugger
Overview
AtMega32 is a microprocessor from AtMega family produced by Atmel Corporation.
AtMega32 is the bigger and more powerful version of AtTinyAvr with more memories, pin
numbers and functions.
Specification
The specifications of Atmega32 are generally higher than other microcontroller in the
same series even though most of the differences are in memory size, boot loader support and
interrupt size.
35
Device
Flash
EEPROM
RAM
Interrupt Vector Size
Memory
AtMega328P
32K Bytes
1K Bytes
2K Bytes
2 instructions words/vector
AtMega168PA
16K Bytes
512 Bytes
1K Bytes
2 instructions words/vector
AtMega88PA
8K Bytes
512 Bytes
1K Bytes
1 instructions words/vector
AtMega48PA
4K Bytes
256 Bytes
512 Bytes
1 instructions words/vector
Table 1: Comparisons between AtMega series.
The ATmega32 target Board
AtMega32 microcontroller consists of 28 pins with 4 ports for input or output. Each port
has 4 pins for 4 bits I/O which determined by the program initialization.
The AtMega32 board consists of many devices placed together on a board. The 4x4
keypad used for this project is located on this board and it is already hardwire to the board and
no further connections are required. The other parts used are the LCD1602 port which controls
the energy aspect of LCD.
36
4
1
3
5
2
Figure 1: AtMega32 Target Board.
37
No
Device
Description
1
4x4 keypad
16 buttons used to give inputs to the chip. It is connected to PORTB of
the micro controller
2
1602 LCD
port
The port gives VCC and GND to LCD as well as pin that control the
contrast of LCD. The contrast can adjusted from the variable resistor
next to it.
3
AtMega32
chip
The 40 pins microcontroller which is the primary components in the
board. The VCC and GND are pre-connected as well as to other device
on the board.
4
8bit LEDs
The LEDs which gives output for the keypad. The output given is
according to keypad coordinates in for of hexadecimal which
correspond to the row and column.
5
JTAG port
The port which the JTAGICE mkII programmer is connected. When the
programmer is connected, the pre-programmed board will go into
standby steps and only runs when control by AVR software.
Table 2: AtMega32 Target Board parts function.
The reset button located on the board is used to rest back the program into beginning and
reruns it again.
Connecting a LCD to the ATmega32 chip on a the ATmega32 Target board
a) The connections of 4x4 keypad to the Atmega32A chip on AtMega32 board
The figure shows the connections of a Hexadecimal keypad to the PORTB of the chip.
The column is represented by C1 to C4 and the row is represented by R1 to R2. The rows and
columns is connected to the 8bit pin of PORTB and since the keypad used is embedded oon the
AtMega32 board, no necessary connections is needed.
38
Initially, the ports at all rows and columns are set at high. When a button is pressed, the
closed position of the switch will send low signal to the corresponding row and column. Each
button sends specific low to specific row and column thus making each button pressed
distinguishable by its lows only. For example, when the button 5 is pressed, it will send 1011
1110 which also equals to 0xbe in hexadecimal. By reading the PORTB of 0xbe, which button is
pressed can be determined.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Figure 3.4: Rows and columns of the 4x4 keypad.
39
Built-in connection of the Hex keypad to the ATmega32 chip on a the ATmega32 Target board
The LCD which consists of 16 pins is connected to two different parts of the board across
two ports. The data pins, pin 7 to 14 are connected to PORTA of the chip and R/S, R/W and
Enable is connected to PORTD. The rest are connected to LCD1602 pins on the board that
provides VCC, GND and contrast control. The contrast is controllable by a variable resistor
located next to LCD1602 port.
Figure 3.5: Pins function for the 16 pins LCD
40
a) The connections of LCD and keypad to AtMega32 microcontroller and board.
The overall circuit connections of the devices to the board are as follows. The
connections are made using jumper cables. The keypad is internally connected to the board
therefore no connections are made. However, the keypad sends inputs to PORTA thus reserving
PORTA only for inputs.
Figure 3.6: Circuit connection of the devices
41
PROCEDURE:
1
Install AVR Studio and C compiler
2
Connect AVR debugger and Target Board
3
Switch ON AVR debugger and Target Board
4
Write sample program (See Appendix B)
5
Set-up AVR Studio to connect to AVR Debugger connected to ATmega32 Target
Board through JTAG communication
6
Run Program
7
Expected Result
Figure 4.2: Some of displayed results from pressing the button
42
APPENDIX B
LCD and 4X4 MATRIX KEYPAD INTERFACING PROGRAM
//***********************************************************
// ******** LCD and 4X4 MATRIX KEY-BOARD INTERFACING ***********
//***********************************************************
//Controller:
ATmega32 (Crystal: 1 Mhz)
//Compiler:
winAVR (AVRStudio)
//LCD conections: PortA --> LCD Data lines, PD5 --> RS, PD6 --> RW, PD7 --> EN
//***********************************************************
#define F_CPU 1000000L
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
//****** LCD Functions declaration ******** //
void LCD_init(void);
void LCD_WriteCommand (unsigned char Command);
void LCD_WriteData (unsigned char Data);
void LCD_DisplayString (char row, char column, char *string);
void LCD_Cursor(char row, char column);
void LCD_CursorHome(void);
int ReadKeyPad(void);
43
#define ENABLE_LCD
PORTD |= 0x80
#define DISABLE_LCD
PORTD &= ~0x80
#define SET_LCD_DATA
PORTD |= 0x20
#define SET_LCD_CMD
PORTD &= ~0x20
#define KB_PORT_OUT
PORTB
#define KB_PORT_IN
PINB
//************************
void port_init(void)
{
DDRB = 0x0f;
//Key-board port, higher nibble - input, lower nibble - output
PORTB = 0xff;
//pull-up enabled for higher nibble
DDRD = 0xe0;
//LCD control lines PD5 --> RS, PD6 --> RW, PD7 --> EN as output
PORTD = 0x00;
DDRA |= 0xff;
//LCD 8 bit Data lines as output
PORTA = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
port_init();
LCD_init();
}
44
//****************** MAIN FUNCTION *******************
int main(void)
{
unsigned int KeyPadData;
init_devices();
char buffer [33];
LCD_DisplayString (1,1,"Fakhrul");
LCD_DisplayString (2,1,"Hayat");
_delay_ms(2000);
// wait for 2000ms
LCD_WriteCommand (0x01);
// clear LCD memory
/*Clears all display and returns the cursor to the home position (Address 0).*/
LCD_DisplayString (1,1,"Final Year");
LCD_DisplayString (2,1,"Project");
LCD_WriteCommand(0xc0);
//moving LCD cursor to second row
_delay_ms(2000);
// wait for 2000ms
LCD_WriteCommand (0x01);
// clear LCD memory
/*Clears all display and returns the cursor to the home position (Address 0).*/
LCD_DisplayString (1,1,"Displaying Output");
LCD_DisplayString (2,1,"Using LCD, Keypad");
LCD_WriteCommand(0xc0);
//moving LCD cursor to second row
LCD_CursorHome();
_delay_ms(2000);
// wait for 1000ms
LCD_WriteCommand (0x01);
// clear LCD memory
/*Clears all display and returns the cursor to the home position (Address 0).*/
45
while(1)
{
LCD_DisplayString (1,1,"Press any button....");
KeyPadData=ReadKeyPad();
//Read from Hex Keypad 0 < valid KeyPadData <16
if (KeyPadData<16)
{
LCD_WriteCommand (0x01);
// clear LCD memory
/*Clears all display and returns the cursor to the home position (Address 0).*/
LCD_Cursor(2,1);
LCD_WriteString("Key ");
itoa (KeyPadData+1,buffer,10);
LCD_WriteString(&buffer);
LCD_WriteString(" was pressed");
_delay_ms(1000);
// wait for 1000ms
}
else
{
LCD_Cursor(2,1);
LCD_WriteString("Waiting...........");
}
}//end of while(1)
return 0;
}//end of main()
46
int ReadKeyPad(void)
{
int keyPressed;
unsigned char upperNibble, keyCode, i;
upperNibble = 0xff;
for(i=0; i<4; i++)
{
_delay_ms(1);
KB_PORT_OUT = ~(0x01 << i);
_delay_ms(1);
//delay for port o/p settling
upperNibble = KB_PORT_IN | 0x0f;
if (upperNibble != 0xff)
{
_delay_ms(20);
//key debouncing delay
upperNibble = KB_PORT_IN | 0x0f;
if(upperNibble == 0xff) goto OUT;
keyCode = (upperNibble & 0xf0) | (0x0f & ~(0x01 << i));
while (upperNibble != 0xff)
upperNibble = KB_PORT_IN | 0x0f;
_delay_ms(20);
//key debouncing delay
switch (keyCode)
//generating key characetr to display on LCD
{
case (0xee): keyPressed = 0;
break;
case (0xde): keyPressed = 1;
break;
47
case (0xbe): keyPressed = 2;
break;
case (0x7e): keyPressed = 3;
break;
case (0xed): keyPressed = 4;
break;
case (0xdd): keyPressed = 5;
break;
case (0xbd): keyPressed = 6;
break;
case (0x7d): keyPressed = 7;
break;
case (0xeb): keyPressed = 8;
break;
case (0xdb): keyPressed = 9;
break;
case (0xbb): keyPressed = 0xA;
break;
case (0x7b): keyPressed = 0xB;
break;
case (0xe7): keyPressed = 0xC;
break;
case (0xd7): keyPressed = 0xD;
break;
case (0xb7): keyPressed = 0xE;
break;
48
case (0x77): keyPressed = 0xF;
break;
default
: keyPressed = 255;
}//end of switch
OUT:;
}//end of if
}//end of for
return (keyPressed);
}
//***********************************************************************
//*********************** LCD Functions ***************************** *****
//***********************************************************************
// *** Initialize the LCD driver ***
void LCD_init(void)
{
_delay_ms(100);
// wait for 100ms
//SET_LCD_WRITE ;
// Set LCD in write mode
LCD_WriteCommand (0x38);
// 8 data lines
LCD_WriteCommand (0x08);
// display off
LCD_WriteCommand (0x01);
// clear LCD memory
_delay_ms (10);
// 10ms delay after clearing LCD
LCD_WriteCommand (0x06);
// cursor setting
LCD_WriteCommand (0x0f);
}
49
// *** Write one byte of data to the LCD ***
void LCD_WriteData (unsigned char Data)
{
SET_LCD_DATA;
// Set LCD in data mode
PORTA = Data;
// Load data to port
ENABLE_LCD;
// Write data to LCD
asm("nop");
asm("nop");
DISABLE_LCD;
// Disable LCD
_delay_ms(1);
// wait for 1ms
}
// *** Display a string at the specified row and column, from FLASH ****
void LCD_DisplayString (char row, char column, char *string)
{
LCD_Cursor (row, column);
while (*string)
LCD_WriteData(*string++);
}
void LCD_WriteString (char *string)
{
while (*string)
LCD_WriteData(*string++);
}
50
// *** Position the LCD cursor at "row", "column". ***
void LCD_Cursor (char row, char column)
{
switch (row)
{
case 1: LCD_WriteCommand (0x80 + column - 1); break;
case 2: LCD_WriteCommand (0xc0 + column - 1); break;
default: break;
}
}
void LCD_CursorHome(void)
{
LCD_WriteCommand (0x02);
}
//*******END OF PROGRAM*******
51
APPENDIX C
COST AND BUDGET TABLE
Item
Price per Unit
Unit
Total
SN AVR16 STK target board +
AtMega32
JTAGICE mkII
RM 350.00
1
RM 350.00
RM 400.00
1
RM 400.00
FM2004A 20x2 LCD
RM 25.00
1
RM 25.00
USB cable type B 1ft
RM 15.00
2
RM 30.00
Female - Female jumper cables x 10
RM 4.00
2
RM 8.00
Straight header pin 16x1
RM 1.00
1
RM 1.00
Misc. soldering
-
-
RM 3.00
TOTAL COST
RM 817.00
52