kavithavcet.files.wordpress.com  · web viewpop the current stack top to the pc. 2. pop the...

21
MICROCONTROLER – KEYBOARD AND DISPLAY INTERFACING Keyboards and LCDs are the most widely used input/output devices of the 8051. Interfacing the keyboard to the 8051 Keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports; therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between rows and columns. In IBM PC keyboards, a single microcontroller (consisting of a microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of hardware and software interfacing of the keyboard. In such systems, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has been activated, and present it to the motherboard. Scanning and identifying the key Figure shows a 4 x 4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1 s for all columns since they are all connected to high (V cc ). If all the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed.

Upload: truongtruc

Post on 07-Feb-2019

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

MICROCONTROLER – KEYBOARD AND DISPLAY INTERFACING

Keyboards and LCDs are the most widely used input/output devices of the 8051.

Interfacing the keyboard to the 8051Keyboards are organized in a matrix of rows and columns. The CPU accesses both

rows and columns through ports; therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between rows and columns. In IBM PC keyboards, a single microcontroller (consisting of a microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of hardware and software interfacing of the keyboard. In such systems, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has been activated, and present it to the motherboard. Scanning and identifying the key

Figure shows a 4 x 4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1 s for all columns since they are all connected to high (V cc). If all the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed.

Matrix Keyboard Connection to Ports

To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, then it reads the columns. If the data read from the columns is D3 – DO = 1111, no key has been pressed and the process continues until a key press is detected. However, if

Page 2: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

one of the column bits has a zero, this means that a key press has occurred. For example, if D3 – DO = 1101, this means that a key in the Dl column has been pressed.

After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row DO only; then it reads the columns. If the data read is all 1’s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. Example 12-3

From Figure , identify the row and column of the pressed key for each of the following.1. D3 – DO = 1110 for the row, D3 – DO = 1011 for the column2. D3 – DO = 1101 for the row, D3 – DO = 0111 for the columnSolution:

From Figure the row and column can be used to identify the key.1. The row belongs to DO and the column belongs to D2; therefore, key number 2 was

pressed.2. The row belongs to Dl and the column belongs to D3; therefore, key number 7 was

pressed.

Interfacing hex keypad to 8051The circuit is very simple and it uses only two ports of the microcontroller, one for

the hex keypad and the other for the seven segment LED display.

Page 3: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

The hex keypad is interfaced to port 1 and seven segment LED display is interfaced to port 0 of the microcontroller. Resistors R1 to R8 limits the current through the corresponding segments of the LED display. Capacitors C1, C2 and crystal X1 completes the clock circuitry for the microcontroller. Capacitor C3, resistor R9 and push button switch S1 forms a debouncing reset mechanism.

Note: The hardware designer has to select keys that will survive in the intended environment. The programmer must write the code that will anticipate and defeat deliberate attempts by human to confuse program. The software must be such that the keyboard application must guard against following Human Factors:

1. More than one key pressed.

2. Key pressed and held.

3. Rapid key press and release.

5.2 Keyboard Configurations

INTERRUPTS

BASICS OF INTERRUPTS

During program execution if peripheral devices needs service from microcontroller, device will generate interrupt and gets the service from microcontroller. When peripheral device activate the interrupt signal, the processor branches to a program called interrupt service routine. After executing the interrupt service routine the processor returns to the main program.

Steps taken by processor while processing an interrupt:

1. It completes the execution of the current instruction.2. PSW is pushed to stack.3. PC content is pushed to stack.4. Interrupt flag is reset.5. PC is loaded with ISR address.ISR will always ends with RETI instruction. The execution of RETI instruction

results in the following.1. POP the current stack top to the PC.2. POP the current stack top to PSW.

Classification of interrupts.1. External and internal interrupts.

External interrupts are those initiated by peripheral devices through the external pins of the microcontroller.

Page 4: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

Internal interrupts are those activated by the internal peripherals of the microcontroller like timers, serial controller etc.)

2. Maskable and non-maskable interrupts.

The category of interrupts which can be disabled by the processor using program is called maskable interrupts.

Non-maskable interrupts are those category by which the programmer cannot disable it using program.3. Vectored and non-vectored interrupt.

Starting address of the ISR is called interrupt vector. In vectored interrupts the starting address is predefined. In non-vectored interrputs, the starting address is provided by the peripheral as follows.

Microcontroller receives an interrupt request from external device. Controller sends an acknowledgement (INTA) after completing the execution of current

instruction. The peripheral device sends the interrupt vector to the microcontroller.

8051 INTERRUPTS 8051 has five interrupts. They are maskable and vectored interrupts. Out of these five, two are external interrupt and three are internal interrupts.

8051 makes use of two registers to deal with interrupts.

IE Register - Interrupt Enable register

• This is an 8 bit register used for enabling or disabling the interrupts. The structure of IE is

Page 5: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

IP Register. This is an 8 bit register used for setting the priority of the interrupts.

TIMER /COUNTERTimers/Counters are used generally for

• Time reference • Creating delay • Wave form properties measurement • Periodic interrupt generation • Waveform generation

Page 6: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

The two timers in 8051 share two SFRs (TMOD and TCON) which control the timers, and each timer also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1).

