c语言编程:8051单片机系统的时钟为11.0592,把0~99从串行口输出。

每个数据之间时间间隔10ms,波特率为9600,8位数据位,1位停止位,无奇偶校验位

#include <reg51.h>

#define XTAL 11059200 // CPU晶振频率
#define baudrate 9600 // 通信波特率
#define uchar unsigned char
#define uint unsigned int
char aa, bb = 0;
//-----------------------------------------------------------
void main(void)
{
PCON = 0x00; // 波特率不倍增.
SCON = 0x50; // 设定串行口工作方式.
TMOD = 0x21; // T1定时方式2, 用于产生波特率.
// T0定时方式1, 用于定时.
TL1 = (uchar)(256 - (XTAL / (384L * baudrate))); // T1赋初值.
TH1 = (uchar)(256 - (XTAL / (384L * baudrate)));
TR1 = 1; // 启动定时器1
TL0 = (65536 - 4608) % 256; //[email protected]
TH0 = (65536 - 4608) / 256;
TR0 = 1; // 启动定时器0
ET0 = 1;
EA = 1; // 中断总允许.
while(1);
}
//-----------------------------------------------------------
void T0_INT() interrupt 1
{
TL0 = (65536 - 9216) % 256; //[email protected]
TH0 = (65536 - 9216) / 256;
aa++;
aa %= 2;
if (aa == 0) {
SBUF = (bb / 10) * 16 + (bb % 10);
bb++;
bb %= 100;
}
}
//-----------------------------------------------------------
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-06-30
//16进制发送的100个数,接收用16进制显示
#include "reg52.h"
#define uchar unsigned char
#define uint unsigned int
uchar a;
void delay_main(uint z)//-----------------主延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void send_char_com( unsigned char ch)
{
ES=0;
TI=0;
SBUF=ch;
while (!TI );
TI= 0 ;
ES=1 ;
}

void main(void)
{
SCON = 0x50; //串口方式1,允许接收
TMOD = 0x20; //定时器1定时方式2
PCON=0x00; //cup正常工作
TCON = 0x50; //设定时器1开始计数
TH1 = 0xfd; // 9600 11.0592MHz 1200波特率e8
TL1 = 0xfd;
TI = 1;//发送数据标志
TR1 =1; //启动定时器1
ES=0;
EA=1;
while(1)
{

if (a==100)
{a=0;
delay_main(1000);

}
send_char_com(a);
delay_main(10);
a=a+1;

}
}
相似回答