给楼主一例子,
//--------------------------------------------------------------
/*====================================
*名称:lcd_str.c
*功能:从串口调试工具发送一字符串到单片机,在lcd1602和串口调试工具上显示【未加入校验】
*作者:flying 09-2-23
*修改:
======================================*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden = P3^5;
sbit lcdrw = P3^6;
sbit lcdrs = P3^7;
uchar table[10];
uchar i, j, flag;
void delay(uint n)
{
uint x,y;
for( x = n; x > 0; x--)
for( y = 110; y > 0; y-- );
}
//lcd写命令函数
void lcdwrite_com( uchar com )
{
lcdrs = 0;
lcdrw = 0;
P0 = com;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd写数据
void lcdwrite_date( uchar date )
{
lcdrs = 1;
lcdrw = 0;
P0 = date;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd初始化
void lcd_init()
{
lcden = 0;
lcdwrite_com( 0x38 );//显示模式设置
lcdwrite_com( 0x0e );//显示光标
lcdwrite_com( 0x01 );//清屏
lcdwrite_com( 0x80 + 2 );//数据指针设置
}
//串口初始化
void serial_init()
{
TMOD = 0x20;
TH1 = 0xfd; //9600赫兹
TL1 = 0xfd;
TR1 = 1; //开启时间中断
SCON = 0x50;//方式2
SM2 = 1; //可以向cpu请求中断
EA = 1; //开总中断
ES = 1; //开串口中断
}
void putchar( uchar ch )//发送数据
{
SBUF = ch;
while( !TI );
TI = 0;
}
void main()
{
lcd_init();
serial_init();
while(1)
{
delay(20);
while( table[j] != '\0' && flag == 1 )
{
j = j >= 10 ? 0 : j;
putchar( table[j] );
lcdwrite_date( table[j] );
j++;
// delay(10);
}
}
}
void Serial() interrupt 4
{
RI = 0;
if( SBUF != '#')//遇到#终止写
{
table = SBUF;
i++;
}
else
flag = 1;
}