Transcript and P1.7

The 8051 Microcontrollers
Introduction
External hardware interrupts
Sampling the low leveltriggered interrupt
Edge-triggered interrupts
RI and TI flags and interrupts
Interrupt priority upon reset
Setting interrupt priority with the IP
register
Real World interfacing

LCD  Liquid Crystal Display



Advantages

Light

Thin

Low power consumption
Disadvantages

Expensive for computer display

Display size is limited by manufacturing complexities
16x2 and 20x2 displays.

Means 16 characters per line

Signals : 3 control lines  EN, RS, and RW.

4 or 8 I/O lines for the data bus.
LCD Interfacing

E
R/W
RS
DB7–DB0
8
microcontroller
LCD
controller
communications
bus
Control and Data
Signals

RS=0  Command,
RS=1  Data

RW=0  Write, RW=1
 Read

E=1  Enable (H-L
pulse)

D0-D7  8 Data lines


LCD Command Codes

Manufacturers Data sheet

Clear screen, move cursor, shift display etc
Using LCD
Instruction
Decimal
HEX
Function set (8-bit interface, 2 lines, 5*7 Pixels)
56
38
Scroll display one character right (all lines)
28
1C
Scroll display one character left (all lines)
24
18
2
2
Move cursor one character left
16
10
Move cursor one character right
20
14
Turn on visible underline cursor
14
0E
Turn on visible blinking-block cursor
15
0F
Make cursor invisible
12
0C
8
08
12
0C
1
01
Home (move cursor to top/left character position)
Blank the display (without clearing)
Restore the display (with cursor hidden)
Clear Screen
Keypad controller
N1
N2
N3
N4
k_pressed
M1
M2
M3
M4
4
key_code
keypad controller
N=4, M=4
key_code
Hexadecimal Keypad Interface
P1.0
1.
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
2.
3.
4.
Get a hexadecimal
character from the
keypad (IN_HEX,
GET_KEY scan
keypad)
Convert the
hexadecimal code to
ASCII (HTOA)
Send the ASCII code
to Display / Memory
(OUTCHR)
Go to step 1
Hexadecimal Keypad Interface
Key start pressed
Debouncing period
Key pressed and stabled
Mechanical bouncing
Similar debouncing for key released
Multiple 7-Segment LEDs Interface
5.0V
4.5V
4.0V
3.5V
3.0V
2.5V
2.0V
1.5V
1.0V
0.5V
0V
1111
1110
1101
1100
1011
1010
1001
1000
0111
0110
0101
0100
0011
0010
0001
0000
proportionality
4
4
3
3
analog output (V)
Vmax = 7.5V
7.0V
6.5V
6.0V
5.5V
analog input (V)
Analog-to-digital converters
2
1
t1
0100
t2
t3
time
t4
1000 0110 0101
Digital output
analog to digital
2
1
t1
t2
0100
t3
1000 0110
Digital input
digital to analog
t4
time
0101
Digital-to-analog conversion using
successive approximation
Given an analog input signal whose voltage should range from 0 to 15 volts, and an 8-bit digital encoding, calculate the correct encoding for
5 volts. Then trace the successive-approximation approach to find the correct encoding.
5/15 = d/(256-1)
d= 85
Encoding: 01010101
Successive-approximation method
½(Vmax – Vmin) = 7.5 volts
Vmax = 7.5 volts.
0
0
0
0
0
0
0
0
½(5.63 + 4.69) = 5.16 volts
Vmax = 5.16 volts.
0
1
0
1
0
0
0
0
½(7.5 + 0) = 3.75 volts
Vmin = 3.75 volts.
0
1
0
0
0
0
0
0
½(5.16 + 4.69) = 4.93 volts
Vmin = 4.93 volts.
0
1
0
1
0
1
0
0
½(7.5 + 3.75) = 5.63 volts
Vmax = 5.63 volts
0
1
0
0
0
0
0
0
½(5.16 + 4.93) = 5.05 volts
Vmax = 5.05 volts.
0
1
0
1
0
1
0
0
½(5.63 + 3.75) = 4.69 volts
Vmin = 4.69 volts.
0
1
0
1
0
0
0
0
½(5.05 + 4.93) = 4.99 volts
0
1
0
1
0
1
0
1
Digital-to-analog conversion using
successive approximation
Given an analog input signal whose voltage should range from 0 to 15 volts, and an 8-bit digital encoding, calculate the correct encoding for
5 volts. Then trace the successive-approximation approach to find the correct encoding.
7/15 = d/(256-1)
d= 119
Encoding: 01110111
Successive-approximation method
0
0
0
0
0
0
0
0
0
1
1
1
0
0
0
0
0
1
0
0
0
0
0
0
0
1
1
1
0
1
0
0
0
1
1
0
0
0
0
0
0
1
1
1
0
1
1
0
0
1
1
1
0
0
0
0
0
1
1
1
0
1
1
1
8051 Interfacing - ADCs, DACs,



