Home > other >  Timer 0 and communication serial port using problems at the same time
Timer 0 and communication serial port using problems at the same time

Time:09-26

I want to realize the buzzer code mainly play the music at the same time serial time in printing, two separate operation no problem, but together can collapse, this is my code
 # include & lt; Reg52. H> 

# define uint unsigned int
# define uchar unsigned char
Sbit beep=P1 ^ 5;
Uchar CNT, flag;
Uchar timer0H timer0L, n, delayt, I;
Uchar h1=0 x30, h2=0 x30,=0 x30 m1, m2=0 x30, s1=0 x30, s2=0 x30;//minutes
Code uchar noteH []={
Xf5 xf2 0, 0 xf3, 0, 0 xf5, 0 xf6, 0 xf7, 0 xf8,//bass 1234567
Xf9 0 xf9, 0, 0 xfa, 0 xfa, 0 XFB, 0 XFB, 0 XFC,//alto 1234567
XFD XFC 0, 0 XFC, 0, 0 XFD, 0 XFD, 0 XFD, 0 xfe,//high 1234567
0 xfe, 0 xfe, 0 xfe, 0 xfe, 0 xfe, 0 xfe, 0 XFF//ultra high 1234567
};
Code uchar noteL []={
0 x42, 0 xc1, 0 x17, 0 xb6, 0 xd0, 0 xd1, 0 xb6,//bass 1234567
X8c x21 0, 0 xe1, 0, 0 xd8, 0 x68, 0 xe9, 0 x5b,//alto 1234567
X44 x8f 0, 0 xee, 0, 0 x6b, 0 xb4, 0 xf4, 0 x2d,//high 1234567
Xa2 x47 0, 0 x77, 0, 0 xb6, 0 xda, 0 xfa, 0 x16//ultra high 1234567
};

Code uchar song []={6,2,1,7,2,1,1,3,3,7,2,1,1,3,2,3,3,2,7,2,6,3,2,1,3,2,1,6,2,3,5,2,1,6,2,2,
1,3,2,5,2,6,3,2,2,4,2,3,3,2,1,4,2,2,1,3,2,3,2,5,1,3,1,1,3,1,1,3,1,7,2,3,
4,2,1,4,2,2,7,2,2,7,2,6,6,2,1,7,2,1,1,3,3,7,2,1,1,3,2,3,3,2,7,2,6,3,2,1,
3,2,1,6,2,3,5,2,1,6,2,2,1,3,2,5,2,6,2,2,1,3,2,1,4,2,2,1,3,1,7,2,1,7,2,1,
1,3,1,1,3,2,2,3,1,2,3,1,3,3,1,1,3,1,1,3,4,1,3,1,7,2,1,6,2,1,6,2,1,7,2,2,
5,2,2,6,2,6,1,3,1,2,3,1,3,3,3,2,3,1,3,3,2,5,3,2,2,3,6,5,2,1,5,2,1,1,3,3,
7,2,1,1,3,2,3,3,2,3,3,8,6,2,1,7,2,1,1,3,2,7,2,2,2,3,2,2,3,2,1,3,3,5,2,1,0,
};//a note there are three Numbers, for which a sound before, in which one octave, for hours after (to make),
Void STR ();//serial print time
Void music ();
Void init ();
Void delay (uint);
Void play ();

Void main ()
{
init();
//RI=1;
While (1)
{
STR ();
Music ();
}
}

Void init ()
{
TMOD |=0 x21;
TH1=0 xf3;
TL1=0 xf3;
SCON=0 x50;
PCON=0 x80;
EA=1;
ES=1;
TR1=1;
ET0=1;
}
Void delay (uint x)
{
Unsigned long j, k;
for(j=0; Jfor(k=0; K<4000; K++);
TR0=0;
}
Void play ()
{
TH0=timer0H;//control tone
TL0=timer0L;
TR0=1;
Delay (delayt);

}
Void SER () interrupt 4
{
Uint x=21730;//delay about 1 s
While (x -);
flag=1;
}

Void timer0 () interrupt 1
{
TR0=0;
Beep=~ beep;
TH0=timer0H;
TL0=timer0L;
TR0=1;
}

Void STR ()
{
//RI=1;
If (flag)
{
Flag=0;
EA=0;

S1 +=1;
If (s1==0 x3a)
{
S1=0 x30;
S2 +=1;
If (s2==0 x36)
{
S2=0 x30;
M1 +=1;
If (m1==0 x3a)
{
M1=0 x30;
M2 +=1;
If (m2==0 x36)
{
M2=0 x30;
H1 +=1;
If (h1==0 x3a)
{
H1=0 x30;
The h2 +=1;
If (h2==0 x32 & amp; & H1==0 x34)
{
H1=0 x30;
The h2=0 x30;
}
}
}
}
}
}


SBUF=h2;
while(! TI);
TI=0;

SBUF=h1;
while(! TI);
TI=0;

SBUF=0 x3a;
while(! TI);
TI=0;

SBUF=m2;
while(! TI);
TI=0;

SBUF=m1;
while(! TI);
TI=0;

SBUF=0 x3a;
while(! TI);
TI=0;

SBUF=s2;
while(! TI);
TI=0;

SBUF=s1;
while(! TI);
TI=0;

SBUF=0 x2c;
while(! TI);
TI=0;

EA=1;
}
}

Void music ()
{
i=0;
While (1)
{
N=song [I] + 7 * (song [I + 1) - 1);//the notes, I first I + 1 is which octaves
Timer0H=noteH [n].
Timer0L=noteL [n].
Delayt=song [I + 2];//delay time length, number of shoot
I +=3;
Play ();
If (song [I]==0)//stop sign
break;
}
delay(1000);
}


For help, how this can be solved, only two timer, T1 in the serial port, T0 in the buzzer,

CodePudding user response:

Timing has a problem: delay time and more long, program a problem for certain,

CodePudding user response:

reference 1st floor yishumei response:
sequence has a problem: delay time and more long, programs must be out of the question,

The buzzer playing music to achieve and should be how to write a serial printing time, play music delay would inevitably many times

CodePudding user response:

refer to the second floor weixin_45260968 response:
Quote: refer to 1st floor yishumei response:
sequence has a problem: delay time and more long, certainly out of the question,

The buzzer playing music to achieve and should be how to write a serial printing time, play music delay would inevitably many times.

What is the delay in these two functions are? What is the principle of control volume?
Void play ()
{
TH0=timer0H;//control tone
TL0=timer0L;
TR0=1;
Delay (delayt);

}
Void SER () interrupt 4
{
Uint x=21730;//delay about 1 s
While (x -);
flag=1;
}

CodePudding user response:

reference yishumei reply: 3/f
Quote: refer to the second floor weixin_45260968 response:

Quote: refer to 1st floor yishumei response:
sequence has a problem: the delay time is long and more, the program problem for certain,

The buzzer playing music to achieve and should be how to write a serial printing time, play music delay would inevitably many times.

What is the delay in these two functions are? What is the principle of control volume?
Void play ()
{
TH0=timer0H;//control tone
TL0=timer0L; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related