标题: 串口相关的寄存器及应用-51的 [打印本页]

作者: 京兆府111    时间: 2018-8-2 21:39
标题: 串口相关的寄存器及应用-51的
#include <reg52.h>
#define PLV 11059200UL
#define BAND_9600 9600UL
sbit LED = P1^5;
#define uchar unsigned char
uchar Data[]="a";
uchar *p;
bit flag = 0;
void sendData()
{

p=Data;
while(*p != '\0')
{
  SBUF = *p;
  while(!TI);
   TI=0;
  p++;
  }
}
void sendInt(int i)
{
SBUF = i/256;
  while(!TI);
   TI=0;
SBUF = i%256;
  while(!TI);
   TI=0;
}
void main()
{

unsigned int i = 0x3522;
int b = 455;
TMOD &=0x0f;
TMOD |=0x20;

SCON =0x50;
//band =x/ 16 ()    band = x/32
//x= plv/12/(256-yh1)

//so:  
TH1=256- PLV/(12*BAND_9600*16*2);
TL1=256-PLV/(12*BAND_9600*16*2);

//PCON |=0x80;
ES=1;
TR1=1;

// REN=1;//SCON&Ouml;&ETH;&sup2;&Ugrave;×÷&sup1;&yacute;&Aacute;&Euml;&iexcl;&pound;&iexcl;&pound;&iexcl;&pound;
EA=1;
sendInt(b);
while(1)
{
if(flag) {sendData();flag=0;}
}

}

void uart(void) interrupt 4
{unsigned char buff;
if(RI)
{

flag = 1;
buff = SBUF;
if (buff == 0x00)
  LED=1;
else if(buff == 0xaa)
  LED=0;
RI=0;
}
}





欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1