ADC – Analog to Digital Converters
Needed to interface most sensors to digital systems
Commonly used device – ADC804
–
–
–
–
–
–
–
–
–
–


Microprocessor compatible, Resolution – 8bits
Single voltage supply (+5V) operation, conversion time (100us)
Differential analog inputs. 0 - 5V input voltage (Vin+, Vin-)
Variable input voltage range (Pin Vref/2)
Internal Clock Generator (RC on CLK IN, CLK R pins)
CS – Chip Select , active low
RD – Read Digital data from ADC, H-L edge triggered
WR -- Start conversion, L-H pulse edge triggered
INTR -- end of conversion, Goes low to indicate conversion done
Data bits -- D0-D7
Step Size - Smallest change – (2 x Vref/2)/ 256 for ADC804
Data Out : – Dout = Vin / Step Size
Main characteristics
–Resolution
–Dynamic range
–Bandwidth
–Conversion time
of ADC
Different types of ADC
–Successive approximation
–Slope integration
–Flash
available
Resolution
Related
to full scale if the ADC is linear
–LSB = A/2n
–Linear 8-bit ADC with a 1V full scale input
–Resolution = 1/28 = 3.9 mV (0.39%)
Interfacing ADC804 to 8051

Signals to be interfaced (on the ADC804)
– D0-D7, RD, WR, INTR, CS


Can do both Memory mapping and IO mapping
IO Mapping (easiest)
– Connect D0-D7, RD, WR, CS, INTR to some port bits on the 8051 (12
in all).
– CS may be unused for IO mapped operation (11 port bits in all)

Memory Mapping (timing is critical)
–
–
–
–
Connect D0-D7 of ADC804 to the data bus of the 8051 system
Connect RD, WR of the ADC804 to the 8051 system (ensure polarity)
Connect CS of ADC804 to an appropriate address decoder output
Connect INTR of ADC804 to an external interrupt Pin on the
8051(INT0 or INT1)
ADC804 - Code Examples

l IO Mapped à P1 to D0-D7, P2.5 to RD, P2.6 to
WR,

P2.7 to INTR
ADC_IO:
MOV P1, #0xFF
setb P2.5
;RD=1
clr P2.6
;WR=0
setb P2.6
;WR=1
WAIT: jb P2.7, WAIT
;wait for INTR
clr P2.5
;RD=0
mov A, P1
;read digital o/p
ret
Temperature Sensor – LM34DZ



LM34DZ ------- Linear, +32F to +212F,
10mV/F
3 pins : Vcc, GND and Vout
Interfacing LM34DZ with ADC804
– ADC804 has 256 steps
– LM34 produces 10mV/F, (212-32 = 180) steps
– So go for a full scale Vout of 256 x 10mv =
2.56V
– Vref/2 should then be 1.28V
– ADC O/P : 0x00 = +32F, 0x01 = +33F, 0x02 =
+34F …..
0xB4 = +212F
Digital to Analog Converter - DAC

Commonly used DAC808 (MC1408)
– R/2R ladder
– Iout = Iref (D7/2 + D6/4 + D5/8 + …… + D0/256)
– Iout converted to voltage by a resistive load or op-amp based
isolator (Rf from Vout to V- and V+ to GND)

PinOut
– D0-D7 à Connected to the Processor’s IO port
– Vref+, Vref-, Vee

Usage:
– Just write a byte to the IO port and the DAC converts it to an
analog value

Some 8051 clones have ADCs and DACs in built
Stepper Motor

Motor that moves in steps, a few degrees
at a time
– Widely used for precise controlled movement
– drive heads, robots, mechanical actuators
etc
– Clockwise and anti clockwise
– Many Stators and a Rotor



commonly seen is 4 stator and hence 4-phase
stepper motor
4 leads for the stator coils and two common leads
A fixed pattern is to be followed in driving the stator
coils to obtain rotation
Stepper Motor – contd.
Step Angle
– Minimum degree of rotation per step
– Steps per revolution
– Step angle of 2  360/2 = 180 Steps per Revolution
– Step angle of 5  360/5 = 72 Steps per Revolution
– RPM and Steps per second (speed of rotation)
Steps per second = (RPM x Steps per Revolution)/60
Stepping
– Sequence of driving the coils
– Normal 4 step: 1001  1100  0110  0011 (2x4=8deg)
– Half Stepping: 1001  1000  1100  0100 and so on (8
steps) . Effectively reduces the step angle by a factor of 1/2
Given Step angle and the step-sequence, any rotation
can be achieved (say 100 degrees)
Stepper motor controller

Stepper motor: rotates fixed
number of degrees when
given a “step” signal



In contrast, DC motor just
rotates when power applied,
coasts to stop
Rotation achieved by
applying specific voltage
sequence to coils
Controller greatly simplifies
this
Sequence
1
2
3
4
5
A
+
+
+
A’
+
+
-
B
+
+
+
B’
+
+
-
Vd
1
16
A’
2
MC3479P 15
A
3
14
4
13
5
12
Bias’/Set
6
11
Phase A’
Clk
7
10
CW’/CCW
O|C
8
9
Full’/Half Step
GND
Red
White
Yellow
Black
Vm
B
B’
GND
A
A’
B
B’
Stepper motor with controller
(Driver)
/* main.c */
MC3479P
Stepper Motor
Driver
10
7
2 A’ B 15
3 A B’ 14
void main(void){
sbit clk=P1^1;
sbit cw=P1^0;
8051
CW’/CCW
CLK
P1.0
P1.1
*/turn the motor forward */
cw=0;
/* set direction */
clk=0;
/* pulse clock */
delay();
clk=1;
void delay(void){
int i, j;
for (i=0; i<1000; i++)
for ( j=0; j<50; j++)
i = i + 0;
}
/*turn the motor backwards */
cw=1;
/* set direction */
clk=0;
/* pulse clock */
delay();
clk=1;
}
Stepper
Motor
The output pins on the stepper motor driver do not
provide enough current to drive the stepper motor.
To amplify the current, a buffer is needed. One
possible implementation of the buffers is pictured
to the left. Q1 is an MJE3055T NPN transistor
and Q2 is an MJE2955T PNP transistor. A is
connected to the 8051 microcontroller and B is
connected to the stepper motor.
+V
1K
Q1
A
B
Q2
1K
Stepper motor without controller
(driver)
8051
P2.4
/*main.c*/
sbit notA=P2^0;
sbit isA=P2^1;
sbit notB=P2^2;
sbit isB=P2^3;
sbit dir=P2^4;
GND/ +V
P2.3
P2.2
P2.1
P2.0
Stepper
Motor
A possible way to implement the buffers is located
below. The 8051 alone cannot drive the stepper motor, so
several transistors were added to increase the current going
to the stepper motor. Q1 are MJE3055T NPN transistors
and Q3 is an MJE2955T PNP transistor. A is connected to
the 8051 microcontroller and B is connected to the stepper
motor.
+V
1K
Q1
B
+V
1K
A
Q2
330
void delay(){
int a, b;
for(a=0; a<5000; a++)
for(b=0; b<10000; b++)
a=a+0;
}
void move(int dir, int steps) {
int y, z;
/* clockwise movement */
if(dir == 1){
for(y=0; y<=steps; y++){
for(z=0; z<=19; z+4){
isA=lookup[z];
isB=lookup[z+1];
notA=lookup[z+2];
notB=lookup[z+3];
delay();
}
}
}
/* counter clockwise movement */
if(dir==0){
for(y=0; y<=step; y++){
for(z=19; z>=0; z - 4){
isA=lookup[z];
isB=lookup[z-1];
notA=lookup[z -2];
notB=lookup[z-3];
delay( );
}
}
}
}
void main( ){
int z;
int lookup[20] = {
1, 1, 0, 0,
0, 1, 1, 0,
0, 0, 1, 1,
1, 0, 0, 1,
1, 1, 0, 0 };
while(1){
/*move forward, 15 degrees (2 steps) */
move(1, 2);
/* move backwards, 7.5 degrees (1step)*/
move(0, 1);
}
}


