https://static.assets-stash.eet-china.com/album/old-resources/2008/10/28/bd985cc3-9b3f-4ea4-9c55-3939ae13240a.rar
//查询方式
/*---------------------------------------------------------------*/
//Proteus串口通讯
//按下按钮,单片机发送数据"Hello! I am glad to see you again.\r"给主机
/*---------------------------------------------------------------*/
#include<reg52.h>
#include<intrins.h>
/*---------------------------------------------------------------*/
sbit K1 = P1^2;
/*---------------------------------------------------------------*/
unsigned char tmp;
unsigned char code str[]={"Hello! I am glad to see you again.\r"};
/*---------------------------------------------------------------*/
void send_str(void);
void send_char(unsigned char txd);
void delayms(void);
/*---------------------------------------------------------------*/
void main(void)
{
TMOD = 0x20; //Timer1,8bits reload mode use to creat bps
TH1 = 0xfd; //9600bps
TL1 = 0xfd;
SCON = 0x50; //com mode1 enable Receive
PCON = 0x00; //no double bps
// PCON &=0xef;
TR1 = 1; //start Timer1
IE = 0x00; //forbid any interrupt
while(1)
{
if(K1==0){delayms();if(K1==0){send_str();while(K1==0);}}
if(RI) //data in ?
{
RI = 0;
tmp = SBUF; //save the received data
delayms();
P0 = tmp; //transmite data to the port P0
send_char(tmp); //回传接收到的数据
}
}
}
/*---------------------------------------------------------------*/
//传送一个字符
void send_char(unsigned char txd)
{
SBUF = txd;
while(!TI); //wait for sending over
TI = 0; //software clear the flag
}
/*---------------------------------------------------------------*/
//传送字符
void send_str(void)
{
unsigned char i="0";
while(str!='\0')
{
/* SBUF = str;
while(!TI); //wait for sending over
TI = 0; //clear the flag
*/
send_char(str);
i++; //the next char
}
/*
for(i=0;str!='\0';i++)
{
SBUF = str;
while(!TI);
TI = 0;
}
*/
}
/*---------------------------------------------------------------*/
//延时10000+0us 函数定义
void delayms(void)
{
unsigned char i,j,k;
for(i=5;i>0;i--)
for(j=4;j>0;j--)
for(k=248;k>0;k--);
}
//中断方式
//发表于 2008-5-22 14:26:49
//单片机串口中断程序,强力推荐!
/*-------------------------------------------------------------*/
//包含头文件
#include<at89x52.h>
unsigned char code tab[]={"Come On!\r"};
/*-------------------------------------------------------------*/
//串口初始化
void UART_Init(void)
{
SCON = 0x50; //serial mode 1, 8-bits UART, enable REN
TMOD = 0x20; //timer 1, mode 2, 8-bits reload
PCON = 0x80; //SMOD = 1; double bps
TH1 = 0xfa; //Baund: 9600bps fosc="11".0592MHz
TL1 = 0xfa;
ES = 1; //enable serial interrupt
TR1 = 1; //timer1 run
EA = 1; //enable all interrupt
}
/*-------------------------------------------------------------*/
//延时5000+0us 函数定义
void delay5ms(void)
{
unsigned char i,j,k;
for(i=135;i>0;i--)
for(j=2;j>0;j--)
for(k=7;k>0;k--);
}
/*-------------------------------------------------------------*/
//串口发送一个字节
void Uart_SendChar(unsigned char dat)
{
SBUF = dat; //待发送的数据写入缓冲区
while(!TI); //等待发送完成
TI = 0; //清零发送标志位
}
/*-------------------------------------------------------------*/
//串口发送一个字符串
void Uart_SendString(unsigned char *dat)
{
while(*dat!='\0')
{
Uart_SendChar(*dat); //好方法!!!
dat++;
}
/* unsigned char i;
for(i=0;dat!='\0';i++){ Uart_SendChar(dat); }
*/
}
/*-------------------------------------------------------------*/
//主函数
void main(void)
{
UART_Init(); //初始化串口
while(1)
{
if(P1_0==0)
{
delay5ms();
if(P1_0==0)
{
Uart_SendString(tab);
while(P1_0==0);
}
}
}
}
/*-------------------------------------------------------------*/
//串口接收中断函数
void INT_UartRcv(void) interrupt 4
{
unsigned char Rcv = 0;
if(RI) //查询接收标志位
{
RI = 0; //接收标志位清零
Rcv = SBUF; //存储缓冲区的数据
Uart_SendChar(Rcv); //返回接收的数据,可以改为其他的函数.
}
}
用户1454094 2010-3-14 20:48