Sunteți pe pagina 1din 69

Microcontroller Based System Design Module 5

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Module 5
Matrix key board interface - AT keyboard commands keyboard response codes watch dog timers - DS1232 watch dog timer real time clocks DS1302 RTC interfacing - measurement of frequency - phase angle - power factor stepper motor interface - dc motor speed control L293 motor driver - design of a position control system - interfacing programs using C and assembly language.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Matrix Keyboard Interfacing


Organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports; 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
Prepared by Emil Raj,Dept. Of ECE,MLMCE

Keyboard Interfacing Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Scanning and identifying the key


4 x 4 matrix connected to two ports. Rows are connected to an output port Columns are selected to an input port. If no key has been pressed, reading the input port will yield 1s for all columns since they are connected to high (Vcc). 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.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Scanning and identifying the key


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. If 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.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

Scanning and identifying the key


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 D0 only; then it reads the columns. If the data read is all 1s, 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. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed.Prepared by Emil Raj,Dept. Of ECE,MLMCE

Keyboard Interfacing Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Row and Column status for each key press.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

PROGRAM
ORG 00h MOV P2,#0FFH REPEAT:MOV P1,#0EH JNB P2.0,NUM_0 JNB P2.1,NUM_1 JNB P2.2,NUM_2 JNB P2.3,NUM_3 MOV P1,#0DH JNB P2.0,NUM_4 JNB P2.1,NUM_5 JNB P2.2,NUM_6 JNB P2.3,NUM_7 ;make port 2 input ;MAKE ROW 0=0 ; check column0 ,then key 0 is pressed ; check column1 ,then key 1 is pressed ; check column2 ,then key 2 is pressed ; check column3 ,then key 3 is pressed ;MAKE ROW 1=0 ; check column0 ,then key 4 is pressed ;check column1 ,then key 5 is pressed ; check column2 ,then key 6 is pressed ; check column3 ,then key 7 is pressed
Prepared by Emil Raj,Dept. Of ECE,MLMCE

MOV P1,#0BH JNB P2.0,NUM_8 JNB P2.1,NUM_9 JNB P2.2,NUM_A JNB P2.3,NUM_B

;MAKE ROW 2=0 ; check column0 ,then key 8 is pressed ;check column1 ,then key 9 is pressed ; check column2 ,then key A is pressed ; check column3 ,then key B is pressed

MOV P1,#07H JNB P2.0,NUM_C JNB P2.1,NUM_D JNB P2.2,NUM_E JNB P2.3,NUM_F ABOVE: SJMP REPEAT

;MAKE ROW 3=0 ; check column0 ,then key C is pressed ;check column1 ,then key D is pressed ; check column2 ,then key E is pressed ; check column3 ,then key F is pressed

Prepared by Emil Raj,Dept. Of ECE,MLMCE

NUM_0: JNB P2.0,NUM_0 MOV P3,#'0' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 0 to port 3 ; repeat key scanning process

NUM_1: JNB P2.1,NUM_1 MOV P3,#'1' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 0 to port 3 ; repeat key scanning process

NUM_2: JNB P2.2,NUM_2 MOV P3,#'2 SJMP ABOVE

;make sure that key has been released ;output the ASCII value 0 to port 3 ; repeat key scanning process

NUM_3: JNB P2.3,NUM_3 MOV P3,#'3' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 3 to port 3 ; repeat key scanning process
Prepared by Emil Raj,Dept. Of ECE,MLMCE

NUM_4: JNB P2.0,NUM_4 MOV P3,#'4' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 4 to port 3 ; repeat key scanning process

NUM_5: JNB P2.1,NUM_5 MOV P3,#'5' SJMP ABOVE NUM_6: JNB P2.2,NUM_6 MOV P3,#'6' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 5 to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value 6 to port 3 ; repeat key scanning process

NUM_7: JNB P2.3,NUM_7 MOV P3,#'7' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 7 to port 3
Prepared by Emil Raj,Dept. Of ECE,MLMCE

; repeat key scanning process

NUM_8:JNB P2.0,NUM_8 MOV P3,#'8' SJMP ABOVE NUM_9:JNB P2.1,NUM_9 MOV P3,#'9' SJMP ABOVE NUM_A:JNB P2.2,NUM_A MOV P3,#'A' SJMP ABOVE NUM_B:JNB P2.3,NUM_B MOV P3,#'B' SJMP ABOVE

;make sure that key has been released ;output the ASCII value 8 to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value 9 to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value A to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value B to port 3 ; repeat key scanning process
Prepared by Emil Raj,Dept. Of ECE,MLMCE

NUM_C:JNB P2.0,NUM_C MOV P3,#'C' SJMP ABOVE NUM_D:JNB P2.1,NUM_D MOV P3,#'D' SJMP ABOVE NUM_E:JNB P2.2,NUM_E MOV P3,#'E' SJMP ABOVE NUM_F:JNB P2.3,NUM_F MOV P3,#'F' SJMP ABOVE END

;make sure that key has been released ;output the ASCII value C to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value D to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value E to port 3 ; repeat key scanning process ;make sure that key has been released ;output the ASCII value F to port 3 ; repeat key scanning process
Prepared by Emil Raj,Dept. Of ECE,MLMCE

MEASURING FREQUENCY OF AN INPUT SIGNAL

Prepared by Emil Raj,Dept. Of ECE,MLMCE

MEASUREMENT OF FREQUENCY
The 8051 can be easily configured and programmed to display the frequency of the input signal. Here the incoming signal is connected to Pin T0 The input to T0 is shown as a high frequency signal. The method is to count the number of pulses in a fixed interval of 125ms. Frequency is calculated as cycles/second and is obtained by multiplying the pulses counted in 125ms by 8.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

Steps for configuring Counter 0 and Timer 1


Counter 0 is configured as an external event counter (C/T = 1). Timer 1 is programmed to give a known delay interval of 125ms. Counter 0 is turned ON at the start of the measuring sequence(TR0 =1).

Timer 1 is started immediately (TR1 = 1). Software now polls the TF1 bit to see if Timer 1 has overflowed. When TF1 is set Counter 0 is read (Read TH0:TL0). The value read in this counter is the number of pulses received per 125ms. This multiplied by 8 will be frequency of the input signal.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

Write a program to count the frequency of an input signal that ranges from 500Hz to 2.0KHz. Assume a clock frequency of 6.00MHz. The input signal is connected to Pin TO of the 8051. Solution
Configure Counter 0 in Mode 1 to count the external pulses Configure Timer 1 to overflow after 125ms. Timer 1 frequency = Fosc/12 = 6.000MHz/12 = 0.5MHz Each Timer 1 count takes l/0.5MHz = 2 us To generate TF1 after 125ms, the registers TH1:TL1 should be loaded with (65536-125ms/2 us) = 65536 - 62500 = 3036 = 0BDChex Load TH1 = 0B; TL1 = DC
Prepared by Emil Raj,Dept. Of ECE,MLMCE

PROGRAM
SetTimers: ORG 00H LJMP SetTimers MOV TMOD, #15H ; Counter 0 in Mode 1 (External Input) ; Timer 1 in Mode 1(Internal Input) ; Lower byte ; Upper byte ; Set Counter 0 Run control bit ; Set Timer 1 Run control bit
; Now check for Counter/Timer l overflow

InitTl:

MOVTL1,#0DCH MOV TH1, #0BH SETB TR0 SETB TR1 JNB TF1, ChkT1Ovf

ChkT1Ovf:

; TF1 bit not set - no overflow ; TF bit set - Timer 1 completed 125ms ; Read TH0:TL0

Prepared by Emil Raj,Dept. Of ECE,MLMCE

T1Ovf:

CLRTF1 CLR TR0 CLR TR1 MOV R0, TLO MOV R1, THO

; Clear TF1 Interrupt flag ; Stop Timer 0 ; Stop Timer 1 ; Low byte of Counter 0 ; High byte of Counter 0 ; Now R1, R0 contains the 16 bit value of ; number of transitions counted using ; Counter 0 for a period of 125ms ;Multiply this value by 8 to get frequency ;Multiply by 8 can be easily done by shifting ;the number 3 times to the left

MOV R2, #3

;load R2 with the number of shifts required

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Loop3: MOV A,R0 CLR C RLC A MOV R0,A MOV A, R1 RLC A MOV R1, A DJNZ R2, Loop3

;Load LSB ;Clear Carry ;Shift left by 1 bit C=8th bit ;Store result back into LSB ; Get MSB ; Shift left with carry from LSB ; Store it back ; Do 3 times ; R1, RO has the 16-bit value of the ;frequency input ; Move the low byte of frequency to PO ; Move the high byte of frequency to P2 ; Next pass ; End of program

MOV P0,R0 MOV P2,R1 AJMP InitTl END

Prepared by Emil Raj,Dept. Of ECE,MLMCE

WATCHDOG TIMER
Hardware that can be used to automatically detect software anomalies and reset the processor if any occur. Based on a counter that counts down from some initial value to zero. The embedded software selects the counter's initial value and periodically restarts it. The processor (and the software it's running) will be restarted as if a human operator had cycled the power.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

WATCHDOG TIMER
The watchdog timer is a chip external to the processor. The output from the watchdog timer is tied directly to the processor's reset signal.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

DS1232 MICROMONITOR CHIP


Monitors three vital conditions for a microprocessor: Power supply, Software execution(watch dog timer) External override(push button reset)

Prepared by Emil Raj,Dept. Of ECE,MLMCE

FEATURES
Halts and restarts an out-of-control microprocessor Holds microprocessor in check during power transients Automatically restarts microprocessor after power failure Monitors pushbutton for external override Eliminates the need for discrete components Space-saving, 8-pin mini-DIP (Optional 16-pin SOIC surface mount package) Available for Industrial temperature range (-40C to +85C)
Prepared by Emil Raj,Dept. Of ECE,MLMCE

PIN DIAGRAM

Prepared by Emil Raj,Dept. Of ECE,MLMCE

MICROMONITOR BLOCK DIAGRAM


.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

PUSH BUTTON RESET

Prepared by Emil Raj,Dept. Of ECE,MLMCE

AT KEYBOARD
AT keyboard is the US standard keyboard introduced in 1986 by IBM Used with the IBM compatible computer (a keyboard with 84 keys). Later replaced with the 101-key Enhanced keyboard. One of the most common keyboards used today. Uses the AT (Din5) port, PS/2 and USB keyboards.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

AT KEYBOARD

Prepared by Emil Raj,Dept. Of ECE,MLMCE

INTERFACE(CONNECTOR)

Prepared by Emil Raj,Dept. Of ECE,MLMCE

AT Keyboard General Description


Keyboards consist of a large matrix of keys, all of which are monitored by an on-board processor (called the "keyboard encoder".) The specific processor monitor which key(s) are being pressed/released and send the appropriate data to the host. Motherboard contains a "keyboard controller that is in charge of decoding all of the data received from the keyboard. All communication between the host and the keyboard uses an IBM protocol.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

AT Keyboard - Scan Codes


If any key is being pressed, released, or held down, the keyboard will send a packet of information known as a "scan code" to computer. There are two different types of scan codes: "make codes" and "break codes". A make code is sent when a key is pressed or held down. A break code is sent when a key is released. Every key is assigned its own unique make code and break code. The set of make and break codes for every key comprises a "scan code set".
Prepared by Emil Raj,Dept. Of ECE,MLMCE

AT KEYBOARD

Prepared by Emil Raj,Dept. Of ECE,MLMCE

SCAN CODES

Prepared by Emil Raj,Dept. Of ECE,MLMCE

REAL-TIME CLOCK
A real-time clock (RTC) is a computer clock (mostly in the form of an IC) that keeps track of the current time. Present in almost any electronic device which needs to keep accurate time. Benefits Low power consumption. Frees the main system for time-critical tasks. Sometimes more accurate than other methods.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

REAL-TIME CLOCK
Power source RTCs often have an alternate source of power. Normally a lithium battery in older systems, but some newer systems use a supercapacitor.

5.5.3 Timing Most RTCs use a crystal oscillator, Examples Many integrated circuit manufacturers make RTCs, including Intersil, Maxim, Philips, Texas Instruments and STMicroelectronics.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

DS1302 TRICKLE CHARGE TIMEKEEPING CHIP


Contains a real time clock/calendar and 31 bytes of static RAM. It communicates with a microprocessor via a simple serial interface. The real time clock/calendar provides seconds, minutes, hours, day, date, month, and year information with leap year compensation valid up to 2100. The clock operates in either the 24hour or 12hour format with an AM/PM indicator. The DS1302 has the additional features of dual power pins for primary (VCC2) and backup power supplies. 2.05.5 volt full operation. TTLcompatible
Prepared by Emil Raj,Dept. Of ECE,MLMCE

DS1302 - Pin Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

DS1302 Block Diagram

Prepared by Emil Raj,Dept. Of ECE,MLMCE

DC MOTOR INTERFACING
(DC) motor is a device that translates electrical pulses into mechanical movement. In the DC motor we have only + and - leads. Connecting them to a DC voltage source moves the motor in one direction. By reversing the polarity, the DC motor will move in the opposite direction. For example, small fans used in many motherboards to cool the CPU are run by DC motors.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

DC MOTOR INTERFACING

Prepared by Emil Raj,Dept. Of ECE,MLMCE

BIDIRECTIONAL CONTROL

Prepared by Emil Raj,Dept. Of ECE,MLMCE

BIDIRECTIONAL CONTROL

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Logic configurations for the H-Bridge design

Prepared by Emil Raj,Dept. Of ECE,MLMCE

A switch is connected to pin P2.7. a) If SW = 0, the DC motor moves clockwise . b) If SW = 1, the DC motor moves counterclockwise
ORG 0000H MATN: CLR P1.0 CLR P1.1 CLR P1.2 CLR P1.3 SETB P2.7 MONITOR: JNB P2.7, CLOCKWISE CLR P1.0 SETB P1.1 SETB P1.2 CLR P1.3 SJMP MONITOR

;switch 1 ;switch 2 ;switch 3 ;switch 4

;switch 1 ;switch 2 ;switch 3 ;switch 4

Prepared by Emil Raj,Dept. Of ECE,MLMCE

CLOCKWISE: SETB P1.0 CLR P1.1 CLR P1.2 SETB P1.3 SJMP MONITOR END

;switch 1 ;switch 2 ;switch 3 ;switch 4

Prepared by Emil Raj,Dept. Of ECE,MLMCE

L293 MOTOR DRIVER


The L293 is an integrated circuit motor driver that can be used for simultaneous, bi-directional control of two small motors.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

INTERFACING Bidirectional Motor control using an L293 chip

Prepared by Emil Raj,Dept. Of ECE,MLMCE

ORG 0000H
MAIN:

CLR P1.0 ;switch 1 CLR P1.1 ;switch 2 CLR P1.2 ;switch 3 SETB P2.7
MONITOR:

SETB P1.0 JNB P2.7, CLOCKWISE CLR P1.1 SETB P1.2 SJMP MONITOR
CLOCKWISE:

SETB P1.1 ; CLR P1.2 ; SJMP MONITOR

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Pulse width modulation (PWM)


Method to maintain a steady speed. The speed of the motor depends on three factors: (a) load, (b) voltage, and (c) current. By changing (modulating) the width of the pulse applied to the DC motor we can increase or decrease the amount of power provided to the motor, thereby increasing or decreasing the motor speed. Wider the pulse, the higher the Speed.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

PWM comparisons

Prepared by Emil Raj,Dept. Of ECE,MLMCE

DC motor control with optoisolator

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Refer to the DC motor connection of above Figure. To the 8051 of this figure, a switch SW i s connected to pin 3.2, which is the INTO pin. Write a program (a) Normally the motor runs with a 33% duty cycle (b) When INTO is activated, the motor runs with 10% duty cycle for a short duration.

