?? 外部中斷模擬uart演示程序.txt
字號(hào):
外部中斷模擬UART演示程序-(精)(18570字)hotpower2004-9-4 23:12:05[72次]
外部中斷模擬UART演示程序(只經(jīng)過了軟件仿真)
HotPower 發(fā)表于 2004-7-8 01:32 侃單片機(jī) ←返回版面 舉報(bào)該貼
/*------------------------------------------------
外部中斷模擬UART演示程序
--------------------------------------------------*/
#include <AT89X52.h>
#include <intrins.h>
/*------------------------------------------------
AT89S5X
--------------------------------------------------*/
sfr AUXR = 0x8e;
sfr WDTRST = 0xa6;
sfr16 TIMEER2 = 0xcc;
sfr16 RCAP = 0xca;
sbit RX = P3^3;//INT1
sbit TX = P1^1;
/*------------------------------------------------
74HC164串行顯示數(shù)據(jù)
--------------------------------------------------*/
#define LedSegA 0x40
#define LedSegB 0x20
#define LedSegC 0x10
#define LedSegD 0x08
#define LedSegE 0x04
#define LedSegF 0x02
#define LedSegG 0x01
#define LedSegH 0x80
#define LedSegNul 0x00
#define LedChar0 LedSegA + LedSegB + LedSegC + LedSegD + LedSegE + LedSegF
#define LedChar1 LedSegB + LedSegC
#define LedChar2 LedSegA + LedSegB + LedSegD + LedSegE +
LedSegG
#define LedChar3 LedSegA + LedSegB + LedSegC + LedSegD +
LedSegG
#define LedChar4 LedSegB + LedSegC + LedSegF +
LedSegG
#define LedChar5 LedSegA + LedSegC + LedSegD + LedSegF +
LedSegG
#define LedChar6 LedSegA + LedSegC + LedSegD + LedSegE + LedSegF +
LedSegG
#define LedChar7 LedSegA + LedSegB + LedSegC
#define LedChar8 LedSegA + LedSegB + LedSegC + LedSegD + LedSegE + LedSegF +
LedSegG
#define LedChar9 LedSegA + LedSegB + LedSegC + LedSegD + LedSegF +
LedSegG
#define LedCharA LedSegA + LedSegB + LedSegC + LedSegE + LedSegF +
LedSegG
#define LedCharB LedSegC + LedSegD + LedSegE + LedSegF +
LedSegG
#define LedCharC LedSegA + LedSegD + LedSegE + LedSegF
#define LedCharD LedSegB + LedSegC + LedSegD + LedSegE +
LedSegG
#define LedCharE LedSegA + LedSegD + LedSegE + LedSegF +
LedSegG
#define LedCharF LedSegA + LedSegE + LedSegF +
LedSegG
void MainInit(void);//系統(tǒng)初始化
void SystemInit(void);//系統(tǒng)初始化
void SystemIoInit(void);//系統(tǒng)接口初始化
void SystemSetup(void);//系統(tǒng)設(shè)置
void UserSetup(void);//用戶運(yùn)行環(huán)境設(shè)置
void TimeInit(void);//定時(shí)器初始化
void ClrWdt(void);//喂狗
//全局變量定義
typedef struct Systemstruct{//系統(tǒng)數(shù)據(jù)結(jié)構(gòu)聲明
unsigned char T0Count;//串行接收計(jì)數(shù)器
unsigned char Count;//串行數(shù)據(jù)接收個(gè)數(shù)
unsigned int RamTest;//內(nèi)存測(cè)試寄存器
unsigned char SBUF;//串行接收數(shù)據(jù)
unsigned char RXBUF[16];//串行數(shù)據(jù)接收緩沖區(qū)
}SystemData;
SystemData SystemBuffers;//申請(qǐng)系統(tǒng)數(shù)據(jù)結(jié)構(gòu)
void main(void)
{
MainInit();//系統(tǒng)初始化
while (1) {//主循環(huán)
EA = 1;//保證中斷可靠
PCON |= 0x01;//進(jìn)入空閑狀態(tài)
_nop_();
_nop_();
}
}
/*------------------------------------
外部INT0中斷服務(wù)程序
------------------------------------*/
void int0proc() interrupt IE0_VECTOR
{
}
/*------------------------------------
定時(shí)器T1中斷服務(wù)程序
------------------------------------*/
void T1_VECTOR() interrupt TF1_VECTOR
{
}
/*------------------------------------
外部INT1中斷服務(wù)程序
------------------------------------*/
void int1proc() interrupt IE1_VECTOR using 1
{
if (!RX && SystemBuffers.T0Count == 0) {//!RX主要防止誤觸發(fā)
TL0 = 0x80;//2400bps(我搞不清具體的數(shù)值),用于測(cè)起始位
TH0 = 0xc0;//4800bps(我搞不清具體的數(shù)值),用于數(shù)據(jù)及停止位
TF0 = 0;
TR0 = 1;//啟動(dòng)定時(shí)器0
ET0 = 1;//開放T0中斷,首次測(cè)起始位
EX1 = 0;//自毀中斷
}
}
/*------------------------------------
定時(shí)器T0中斷服務(wù)程序
------------------------------------*/
void T0_VECTOR() interrupt TF0_VECTOR using 1
{
if (SystemBuffers.T0Count == 0) {
if (!RX) {//是起始位
SystemBuffers.T0Count ++;
}
}
else {
if (SystemBuffers.T0Count >= 9) {//停止位
TH0 = 0;//1200bps(我搞不清具體的數(shù)值)
TL0 = 0;
SystemBuffers.T0Count = 0;//下次再找起始位
if (RX) {//是停止位
if (SystemBuffers.Count < 16) {//緩沖區(qū)數(shù)據(jù)未滿可繼續(xù)存入數(shù)據(jù)
SystemBuffers.RXBUF[SystemBuffers.Count] = SystemBuffers.SBUF;//存入一
個(gè)字節(jié)
SystemBuffers.Count ++;
}
TR0 = 0;//關(guān)閉定時(shí)器0
ET0 = 0;//自毀中斷
EX1 = 1;//開放INT1中斷,下次測(cè)起始位
}
}
else {//SystemBuffers.T0Count=1..8為8位串行數(shù)據(jù)
SystemBuffers.SBUF >>= 1;
if (RX) SystemBuffers.SBUF |= 0x80;
SystemBuffers.T0Count ++;
}
}
}
void MainInit(void)//系統(tǒng)初始化
{
SystemIoInit();//系統(tǒng)接口初始化
ClrWdt();//清除看門狗計(jì)數(shù)器
if (SystemBuffers.RamTest != 0x55aa) {//內(nèi)存測(cè)試
SystemInit();//系統(tǒng)上電初始化
}
SystemSetup();//系統(tǒng)運(yùn)行環(huán)境設(shè)置
UserSetup();//用戶運(yùn)行環(huán)境設(shè)置
}
void SystemIoInit(void)
{
IE = 0x00;//關(guān)閉中斷
P0 = 0xff;//P0口初始化
P1 = 0xff;//P1口初始化
P2 = 0xff;//P2口初始化
P3 = 0xff;//P3口初始化
}
void SystemInit(void)//系統(tǒng)初始化
{
unsigned char i;
SystemBuffers.RamTest = 0x55aa;//內(nèi)存初始化
SystemBuffers.T0Count = 0;
SystemBuffers.Count = 0;
for (i = 0; i < 16; i ++)
SystemBuffers.RXBUF[i] = 0;
}
void SystemSetup(void)//系統(tǒng)設(shè)置
{
AUXR = 0x01;//關(guān)閉EMI
}
void UserSetup(void)//用戶運(yùn)行環(huán)境設(shè)置
{
TimeInit();
SCON = 0x50;//UART的工作方式為方式2,且允許接收數(shù)據(jù)
TMOD = 0x22;//定時(shí)器1工作在方式2下,定時(shí)器0也工作在方式2
TCON = 0x45;//啟動(dòng)定時(shí)器1并設(shè)定外部中斷0的中斷方式
IP = 0x10; //設(shè)定終端優(yōu)先級(jí),UART的中斷優(yōu)先級(jí)比T0的中斷優(yōu)先級(jí)要高
IE = 0x94; //開總中斷并允許INT1和串行口中斷
}
/*----------------------------------
定時(shí)器定時(shí)參數(shù)設(shè)置
----------------------------------*/
void TimeInit()
{
/*----------------------------------
定時(shí)器0定時(shí)參數(shù)設(shè)置
----------------------------------*/
TL0 = 0;
TH0 = 0;//當(dāng)波特率為1200bps時(shí),要求定時(shí)器0每過278uS就中斷一次
// TR0 = 1;//啟動(dòng)定時(shí)器0
/*----------------------------------
定時(shí)器0定時(shí)參數(shù)設(shè)置
----------------------------------*/
TL1 = 0xfa;
TH1 = 0xfa;//設(shè)置波特率為4800bps
TR1 = 1;//啟動(dòng)定時(shí)器1
/*----------------------------------
定時(shí)器2定時(shí)參數(shù)設(shè)置
----------------------------------*/
// TIMEER2 = 0x900;//2.5mS
// RCAP = 0x900;
// TR2 = 1;//啟動(dòng)定時(shí)器2
}
void ClrWdt(void)//喂狗
{
WDTRST = 0x1e;//清89s52內(nèi)狗
WDTRST = 0xe1;//清89s52內(nèi)狗
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -