interrupt
1. Pin usage
2. Interrupt entry address
3. Register
3.1 TCON register (bit addressable)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | ie0 | IT0 |
timer
- TF: timer/counteroverflow interruptAfter overflow, it is set to 1 by hardware; after entering the interrupt service routine, it is cleared to 0 by hardware or software.
- TR: timer/counterStart and stop position
external interruption
- IE:interrupt flagHardware cleared to 0
- IT:Trigger mode1: falling edge trigger; 0: low level trigger
3.2 SCON register (bit addressable)
- D1: TI serial port transmit buffer (SBUF)nullInterrupt request flag
- D0: RI serial port receive buffer (SBUF)FullInterrupt request flag
- Must be cleared to "0" by software during interrupt service
3.3 Interrupt enable register IE (bit addressable)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | EA | – | – | IS | ET1 | EX1 | ET0 | EX0 |
-
EA: total allowed
-
ES: serial port
-
ET: timer
-
EX: external interrupt
Write 1 to indicate permission
3.4 Interrupt priority register IP (bit addressable)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | – | – | – | PS | PT1 | PX1 | PT0 | PX0 |
-
Write 1 to indicate high priority interrupt
-
High-priority interrupts can interrupt low-priority interrupts, but the same level cannot interrupt them.
-
Query sequence when multiple peer interruptions occur:INT0>>T0>>INT1>>T1>>Serial
4. Response conditions
-
For an interrupt request from an interrupt source to be responded to, the following necessary conditions must be met:
a. Interrupt request flag=1
b. Interrupt enable bit=1
c. EA=1
d. No interrupt of the same level or higher is being executed.
-
Interrupt responses are blocked when one of the following three conditions is encountered:
a. The CPU is processing an interrupt of the same level or higher priority
b. The machine cycle queried is not the last machine cycle of the instruction currently being executed.
c. The instruction being executed is RETI or an instruction to access IE or IP.
5. External interrupt response time
More than 3 machine cycles, less than 8 machine cycles
6. Interrupt function
General form of interrupt service function
void 函数名(void) interrupt n using m
n: interrupt number
m: Working register area selection (if the using option is not selected, the contents of all working registers in the interrupt function will be saved to the stack)
No return value, no parameters passed, cannot be called directly
7. Example
void main()
{
EX0=1; // 允许外部中断0中断
EX1=1; // 允许外部中断1中断
IT0=1; // 选择外部中断0为跳沿触发方式
IT1=1; // 选择外部中断1为跳沿触发方式
PX0=0; // 外部中断0为低优先级
PX1=1; // 外部中断1为高优先级
EA=1; // 总中断允许
while( 1 )
{
;
}
}
void int0_isr(void) interrupt 0 // 外中断0的中断服务函数
{
;
}
void int1_isr(void) interrupt 2 // 外中断1的中断服务函数
{
;
}
timer
1. Two working modes
Timer: for system clock signal12 frequency dividerThe internal pulse signal count after. (Internal pulse signal period = machine period)
Counter: Count the external pulses applied to the two pins T0 (P3.4) and T1 (P3.5)
2. Register
2.1 Working mode register TMOD (Not bit addressable)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | GATE | C/~T | M1 | M0 | GATE | C/~T | M1 | M0 |
timer | T1 | T1 | T1 | T1 | T0 | T0 | T0 | T0 |
-
GATE: Gate control bit, controls startup mode
0, only controlled by TR (TR=1, start)
1. High level on external interrupt pin (INT0 or INT1) + TRx (TR=1, and INT0 or INT1 pin is high level, start)
-
C/~T: Mode selection bit (1, counter; 0, timer)
-
M1, M0: working mode selection bit
Method 1, after entering the interrupt service routine, first write the initial value
Method 2, TH and TL write initial values. After the TL count overflows, the initial value written for the first time in TH is automatically entered into TL to achieve automatic reloading.
Mode 3, T0 is divided into two independent 8-bit counters TL0 and TH0
TL0, use the status control bits C/T, GATE, TR0, TF0 of T0
TH0, fixed as an 8-bit timer, cannot be used as an external counting mode. It uses the status control bit TR1 of T1 and occupies the interrupt request source TF1 of timer T1.
① Generally, when T1 is used as the baud rate generator of the serial port, T0 only works in mode 3.
② When T0 is in working mode 3, T1 can be set to mode 0, mode 1 and mode 2, used as a baud rate generator for the serial port, or does not require interruption occasion.
2.2 TCON (bit addressable)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | TF1 | TR1 | TF0 | TR0 | IE1 | IT1 | ie0 | IT0 |
- TF
- TR
3. Initial value of counting
Take mode 1, 16-bit timer/counter as an example
1 machine cycle = 12 clock cycles = 12 / crystal oscillator frequency (such as a 12MHz crystal oscillator, the machine cycle is 1us)
Timing time = (65536 - X) * 12 / crystal oscillator frequency
X = 65536 − Timing time * Crystal oscillator frequency / 12
TH = X / 256;
TL = x % 256;
4. External input counting signal
Since it takes time to confirm a negative transition2 machine cycles, so the highest frequency of the externally input counting pulse is the system oscillator frequency1/24
The input signal must be maintained for at least one machine cycle
5. Example
Timer 0, works in mode 1, the timing time is set to 50ms, and the LED turns on and off every 1s (12MHz crystal oscillator)
void main(void)
{
TMOD = 0x01; //设置定时器T0为方式1定时,0000 0001
TH0 = (65536-50000) / 256; //定时器T0赋初值
TL0 = (65536-50000) % 256;
ET0 = 1; //T1中断允许
EA = 1; //总允许
TR0 = 1; //T0启动
while(1)
{
;
}
}
void T0_interserve(void) interrupt 1 //定时器T0中断服务子程序,50ms
{
static unsigned char T0_counter = 0;
TH0 = (65536-50000) / 256; //定时器T0赋初值
TL0 = (65536-50000) % 256;
T0_counter++;
if(T0_counter >= 20) //1s=50ms*20
{
LED = ~LED;
T0_counter = 0;
}
}
Serial communication
1. Register
1.1 SCON (bit addressable)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | SM0 | SM1 | SM2 | REN | TB8 | RB8 | OF | RI |
-
SM0, SM1: working mode selection bits
-
SM2: Multi-machine communication control bit
Multi-machine communication can only be achieved in mode 2 and mode 3
-
REN: Allow serial reception bit
Write 1 to allow the serial port to receive data
-
TB8: The 9th bit of data sent
Set to "1" or clear to "0" by software
① In dual-computer serial communication, TB8 is generally used as a parity bit.
② In multi-machine communication, it is used to indicate whether the host is sending an address frame or a data frame. TB8=1 is an address frame, and TB8=0 is
data. frame. -
RB8: The 9th bit of data received
-
TI: Transmit interrupt flag bit (SBUFnull)
Set to "1" by hardware and must be cleared to "0" by software
-
RI: receive interrupt flag bit (SBUFFull)
Set to "1" by hardware and must be cleared to "0" by software
1.2 PCON
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | SMOD | – | – | – | – | – | – | – |
SMOD: baud rate selection
Overflow rate of timer T1 = 1 / overflow time of timer T1 = 1 / (256 - X) * 12 / crystal oscillator frequency
2. Example
A machine sends
#include <reg51.h>
#define UCHAR unsigned char
UCHAR Temp=0;
void main()
{
TMOD=0x20; // 设置定时器T1为方式2
TH1=0xfd; // 波特率9600
TL1=0xfd;
SCON=0x40; // 串口设置为方式1,只发送
PCON=0x00; // SMOD=0
ES = 1;
EA = 1;
TR1 = 1; // 启动T1
P1=0xff; // P1口为输入
Temp=P1; // 读入P1口开关的状态数据
SBUF=Temp; // 数据送串行口发送
while(1)
; // 循环等待
}
void sci_int(void) interrupt 4
{
if(TI)
{
TI=0; // 清TI
Temp=P1; // 读入P1口开关的状态数据
SBUF=Temp; // 数据送串行口发送
}
if(RI)
RI = 0;
}
Machine B accepts
#include <reg51.h>
#define UCHAR unsigned char
UCHAR Temp;
void main( )
{
TMOD=0x20; // 设置定时器T1为方式2
TH1=0xfd; // 波特率9600
TL1=0xfd;
SCON = 0x50; // 方式1接收,REN=1
PCON = 0x00; // SMOD=0
ES = 1;
EA = 1;
TR1 = 1; // 启动T1
while(1)
;
}
void sci_int(void) interrupt 4
{
if(RI)
{
RI=0; // 接收到数据,清RI
Temp = SBUF;
P1=Temp;
}
if (TI)
TI = 0;
}
memory expansion
1. Commonly used memory address space allocation methods
1.1 Line selection method
1.2 Decoding method
- Chip select is implemented using high-order address lines.
8255
There are two ways to address commonly used I/O ports, one is independent addressing and the other is unified addressing. The AT89S51 microcontroller uses a unified addressing method.
1. Internal structure:
- On-chip address: PA: PB:1 PC:2 Control port: 3
2. Mode control word register
a. Working mode selection control word (D7=1)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | 1 | A way | A way | AInput/Output | C high 4 input/output | B way | B input/output | C low 4 input/output |
82C55 has 3 basic working modes:
- Mode 0—Basic input/output; (unconditional transmission)
- Mode 1—Strobe input/output; (conditional transmission)
- Mode 2—bidirectional transmission (only the PA port has this working mode)
PA port can work in mode 0, 1 and 2
The PB port can only work in modes 0 and 1
The PC port is divided into two parts, the PA port is called group A, and the PB port is called group B.
Example: AT89S51 microcontroller writes the working mode control word 95H to the control word register of 82C55 (assuming the port address is FF7FH)
#include <absacc.h>
#define COM8255 XBYTE[0xff7f] // 0xff7f为82C55的控制寄存器地址
#define UCHAR unsigned char
……
void init8255( void )
{
COM8255 = 0x95; // 工作方式选择控制字写入82C55的控制寄存器
……
}
b. PC port bit set/reset control word (D7=0)
Bit | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
---|---|---|---|---|---|---|---|---|
name | 0 | – | – | – | PC port selection | PC port selection | PC port selection | 1 set/0 reset |