Sunteți pe pagina 1din 27

TRAINING REPORT ON 8051 MICROCONTROLLER

Table of Contents
DAY 1:.............................................................................................................................................................................. 3 a) b) c) d) e) f) g) h) DEFINITION: ........................................................................................................................................................ 3 HISTORY: ............................................................................................................................................................. 3 MICROCONTROLLER VS MICROPROCESSOR ....................................................................................................... 3 IMPORTANT FEATURES ....................................................................................................................................... 3 PIN DIAGRAM...................................................................................................................................................... 4 BLOCK DIAGRAM................................................................................................................................................. 5 8051 programming model .................................................................................................................................. 5 8051 Instruction Set Summary ........................................................................................................................... 6

Softwares to be used: ................................................................................................................................................... 10 KEIL:........................................................................................................................................................................... 10 KEIL TUTORIAL .......................................................................................................................................................... 11 FLASH MAGIC: ........................................................................................................................................................... 12 FLASH MAGIC TUTORIAL: .......................................................................................................................................... 12 LCD PROGRAMMING .................................................................................................................................................... 15 Pin Description .......................................................................................................................................................... 15 Commands and Instruction set ................................................................................................................................. 15 SERIAL PROGRAMMING ................................................................................................................................................ 17 Introduction .............................................................................................................................................................. 17 MAX232 Interfacing with Microcontrollers .............................................................................................................. 18 8051 Programming for UART .................................................................................................................................... 19 Uses of Timers & Counters ........................................................................................................................................... 21 Martix Keypad Interfacing with Microcontrollers: Introduction .................................................................................. 25 Introduction .............................................................................................................................................................. 25

DAY 1:

a) DEFINITION:
A microcontroller (also microcomputer, MCU or C) is a small computer on a single integrated circuit consisting internally of a relatively simple CPU, clock, timers, I/O ports, and memory.

b) HISTORY:
a) First general purpose microprocessor was invented by INTEL: Intel 4004 in early 1970s. it is a 4 bit microprocessor. b) In 1974 the more powerful second generation microprocessor (the 8008) was announced fabricated as a single chip. Followed by the Intel 8080. These are 8 bit microprocessors which can be operated by +5V. c) At about the same time Motorola released its first microprocessor, the 6800, which was also an 8 bit processor with about the same processing power as that of the intel 8080. d) Then the third generation 16 bit 8086 microprocessor which, in its 8088 pseudo 16 bit form, was used in the first IBM PCs. The 8088 was an 8086 but with only an 8 bit data bus. e) In time this was followed by the 80186, the 80286, the 800386 ( a 32 bit processor), and 80486, leading to the Pentium range of microprocessors (64 bit processors) available today. f) The first computer system on a chip optimized for control applications - microcontroller was the Intel 8048 released in 1975, with both RAM and ROM on the same chip.

c) MICROCONTROLLER VS MICROPROCESSOR
Microprocessor CPU is stand-alone, RAM, ROM, I/O, timer are separate Designer can decide on the amount of ROM, RAM and I/O ports. expansive versatility general-purpose
Microcontroller

CPU, RAM, ROM, I/O and timer are all on a single chip fix amount of on-chip ROM, RAM, I/O ports for applications in which cost, power and space are critical single-purpose

d) IMPORTANT FEATURES

It provides many functions (CPU, RAM, ROM, I/O, interrupt logic, timer, etc.) in a single package 8-bit ALU, Accumulator and Registers; hence it is an 8-bit microcontroller 8-bit data bus - It can access 8 bits of data in one operation 16-bit address bus - It can access 216 memory locations - 64 kB (65536 locations) each of RAM and ROM On-chip RAM - 128 bytes (data memory)

On-chip ROM - 4 kB (program memory) Four byte bi-directional input/output port UART (serial port) Two 16-bit Counter/timers Two-level interrupt priority Power saving mode

e) PIN DIAGRAM

f) BLOCK DIAGRAM

g) 8051 programming model

