51单片机串口向计算机发送字符串C程序
时间:11-17
来源:互联网
点击:
51单片机串口向计算机发送字符串C程序,本布衣实验过的,可以用,串口需有一个232-TTL电平转换
通过串口调试助手可以看到发送字符信息。波特率为9600
#include
#include
//--------------------------------
char *name_month(int n);
void Delay(int x);
void put(char str);
void initUart(void);
void putString(unsigned char const *pstr);//const
//------------------------------
void put(char str)
{
SBUF=str;
while(!TI);
TI=0;
}
//------------------------------
void initUart(void)
{
TMOD=0x20;
SCON=0x50;
TH1=250;
TL1=250;
TR1=1;
EA = 1;
Delay(2);
putString("Uart init!\n");
}
//--------------------------
void Delay(int dly)
{
int j,k;
for (j=0;j for(k=0;k<125;k++);
}
//---------------------------------
char *name_month(int n)
{
static char *name[]=
{
"Illegal month\n",
"January\n",
"February\n",
"March\n",
"April\n",
"May\n",
"June\n",
"July\n",
"August\n",
"September\n",
"October\n",
"November\n",
"December\n"
};
return((n<1||n>12) ? name[0]:name[n]);
}
//------------------------------------
void putString(unsigned char const *pstr)//const
{
while(1)
{
if(*pstr==\0) break;
put(*pstr++);
}
}
//--------------------------------------
void main(void)
{
int i;
char *s;
initUart();
putString("I come here pointer!\n");
for (i=1;i<13;i++)
{
s=name_month(i);
while(1)
{
if (*s==\0) break;
put(*s++);
}
Delay(1000);
}
}
通过串口调试助手可以看到发送字符信息。波特率为9600
#include
#include
//--------------------------------
char *name_month(int n);
void Delay(int x);
void put(char str);
void initUart(void);
void putString(unsigned char const *pstr);//const
//------------------------------
void put(char str)
{
SBUF=str;
while(!TI);
TI=0;
}
//------------------------------
void initUart(void)
{
TMOD=0x20;
SCON=0x50;
TH1=250;
TL1=250;
TR1=1;
EA = 1;
Delay(2);
putString("Uart init!\n");
}
//--------------------------
void Delay(int dly)
{
int j,k;
for (j=0;j
}
//---------------------------------
char *name_month(int n)
{
static char *name[]=
{
"Illegal month\n",
"January\n",
"February\n",
"March\n",
"April\n",
"May\n",
"June\n",
"July\n",
"August\n",
"September\n",
"October\n",
"November\n",
"December\n"
};
return((n<1||n>12) ? name[0]:name[n]);
}
//------------------------------------
void putString(unsigned char const *pstr)//const
{
while(1)
{
if(*pstr==\0) break;
put(*pstr++);
}
}
//--------------------------------------
void main(void)
{
int i;
char *s;
initUart();
putString("I come here pointer!\n");
for (i=1;i<13;i++)
{
s=name_month(i);
while(1)
{
if (*s==\0) break;
put(*s++);
}
Delay(1000);
}
}
51单片机串口发送字符 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)