INTRODUCTION TO MCS-51 PERIPHERALS 1
INTRODUCTION TO
MCS-51 PERIPHERALS
1
The Features of The 8051 Core
8-bit CPU
Extensive Boolean processing (Single-bit logic) capabilities
64K Program Memory address space
64K Data Memory address space
128 bytes of on-chip Data RAM
32 bi-directional and individually addressable I/0 lines
Two 16-bit timer/counters
Full duplex UART
6-source/5-vector interrupt structure with two priority levels
On-chip clock oscillator
2
Memory Structure
Program memory Data memory 3
ต าแหนงของ ISR
(Interrupt Service
Routine)
MCS-51 ในแบบทม Program memory ภายใน 4K, 8K และ ไมมเลย
MCS-51 with 128 bytes Internal Data
memory
MCS-51 with 256 bytes Internal Data memory
FFFF
0000
64KBYTES
EXTERNAL
60KBYTES
EXTERNAL
4K BYTESINTERNAL
FFFF
0000
0FFF
1000
56KBYTES
EXTERNAL
8K BYTESINTERNAL
FFFF
0000
1FFF
2000
FFFF
0000
64KBYTES
EXTERNAL
SFRsDIRECTADDRESSINGONLY
DIRECT&INDIRECT
ADDRESSING
FF
00
7F80 +
FFFF
0000
64KBYTESEXTERNAL
SFRsDIRECTADDRESSINGONLYDIRECT&INDIRECTADDRESSING
00
FF
7F80
FF
+80
INDIRECTADDRESSINGONLY
Internal Data Memory
แบงออกเปน สามสวนดวยกน คอ Lower 128, Upper 128 และ SFR
Lower 128 bytes of internal data memory
4
ACCESSIBLE
BY DIRECT
AND INDIRECT
ADDRESSING
ACCESSIBLE
BY DIRECT
ADDRESSING
SPECIAL
FUNCTION
REGISTERS
ACCESSIBLE
BY INDIRECT
ADDRESSING
ONLY
FFH
80H
FFH
80H7FH
00H
LOWER
128
UPPER
128
PORTS
STATUS AND
CONTROL BITS
TIMER
REGISTERS
STRACK POINTER
ACCUMULATOR
(ECT.)
4 BANKS OF
8 REGISTERS
R0-R7
7FH
1FH
2FH
17H
0FH
07H08H
10H
18H
20H
0
BIT-ADDRESSABLE SPACE
(BIT ADDRESSABLE 0-7F
RESET VALUE OF
STACK POINTER
BANK SELECT
BIT IN PSW
00
01
10
11
Upper 128 bytes of internal data memory
Upper 128 น จะมสองสวนทบซอนกนอยใน MCS-51 แบบทม Internal
data memory 256 ต าแหนงเทานน เชน 80C52, 83C154, 83C154D
การเขาถงขอมลทอยใน Upper 128 นจะตองท าโดยใชวธการของ Indirect
addressing เทานน
P89V51RD2
5
64 k internal program memory
12-clock or 6-clock machine cycle
4 interrupt priority levels
Compatible with both TTL and CMOS logic
The data RAM has 1024 bytes of internal memory The device can also address up to
64 kB for external data memory.
MCS-51 Peripherals 6
CPU
Timer/Counter 1Timer/Counter 0
T0 T1
Clock
/12
Serial Port(UART)
External Interrupt 0 External Interrupt 1
INT0 INT1
RXDTXD
Timers/Counters 7
ใน 8051 จะม Timer/Counter อย 2 ชด คอ Timer 0 และTimer 1 ซงนอกจากจะเปนTimers แลว สามารถใชเปน Counters ไดอกดวย โดยการโปรแกรมในรจสเตอร TMOD
TCON (Timer Control Register)
บต ชอ ต าแหนงบต ความหมาย
TCON.7 TF1 8FH บตแฟลกแสดงการโอเวอรโฟลวของ Timer 1 จะ Set โดย Hardware และ Clear โดย Software
TCON.6 TR1 8EH บตควบคมการปด-เปด Timer 1 Set และ Clear โดย Software TCON.5 TF0 8DH แฟลกแสดงการโอเวอรโฟลวของ Timer 0 TCON.4 TR0 8CH บตควบคมการปด-เปด Timer 0
TCON.3 IE1 8BH บตแฟลกแสดงการอนเทอรรพทจาก INT1 จะ Set โดย Hardware และสามารถ Clear ไดดวย Software
TCON.2 IT1 8AH บตเลอกชนดของสญญาณอนเทอรรพทจากอนเทอรรพทภายนอก INT1 สามารถ Set และ Clear ไดดวย Software
TCON.1 IE0 89H บตแฟลกแสดงการอนเทอรรพทจาก INT0
TCON.0 IT0 88H บตเลอกชนดของสญญาณอนเทอรรพทจากอนเทอรรพทภายนอก INT0
Timers 8
TMOD (Timer Mode Control)
บต ชอ Timer ความหมาย
7 GATE 1 บต Gate ของ Timer 1
6 C/T 1 บตเลอก Counter/Timer ของ Timer 1
5 M1 1 Timer 1 Mode bit 1
4 M0 1 Timer 1 Mode bit 0
3 GATE 0 บต Gate ของ Timer 0
2 C/T 0 บตเลอก Counter/Timer ของ Timer 0
1 M1 0 Timer 0 Mode bit 1
0 M0 0 Timer 0 Mode bit 0
9
Timers
M1 M0 การท างาน
0 0 Mode 0 Register THx และ TLx จะท างานเปนตวนบ 13 Bit คาจากการนบ 8 Bit บนมาจาก THx และอก 5 Bit ลางมาจากคา 5 Bit ลางของ TLx
0 1 Mode 1 Register THx และ TLx ท างานเปนตวนบ 16 Bit คาจากการนบ 8 Bit บนอยใน THx
และคาจากการนบ 8 Bit ลางอยใน TLx
1 0 Mode 2 ในการนบของ TLx ขนาด 8 Bit เมอนบถงคาสงสดคอ FFH แลวเมอมการนบตอจะเกด Overflow แลวจะ Reload เอาขอมลจาก THx เขาไปยง TLx เพอเปนคาเรมตนในการนบครงตอไป
1 1 Mode 3 การท างานของ Timer 0 และ Timer 1 จะตางกน Timer 0 TL0 จะท างานเปน 8 Bit Timer/Counter ซงใชงานเหมอนกบ Mode 0 และ TH0
จะท างานเปน Timer ซงควบคมการท างานไดจาก Control bit ของ Timer 1
Timer 1 หยดท างาน
Timers
Timer Mode and Overflow Flag
Mode 0 (13-bit Timer Mode)
Mode 1 (16-bit Timer Mode) เหมอน Mode 0 แตเปน 16 bits
Mode 2 (8-bit Auto–Reload Mode) เมอ TLx เกด Overflow หรอเกดเปลยนแปลงจาก FFH เปน 00H จะมการโหลดคาทเกบไวใน THx ไปเกบไวใน TLx ซงจะเปนคาเรมตนของการนบครงตอไป
10
Timers
Mode 3 (Split Timer Mode)
Timer 0 จะแยกเปน 2 ตวตวละ 8 บต คอ TL0 และ TH0 เมอ Timer เกด Overflow จะมการเซตบต TF0 และ TF1
ในโหมดน Timer 1 จะไมถกใชงานแตเราสามารถสวตชให Timer 1 ไปท างานในโหมดอนได แตการท างานของ Timer 1 จะไมมการอนเทอรรพทเกดขน
11
Timers
การใชเปนตวนบ (Counter) ถาบต C/T เปน “1” ตว Timer จะน า Clock มาจากภายนอกโดยใชขา P3.4 หรอ T0
เปนขา Input Clock ใหกบ Timer 0 และใชขา P3.5 หรอ T1 เปน Input Clock
ใหกบ Timer 1
การนบ 1 ครงจะตองใชเวลา 2 Machine Cycles ดงนนความถสงสดทจะให Timer
ท างานเปน Counter นบได จะมคามากทสด 500kHz ถา MCS-51 ท างานทความถ
สญญาณนาฬกา 12 MHz
12
Timers
Example 1 การเรม, หยดและการควบคม Timers void main (void)
{
TMOD = (TMOD & 0xF0) | 0x01; /* Choose Timer 0, Mode 1(16 bits timer)*/
………………
………………
while (1)
{
TH0 = 0xFF; /*Initialize the timer with FF9CH (65536D-100D) */
TL0 = 0x9C; /* so the timer will overflow after 100 clock cycles*/
TR0 = 1; /* Start Timer 0*/
L1: if(TF0==0)
{
goto L1; /* Wait until Timer 0 overflows*/
}
TR0=0;/* Stop Timer 0*/
TF0 = 0;/* Clear Timer overflow flag 1*/
……………
……………
}
}
13
Timers 14
Example 2.1 สราง pulse โดยใช Software
Delay
#include <REG51.h>
#include <math.h>
delay(unsigned int y){
unsigned int i;
for(i=0;i<y;i++){;}
}
main(){
while(1){
delay(30000);
P1^7 = 1;
delay(30000);
P1^7 = 0;
}
}
Example 2.2 สราง pulse โดยใช Hardware
Delay
#include<reg51.h>
#include <absacc.h>
void timer0_ISR (void) interrupt 1
{
P1^7 =!P1^7;
TF0 = 0;
}
void main (void)
{
TMOD = (TMOD & ~0x0F) | 0x01; /* Set T/C0 Mode 1*/
EA = 1; /* Global Interrupt Enable */
ET0 = 1; /* Enable Timer 0 Interrupts */
TH0 = 0x8A; /* Load TH0 */
TL0 = 0xCF; /* Load TL0 */
TR0 = 1; /* Start Timer 0 */
P1^7 = 1; while (1)
{
…………… /*Other operation*/
……………
}
}
x
x
x
x
x
x
x
1
Port1
30 msec
Timers
Example 3 สรางสญญาณ square wave 10kHz โดยให High 50us และ Low 50 us, Clock 12 MHz
ใช Timer mode 2 โดยให overflow ทก 50 us #include<reg51.h>
#include <absacc.h>
void main (void)
{
TMOD = (TMOD & 0xF0) | 0x02; /* Set T/C0 Mode 2*/
TH0 = 0xCE; /* Load TH0 by 256D-50D=CEH */
TR0 = 1; /* Start Timer 0 */
while (1)
{
If (TF0==1)
{P1^7 = !P1^7;
TF0=0; }
}
}
15
Timers
Example 4 สรางสญญาณ square wave 1kHz โดยให High 500us และ Low 500 us, Clock 12 MHz
ชวงเวลามากกวา 256 (FFH) ใช Timer mode 1 (16 bits) โดยให overflow ทก 500 us #include<reg51.h>
#include <absacc.h>
void main (void)
{
TMOD = (TMOD & 0xF0) | 0x01;
/* Set T/C0 Mode 1*/
TH0 = 0xFE;
/* Load TH0 and TL0 by 65536D-500D=FE0CH */
TL0 = 0x0C;
TR0 = 1; /* Start Timer 0 */
while (1)
{ If (TF0==1)
16
{
P1^7 = !P1^7;
TF0=0;
TR0 = 0; /* Stop Timer 0 */
TH0 = 0xFE; /* Load TH0 and TL0 by
65536D-500D=FE0CH */
TL0 = 0x0C;
TR0 = 1; /* Start Timer 0 */
}
}
}
Timers
Example 5 Delay function [delay()] 50 ms, Clock 12 MHz ใช Timer mode 1 โดยให overflow ทก 50 ms void delay (void)
{
TMOD = (TMOD & ~0x0F) | 0x01; /* Set T/C0 Mode 1*/
TH0 = 0x3C; /* Load TH0 and TL0 by 65536D-50000D=3CB0H */
TL0 = 0xB0;
TF0=0
TR0 = 1; /* Start Timer 0 */
while (TF0==0);
TF0=0;
TR0 = 0; /* Stop Timer 0 */
}
17
Input /Output Scheduling
Polling
18
while(1)
{ if(P1^0 == 1)
/* Polling for device 0*/
{
service_0();
/* if bit 0 = „1‟ call service_0*/
}
if(P1^1 == 1)
/* Polling for device 1*/
{
service_1();
/* if bit 0 = „1‟ call service_1*/
}
if(P1^2 == 1)
/* Polling for device 2*/
{
service_2();
/* if bit 0 = „1‟ call service_2*/
}
}//end of while(1)
B Requesting
Service ?
Service Routine for
Service B
YES
YES
NO
NO
A Requesting
Service ?
Service Routine for
Service A
NO
C Requesting
Service ?
Service Routine for
Service C
YES
Input /Output Scheduling
Interrupt
DMA
19
Interrupts
20
ระบบการ interrupt ของ 8052 และ 83154
Interrupts
IP (Interrupt Priority Register)
21
Interrupts
IE (Interrupt Enable Register)
22
Interrupts
External interrupts
Timer 0 และ Timer 1 Interrupts
Serial port Interrupt
23
EA --- ET2 ES ET1 EX1 ET0 EX0
01234567
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
01234567
EA --- ET2 ES ET1 EX1 ET0 EX0
01234567
TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
01234567
EA --- ET2 ES ET1 EX1 ET0 EX0
01234567
SM0 SM1 SM2 REN TB8 RB8 TI RI
01234567
EA = 1; EX0 = 1; EX1 = 1; IT0/IT1 = 1 External interrupt caused by a falling edge signal on P3.2/P3.3 IT0/IT1 = 0 External interrupt caused by a low level signal on P3.2/P3.3
EA = 1; ET0 = 1; ET1 = 1; TF0/TF1 = 1 แสดงวาเกดการ interrupt
EA = 1; ES = 1; เมอ TI/RI = 1 จะเกดการ interrupt
IE
TCON
TCON
IE
IE
SCON
Interrupts
TCON (Timer Control Register)
IE1 : TCON.3 แสดงการเกด interrupt มคาเปน 1 เมอมสญญาณ interrupt เขามา มคาเปน 0 เมอมการตอบสนองการ interrupt แลว
IT1 :TCON.2 ถามคาเปน 1 จะเกดการ interrupt เมอสญญาณ
เปลยนจาก 1 เปน 0 ถามคาเปน 0 จะเกดการ interrupt เมอสญญาณ เปน 0
IE0 :TCON.1 เหมอนทอธบายไปแลว ส าหรบ
IT0 :TCON.0 เหมอนทอธบายไปแลว ส าหรบ
24
INT1
INT1
INT0
INT0
Interrupts
Source Vector address
RST 0000H
IE0 0003H
TF0 000BH
IE1 0013H
TF1 001BH
RI + TI 0023H
TF2 + EXF2 002BH
25
Source Interrupt Number
IE0 0
TF0 1
IE1 2
TF1 3
RI + TI 4
TF2 + EXF2 5
Interrupts
Polling Interrupt
Multiple Devices Connected to a Single Interrupt Line
26
MPUINT 1
I/O Interface 1
I/O Interface 2
I/O Interface 3
P1.0 P1.1 P1.2
#include <intrins.h>
……
ISR_1(void) // ISR for interface 1
{…..}
ISR_2(void) // ISR for interface 2
{…..}
ISR_3(void) // ISR for interface 3
{…..}
poll_int () interrupt 2 //The function the be executed
//when external interrupt 1 occurs
{
_push_(PSW); /*Save old status …*/
if (P1^0==0){ ISR_1()}
if (P1^1==0){ ISR_2()}
if (P1^2==0){ ISR_3()}
_pop_(PSW);}
Interrupts
ตวอยาง Intrusion warning system ออกแบบระบบกนผบกรกซงจะสงเสยงความถ 4000 Hz เตอนเปนเวลา 1 วนาท (ใชล าโพงตอกบ P1.7) เมอมสญญาณจาก sensor ทประตท าให INT0 เปน High-to-low transition
ในตวอยางนจ าเปนตองใช 3 interrupts ไดแก external 0 (door sensor), Timer 0 (400 Hz tone), และ Timer 1 (1 second timeout)
#include<reg51.h>
#include <intrins.h>
#include <math.h>
unsigned int count
void ex0_isr (void) interrupt 0
{ count=20;
TF0=1; // force timer 0 interrupt
TF1=1; // force timer 1 interrupt
ET0=1; // begin tone for 1 second
ET1=1; // enable timer interrupts
}
void timer1_ISR (void) interrupt 3
{
TR1=1; //65536-1250=64285 (FB1E)
TH1=0xFB; //count for 4000Hz
TL1=0x1E;
P1^7=!P1^7; //Play sound
TR1=1;
}
27
void timer0_ISR (count) interrupt 1 { TR0=0; //Stop timer0 count--; if (count<=0) { ET0=0; // Disable itself ET1=0; // Disable tone } TH0=0x3C; //65536-50000=15536 (3CB0) TL0=0xB0; //0.05 sec delay TR0=1; } void main (void) { TMOD=0x11; //16-bit timer mode IT0 = 1; // Configure interrupt 0 for falling edge EX0 = 1; // Enable EX0 Interrupt IT1 = 1; // Configure interrupt 1 for falling edge EA = 1; // Enable Global Interrupt Flag }
Interrupts
28
ตวอยาง ระบบและโปรแกรมเพอควบคมมอเตอรใหหยดท างานเมอมตวมอเตอรความรอนสงเกนไปหรอหมนเรวเกนกวา 10,000 r.p.m. เชนเชอรวดอณหภมซงตออยกบ
P3.2 (External interrupt 0) จะให logic 0 เมออณหภมสงเกนไป และ optical encoder ซงตออยกบ P3.5 (Timer/Counter 1) จะสงสญญาณทกครงทมอเตอรหมน 1 รอบ วธหยดการท างานของระบบท าไดโดยใหสญญาณท P1.0 เปน 1 โดยทระบบนใช 24MHz crystal oscillator
/* 10000 rpm=166.6r/s, 160 r/s=2r/12500us
#include <REG51.h>
#include <math.h>
#define limit 2
#define stop_signal P1^0
unsigned char sub_counter;
setup_peripherals(){
EA = 1; //setup external interrupt
EX0 = 1;
IT0 = 1;
TMOD = 0x52; // timer 0 mode 2, counter 1 mode 1
TR0 = 1; //setup timer 0
TH0 = 6;
//makes the timer to overflow every 125 uS
ET0 = 1;
TR1 = 1 ; //setup timer 1
}
timer_0() interrupt 1{
sub_counter++ ;
if (sub_counter == 100){
// divide the overflow frequency further more by 100
sub_counter = 0;
// this part is executed every 12500 uS
if ((TL1 + (TH1 * 256)) > limit){
// 2/12500us ~160 r/s //Stop the motor
stop_signal = 1;
TL1 = 0;
TH1 = 0;
}
}
}
over_heat_alarm () interrupt 0{
stop_signal = 1;
}
void main(){
stop_signal = 0; // the motor runs normally
setup_peripherals();
while(1){ // Do nothing, the whole program is carried out by
interrupts!
}
}
สอบปฏบต 1
#include<reg51.h>
#include <intrins.h>
#include <absacc.h>
char portLED;
bit flag;
sbit sw0 = P3^2;
sbit sw1 = P3^3;
/********** BASIC FUNCTION **********/
void dmsec (unsigned int count) // mSec Delay
{ unsigned int i; // Keil CA51 (x2)
while (count)
{ i = 230; while (i>0) i--; count--; }
}
29
/********** MAIN PROGRAM **********/
void main()
{
P3 = 0xff;
portLED = 0x01;
while(1)
{
if(sw1 == 0){
P1 = portLED;
dmsec(500);
}else if(flag){
portLED = _cror_(portLED,1);
}
else{
portLED = _crol_(portLED,1);
}
P1 = portLED;
dmsec(500);
if(sw0 == 0){flag=!flag;}
}
}