The figure also shows the usual CPU components: program counter, ALU , working register and clock circuit. The 8051 architecture consists of these specific feature: eight- bit CPU with registers A (the accumulator) and B Sixteen bit program counter (PC) and data pointer(DPTR) Eight bit program status word (PSW) Eight bit stack pointer (SP) Internal ROM of 4k Internal RAM of 128 bytes --- four register banks each containing eight registers --- Sixteen bytes which may be addressed at bit level --- Eight bytes of general purpose data memory Thirty two i/p-o/p pins arranged as 4 eight bit ports- P0-P3 Two sixteen bit timer/counter (T0-T1) Full duplex serial data receiver/transmitter: SBUF Control registers: TMOD, SCON ,PCON, TCON, IP AND IE

h) 8051 Instruction Set Summary


Rn Data @Ri #data #data16 addr16 addr11 rel bit Register R7-R0 of the currently selected Register Bank. 8-bit internal data locations address. This could be an internal Data RAM location (0-127) or a SFR [i.e. I/O port, control register, status register, etc. (128-255)]. 8-bit Internal Data RAM location (0-255) addressed indirectly through register R1 or R0. 8-bit constant included in instruction. 16-bit constant included in instruction. 16-bit destination address. Used by LCALL and LJMP. A branch can be anywhere within the 64k byte Program Memory address space. 11-bit destination address. Used by ACALL and AJMP. The branch will be within the same 2k byte page of Program Memory as the first byte of the following instruction. Signed (twos component) 8-bit offset byte. Used by SJMP and all conditional jumps. Range is 128 to +127 bytes relative to first byte of the following instruction. Direct Addressed bit in Internal Data RAM or Special Function Register.

Instruction ADD ADDC SUBB MUL DIV DA RRC RLC SETB C

Flag C OV AC XXX XXX XXX OX OX X X X 1

Instruction CLR C CPL C ANL C,bit ANL C,/bit ORL C,bit ORL C,/bit MOV C,bit CJNE

Flag C OV AC O X X X X X X X

Arithmetic Operations Mnemonic Description ADD A,Rn ADD A,@Ri ADDC A,Rn ADDC A,@Ri SUBB A,Rn SUBB A,@Ri INC A INC Rn INC direct INC @Ri DEC A DEC Rn DEC direct DEC @Ri INC DPTR MUL AB DIV AB Add register to A Add indirect RAM to A Add register to A with Carry Add indirect RAM to A with Carry Subtract register from A with Borrow Subtract indirect RAM from A with Borrow Increment A Increment register Increment direct byte Increment indirect RAM Decrement A Decrement register Decrement direct byte Decrement indirect RAM Increment Data Pointer Multiply A and B (A x B => BA) Divide A by B (A/B => A + B) ADD A,direct Add direct byte to A ADD A,#data Add immediate data to A ADDC A,direct Add direct byte to A with Carry ADDC A,#data Add immediate data to A with Carry SUBB A,direct Subtract direct byte from A with Borrow

Bytes Cycles 1 2 1 2 1 2 1 2 1 2 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 4 4 1

SUBB A,#data Subtract immediate data from A with Borrow 2

DA A Decimal Adjust A Logical Operations Mnemonic Description ANL A,Rn ANL A,direct ANL A,@Ri ANL A,#data ANL direct,A ORL A,Rn ORL A,direct ORL A,@Ri AND register to A AND direct byte to A AND indirect RAM to A AND immediate data to A AND A to direct byte OR register to A OR direct byte to A OR indirect RAM to A

Bytes Cycles 1 2 1 2 2 3 1 2 1 1 1 1 1 1 2 1 1 1

ANL direct,#data AND immediate data to direct byte

ORL A,#data ORL direct,A XRL A,Rn XRL A,direct XRL A,@Ri XRL A,#data XRL direct A CLR A CPL A RL A RLC A RR A RRC A

OR immediate data to A OR A to direct byte Exclusive-OR register to A Exclusive-OR direct byte to A Exclusive-OR indirect RAM to A Exclusive-OR immediate data to A Exclusive-OR A to direct byte Clear A Complement A Rotate A Left Rotate A Left through Carry Rotate A Right Rotate A Right through Carry

2 2 3 1 2 1 2 2 1 1 1 1 1 1 1

1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 Bytes Cycles 1 2 1 2 1 2 2 2 2 3 2 3 1 2 2 2 1 1 1 1 1 1 1 1 2 1 1 2 2 2 2 1 2 1 1 2 2 2

ORL direct,#data OR immediate data to direct byte

XRL direct,#data Exclusive-OR immediate data to direct byte 3

SWAP A Swap nibbles within A Data Transfer Operations Mnemonic Description MOV A,Rn MOV A,direct MOV A,@Ri MOV A,#data MOV Rn,A MOV Rn,direct MOV Rn,#data MOV direct,A MOV direct,Rn MOV direct,direct MOV direct,@Ri MOV direct,#data MOV @Ri,A MOV @Ri,direct MOV @Ri,#data Move register to A Move direct byte to A Move indirect RAM to A Move immediate data to A Move A to register Move direct byte to register Move immediate data to register Move A to direct byte Move register to direct byte Move direct byte to direct byte Move indirect RAM to direct byte Move immediate data to direct byte Move A to indirect RAM Move direct byte to indirect RAM Move immediate data to indirect RAM

MOV DPTR,#data16 Load Data Pointer with 16-bit constant MOVC A,@A+DPTR Move Code byte relative to DPTR to A MOVC A,@A+PC MOVX A,@Ri Move Code byte relative to PC to A Move External RAM (8-bit addr) to A

MOVX A,@DPTR MOVX @Ri,A MOVX @DPTR PUSH direct POP direct XCH A,Rn XCH A,direct XCH A,@Ri

Move External RAM (16-bit addr) to A Move A to External RAM (8-bit addr) A Move A to External RAM (16-bit addr) Push direct byte onto stack Pop direct byte from stack Exchange register with A Exchange direct byte with A Exchange indirect RAM with A

1 1 1 2 2 1 2 1

2 2 2 2 2 1 1 1 1

XCHD A,@Ri Exchange low-order Digit indirect RAM with A 1 Single Bit (Boolean Variable) Operations Mnemonic Description Bytes Cycles CLR C CLR bit SETB C SETB bit CPL C CPL bit ANL C,bit ORL C,bit Clear Carry flag Clear direct bit Set Carry flag Set direct bit Complement Carry flag Complement direct bit AND direct bit to Carry flag OR direct bit to Carry flag 1 2 1 2 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 1 2

ANL C,/bit AND complement of direct bit to Carry flag 2 ORL C,/bit OR complement of direct bit to Carry flag MOV C,bit Move direct bit to Carry flag MOV bit,C Move Carry flag to direct bit

Program Flow Control Mnemonic Description ACALL addr11 LCALL addr16 RET RETI SJMP addr11 LJMP addr16 SJMP rel JMP @A+DPTR JZ rel JNZ rel Absolute subroutine call Long subroutine call Return from subroutine Return from interrupt Absolute Jump Long Jump Short Jump at relative address Jump indirect relative to DPTR Jump if A is Zero Jump if A is Not Zero

Bytes Cycles 2 3 1 1 2 3 2 1 2 2 2 2 2 2 2 2 2 2 2 2

JC rel JNC rel JB bit,rel JNB bit,rel JBC bit,rel CJNE A,direct,rel CJNE A,#data,rel CJNE Rn,#data,rel DJNZ Rn,rel DJNZ direct,rel NOP

Jump if Carry flag is set Jump if No Carry flag Jump if direct Bit is set Jump if direct Bit is Not set Jump if direct Bit is set and Clear bit Compare direct to A and Jump if Not Equal Compare immediate to A and Jump if Not Equal

2 2 3 3 3 3 3

2 2 2 2 2 2 2 2 2 2 2 1

Compare immediate to register and Jump if Not Equal 3 Decrement register and Jump if Not Zero Decrement direct byte and Jump if Not Zero No operation 2 3 1

CJNE @Ri,#data,rel Compare immediate to indirect and Jump if Not Equal 3

Softwares to be used:
KEIL:
This tutorial will assist you in writing your first 8051 Assembly language program using the popular Keil Compiler. Keil offers an evaluation package that will allow the assembly and debugging of files 2K or less. This package is freely available at their web site. Keils website address is www.keil.com.

KEIL TUTORIAL
1. Open Keil from the Start menu 2. The Figure below shows the basic names of the windows referred in this document 3. Select New Project from the Project Menu. 4. Name the project new 5. Click on the Save Button. 6. The device window will be displayed. 7. Select the part you will be using to test with. Here we select NXP(P89v51rd2) 8. Double Click on NXP(founded by philips) 9. Scroll down and select the P89v51rd2 Part 10. Click OK

Creating Source File


1. 2. 3. 4. 5. 6. Click File Menu and select New. A new window will open up in the Keil IDE. Write your program in the file window. Click on File menu and select Save As Name the file new.asm Click the Save Button.

Adding File to the Project


1. Expand Target 1 in the Tree Menu . Right click on source group 1. 2. Click on Add files to group Source group 1 3. Select the file and click on add. 4. Expand the Source Group 1 in the Tree menu to ensure that the file was added to the project 5. Select Target Tab 6. Change Xtal (Mhz) from 50.0 to 11.0592 7. Select Output Tab 8. Click on Create Hex File check box 9. Click OK Button 10. Click on Project Menu and select 11. Rebuild all Target Files 12. In the Build Window it should report 13. 0 Errors (s), 0 Warnings 14. You are now ready to Program your Part

Testing Program in Debugger

1. Comment out line ACALL DELAY by placing a Semicolon at the beginning. This will allow you to see the port Change immediately. 2. Click on the File Menu and select Save 3. Click on Project Menu and select Rebuild all Target Files

4. In the Build Window it should report 0 Errors (s), 0 Warnings 5. Click on Debug Menu and Select Start/Stop Debug Session. Running the Keil Debugger 1. 2. 3. 4. The Keil Debugger should be now be Running Click on Peripherals.Select I/O Ports, Select Port 1. A new window should port will pop up. This represents the Port and Pins. Step through the code by Pressing F11 on the Keyboard. The Parallel Port 1 Box should change as you completely step through the code.

To exit out, Click on Debug Menu and Select Start/Stop Debug Session

FLASH MAGIC:
NXP Semiconductors produce a range of Microcontrollers that feature both on-chip Flash memory and the ability to be reprogrammed using In-System Programming technology.Flash Magic is Windows software from the Embedded Systems Academy that allows easy access to all the ISP features provided by the devices. These features include: Erasing the Flash memory (individual blocks or the whole device) Programming the Flash memory Modifying the Boot Vector and Status Byte Reading Flash memory Performing a blank check on a section of Flash memory Reading the signature bytes Reading and writing the security bits Direct load of a new baud rate (high speed communications) Sending commands to place device in Bootloader mode

FLASH MAGIC TUTORIAL:


The following is a screenshot of the main Flash Magic window. The appearance may differ Slightly depending on the device selected.

STEP 1: select the device as 89V51RD2, com port -COM 1, baud rate- 9600, Interface None (ISP)

STEP 2: Goto OPTIONS- advanced options

STEP 3: Untick all the options.

STEP 4: Browse the HEX file you want to burn

STEP 5: Click on start button. Keep the reset button in off state

STEP 6: when this window opens , press the reset button.

STEP 7: To view the output or make controller work for you. Switch off the reset button and switch it on again.

WRITING AND DEBUGGING A BASIC PROGRAM

KIT DESCRIPTION

LCD PROGRAMMING
Pin Description
The most commonly used LCDs found in the market today are 1 Line, 2 Line or 4 Line LCDs which have only 1 controller and support at most of 80 charachers, whereas LCDs supporting more than 80 characters make use of 2 HD44780 controllers. Most LCDs with 1 controller has 14 Pins and LCDs with 2 controller has 16 Pins (two pins are extra in both for back-light LED connections). Pin description is shown in the table below.

