給樓主一例子,
//--------------------------------------------------------------
/*====================================
*名稱(chēng):lcd_str.c
*功能:從串口調(diào)試工具發(fā)送一字符串到單片機(jī),在lcd1602和串口調(diào)試工具上顯示【未加入校驗(yàn)】
*作者: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寫(xiě)命令函數(shù)
void lcdwrite_com( uchar com )
{
lcdrs = 0;
lcdrw = 0;
P0 = com;
delay(5);
lcden = 1;
delay(5);
lcden = 0;
}
//lcd寫(xiě)數(shù)據(jù)
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 );//顯示模式設(shè)置
lcdwrite_com( 0x0e );//顯示光標(biāo)
lcdwrite_com( 0x01 );//清屏
lcdwrite_com( 0x80 + 2 );//數(shù)據(jù)指針設(shè)置
}
//串口初始化
void serial_init()
{
TMOD = 0x20;
TH1 = 0xfd; //9600赫茲
TL1 = 0xfd;
TR1 = 1; //開(kāi)啟時(shí)間中斷
SCON = 0x50;//方式2
SM2 = 1; //可以向cpu請(qǐng)求中斷
EA = 1; //開(kāi)總中斷
ES = 1; //開(kāi)串口中斷
}
void putchar( uchar ch )//發(fā)送數(shù)據(jù)
{
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 != '#')//遇到#終止寫(xiě)
{
table = SBUF;
i++;
}
else
flag = 1;
}