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

📄 sio520.c

📁 windond 77e58双串口编程资料
💻 C
字号:
#include <77e58.h>

unsigned char data S0Data[10];
unsigned char data S0Count;
unsigned char data S1Data[10];
unsigned char data S1Count;
bit SioEnd;


void sio0(void) interrupt 4 {
  if(!TI) {
    RI = 0;
    S0Count++;
    if(S0Count < 10) {
      S0Data[S0Count] = SBUF;
    }
  }
  else {
    TI = 0;
  }
  SioEnd = 1;
  return;
}


void sio1(void) interrupt 7 {
  if(!TI1) {
    RI1 = 0;
    S1Count++;
    if(S1Count < 10) {
      S1Data[S1Count] = SBUF;
    }
  }
  else {
    TI1 = 0;
  }
  SioEnd = 1;
  return;
}


void Init() {
  unsigned char i;
  P0 = 0xff;
  P1 = 0xff;
  P2 = 0xff;
  P3 = 0xff;
  S0Count = 0;
  S1Count = 0;
  for(i = 0; i < 10; i++) {
    S0Data[i] = 0;
    S1Data[i] = 0;
  }
}


void Alarm() {
  if(S0Count >= 10) {
    P1 &= 0xff-1;
    ES = 0;
  }
  if(S1Count >= 10)
    P1 &= 0xff-2;
    ES1 = 0;
}


void DisplayStatus() {
  if( (S0Count || S1Count) != 0 ) {
    Alarm();
  }
}


void Init_Mcu(void)
{
  PMR=0x40;  // 4clocks/machinecycle,switch disable
             // ALE enable and internal SRAM disable
             // oscilator enable
  PCON=0xb0; // this is to set SM0 to be UART mode setting
             // double the serial rate,SMOD=1
             // GF0 and GF1 can be used as other purpose
  DPS=0x00;  // DPTR will be selected here,DPTR selected here
  TCON=0x00; /* external interrupt should be set to low level trigger and T0 and T1 should stop now*/
  TMOD=0x21; /* T1  should be set autoload 8 bits timer mode to act as baudrate generator*/
             /* T0 should act as 16 bits timer*/
  CKCON=0xc1;/* to set Timer0 and Timer1 clock*/
             /* Clock of Timer0 ,Timer1 and Timer2 is divided by 12,MOVX strecash occupy 3(default) machine cycle watch dog time out 2(26)+512   */
  SCON0=0x5c;/* receive and transmitter will be enbled*/
  SCON1=0x5c;/* serial port1 and port0 will act in Mode1
                 the baudrate will be varible*/
             // transimitter enable now
  IE=0x52;   /* this is to set interrupt enable*/
             // Enabled: T0,S0 and S1,T2
             // disble: T1,EXT0 and EXT1
  WDCON=0x80;// double serial1 baudrate,disable wathcdog reset
  IP=0x10;   /* this is to set interrupt prioprity*/
             // S0 will be high and else will be low
  EIE=0x00;  // disable watchdog interrupt and extern2-5 interrupt
  EIP=0x00;  // all extended external interrupt disabled above
  T2CON=0x00;// this is to decide which Timer will act as
             // baudrater for serial port0:TIMER1
             // Timer2 work in reload mode

  T2MOD=0xf0;// EXTN2,3,4,5 flag auto clear,T2 down counter disable
  EA=0;	     /* disable all interrupt now ,should be opened later*/
             //there is another useful register can be used: STATUS,pay attention for it
}


void WriteSIO(unsigned char CommNum,unsigned char x) {
  if(CommNum != 0) {
    SBUF1 = x;
    do{}while(!TI1);
    TI1 = 0;
    SioEnd = 1;
  }
  else {
    SBUF = x;
    do{}while(!TI);
    TI = 0;
    SioEnd = 1;
    }
}


void main(void)
{
  unsigned char data i;
  Init();
  Init_Mcu();
  ES = 1;
  ES1 = 1;
  REN = 1;
  REN_1 = 1;
  EA = 1;
  TR1 = 1;
  SioEnd = 1;
  while(1) {
    for(i = 0; i <= 5; i++) {
      SBUF = 0xaa;//WriteSIO(0, 0x55);
      SioEnd = 0;
      do{}while(!SioEnd);
      SBUF1 = 0x55;//WriteSIO(1, 0x55);
      SioEnd = 0;
      do{}while(!SioEnd);
      DisplayStatus();
    }
  }
}

⌨️ 快捷键说明

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