52 control the output current of the dac0832 chip, so that the light-emitting diode D12 is reduced evenly from the off to the brightest, and then from the brightest to the brightest, the buzzer generates an alarm, the time for completing the entire cycle is controlled at around 5s.
# Include <reg52.h>
# Define uchar unsigned char
# Define uint unsigned int
Sbit Dula = P2 ^ 6;
Sbit wela = P2 ^ 7;
Sbit dawr = P3 ^ 6;
Sbit DACS = P3 ^ 2;
Sbit beep = P2 ^ 3;
Void delayms (uint XMS)
{
Uint I, J;
For (I = XMS; I> 0; I --)
For (j = 110; j> 0; j --);
}
Void main ()
{
Uchar Val, flag;
Dula = 0;
Wela = 0;
DACS = 0;
Dawr = 0;
P0 = 0;
While (1)
{
If (flag = 0)
{
Val + = 5;
P0 = val;
If (val = 255)
{
Flag = 1;
Beep = 0;
Delayms (100 );
Beep = 1;
}
Delayms (50 );
}
Else
{
Val-= 5;
P0 = val;
If (val = 0)
{
Flag = 0;
Beep = 0;
Delayms (100 );
Beep = 1;
}
Delayms (50 );
}
}
}
Enable the selection of D/A slices and then enable the write end. The latency is 50 ms, 50*51 = 2551 ms, ignoring the 100 ms occupied by the buzzer, which is about 2.5 s. The other half cycle is also 2.5 s, totaling 5 s.