⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 外部中断模拟uart演示程序.txt

📁 这是单片机C51的各种代码包
💻 TXT
字号:
外部中断模拟UART演示程序-(精)(18570字)hotpower2004-9-4 23:12:05[72次]

外部中断模拟UART演示程序(只经过了软件仿真) 
HotPower 发表于 2004-7-8 01:32 侃单片机 ←返回版面   举报该贴 

/*------------------------------------------------
      外部中断模拟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串行显示数据
--------------------------------------------------*/
#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);//系统初始化
void SystemInit(void);//系统初始化
void SystemIoInit(void);//系统接口初始化
void SystemSetup(void);//系统设置
void UserSetup(void);//用户运行环境设置
void TimeInit(void);//定时器初始化
void ClrWdt(void);//喂狗


//全局变量定义
typedef struct Systemstruct{//系统数据结构声明
  unsigned char T0Count;//串行接收计数器
  unsigned char Count;//串行数据接收个数
  unsigned int  RamTest;//内存测试寄存器
  unsigned char SBUF;//串行接收数据
  unsigned char RXBUF[16];//串行数据接收缓冲区
}SystemData;

SystemData SystemBuffers;//申请系统数据结构

void main(void)
{
  MainInit();//系统初始化
  while (1) {//主循环
    EA = 1;//保证中断可靠
    PCON |= 0x01;//进入空闲状态
    _nop_();
    _nop_();
  }
}
/*------------------------------------
         外部INT0中断服务程序
------------------------------------*/
void int0proc() interrupt IE0_VECTOR
{
}
/*------------------------------------
         定时器T1中断服务程序
------------------------------------*/
void T1_VECTOR() interrupt TF1_VECTOR
{
}
/*------------------------------------
         外部INT1中断服务程序
------------------------------------*/
void int1proc() interrupt IE1_VECTOR using 1
{
  if (!RX && SystemBuffers.T0Count == 0) {//!RX主要防止误触发
    TL0 = 0x80;//2400bps(我搞不清具体的数值),用于测起始位
    TH0 = 0xc0;//4800bps(我搞不清具体的数值),用于数据及停止位
    TF0 = 0;
    TR0 = 1;//启动定时器0
    ET0 = 1;//开放T0中断,首次测起始位
    EX1 = 0;//自毁中断
  }
}
/*------------------------------------
         定时器T0中断服务程序
------------------------------------*/
void T0_VECTOR() interrupt TF0_VECTOR using 1
{
  if (SystemBuffers.T0Count == 0) {
    if (!RX) {//是起始位
      SystemBuffers.T0Count ++;
    }
  }
  else {
    if (SystemBuffers.T0Count >= 9) {//停止位
      TH0 = 0;//1200bps(我搞不清具体的数值)
      TL0 = 0;
      SystemBuffers.T0Count = 0;//下次再找起始位
      if (RX) {//是停止位
        if (SystemBuffers.Count < 16) {//缓冲区数据未满可继续存入数据
          SystemBuffers.RXBUF[SystemBuffers.Count] = SystemBuffers.SBUF;//存入一
个字节
          SystemBuffers.Count ++;
        }
        TR0 = 0;//关闭定时器0
        ET0 = 0;//自毁中断
        EX1 = 1;//开放INT1中断,下次测起始位
      }
    }
    else {//SystemBuffers.T0Count=1..8为8位串行数据
      SystemBuffers.SBUF >>= 1;
      if (RX) SystemBuffers.SBUF |= 0x80; 
      SystemBuffers.T0Count ++;
    }
  }
}

void MainInit(void)//系统初始化
{
  SystemIoInit();//系统接口初始化
  ClrWdt();//清除看门狗计数器
  if (SystemBuffers.RamTest != 0x55aa) {//内存测试
    SystemInit();//系统上电初始化
  }
  SystemSetup();//系统运行环境设置
  UserSetup();//用户运行环境设置
}
void SystemIoInit(void)
{
  IE = 0x00;//关闭中断
  P0 = 0xff;//P0口初始化
  P1 = 0xff;//P1口初始化
  P2 = 0xff;//P2口初始化
  P3 = 0xff;//P3口初始化
}

void SystemInit(void)//系统初始化
{
unsigned char i;
  SystemBuffers.RamTest = 0x55aa;//内存初始化
  SystemBuffers.T0Count = 0;
  SystemBuffers.Count = 0;
  for (i = 0; i < 16; i ++)
    SystemBuffers.RXBUF[i] = 0;
}

void SystemSetup(void)//系统设置
{
  AUXR = 0x01;//关闭EMI
}
void UserSetup(void)//用户运行环境设置
{
  TimeInit();
  SCON = 0x50;//UART的工作方式为方式2,且允许接收数据
  TMOD = 0x22;//定时器1工作在方式2下,定时器0也工作在方式2
  TCON = 0x45;//启动定时器1并设定外部中断0的中断方式
  IP = 0x10;  //设定终端优先级,UART的中断优先级比T0的中断优先级要高
  IE = 0x94;  //开总中断并允许INT1和串行口中断
}

/*----------------------------------
       定时器定时参数设置
----------------------------------*/
void TimeInit()
{
/*----------------------------------
      定时器0定时参数设置
----------------------------------*/
  TL0 = 0;
  TH0 = 0;//当波特率为1200bps时,要求定时器0每过278uS就中断一次
//  TR0 = 1;//启动定时器0
/*----------------------------------
      定时器0定时参数设置
----------------------------------*/
  TL1 = 0xfa;
  TH1 = 0xfa;//设置波特率为4800bps
  TR1 = 1;//启动定时器1
/*----------------------------------
      定时器2定时参数设置
----------------------------------*/
//  TIMEER2 = 0x900;//2.5mS
//  RCAP    = 0x900;
//  TR2     = 1;//启动定时器2
}


void ClrWdt(void)//喂狗
{
  WDTRST = 0x1e;//清89s52内狗
  WDTRST = 0xe1;//清89s52内狗
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -