Documente Academic
Documente Profesional
Documente Cultură
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.
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
;make sure that key has been released ;output the ASCII value 0 to port 3 ; repeat key scanning process
;make sure that key has been released ;output the ASCII value 0 to port 3 ; repeat key scanning process
;make sure that key has been released ;output the ASCII value 0 to port 3 ; repeat key scanning process
;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
;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
;make sure that key has been released ;output the ASCII value 7 to port 3
Prepared by Emil Raj,Dept. Of ECE,MLMCE
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
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
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
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
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
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.
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
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.
AT KEYBOARD
INTERFACE(CONNECTOR)
AT KEYBOARD
SCAN CODES
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.
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
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.
DC MOTOR INTERFACING
BIDIRECTIONAL CONTROL
BIDIRECTIONAL CONTROL
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
CLOCKWISE: SETB P1.0 CLR P1.1 CLR P1.2 SETB P1.3 SJMP MONITOR END
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:
PWM comparisons
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
.
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.
;load step sequence ;issue sequence to motor ; rotate right clockwise ; wait ; keep going
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.
; 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
CW:
RR A ACALL DELAY MOV PI, A SJMP TURN MOV R2, #100 MOV R3, #255 DJNZ R3, H2 DJNZ R2,H1 RET END
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