8051 has two 16-bit programmable UP timers/counters. They can be configured to operate either as timers or as event counters

• In the "timer" function mode, the counter is incremented in every machine cycle. • In the "counter" function mode, the register is incremented in response to a 1 to 0

transition at its corresponding external input pin (T0 or T1). It requires 2 machine cycles to detect a high to low transition.

Timer Mode control (TMOD) Special Function Register

TMOD register is not bit addressable.

Gate: Based on the status of the signal. Timer starts counting using either internal clock (timer mode) or external pulses (counter mode).C/T*: It is used for the selection of Counter/Timer mode. Mode Select Bits.M1 and M0 are mode select bits. 

Mode select bits: The M1 and M0 are mode select bits, which are used to select the timer operations. There are four modes to operate the timers.

Page 7: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

Mode 0: This is a 13-bit mode that means the timer operation completes with “8192” pulses.Mode 1: This is a16-bit mode, which means the timer operation completes with maximum clock pulses that “65535”.Mode 2: This mode is an 8-bit auto reload mode, which means the timer operation completes with only “256” clock pulses.Mode 3: This mode is a split-timer mode, which means the loading values in T0 and automatically starts the T1.

Mode selection Values of timers and counter in 8051

Timer control (TCON) Special function register:• TCON is bit addressable. The address of TCON is 88H. It is partly related to Timer

and partly to interrupt.

••

Page 8: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

Time Delay Calculations for 8051 MicrocontrollerThe 8051 microcontroller works with 11.0592 MHz frequency.Frequency 11.0592MHz=12 pules1 clock pulse =11.0592MHz/12F =0.921 MHzTime delay=1/FT=1/0.92MHzT=1.080506 us (for ‘1’ cycle)1000us=1MS1000ms=1sec

Procedure to Calculate the Delay Program1. Load the TMOD register value for ‘Timer0’ and ‘Timer1’in different modes. For example, if we want to operate timer1 in mode1 it must be configured as “TMOD=0x10.2. To operate the timer in mode 1, timer takes the maximum pulses of 65535. Then the calculated time-delay pulses must be subtracted from the maximum pulses, and afterwards converted to hexadecimal value. This value has to be loaded in timer1 higher bit and lower bits. This timer operation is programmed using embedded C in a microcontroller.Example: 500us time delay500us/1.080806us461pulsesP=65535-461P=6507465074 conveted by hexa decimal =FE32

Page 9: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

TH1=0xFE;TL1=0x32;3. Start the timer1 “TR1=1;”4. Monitor the flag bit “while (TF1==1)”5. Clear the flag bit “TF1=0”6. Cleat the timer “TR1=0”

Steps for programming timers in 8051 Mode 1: Load the TMOD value register indicating which timer (0 or 1) is to be used and which

timer mode is selected. Load registers TL and TH with initial count values. Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer 1. Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruction to see if it is

raised. Get out of the loop when TF becomes high. Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer 1,

respectively. Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for

timer 0 and timer 1, respectively. Go back to step 2 to load TH and TL again.

Program: count value is FFO6

MOV TMOD,#10 ;Timer 1, mode 1 AGAIN: MOV TL1,#06H ;TL0 = 06H

MOV TH1,#0FFH ;TH0 = FFH SETB TR1 ;Start timer 1

BACK: JNB TF1,BACK ;Stay until timer rolls over CLR TR1 ;Stop timer 1 CPL P1.5 ;Complement P1.5 to get Hi, Lo CLR TF1 ;Clear timer flag 1 SJMP AGAIN ;Reload timer

Mode 0:

The programming techniques mentioned here are also applicable to counter/timer mode 0. The only difference is in the number of bits of the initialization value.

Mode 2: Load the TMOD value register indicating which timer (0 or 1) is to be used; select

timer mode 2.

Page 10: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

Load TH register with the initial count value. As it is an 8-bit timer, the valid range is from 00 to FFH.

Start the timer. Keep monitoring the timer flag (TFx) with the “JNB TFx,target” instruction to see if

it is raised. Get out of the loop when TFx goes high. Clear the TFx flag. Go back to step 4, since mode 2 is auto-reload.

Waveform Generation To generate a sine wave, we first need a table whose values represent the magnitude