LCD stands for Liquid Crystal Display

It is an advanced display system

“Liquid Crystals” are semi-solid substances that are
sensitive to temperature and electricity
Commonly used in


Digital watches, calculators , laptops
Compared with conventional CRT display
systems


Advantages

Light

Thin

Low power consumption
Disadvantages

Expensive for computer display
LCD Liquid Crystal Display

16x2 and 20x2 displays.

Means 16 characters per line by 2 lines & 20
characters per line by 2 lines

HD44780U standard for communication
with LCD (Controller Chip)

Signals : 3 control lines  EN, RS, and
RW.

4 or 8 I/O lines for the data bus.
EN : Enable signal (Logic High signal )
To send data to the LCD, your program should first set this line high (1) and
then set the other two control lines and/or put data on the data bus. When the
other lines are completely ready, bring EN low (0) again. The 1-0 transition
tells the 44780 to take the data currently found on the other control lines and
on the data bus and to treat it as a command.
The RS line is the "Register Select" line. When RS is low (0), the data is to
be treated as a command or special instruction (such as clear screen,
position cursor, etc.). When RS is high (1), the data being sent is text data
which should be displayed on the screen. For example, to display the letter
"T" on the screen you would set RS high.
The RW line is the "Read/Write" control line. When RW is low (0), the
information on the data bus is being written to the LCD. When RW is high
(1), the program is effectively querying (or reading) the LCD. Only one
instruction ("Get LCD status") is a read command. All others are write
commands--so RW will almost always be low.
The data bus consists of 4 or 8 lines DB0 to DB7.
LCD controller
void WriteChar(char c){
RS = 1;
DATA_BUS = c;
EnableLCD(45);
/* indicate data being sent */
/* send data to LCD */
/* toggle the LCD with appropriate delay */
}
E
R/W
RS
communications
bus
DB7–DB0
8
microcontroller
LCD
controller
CODES
I/D = 1 cursor moves left
I/D = 0 cursor moves
right
S = 1 with display shift
S/C =1 display shift
S/C = 0 cursor movement
R/L = 1 shift to right
R/L = 0 shift to left
RS
R/W
DB7
DB6
DB5
DB4
DB3
DB2
DB1
DB0
Description
0
0
0
0
0
0
0
0
0
1
Clears all display, return cursor home
0
0
0
0
0
0
0
0
1
*
Returns cursor home
0
0
0
0
0
0
0
1
I/D
S
0
0
0
0
0
0
1
D
C
B
0
0
0
0
0
1
S/C
R/L
*
*
Move cursor and shifts display
0
0
0
0
1
DL
N
F
*
*
Sets interface data length, number of
display lines, and character font
1
0
DL = 1 8-bit
DL = 0 4-bit
N = 1 2 rows
N = 0 1 row
F = 1 5x10 dots
F = 0 5x7 dots
WRITE DATA
Sets cursor move direction and/or
specifies not to shift display
ON/OFF of all display(D), cursor
ON/OFF (C), and blink position (B)
Writes Data
Keypad controller
N1
N2
N3
N4
k_pressed
M1
M2
M3
M4
4
key_code
keypad controller
N=4, M=4
key_code
Hexadecimal Keypad Interface
P1.0
1.
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
2.
3.
4.
Get a hexadecimal
character from the
keypad (IN_HEX
software-debouncing
and GET_KEY scan
keypad)
Convert the
hexadecimal code to
ASCII (HTOA)
Send the ASCII code
to VDT (OUTCHR)
Go to step 1
Hexadecimal Keypad Interface
Key start pressed
Debouncing period
Key pressed and stabled
Mechanical bouncing
Similar debouncing for key released
Hexadecimal Keypad Interface
;**********************************************************************************
;
KEYPAD INTERFACE EXAMPLE
;
; This program reads hexadecimal characters from a
; keypad attached to Port i and echoes keys pressed
; to the console.
;**********************************************************************************
HTOA
EQU
033CH
;MON51 subroutines (V12)
OUTCHR EQU
01DEH
MAIN:
ORG
CALL
CALL
CALL
SJMP
8000H
IN_HEX
HTOA
OUTCHR
MAIN
;get code from keypad
;convert to ASCII
;echo to console
;repeat
Hexadecimal Keypad Interface
;*************************************************************************
; IN_HEX - input hex code from keypad with debouncing
;
for key press and key release (50 repeat
;
operations for each)
;*************************************************************************
IN_HEX: MOV
R3,#50
;debounce count
BACK: CALL GET_KEY
;key pressed?
JNC
IN_HEX
;no: check again
DJNZ R3,BACK
;yes: repeat 50 times
PUSH ACC
;save hex code
BACK2: MOV
R3,#50
;wait for key up
BACK3: CALL GET_KEY
;key still pressed?
JC
BACK2
;yes: keep checking
DJNZ R3,BACK3
;no: key release, repeat 50 times
POP
ACC
;recover hex code and
RET
; return
Hexadecimal Keypad Interface
************************************************************************************
; GET_KEY - get keypad status
;
- return with C = 0 if no key pressed
;
- return with C = 1 and hex code in ACC if
;
a key is pressed
************************************************************************************
GET_KEY: MOV
A,#0FEH
;start with column 0
MOV
R6,#4
;use R6 as column counter
TEST:
MOV
P1,A
;activate column line
MOV
R7,A
;save ACC
MOV
A, P1
;read back Port 0
ANL
A,#0F0H
;isolate row lines
CJNE
A,#0F0H,KEY_HIT
;row line active?
MOV
A,R7
;no: move to next
RL
A
; column line
DJNZ
R6,TEST
CLR
C
;no key pressed
SJMP
EXIT
;return with C = 0
Hexadecimal Keypad Interface
KEY_HIT: MOV
MOV
CLR
SUBB
MOV
MOV
SWAP
MOV
AGAIN: RRC
JNC
INC
INC
INC
INC
DJNZ
DONE: SETB
MOV
EXIT:
RET
END
R7,A
A,#4
C
A,R6
R6,A
A,R7
A
R5,#4
A
DONE
R6
R6
R6
R6
R5,AGAIN
C
A, R6
;save scan code in R7
;prepare to calculate
; column weighting
;4 - R6 = column number 0-3
;save in R6
;restore scan code
;put in low nibble
;use R5 as counter
;rotate for row num until 0
;done when C = 0,
;add 4 to keycode to goto next
; row until active row found
;C = 1 (key pressed)
;code in A (whew! ! !)
Multiple 7-Segment LEDs Interface
7 Segment
LED
Interface
LINE SOURCE
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$DEBUG
$NOPAGING
$NOSYMBOLS
;FILE: MC14499.SRC
;************************************************************************************
; MC14499 7-SEGMENT LED DISPLAY INTERFACE EXAMPLE
;
; This program updates a 4-digit display 10 times per
; second using interrupts. The digits are 7-segment
; LEDs driven by an MC14499 decoder/driver connected
; to P1.5 (/ENABLE), P1.6 (CLOCK), and P1.7 (DATA
; IN). Interrupts are generated by the 8155's TIMER
; OUT line connected to /INT0. TIMER OUT oscillates
; at 500 Hz and generates an interrupt on each 1-to-0
; transition. An interrupt counter is used to update
; the display every 50 interrupts, for an update
; frequency of 10 Hz.
7 Segment LED Interface
18
19
20
21
22
23
24
25
26
27
28
29
;
*
; The example illustrates the foreground/background
; concept for interrupt-driven systems. Once the
; 8155 is initialized and External 0 interrupts are
; enabled, the program returns to the monitor program.
; MON51 itself does not use interrupts; however, it
; executes as usual in the foreground while
; interrupts take place in the background. If the
; MON51 command SI (set internal memory) is used to
; change locations DIGITS or DIGITS+1, then the value
; written is immediately seen (within 0.1 s) on the
; LED display.
7 Segment
Interface
31
MON51 LED
CODE
00BCH
;MON51 (V12) entry
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
X8155
TIMER
COUNT
MODE
DIN
CLOCK
ENABLE
XDATA
XDATA
EQU
EQU
BIT
BIT
BIT
0100H
X8155 + 4
4000
01000000B
P1.7
P1.6
P1.5
;8155 control register address
;timer count registers
;interrupts ~ 2000 us
;timer mode bits
;MC14499 interface lines
DSEG
DIGITS: DS
ICOUNT: DS
AT 70H
2
1
;absolute internal segment
; (no conflict with MON51)
;interrupt count
CSEG
8000H
MAIN
EX0ISR
T0ISR
EX1ISR
T1ISR
SPISR
T2ISR
; reset and program entry point
; 8155 caused ext 0 interrupt
; Timer 0 interrupt
; External 1 interrupt
; Timer 1 interrupt
; Serial Port interrupt
; Timer 2 interrupt
AT
LJMP
LJMP
LJMP
LJMP
LJMP
LJMP
LJMP
7 53Segment
LED Interface
;****************************************************************************
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
; MAIN PROGRAM BEGINS (INIT 8155 & ENABLE INTERRUPTS)
;****************************************************************************
MAIN:
MOV
DPTR,#TIMER
;initialize 8155 timer count low
MOV
A,#LOW(COUNT)
MOVX @DPTR,A
INC
DPTR
;initialize high register & mode
MOV
A,#HIGH(COUNT) OR MODE
MOVX @DPTR,A
MOV
DPTR,#X8155
;8155 command register
MOV
A,#0C0H
;start 8155 timer command
MOVX @DPTR,A
;500 Hz square wave
MOV
ICOUNT,#50
;initialize int. counter
SETB EA
;enable 8051 interrupts
SETB EX0
;enable External 0 int.
SETB IT0
;negative-edge triggered
LJMP MON51
;return to MON51
7 Segment
LED
Interface
71
;*************************************************************************
72
73
74
75
76
77
; EXTERNAL 0 INTERRUPT SERVICE ROUTINE
;*************************************************************************
EX0ISR: DJNZ ICOUNT,EXIT
;on 50th interrupt,
MOV
ICOUNT,#50
; reset counter and
ACALL UPDATE
; refresh LED display
EXIT:
RETI
7 Segment
LED Interface
79
;********************************************************************************
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
; UDATE 4-DIGIT LED DISPLAY (EXECUTION TIME = 84 us)
;
; ENTER:Four BCD digits in internal memory
;
locations DIGITS and DIGITS+1 (MSD in
;
high nibble of DIGITS)
; EXIT: MC14499 display updated
; USES: P1.5, P1.6, P1.7
;
All memory locations and regs intact
;********************************************************************************
UPDATE: PUSH ACC
;save Accumulator on stack
CLR
ENABLE
;prepare MC14499
MOV
A, DIGITS
;get first two digits
ACALL OUT8
;send two digits
MOV
A, DIGITS+ 1
;get second byte
ACALL OUT8
;send last two digits
SETB ENABLE
;disable MC14499
POP
ACC
;restore ACC from stack
RET
7 99Segment
LED
Interface
;***********************************************************
100
101
102
103
104
105
106
107
108
109
110
111
112
113
; SEND 8 BITS IN ACCUMULATOR TO MC14499 (MSB FIRST)
;***********************************************************
USING 0
;assume reg. bank 0 enabled
OUT8: PUSH AR7
;save R7 on stack
MOV
R7,#8
;use R7 as bit counter
AGAIN: RLC
A
;put bit in C flag
MOV
DIN,C
;send it to MC14499
CLR
CLOCK
;3 us low pulse on clock line
NOP
;NOPs needed to stretch pulse
NOP
; (minimum pulse width is
SETB CLOCK
; is 2 us)
DJNZ R7,AGAIN
;repeat until all 8 bits sent
POP
AR7
;restore R7 from stack
RET
7 Segment LED Interface
114 ;*************************************************************************
115 ; UNUSED INTERRUPTS (ERROR
116 ; RETURN TO MONITOR PROGRAM)
117;*************************************************************************
118
T0ISR:
119
EX1ISR:
120
T1ISR:
121
SPISR:
122
T2ISR:
CLR
EA
;shut off interrupts &
123
LJMP MON51
;return to MON51
124
END
Loudspeaker Interface
A-major musical scale:
A above middle C: 440Hz
Other notes: 440Hz x 2^(n/12)
n: number of steps (semitones)
A’, one octave, or 12 steps above A
A’: 440 x 2^(12/12) = 880Hz
With ref. To bottom note, the steps
are 2,4,5,7,9,11,12.
8031
P1.7
Loudspeaker
74LS04
Two timings are required for a musical scale:
1. timing from one note to the next note
2. timing for toggling the port bit at note freq
e.g., 4 notes/second or quarter note: 250 ms
timeout is needed
T0: 50 ms timeout for note – length of note (5x50 = 250 ms)
T1: variable timeout for different notes – pitch of note
Loudspeaker Interface
5
;***********************************************
*************************
6
;
7
;
8
; This program plays an A major musical scale using
9
; a loudspeaker driven by a inverter through P1.7
10
LOUDSPEAKER INTERFACE EXAMPLE
;
************************************************
*******
11
;
12
;
13
; ....................................................
14
;
A
440.00
2273
1136
15
;
B
493.88
2025
1012
16
;
C#
554.37
1804
902
17
;
D
587.33
1703
851
18
;
E
659.26
1517
758
19
;
F#
739.99
1351
676
20
;
G#
830.61
1204
602
Note Frequency (Hz)
Period (us)
Period/2 (us)
Loudspeaker Interface
22
;*************************************************
*******
23
MONITOR CODE
00BCH ;MON51 (V12) entry point
24
COUNT
EQU
-50000 ;0.05 seconds per timeout
25
REPEAT
EQU
5
;5 x 0.05 = 0.25 seconds/note
26
27
;*************************************************
*******
28
; Note: X3 not installed on SBC-51, therefore
29
; interrupts directed to the following jump table
30
; beginning at 8000H
31
;*************************************************
*****
32
ORG
8000H
;RAM entry points for...
33
LJMP
MAIN
;main program
34
LJMP
EXT0ISR
;External 0 interrupt
35
LJMP
T0ISR
;Timer 0 interrupt
36
LJMP
EXT1ISR
;External 1 interrupt
Loudspeaker Interface
41
;***********************************************
*****
42
; MAIN PROGRAM BEGINS
43
;***********************************************
******
44
MAIN:
MOV
TMOD,#11H
;both timers 16-bit mode
45
MOV
R7,#0
;use R7 as note counter
46
MOV
R6,#REPEAT
;use R6 as timeout counter
47
MOV
IE,#8AH
;Timer 0 & 1 interrupts on
48
SETB
TF1
;force Timer 1 interrupt
49
SETB
TF0
;force Timer 0 interrupt
50
SJMP
$
;ZzZzZzZz time for a nap
Loudspeaker Interface
52
;**************************************************
*****
53
; TIMER 0 INTERRUPT SERVICE ROUTINE (EVERY 0.05 SEC.)
54
;************************************************
***********
55
T0ISR:
CLR
TR0
;stop timer
56
MOV
TH0,#HIGH (COUNT)
;reload
57
MOV
TL0,#LOW (COUNT)
58
DJNZ
R6,EXIT
;if not 5th int, exit
59
MOV
R6,#REPEAT
;if 5th, reset
60
INC
R7
;increment note
61
CJNE
R7,#LENGTH,EXIT
;beyond last note?
62
MOV
R7,#0
63
EXIT:
SETB
;yes: reset, A=440 Hz
TR0
;no: start timer, go
Loudspeaker Interface
66
;******************************************************
*****
67
; TIMER 1 INTERRUPT SERVICE ROUTINE (PITCH OF NOTES)
68
;
69
; Note: The output frequencies are slightly off due
70
; to the length of this ISR. Timer reload values
71
72
; need adjusting.
;****************************************************
*********
73
T1ISR:
CPL
P1.7
;music maestro!
74
CLR
TR1
;stop timer
75
MOV
A,R7
;get note counter
76
RL
A
;multiply (2 bytes/note)
77
CALL
GETBYTE
;get high-byte of count
78
MOV
TH1,A
;put in timer high register
79
MOV
A,R7
;get note counter again
80
RL
A
81
INC
A
;past high-byte (whew!)
82
CALL
GETBYTE
;get low-byte of count
83
MOV
TL1,A
;put in timer low register
84
SETB
TR1
;start timer
;align on word boundary
Loudspeaker Interface
87
;******************************************************
****
88
; GET A BYTE FROM LOOK-UP OF NOTES IN A MAJOR SCALE
89
;******************************************************
*******
90
GETBYTE:
INC
A
91
MOVC
A,@A+PC
92
RET
93
TABLE:
94
;table look-up subroutine
DW
-1136
;A
DW
-1136
;A (play again; half note)
95
DW
-1012
;B (quarter note, etc.)
96
DW
-902
;C# - major third
97
W
-851
;D
98
DW
-758
;E - perfect fifth
99
DW
-676
;F#
100
DW
-602
;G#
101
DW
-568
;A'
102
DW
-568
103
DW
-568
104
DW
-568
105
LENGTH
EQU
;A' (play 4 times; whole note)
($ - TABLE) / 2 ;LENGTH = # of notes
Loudspeaker Interface
107 ;***********************************************
***
108 ;
UNUSED INTERRUPTS - BACK TO MONITOR PROG (ERROR)
109;**********************************************
********
110
EXT0ISR:
111
EXT1ISR:
112
SPISR:
113
T2ISR: CLR
EA
;shut off interrupts and
114
LJMP MONITOR
115
END
; return to MON51
What is Real-Time?