g)
Pin No.

Figure 1: Character LCD type HD44780 Pin diagram

Name

Description

Pin no. 1 VSS Pin no. 2 VCC Pin no. 3 VEE Pin no. 4 RS Pin no. 5 R/W Pin no. 6 EN Pin no. 7 D0 Pin no. 8 D1 Pin no. 9 D2 Pin no. 10 D3 Pin no. 11 D4 Pin no. 12 D5 Pin no. 13 D6 Pin no. 14 D7
h)

Power supply (GND) Power supply (+5V) Contrast adjust 0 = Instruction input 1 = Data input 0 = Write to LCD module 1 = Read from LCD module Enable signal Data bus line 0 (LSB) Data bus line 1 Data bus line 2 Data bus line 3 Data bus line 4 Data bus line 5 Data bus line 6 Data bus line 7 (MSB)

Table 1: Character LCD pins with 1 Controller

Commands and Instruction set


Only the instruction register (IR) and the data register (DR) of the LCD can be controlled by the MCU.

Before starting the internal operation of the LCD, control information is temporarily stored into these registers to allow interfacing with various MCUs, which operate at different speeds, or various peripheral control devices. The internal operation of the LCD is determined by signals sent from the MCU. These signals, which include register selection signal (RS), read/write signal (R/W), and the data bus (DB0 to DB7), make up the LCD instructions (Table 3). There are four categories of instructions that:

Designate LCD functions, such as display format, data length, etc. Set internal RAM addresses Perform data transfer with internal RAM Perform miscellaneous functions
No. Instruction Hex Decimal

1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17

Function Set: 8-bit, 1 Line, 5x7 Dots Function Set: 8-bit, 2 Line, 5x7 Dots Function Set: 4-bit, 1 Line, 5x7 Dots Function Set: 4-bit, 2 Line, 5x7 Dots Entry Mode Display off Cursor off (clearing display without clearing DDRAM content) Display on Cursor on Display on Cursor off Display on Cursor blinking Shift entire display left Shift entire display right Move cursor left by one character Move cursor right by one character Clear Display (also clear DDRAM content) Set DDRAM address or coursor position on display Set CGRAM address or set pointer to CGRAM location

0x30 0x38 0x20 0x28 0x06 0x08 0x0E 0x0C 0x0F 0x18 0x1C 0x10 0x14 0x01

48 56 32 40 6 8 14 12 15 24 30 16 20 1

0x80+add* 128+add* 0x40+add** 64+add**

Table 4: Frequently used commands and instructions for LCD

Table 3: Command and Instruction set for LCD type HD44780

SERIAL PROGRAMMING
Introduction
UART (Universal Asynchronous Receiver Transmitter) or USART (Universal Synchronous Asynchronous Receiver Transmitter) are one of the basic interface which you will find in almost all the controllers available in the market till date. This interface provide a cost effective simple and reliable communication between one controller to another controller or between a controller and PC.

RS-232

Basics

RS-232 (Recommended Standard 232) is a standard for serial binary data signals connecting between a DTE (Data terminal equipment) and a DCE (Data Circuit-terminating Equipment). VoltageLevels:

The RS-232 standard defines the voltage levels that correspond to logical one and logical zero levels. Valid signals are plus or minus 3 to 25 volts. The range near zero volts is not a valid RS-232 level; logic one is defined as a negative voltage, the signal condition is called marking, and has the functional significance of OFF. Logic zero is positive, the signal condition is spacing, and has the function ON. So a Logic Zero represented as +3V to +25V and Logic One represented as -3V to -25V.

RS-232 Level Converters


Usually all the digital ICs works on TTL or CMOS voltage levels which cannot be used to communicate over RS-232 protocol. So a voltage or level converter is needed which can convert TTL to RS232 and RS232 to TTL voltage levels. The most commonly used RS-232 level converter is MAX232. This IC includes charge pump which can generate RS232 voltage levels (-10V and +10V) from 5V power supply. It also includes two receiver and two transmitters and is capable of full-duplex UART/USART communication.

