单片机串行通信和定时器0同时工作该如何设置,帮我看一下写的程序 5
/*此程序的目的是串口发送1蜂鸣器400ms频率发声,发送2以200ms频率发声,发送3,以100ms频率发生声,发送4,停止发声*/#include<reg52.h>#...
/*此程序的目的是串口发送1蜂鸣器400ms频率发声,发送2以200ms频率发声,发送3,以100ms频率发生声,发送4,停止发声*/
#include <reg52.h>
#define BAUD 9600
#define FOSC 11059200
#define SMOD 0
#if SMOD
#define TC_VAL (256 - FOSC/16/12/BAUD)
#else
#define TC_VAL (256 - FOSC/32/12/BAUD)
#endif
#define uchar unsigned char
sbit beep = P1^0;
uchar a = 0, t = 41,tt = 0, flag = 1;
void inti();
void main()
{
inti();
while (1)
{
P0 = a;
switch (a)
{
case 1: t = 40;flag = 0;break;
case 2: t = 20;flag = 0;break;
case 3: t = 10;flag = 0;break;
case 4: t = 41;break;
if(tt==t)
{
tt = 0;
beep = ~beep;
}
}
}
}
void inti()
{
TMOD = 0x20;
TH0 = (65536-10000)/256;
TL0 = (65536-10000)%256;
TL1 = TC_VAL;
TH1 = TL1;
TR1 = 1;
TR0 = 1;
REN = 1;
SM0 = 0;
SM1 = 1;
PCON = 0x00;
PS = 1;
EA = 1;
ET0 = 1;
ES = 1;
}
void b() interrupt 4
{
RI = 0;
a = SBUF;
}
void time0() interrupt 1
{
TH0 = (65536-10000)/256;
TL0 = (65536-10000)%256;
++tt;
if(tt == 40)
tt = 0;
} 展开
#include <reg52.h>
#define BAUD 9600
#define FOSC 11059200
#define SMOD 0
#if SMOD
#define TC_VAL (256 - FOSC/16/12/BAUD)
#else
#define TC_VAL (256 - FOSC/32/12/BAUD)
#endif
#define uchar unsigned char
sbit beep = P1^0;
uchar a = 0, t = 41,tt = 0, flag = 1;
void inti();
void main()
{
inti();
while (1)
{
P0 = a;
switch (a)
{
case 1: t = 40;flag = 0;break;
case 2: t = 20;flag = 0;break;
case 3: t = 10;flag = 0;break;
case 4: t = 41;break;
if(tt==t)
{
tt = 0;
beep = ~beep;
}
}
}
}
void inti()
{
TMOD = 0x20;
TH0 = (65536-10000)/256;
TL0 = (65536-10000)%256;
TL1 = TC_VAL;
TH1 = TL1;
TR1 = 1;
TR0 = 1;
REN = 1;
SM0 = 0;
SM1 = 1;
PCON = 0x00;
PS = 1;
EA = 1;
ET0 = 1;
ES = 1;
}
void b() interrupt 4
{
RI = 0;
a = SBUF;
}
void time0() interrupt 1
{
TH0 = (65536-10000)/256;
TL0 = (65536-10000)%256;
++tt;
if(tt == 40)
tt = 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询