Current location - Education and Training Encyclopedia - Graduation thesis - Single-chip microcomputer timed doorbell
Single-chip microcomputer timed doorbell
The problem lies in the cycle, which is completed twice in an instant, and the second time can't start until the first "Ding Dong" rings.

# include & ltreg 5 1 . h & gt;

# Define uchar unsigned characters

# Define uint unsigned integer

uint p = 0; //must be a uint definition (p is 2 bytes).

sbit k=p 1^7; //key

sbit doorbell=p3^0; //doorbell

Unsigned int I;;

void main()

{

Doorbell = 0;

TMOD = 0x 00;

TH0 =(8 192-700)/32; //700us timing

TL0 =(8 192-700)% 32;

IE = 0x82

while( 1)

{

If(K==0)// key to start the timer.

{

for(I = 0; I<2; I want to "Ding Dong" twice, but I can only "Ding Dong" once.

{

TR0 = 1;

while(TR0);

}

}

}

}

Invalid timer 0 () interrupt 1

{

Doorbell = ~ doorbell;

p++;

If (p & lt600) // If you need to lengthen the sound, you can adjust it to 400,800.

{

TH0 =(8 192-700)/32; //700us timing

TL0 =(8 192-700)% 32;

}

else if(p & lt; 1200)

{

TH0 =(8 192- 1000)/32; // 1 millisecond timing

TL0 =(8 192- 1000)% 32;

}

other

{

TR0 = 0;

p = 0;

}

}