ORG 0000H LJMP MAIN ;------------the ISR for interrupt INTO---------------ORG 0003H SJMP FIRST ;------------main program for initialization-----------ORG 0030H
Prepared by Emil Raj,Dept. Of ECE,MLMCE

MAIN: MOV IE,#81H ;enable INT 0 HERE: SETB Pl.0 MOV R0,#33 ;PI.0 set for 33% time ACALL DELAY CLR Pl.0 MOV R0,#67 ;PI.0 cleared for 67% time ACALL DELAY SJMP HERE ;--------------this is the ISR for INT 0-------------------------------FIRST: MOV R5,#0FFH ;this is to create a delay THERE: SETB PI.0 MOV R0,#10 ;P1.0 set for 10 % time ACALL DELAY CLR PI. 0 MOV R0,#90 ;P1.0 cleared for 90% time ACALL DELAY DJNZ R5, THERE ; exit from ISR when R5 = 0 RETI Prepared by Emil Raj,Dept. Of ECE,MLMCE

;--------------subroutine named DELAY-----------------DELAY : RPT1 : MOV Rl,#20 RPT 2 : MOV R2,#100 RPT 3 : DJNZ R2,RPT3 DJNZ R1,RPT2 DJNZ R0,RPT1 RET END
.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Stepper Motor
A stepper motor is a widely used device that translates electrical pulses into mechanical movement. Applications such - disk drives, dot matrix printers, and robotics, Position control. Stepper motors commonly have a permanent magnet rotor surrounded by a stator.
Prepared by Emil Raj,Dept. Of ECE,MLMCE

Stepper Motor
Most common stepper motors have four stator windings that are paired with a centertapped common. The center tap allows a change of current direction in each of two coils thereby resulting in a polarity change of the stator.

Prepared by Emil Raj,Dept. Of ECE,MLMCE

STEPPER MOTOR INTERFACING

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Stepper Motor Step Angles

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Program to rotate motor continuously


ORG BACK: 00H MOV A,#66H MOV PI,A RR A ACALL DELAY SJMP BACK MOV R2,#100 MOV R3,#255 DJNZ R3,H2 DJNZ R2,HI RET END
Prepared by Emil Raj,Dept. Of ECE,MLMCE

;load step sequence ;issue sequence to motor ; rotate right clockwise ; wait ; keep going

DELAY: HI: H2:

A switch is connected to pin P2.7. Write a program to monitor the status of SW and perform the following (a) I f SW = 0, the stepper motor moves clockwise. (b) If SW = 1, the stepper motor moves counterclockwise.

ORG OH MAIN: SETB P2.7 MOV A, #66H MOV PI, A

; starting address ; make an input ; starting phase value ; send value to port

TURN: JNB P2.7, CW RLA ACALL DELAY MOV PI,A SJMP TURN ; check switch result ; rotate left ; call delay ; write value to port ; repeat

Prepared by Emil Raj,Dept. Of ECE,MLMCE

CW:

RR A ACALL DELAY MOV PI, A SJMP TURN MOV R2, #100 MOV R3, #255 DJNZ R3, H2 DJNZ R2,H1 RET END

; rotate right ; call delay ; write value to port ; repeat

DELAY: H1: H2:

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Program To rotate a motor 64 in the clockwise direction. The motor has a step angle of 2. Solution: A motor with a 2 step angle has the following characteristics: step angle: 2 steps per revolution: 180 movement per 4-step sequence: 8 To move the rotor 64, we have to send eight consecutive 4-step sequences, i.e., 32 steps. by Emil Raj,Dept. Of ECE,MLMCE Prepared

ORG 0000H MOV A,#66H MOV R0,#32 BACK: RR A MOV PI,A ACALL DELAY DJNZ R0,BACK END

Prepared by Emil Raj,Dept. Of ECE,MLMCE

Prepared by Emil Raj,Dept. Of ECE,MLMCE

POSITION CONTROL SYSTEM


Give a brief description of each block. Give the explanation of stepper motor. Draw the interfacing diagram for stepper motor. Write the pgm for rotating it a particular degree.eg:64 degree

Prepared by Emil Raj,Dept. Of ECE,MLMCE

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