Hard Real-Time - Late data is bad data
Nuclear power plant controller
Aircraft fly-by-wire

Firm Real-Time – Some lateness can be
tolerated
Patient ventilator system
Interactive video game

Soft Real-Time – Average response
time must be met
Credit card authorization system
Student registration system
What is Real-Time?

Hard Real-Time - Late data is bad data
Nuclear power plant controller
Aircraft fly-by-wire

Firm Real-Time – Some lateness can be
tolerated
Patient ventilator system
Interactive video game

Soft Real-Time – Average response
time must be met
Credit card authorization system
Student registration system
Hexadecimal Keypad Interface
;**********************************************************************************
;
KEYPAD INTERFACE EXAMPLE
;
; This program reads hexadecimal characters from a
; keypad attached to Port i and echoes keys pressed
; to the console.
;**********************************************************************************
HTOA
EQU
033CH
;MON51 subroutines (V12)
OUTCHR EQU
01DEH
ORG
MAIN:
8000H
CALL
CALL
CALL
SJMP
IN_HEX
HTOA
OUTCHR
MAIN
;get code from keypad
;convert to ASCII
;echo to console
;repeat
Hexadecimal Keypad Interface
;*************************************************************************
; IN_HEX - input hex code from keypad with debouncing
;
for key press and key release (50 repeat
;
operations for each)
;*************************************************************************
IN_HEX: MOV
R3,#50
;debounce count
BACK: CALL GET_KEY
;key pressed?
JNC
IN_HEX
;no: check again
DJNZ R3,BACK
;yes: repeat 50 times
PUSH ACC
;save hex code
BACK2: MOV
R3,#50
;wait for key up
BACK3: CALL GET_KEY
;key still pressed?
JC
BACK2
;yes: keep checking
DJNZ R3,BACK3
;no: key release, repeat 50 times
POP
ACC
;recover hex code and
RET
; return
Hexadecimal Keypad Interface
************************************************************************************
; GET_KEY - get keypad status
;
- return with C = 0 if no key pressed
;
- return with C = 1 and hex code in ACC if
;
a key is pressed
************************************************************************************
GET_KEY: MOV
A,#0FEH
;start with column 0
MOV
R6,#4
;use R6 as column counter
TEST:
MOV
P1,A
;activate column line
MOV
R7,A
;save ACC
MOV
A, P1
;read back Port 0
ANL
A,#0F0H
;isolate row lines
CJNE
A,#0F0H,KEY_HIT
;row line active?
MOV
A,R7
;no: move to next
RL
A
; column line
DJNZ
R6,TEST
CLR
C
;no key pressed
SJMP
EXIT
;return with C = 0
Hexadecimal Keypad Interface
KEY_HIT: MOV
MOV
CLR
SUBB
MOV
MOV
SWAP
MOV
AGAIN: RRC
JNC
INC
INC
INC
INC
DJNZ
DONE: SETB
MOV
EXIT:
RET
END
R7,A
A,#4
C
A,R6
R6,A
A,R7
A
R5,#4
A
DONE
R6
R6
R6
R6
R5,AGAIN
C
A, R6
;save scan code in R7
;prepare to calculate
; column weighting
;4 - R6 = column number 0-3
;save in R6
;restore scan code
;put in low nibble
;use R5 as counter
;rotate for row num until 0
;done when C = 0,
;add 4 to keycode to goto next
; row until active row found
;C = 1 (key pressed)
;code in A (whew! ! !)