以下代码,功能能为 发送hello,world!!
#include <STC15F2K60S2.H>
#include <string.h>
#include <stdio.h>
#include "intrins.h"
void UartInit(void) //1200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初始值
TH1 = 0xF7; //设置定时初始值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
}
unsigned char Busy = 0;
void UartIsr() interrupt 4
{
while(RI){//接受完一个字节的字符后 RI=1
RI = 0;
}
while(TI){//发送完一个字节的字符后 RI=1
TI = 0;
Busy = 0;
}
}
void UartSandBytes(unsigned char *Data, unsigned char len)
{
while (len--)
{
SBUF = *Data;
Busy = 1;
Data++;
while (Busy);
}
}
void main()
{
unsigned char str[] = "HELLOW WORLD!!\n\r" ;
unsigned int cnt = 0;
P2 = 0x00;
P0 = 0xFF; P2 = 0x80; P2 = 0x00; //关闭LED
P0 = 0x00; P2 = 0xA0; P2 = 0x00; //关闭蜂鸣器
// Timer0Init();
UartInit();
ET0 = 1;
ES = 1;
EA = 1;
UartSandBytes(str,strlen(str));
while (1)
{}
}
以下例程的功能为 发送AAASSS时,串口会返回hellow,world!!
#include <STC15F2K60S2.H>
#include <string.h>
#include <stdio.h>
#include "intrins.h"
void UartInit(void) //1200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初始值
TH1 = 0xF7; //设置定时初始值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
}
unsigned char code passwd[] = "AAASSS";
unsigned char passwd_com = 0;
unsigned char Busy = 0;
void UartIsr() interrupt 4
{
if (RI) //接收一个字节完成
{
RI = 0;
if (passwd[passwd_com] == SBUF) //当前密码位匹配
{
passwd_com++;
}
else //当前密码位不匹配
{
passwd_com = 0;
}
}
if (TI) //发送一个字节完成
{
TI = 0;
Busy = 0;
}
}
void UartSandBytes(unsigned char *Data, unsigned char len)
{
while (len--)
{
SBUF = *Data;
Busy = 1;
Data++;
while (Busy);
}
}
void Delay1000ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 46;
j = 153;
k = 245;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
unsigned char str[] = "HELLOW WORLD!!\n\r" ;
P2 = 0x00;
P0 = 0xFF; P2 = 0x80; P2 = 0x00; //关闭LED
P0 = 0x00; P2 = 0xA0; P2 = 0x00; //关闭蜂鸣器
UartInit();
ET0 = 1;
ES = 1;
EA = 1;
while (1)
{
if (passwd_com >= 6)
{
passwd_com = 0;
UartSandBytes(str, strlen(str));
}
Delay1000ms();
}
}
下面的例程功能为 使用sprint()函数,将变量转换为字符
#include <STC15F2K60S2.H>
#include <string.h>
#include <stdio.h>
#include "intrins.h"
void UartInit(void) //1200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器时钟1T模式
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初始值
TH1 = 0xF7; //设置定时初始值
ET1 = 0; //禁止定时器%d中断
TR1 = 1; //定时器1开始计时
}
unsigned char code passwd[] = "AAASSS";
unsigned char passwd_com = 0;
unsigned char Busy = 0;
void UartIsr() interrupt 4
{
if (RI) //接收一个字节完成
{
RI = 0;
if (passwd[passwd_com] == SBUF) //当前密码位匹配
{
passwd_com++;
}
else //当前密码位不匹配
{
passwd_com = 0;
}
}
if (TI) //发送一个字节完成
{
TI = 0;
Busy = 0;
}
}
void UartSandBytes(unsigned char *Data, unsigned char len)
{
while (len--)
{
SBUF = *Data;
Busy = 1;
Data++;
while (Busy);
}
}
void Delay1000ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 46;
j = 153;
k = 245;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
unsigned char str[40];
unsigned int cnt = 0;
P2 = 0x00;
P0 = 0xFF; P2 = 0x80; P2 = 0x00; //关闭LED
P0 = 0x00; P2 = 0xA0; P2 = 0x00; //关闭蜂鸣器
UartInit();
ET0 = 1;
ES = 1;
EA = 1;
while (1)
{
if (passwd_com >= 6)
{
passwd_com = 0;
sprintf(str, "system running time %us\r\n", cnt);
UartSandBytes(str, strlen(str));
}
cnt++;
Delay1000ms();
}
}
pro
#include <Uart.h>
bit clock = 0;
void UartInit(void) //9600bps@12.000MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器时钟1T模式
T2L = 0xC7; //设置定时初始值
T2H = 0xFE; //设置定时初始值
AUXR |= 0x10; //定时器2开始计时
ES = 1; //使能串口1中断
}
unsigned char Busy = 0;
void Uare_isr() interrupt 4
{
if(RI){
RI = 0;
if('A' == SBUF){
clock = 0;
}
if('B' == SBUF){
clock = 1;
}
}
if(TI){
TI = 0;
Busy = 0;
}
}
void Sendbyte(unsigned char *dat,unsigned char len)
{
while(len--){
SBUF = *dat;
dat++;
Busy = 1;
while(Busy);
}
}