of the sine of angles between 0 and 360 degrees. The values for the sine function vary from -1.0 to +1.0 for 0- to 360-degree angles.

Therefore, the table values are integer numbers representing the voltage magnitude for the sine of theta.

This method ensures that only integer numbers are output to the DAC by the 8051 microcontroller. Table 13-7 shows the angles, the sine values, the voltage magnitudes, and the integer values representing the voltage magnitude for each angle (with 30-degree increments).

To generate Table, we assumed the full-scale voltage of 10 V for DAC output (as designed in Figure 13-18). Full-scale output of the DAC is achieved when all the data inputs of the DAC are high. Therefore, to achieve the full-scale 10 V output, we use the following equation.

Vout = 5 V + (5* sin theta) Vout of DAC for various angles is calculated and shown in Table.

Page 11: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

• To find the value sent to the DAC for various angles, we simply multiply the Vout voltage by 25.60 because there are 256 steps and full-scale Vout is 10 volts.

• Therefore, 256 steps /10 V = 25.6 steps per volt. To further clarify this, look at the following code.

• This program sends the values to the DAC continuously (in an infinite loop) to produce a sine wave.

*****Interfacing of ADC/DAC CONVERTER

In most of the cases, the PPI 8255 is used for interfacing the analog to digital converters with microprocessor. The analog to digital converters is treaded as an input device by the microprocessor, that sends an initialising signal to the ADC to start the analogy to digital data conversation process. The start of conversation signal is a pulse of a specific duration.

Page 12: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

• The process of analog to digital conversion is a slow process, and the microprocessor has to wait for the digital data till the conversion is over.

• After the conversion is over, the ADC sends end of conversion EOC signal to inform the microprocessor that the conversion is over and the result is ready at the output buffer of the ADC.

• These tasks of issuing an SOC pulse to ADC, reading EOC signal from the ADC and reading the digital output of the ADC are carried out by the CPU using 8255 I/O ports.

• The time taken by the ADC from the active edge of SOC pulse till the active edge of EOC signal is called as the conversion delay of the ADC.

• It may range anywhere from a few microseconds in case of fast ADC to even a few hundred milliseconds in case of slow ADCs.

• The available ADC in the market use different conversion techniques for conversion of analog signal to digitals.

• Successive approximation techniques and dual slope integration techniques are the most popular techniques used in the integrated ADC chip.

General algorithm for ADC interfacing contains the following steps:1. Ensure the stability of analog input, applied to the ADC.2. Issue start of conversion pulse to ADC3.Read end of conversion signal to mark the end of conversion processes.4. Read digital data output of the ADC as equivalent digital output.5. Analog input voltage must be constant at the input of the ADC right from the start of conversion till the end of the conversion to get correct results. This may be ensured by a sample and hold circuit which samples the analog signal and holds it constant for a specific time duration. The microprocessor may issue a hold signal to the sample and hold circuit.6. If the applied input changes before the complete conversion process is over, the digital equivalent of the analog input calculated by the ADC may not be correct.

Page 13: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

Analog input IN0 to IN7Address Lines A B CDigital output bits D0 to D7SC - Start conversion; input pin; a low to high pulse is givenEOC -End of conversion; output pin; goes low when the conversion is overALE - Input pin; low to high pulse is required to latch in the addressClock input - Clock input; to provide external clock

Page 14: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts
Page 15: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

Digital to Analog Converter

Page 16: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts

• The digital-to-analog converter (DAC) is a device widely used to convert digital pulses to analog signals. two methods of creating a DAC: binary weighted and R/2R ladder. 

• The first criterion for judging a DAC is its resolution, which is a function of the number of binary inputs. The common ones are 8, 10, and 12 bits. The number of data bit inputs decides the resolution of the DAC since the number of analog output levels is equal to 2″, where n is the number of data bit inputs.

• Therefore, an 8-input DAC such as the DAC0808 provides 256 discrete voltage (or current) levels of output. Similarly, the 12-bit DAC provides 4096 discrete voltage levels

MC1408 DAC (or DAC0808) • In the MC1408 (DAC0808), the digital inputs are converted to current (Iout), and by

connecting a resistor to the Ioutpin, we convert the result to voltage.• The total current provided by the Iout pin is a function of the binary numbers at the DO

– D7 inputs of the DAC0808 and the reference current (Iref), and is as follows:

where DO is the LSB, D7 is the MSB for the inputs, and Iref is the input current that must be applied to pin 14. The Iref current is generally set to 2.0 mA.

Page 17: kavithavcet.files.wordpress.com  · Web viewPOP the current stack top to the PC. 2. POP the current stack top to PSW. Classification of interrupts. 1. External and internal interrupts