MAX232 Interfacing with Microcontrollers


To communicate over UART or USART, we just need three basic signals which are namely, RXD (receive), TXD (transmit), GND (common ground). So to interface MAX232 with any microcontroller (AVR, ARM, 8051, PIC etc..) we just need the basic signals. A simple schematic diagram of connections between a microcontroller and MAX232 is shown below

8051 Programming for UART


In 8051, we make use of Timer 1 to generate the required baud rate. Following are the registers that are need to be configured to communicate over UART. TMOD SCON TH1 TL1 TCON TMOD: This register is used to set the mode of Timer0 and Timer1. It is also used to select whether the timers are used as Timer or Counter. SCON: Serial Control register has various functions like.. it has flags for Framing error, Transmit interrup and receive interrupt. Its used to select the serial port mode, to enable or disable the reception etc. TCON: This register has various flag and control bits e.g. Timer overflow flags, interrupt edge flags, timer control bits to start/stop the timer. TH1 & TL1: Timer registers for Timer 1 determines the baud rate of UART.

Initializing USART in 8051


CODE:

Serial_Init: ;Set timer 1 mode to 8-bit Auto-Reload mov TMOD,#20H

;Enable reception ;Set Serial port mode to 8-bit UART mov SCON,#50H ;Set baudrate to 9600 at 11.0592MHz mov TH1,#0FDH mov TL1,#0FDH ;Start Timer setb TR1 ret

in C we can do this as..


CODE:

#include <reg51.h>. void serial_init(){ TMOD = 0x20; SCON = 0x50; TH1 = 0xFD; TL1 = 0xFD; TR1 = 1; }

To Send data to the serial port we just have to move the data in SBUF (serial buffer register) and wait for the Transmit Interrupt flag to be set. While receiving we wait for the Receive interrupt flag to be set and read the data from SBUF register. This can be done as shown below...
CODE:

Serial_Send: ;wait for last data to be ;sent completely jnb TI,Serial_Send ;clear the transmit interrupt flag clr TI ;Then move the data to send in SBUF mov SBUF,A ret Serial_Read: ;Wait for Receive interrupt flag jnb RI,Serial_Read ;If falg is set then clear it clr RI ;Then read data from SBUF mov A,SBUF ret

in C we can do this as..


CODE:

void serial_send(unsigned char dat){ while(!TI); TI = 0; SBUF = dat; } unsigned char serial_read(){ while(!RI);

RI = 0; return SBUF; }

Uses of Timers & Counters


- Interval Timing Periodic event timing Time base for measurements - Event Counting - Baud Rate Generation 8051 Timers - 2 timers (Timer 0 and Timer 1) - 16-bit timers (65,535) max - Flag is set when the timer overflows - Timers can be based on internal clock (OSC/6) or from external source (counter mode).

Internal operation of the 8051 timer (same for both timers)

Timer Registers - TCON Timer Control - TMOD Timer Mode - TH0/TL0 Timer 0 16 bit register (byte addressable only) - TH1/TL1 Timer 1 16 bit register (byte addressable only) TCON TCON SFR and its individual bits - IT0/IT1: Used for timer Interrupts - IE0/IE1: Used for external Interrupts - TR0/TR1: Timer 0/1 run control flag

1 = Run - TF0/TF1: Timer 0/1 overflow flag 1 = Overflow

TMOD
TMOD SFR and its individual bits - M0/M1: sets the Mode of the

respective timer - C/T: External Counter/Internal Timer select 1 = Counter, 0 = Timer

- Gate: When set (1), timer runs only when respective INT input is

Timer Modes - 0: 13 bit timer - 1: 16-bit timer - 2: 8-Bit auto reload - 3: Split timer mode Mode 0: 13-Bit Timer - Lower byte (TL0/TL1) + 5 bits of upper bytes (TH0/TH1). - Backward compatible to the 8048 - Not generally used

Timer operation in Mode 0

Mode 1: 16-bit - All 16 bits of the timer (TH0/TL0, TH1,TL1) are used. - Maximum count is 65,536 - At 12Mhz, maximum interval is 65536 microseconds or 65.536 milliseconds - TF0 must be reset after each overflow - THx/TLx must be manually reloaded after each overflow

Timer operation in Mode 1

Mode 2: 8-bit Auto Reload - Only the lower byte (TLx) is used for counting. - Upper byte (THx) holds the value to reload into TLx after an overflow. - TFx must be manually cleared. - Maximum count is 256 - Maximum interval is 256 Microseconds or .256 milliseconds

Timer operation in Mode 2

Mode 3- Split Timer - Splits Timer 0 into two 8-bit timers - TL0 sets TF0 - TH0 sets TF1 - Timer 1 is available for other 3 modes, but the TF1 is not available.

Timer operation in Mode 3

Timer Delay and Timer Reload Value Timer Delay = Delay Value Timer Clock Cycle Duration Delay Value = how many counts before register(s) roll over Timer Clock Cycle Duration = 6/oscillator frequency Delay Value = Maximum Register Count Timer Reload Value Maximum Register Count = 65535 Timer Reload Value = ? Example 1

Calculation of Timer 0 reload value needed to achieve timer delay of 20 ms. Oscillator frequency is 11.0592 MHz. Delay Value = Timer Delay / Timer Clock Cycle Duration = 20 X 10 6 11.0592 x 106 = 36864 (must be rounded to the nearest integer) Timer Reload Value = Maximum Register Count - Delay Value = 65535 36864 = 28671 = 0x6FFF so Timer 0 is loaded with: TH0 = 0x6F; TL0 = 0xFF; Example 2 Function to generate 100 s delay using timer 0. Procedure is: Initialise TMOD register Initialise TL0 and TH0 Start the Timer Monitor TF0 until it is set Delay: MOV TMOD,#01H ; Initialise TMOD MOV TL0,#47H ; initialise TL0 MOV TL0,#FFH ; initialise TH0 SETB TR0 ; start timer Wait: JNB TF0,Wait ; wait for TF0 CLR TR0 ; stop timer CLR TF0 ; clear TF0 RET Delay Value = Timer Delay / Timer Clock Cycle Duration = 20 X 10 6 11.0592 x 106 Timer Reload Value = 65535 184 = 65351 = 0xFF47 so Timer 0 is loaded with: TH0 = 0x6F; TL0 = 0xFF;

Martix Keypad Interfacing with Microcontrollers: Introduction


Introduction
Keypads are a part of HMI or Human Machine Interface and play really important role in a small embedded system where human interaction or human input is needed. Martix keypads are well known for their simple architecture and ease of interfacing with any microcontroller. In this part of tutorial we will learn how to interface a 4x4 matrix keypad with AVR and 8051 microcontroller. Also we will see how to program then in Assembly and C.

Constructing a Matrix Keypad


Constuction of a keypad is really simple. As per the outline shown in the figure below we have four rows and four columns. In between each overlapping row and column line there is a key.

So keeping this outline we can constuct a keypad using simple SPST Switches as shown below:

Now our keypad is ready, all we have to do is connect the rows and columns to a port of microcontroller and program the controller to read the input.

Scanning a Matrix Keypad


There are many methods depending on how you connect your keypad with your controller, but the basic

logic is same. We make the coloums as i/p and we drive the rows making them o/p, this whole procedure of reading the keyboard is called scanning. In order to detect which key is pressed from the matrix, we make row lines low one by one and read the coloums. Lets say we first make Row1 low, then read the columns. If any of the key in row1 is pressed will make the corrosponding column as low i.e if second key is pressed in Row1, then column2 will give low. So we come to know that key 2 of Row1 is pressed. This is how scanning is done. So to scan the keypad completely, we need to make rows low one by one and read the columns. If any of the button is pressed in a row, it will take the corrosponding column to a low state which tells us that a key is pressed in that row. If button 1 of a row is pressed then Column 1 will become low, if button 2 then column2 and so on...

S-ar putea să vă placă și